Skip to content

Commit c559c8a

Browse files
committed
Some cleanup and some tests
1 parent d7ffaed commit c559c8a

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

sentry_sdk/_lru_cache.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,8 @@ def get_all(self):
181181
nodes = []
182182
node = self.root[NEXT]
183183

184-
# To ensure the loop always terminates we iterate to the maximum
185-
# size of the LRU cache.
186-
for _ in range(self.max_size):
187-
# The cache may not be full. We exit early if we've wrapped
188-
# around to the head.
189-
if node is self.root:
190-
break
184+
while node is not self.root:
191185
nodes.append((node[KEY], node[VALUE]))
192186
node = node[NEXT]
193187

194-
return nodes
188+
return nodes

tests/test_lru_cache.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from copy import copy
2+
from copy import copy, deepcopy
33

44
from sentry_sdk._lru_cache import LRUCache
55

@@ -76,3 +76,38 @@ def test_cache_copy():
7676
cache.get(1)
7777
assert copied.get_all() == [(1, 1), (2, 2), (3, 3)]
7878
assert cache.get_all() == [(2, 2), (3, 3), (1, 1)]
79+
80+
81+
def test_cache_deepcopy():
82+
cache = LRUCache(3)
83+
cache.set(0, 0)
84+
cache.set(1, 1)
85+
86+
copied = deepcopy(cache)
87+
cache.set(2, 2)
88+
cache.set(3, 3)
89+
assert copied.get_all() == [(0, 0), (1, 1)]
90+
assert cache.get_all() == [(1, 1), (2, 2), (3, 3)]
91+
92+
copied = deepcopy(cache)
93+
cache.get(1)
94+
assert copied.get_all() == [(1, 1), (2, 2), (3, 3)]
95+
assert cache.get_all() == [(2, 2), (3, 3), (1, 1)]
96+
97+
98+
def test_cache_pollution():
99+
cache1 = LRUCache(max_size=2)
100+
cache1.set(1, True)
101+
cache2 = copy(cache1)
102+
cache2.set(1, False)
103+
assert cache1.get(1) is True
104+
assert cache2.get(1) is False
105+
106+
107+
def test_cache_pollution_deepcopy():
108+
cache1 = LRUCache(max_size=2)
109+
cache1.set(1, True)
110+
cache2 = deepcopy(cache1)
111+
cache2.set(1, False)
112+
assert cache1.get(1) is True
113+
assert cache2.get(1) is False

tests/test_scope.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ def test_all_slots_copied():
4343
assert getattr(scope_copy, attr) == getattr(scope, attr)
4444

4545

46+
def test_scope_flags_copy():
47+
# Assert forking creates a deepcopy of the flag buffer. The new
48+
# scope is free to mutate without consequence to the old scope. The
49+
# old scope is free to mutate without consequence to the new scope.
50+
old_scope = Scope()
51+
old_scope.flags.set("a", True)
52+
53+
new_scope = old_scope.fork()
54+
new_scope.flags.set("a", False)
55+
old_scope.flags.set("b", True)
56+
new_scope.flags.set("c", True)
57+
58+
assert old_scope.flags.get() == [
59+
{"flag": "a", "result": True},
60+
{"flag": "b", "result": True},
61+
]
62+
assert new_scope.flags.get() == [
63+
{"flag": "a", "result": False},
64+
{"flag": "c", "result": True},
65+
]
66+
67+
4668
def test_merging(sentry_init, capture_events):
4769
sentry_init()
4870

0 commit comments

Comments
 (0)