-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathtest_integration_cache.py
More file actions
265 lines (231 loc) · 6.96 KB
/
test_integration_cache.py
File metadata and controls
265 lines (231 loc) · 6.96 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import os
import time
import pytest
from . import run
from .conditions import has_breakpad, has_files, has_http
pytestmark = [
pytest.mark.skipif(not has_files, reason="tests need local filesystem"),
pytest.mark.skipif(not has_http, reason="tests need http transport"),
]
@pytest.mark.parametrize("cache_keep", [True, False])
@pytest.mark.parametrize(
"backend",
[
"inproc",
pytest.param(
"breakpad",
marks=pytest.mark.skipif(
not has_breakpad, reason="breakpad backend not available"
),
),
],
)
def test_cache_keep(cmake, backend, cache_keep, unreachable_dsn):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": backend})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=unreachable_dsn)
# capture
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "flush", "crash"]
+ (["cache-keep"] if cache_keep else []),
expect_failure=True,
env=env,
)
assert not cache_dir.exists() or len(list(cache_dir.glob("*.envelope"))) == 0
# flush + cache
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "flush", "no-setup"]
+ (["cache-keep"] if cache_keep else []),
env=env,
)
assert cache_dir.exists() or cache_keep is False
if cache_keep:
cache_files = list(cache_dir.glob("*.envelope"))
assert len(cache_files) == 1
if backend != "inproc":
dmp_files = list(cache_dir.glob("*.dmp"))
assert len(dmp_files) == 1
assert cache_files[0].stem == dmp_files[0].stem
@pytest.mark.parametrize(
"backend",
[
"inproc",
pytest.param(
"breakpad",
marks=pytest.mark.skipif(
not has_breakpad, reason="breakpad backend not available"
),
),
],
)
def test_cache_max_size(cmake, backend, unreachable_dsn):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": backend})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=unreachable_dsn)
for i in range(5):
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "flush", "crash"],
expect_failure=True,
env=env,
)
# flush + cache
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "flush", "no-setup"],
env=env,
)
# 5 x 2mb
assert cache_dir.exists()
cache_files = list(cache_dir.glob("*.envelope"))
for f in cache_files:
with open(f, "r+b") as file:
file.truncate(2 * 1024 * 1024)
# max 4mb
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "no-setup"],
env=env,
)
cache_files = list(cache_dir.glob("*.envelope"))
assert len(cache_files) <= 2
assert sum(f.stat().st_size for f in cache_files) <= 4 * 1024 * 1024
@pytest.mark.parametrize(
"backend",
[
"inproc",
pytest.param(
"breakpad",
marks=pytest.mark.skipif(
not has_breakpad, reason="breakpad backend not available"
),
),
],
)
def test_cache_max_age(cmake, backend, unreachable_dsn):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": backend})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=unreachable_dsn)
for i in range(5):
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "flush", "crash"],
expect_failure=True,
env=env,
)
# flush + cache
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "flush", "no-setup"],
env=env,
)
# 2,4,6,8,10 days old
assert cache_dir.exists()
cache_files = list(cache_dir.glob("*.envelope"))
for i, f in enumerate(cache_files):
mtime = time.time() - ((i + 1) * 2 * 24 * 60 * 60)
os.utime(str(f), (mtime, mtime))
# max 5 days
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "no-setup"],
env=env,
)
cache_files = list(cache_dir.glob("*.envelope"))
assert len(cache_files) == 2
for f in cache_files:
assert time.time() - f.stat().st_mtime <= 5 * 24 * 60 * 60
@pytest.mark.parametrize(
"backend",
[
"inproc",
pytest.param(
"breakpad",
marks=pytest.mark.skipif(
not has_breakpad, reason="breakpad backend not available"
),
),
],
)
def test_cache_max_items(cmake, backend, unreachable_dsn):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": backend})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=unreachable_dsn)
for i in range(6):
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "flush", "crash"],
expect_failure=True,
env=env,
)
# flush + cache
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "flush", "no-setup"],
env=env,
)
# max 5 items
assert cache_dir.exists()
cache_files = list(cache_dir.glob("*.envelope"))
assert len(cache_files) == 5
@pytest.mark.parametrize(
"backend",
[
"inproc",
pytest.param(
"breakpad",
marks=pytest.mark.skipif(
not has_breakpad, reason="breakpad backend not available"
),
),
],
)
def test_cache_max_items_with_retry(cmake, backend, unreachable_dsn):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": backend})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=unreachable_dsn)
# Create cache files via crash+restart cycles
for i in range(4):
run(
tmp_path,
"sentry_example",
["log", "cache-keep", "crash"],
expect_failure=True,
env=env,
)
# flush + cache
run(
tmp_path,
"sentry_example",
["log", "cache-keep", "flush", "no-setup"],
env=env,
)
# Pre-populate cache/ with retry-format envelope files
cache_dir.mkdir(parents=True, exist_ok=True)
for i in range(4):
ts = int(time.time() * 1000)
f = cache_dir / f"{ts}-00-00000000-0000-0000-0000-{i:012x}.envelope"
f.write_text("dummy envelope content")
# Trigger sentry_init which runs cleanup
run(
tmp_path,
"sentry_example",
["log", "cache-keep", "no-setup"],
env=env,
)
# max 5 items total in cache/
assert cache_dir.exists()
cache_files = list(cache_dir.glob("*.envelope"))
assert len(cache_files) <= 5