Skip to content

Commit 925c4a9

Browse files
committed
fix: unit test
1 parent b1beb88 commit 925c4a9

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

sentry_dynamic_sampling_lib/shared.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def ignored_user_agents(self):
5050
@ignored_user_agents.setter
5151
@synchronized
5252
def ignored_user_agents(self, new_ignored_user_agents):
53-
self._ignored_user_agents = set(new_ignored_user_agents)
53+
self._ignored_user_agents = tuple(new_ignored_user_agents)
5454

5555
@property
5656
@synchronized

tests/test_sampler.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ def test_trace_sampler_call():
225225
assert ts(ctx) == 0
226226

227227
ts._controller.app_config.ignored_paths = []
228+
ts._controller.app_config.ignored_user_agents = ("kube",)
229+
ctx = {"wsgi_environ": {"PATH_INFO": "/re", "HTTP_USER_AGENT": "kube/1.26"}}
230+
assert ts(ctx) == 0
231+
232+
ts._controller.app_config.ignored_paths = []
233+
ts._controller.app_config.ignored_user_agents = tuple()
228234
ctx = {"wsgi_environ": {"PATH_INFO": "/re"}}
229235
assert ts(ctx) == ts._controller.app_config.sample_rate
230236
ts._controller.metrics.count_path.assert_called_once_with("/re")

tests/test_shared.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,32 @@ def test_config():
2929
c.ignored_paths = ["a", "b"]
3030
assert c.ignored_paths == {"a", "b"}
3131

32-
# assert use lock
3332
assert synchronized_mock.call_count == 7
3433

34+
c.ignored_user_agents = ["a", "b"]
35+
assert c.ignored_user_agents == ("a", "b")
36+
37+
# assert use lock
38+
assert synchronized_mock.call_count == 9
39+
3540
c.ignored_tasks = ["a", "b"]
3641
assert c.ignored_tasks == {"a", "b"}
3742

3843
# assert use lock
39-
assert synchronized_mock.call_count == 9
44+
assert synchronized_mock.call_count == 11
4045

4146
data = {"active_sample_rate": 10, "wsgi_ignore_path": (1, 2), "celery_ignore_task": (3, 4)}
4247
c.update(data)
4348

4449
# assert use lock
45-
assert synchronized_mock.call_count == 10
50+
assert synchronized_mock.call_count == 12
4651

4752
assert c.sample_rate == 10
4853
assert c.ignored_paths == {1, 2}
4954
assert c.ignored_tasks == {3, 4}
5055

5156
# assert use lock
52-
assert synchronized_mock.call_count == 13
57+
assert synchronized_mock.call_count == 15
5358

5459

5560
def test_metric():
@@ -73,26 +78,33 @@ def test_metric():
7378
m.count_path("/metric/")
7479
assert synchronized_mock.call_count == 1
7580

76-
m.count_task("celery.run")
81+
m.count_user_agent("kube/1.26")
7782
assert synchronized_mock.call_count == 2
7883

84+
m.count_task("celery.run")
85+
assert synchronized_mock.call_count == 3
86+
7987
# iteration
80-
assert list(m) == [(MetricType.WSGI, Counter(["/metric/"])), (MetricType.CELERY, Counter(["celery.run"]))]
88+
assert list(m) == [
89+
(MetricType.WSGI, {"path": Counter(["/metric/"]), "user_agent": Counter(["kube/1.26"])}),
90+
(MetricType.CELERY, {"task": Counter(["celery.run"])}),
91+
]
8192

82-
assert synchronized_mock.call_count == 4
93+
assert synchronized_mock.call_count == 5
8394

8495
assert list(m) == []
8596

86-
assert synchronized_mock.call_count == 6
97+
assert synchronized_mock.call_count == 7
8798

8899
m.set_mode(MetricType.WSGI, False)
89100
m.set_mode(MetricType.CELERY, False)
90101

91102
assert list(m) == []
92-
assert synchronized_mock.call_count == 6
103+
assert synchronized_mock.call_count == 7
93104

94105
m.count_path("/metric/")
106+
m.count_user_agent("kube/1.26")
95107
m.count_task("celery.run")
96108

97-
assert synchronized_mock.call_count == 8
109+
assert synchronized_mock.call_count == 10
98110
assert list(m) == []

0 commit comments

Comments
 (0)