forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_lru_cache.py
113 lines (86 loc) · 2.48 KB
/
test_lru_cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import pytest
from copy import copy, deepcopy
from sentry_sdk._lru_cache import LRUCache
@pytest.mark.parametrize("max_size", [-10, -1, 0])
def test_illegal_size(max_size):
with pytest.raises(AssertionError):
LRUCache(max_size=max_size)
def test_simple_set_get():
cache = LRUCache(1)
assert cache.get(1) is None
cache.set(1, 1)
assert cache.get(1) == 1
def test_overwrite():
cache = LRUCache(1)
assert cache.get(1) is None
cache.set(1, 1)
assert cache.get(1) == 1
cache.set(1, 2)
assert cache.get(1) == 2
def test_cache_eviction():
cache = LRUCache(3)
cache.set(1, 1)
cache.set(2, 2)
cache.set(3, 3)
assert cache.get(1) == 1
assert cache.get(2) == 2
cache.set(4, 4)
assert cache.get(3) is None
assert cache.get(4) == 4
def test_cache_miss():
cache = LRUCache(1)
assert cache.get(0) is None
def test_cache_set_overwrite():
cache = LRUCache(3)
cache.set(0, 0)
cache.set(0, 1)
assert cache.get(0) == 1
def test_cache_get_all():
cache = LRUCache(3)
cache.set(0, 0)
cache.set(1, 1)
cache.set(2, 2)
cache.set(3, 3)
assert cache.get_all() == [(1, 1), (2, 2), (3, 3)]
cache.get(1)
assert cache.get_all() == [(2, 2), (3, 3), (1, 1)]
def test_cache_copy():
cache = LRUCache(3)
cache.set(0, 0)
cache.set(1, 1)
copied = copy(cache)
cache.set(2, 2)
cache.set(3, 3)
assert copied.get_all() == [(0, 0), (1, 1)]
assert cache.get_all() == [(1, 1), (2, 2), (3, 3)]
copied = copy(cache)
cache.get(1)
assert copied.get_all() == [(1, 1), (2, 2), (3, 3)]
assert cache.get_all() == [(2, 2), (3, 3), (1, 1)]
def test_cache_deepcopy():
cache = LRUCache(3)
cache.set(0, 0)
cache.set(1, 1)
copied = deepcopy(cache)
cache.set(2, 2)
cache.set(3, 3)
assert copied.get_all() == [(0, 0), (1, 1)]
assert cache.get_all() == [(1, 1), (2, 2), (3, 3)]
copied = deepcopy(cache)
cache.get(1)
assert copied.get_all() == [(1, 1), (2, 2), (3, 3)]
assert cache.get_all() == [(2, 2), (3, 3), (1, 1)]
def test_cache_pollution():
cache1 = LRUCache(max_size=2)
cache1.set(1, True)
cache2 = copy(cache1)
cache2.set(1, False)
assert cache1.get(1) is True
assert cache2.get(1) is False
def test_cache_pollution_deepcopy():
cache1 = LRUCache(max_size=2)
cache1.set(1, True)
cache2 = deepcopy(cache1)
cache2.set(1, False)
assert cache1.get(1) is True
assert cache2.get(1) is False