Skip to content

Commit

Permalink
fix: unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl0ven committed May 30, 2023
1 parent b1beb88 commit 925c4a9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion sentry_dynamic_sampling_lib/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def ignored_user_agents(self):
@ignored_user_agents.setter
@synchronized
def ignored_user_agents(self, new_ignored_user_agents):
self._ignored_user_agents = set(new_ignored_user_agents)
self._ignored_user_agents = tuple(new_ignored_user_agents)

@property
@synchronized
Expand Down
6 changes: 6 additions & 0 deletions tests/test_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ def test_trace_sampler_call():
assert ts(ctx) == 0

ts._controller.app_config.ignored_paths = []
ts._controller.app_config.ignored_user_agents = ("kube",)
ctx = {"wsgi_environ": {"PATH_INFO": "/re", "HTTP_USER_AGENT": "kube/1.26"}}
assert ts(ctx) == 0

ts._controller.app_config.ignored_paths = []
ts._controller.app_config.ignored_user_agents = tuple()
ctx = {"wsgi_environ": {"PATH_INFO": "/re"}}
assert ts(ctx) == ts._controller.app_config.sample_rate
ts._controller.metrics.count_path.assert_called_once_with("/re")
Expand Down
32 changes: 22 additions & 10 deletions tests/test_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,32 @@ def test_config():
c.ignored_paths = ["a", "b"]
assert c.ignored_paths == {"a", "b"}

# assert use lock
assert synchronized_mock.call_count == 7

c.ignored_user_agents = ["a", "b"]
assert c.ignored_user_agents == ("a", "b")

# assert use lock
assert synchronized_mock.call_count == 9

c.ignored_tasks = ["a", "b"]
assert c.ignored_tasks == {"a", "b"}

# assert use lock
assert synchronized_mock.call_count == 9
assert synchronized_mock.call_count == 11

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

# assert use lock
assert synchronized_mock.call_count == 10
assert synchronized_mock.call_count == 12

assert c.sample_rate == 10
assert c.ignored_paths == {1, 2}
assert c.ignored_tasks == {3, 4}

# assert use lock
assert synchronized_mock.call_count == 13
assert synchronized_mock.call_count == 15


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

m.count_task("celery.run")
m.count_user_agent("kube/1.26")
assert synchronized_mock.call_count == 2

m.count_task("celery.run")
assert synchronized_mock.call_count == 3

# iteration
assert list(m) == [(MetricType.WSGI, Counter(["/metric/"])), (MetricType.CELERY, Counter(["celery.run"]))]
assert list(m) == [
(MetricType.WSGI, {"path": Counter(["/metric/"]), "user_agent": Counter(["kube/1.26"])}),
(MetricType.CELERY, {"task": Counter(["celery.run"])}),
]

assert synchronized_mock.call_count == 4
assert synchronized_mock.call_count == 5

assert list(m) == []

assert synchronized_mock.call_count == 6
assert synchronized_mock.call_count == 7

m.set_mode(MetricType.WSGI, False)
m.set_mode(MetricType.CELERY, False)

assert list(m) == []
assert synchronized_mock.call_count == 6
assert synchronized_mock.call_count == 7

m.count_path("/metric/")
m.count_user_agent("kube/1.26")
m.count_task("celery.run")

assert synchronized_mock.call_count == 8
assert synchronized_mock.call_count == 10
assert list(m) == []

0 comments on commit 925c4a9

Please sign in to comment.