Skip to content

Commit

Permalink
collector/BaseCollector/get_all: add test for dict modification
Browse files Browse the repository at this point in the history
  • Loading branch information
huwcbjones committed Jun 18, 2024
1 parent 2fabe65 commit 931e06e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
from threading import Thread

from aioprometheus.collectors import (
REGISTRY,
Expand Down Expand Up @@ -166,6 +167,31 @@ def country_fetcher(x):
sorted_result = sorted(c.get_all(), key=country_fetcher)
self.assertEqual(sorted_data, sorted_result)

def test_get_all_change_iter(self) -> None:
"""
test iterating over get_all when the underlying dict changes
For https://github.com/claws/aioprometheus/issues/85
"""
c = Collector(**self.default_data)

# Create a large collection of values to increase the chance of a race
for i in range(100000):
c.set_value({f"b-{i}": "a"}, i)

# Now create a thread that will add a huge amount more values
def set_more_values():
for j in range(10000):
c.set_value({f"a-{j}": "a"}, j)

t = Thread(daemon=True, target=set_more_values)
t.start()

# Hope that due to the large amount of labels involved, we will smack
# head-first into the race of iterating over the dict whilst adding more
# unique labels to it.
list(c.get_all())


class TestCounterMetric(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 931e06e

Please sign in to comment.