Skip to content

Commit e28c5e6

Browse files
committed
test: add native crash helper for daemon waits
1 parent 4b54261 commit e28c5e6

10 files changed

Lines changed: 58 additions & 38 deletions

tests/__init__.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ def adb(*args, **kwargs):
2525

2626
SENTRY_VERSION = "0.14.2"
2727

28-
from .assertions import wait_for_daemon
29-
from .cmake import cmake_option
28+
from .assertions import wait_for_daemon as _wait_for_daemon
3029

3130

3231
def make_dsn(httpserver, auth="uiaeosnrtdy", id=123456, proxy_host=False):
@@ -100,19 +99,18 @@ def extract_request(httpserver_log, cond):
10099
return (None, httpserver_log)
101100

102101

103-
def run(cwd, exe, args, expect_failure=False, env=None, **kwargs):
102+
def run(
103+
cwd, exe, args, expect_failure=False, env=None, wait_for_daemon=False, **kwargs
104+
):
104105
if env is None:
105106
env = dict(os.environ)
106-
should_wait_for_daemon = (
107-
expect_failure and cmake_option(cwd, "SENTRY_BACKEND") == "native"
108-
)
109107
if kwargs.get("check"):
110108
raise pytest.fail.Exception(
111109
"`check` is inferred from `expect_failure`, and should not be passed in the kwargs"
112110
)
113111
check = expect_failure == False
114112
__tracebackhide__ = True
115-
started_at = time.time() if should_wait_for_daemon else None
113+
started_at = time.time()
116114
if os.environ.get("ANDROID_API"):
117115
# older android emulators do not correctly pass down the returncode
118116
# so we basically echo the return code, and parse it manually
@@ -187,10 +185,10 @@ def run(cwd, exe, args, expect_failure=False, env=None, **kwargs):
187185
]
188186
try:
189187
result = subprocess.run([*cmd, *args], cwd=cwd, env=env, check=check, **kwargs)
190-
if should_wait_for_daemon:
191-
assert wait_for_daemon(
188+
if wait_for_daemon:
189+
assert _wait_for_daemon(
192190
cwd, started_at
193-
), "native crash daemon did not finish before timeout"
191+
), "native crash daemon did not finish within timeout"
194192
if expect_failure:
195193
assert (
196194
result.returncode != 0
@@ -208,6 +206,18 @@ def run(cwd, exe, args, expect_failure=False, env=None, **kwargs):
208206
) from None
209207

210208

209+
def run_native_crash(cwd, exe, args, env=None, **kwargs):
210+
return run(
211+
cwd,
212+
exe,
213+
args,
214+
expect_failure=True,
215+
env=env,
216+
wait_for_daemon=True,
217+
**kwargs,
218+
)
219+
220+
211221
def check_output(*args, **kwargs):
212222
stdout = run(*args, stdout=subprocess.PIPE, **kwargs).stdout
213223
# capturing stdout on windows actually encodes "\n" as "\r\n", which we

tests/cmake.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,6 @@
1717
get_tsan_env,
1818
)
1919

20-
_cmake_build_options = {}
21-
22-
23-
def cmake_option(cwd, name):
24-
options = _cmake_build_options.get(os.fspath(cwd))
25-
if options is None:
26-
options = _cmake_build_options.get(os.path.realpath(cwd))
27-
if options is None:
28-
return None
29-
return options.get(name)
30-
3120

3221
class CMake:
3322
def __init__(self, factory):
@@ -65,15 +54,13 @@ def compile(self, targets, options=None, cflags=None):
6554

6655
# ensure that there are no left-overs from previous runs
6756
shutil.rmtree(build_tmp_path / ".sentry-native", ignore_errors=True)
68-
_cmake_build_options[os.fspath(build_tmp_path)] = dict(options)
6957

7058
# Inject a sub-path into the temporary build directory as the CWD for all tests to verify UTF-8 path handling.
7159
if os.environ.get("UTF8_TEST_CWD"):
7260
# this is Thai and translates to "this is a test directory"
7361
utf8_parent = self.factory.mktemp("utf8")
7462
utf8_subpath = utf8_parent / "นี่คือไดเร็กทอรีทดสอบ"
7563
utf8_subpath.symlink_to(build_tmp_path, target_is_directory=True)
76-
_cmake_build_options[os.fspath(utf8_subpath)] = dict(options)
7764
return utf8_subpath
7865

7966
return build_tmp_path

tests/test_e2e_sentry.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,14 @@ def run_crash_e2e(tmp_path, exe, args, env):
372372

373373
# Use check_output to capture stdout for test ID extraction
374374
try:
375-
output = check_output(tmp_path, exe, args, env=env, expect_failure=True)
375+
output = check_output(
376+
tmp_path,
377+
exe,
378+
args,
379+
env=env,
380+
expect_failure=True,
381+
wait_for_daemon=True,
382+
)
376383
except AssertionError:
377384
if is_kcov:
378385
# kcov may exit with 0 even on crash, try without expect_failure

tests/test_integration_http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from . import (
1010
make_dsn,
1111
run,
12+
run_native_crash,
1213
Envelope,
1314
split_log_request_cond,
1415
is_feedback_envelope,
@@ -826,11 +827,10 @@ def test_native_crash_http(cmake, httpserver):
826827

827828
# Use stdout for initialization delay under TSAN
828829
# Configure ASAN to not intercept crash signals
829-
run(
830+
run_native_crash(
830831
tmp_path,
831832
"sentry_example",
832833
["log", "stdout", "attachment", "crash"],
833-
expect_failure=True,
834834
env=get_asan_crash_env(env),
835835
)
836836

tests/test_integration_logger.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def _run_logger_crash_test(backend, cmake, logger_option):
4040
stdout=subprocess.PIPE,
4141
stderr=subprocess.STDOUT,
4242
expect_failure=True,
43+
wait_for_daemon=backend == "native",
4344
)
4445

4546
# Process should have crashed (non-zero exit code)

tests/test_integration_logs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from . import (
66
make_dsn,
77
run,
8+
run_native_crash,
89
Envelope,
910
split_log_request_cond,
1011
is_logs_envelope,
@@ -277,11 +278,10 @@ def test_logs_on_crash_native(cmake, httpserver, rerun):
277278
env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver))
278279

279280
with httpserver.wait(timeout=10):
280-
run(
281+
run_native_crash(
281282
tmp_path,
282283
"sentry_example",
283284
["log", "capture-log", "crash"],
284-
expect_failure=True,
285285
env=env,
286286
)
287287

tests/test_integration_metrics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from . import (
66
make_dsn,
77
run,
8+
run_native_crash,
89
Envelope,
910
split_log_request_cond,
1011
is_metrics_envelope,
@@ -431,11 +432,10 @@ def test_metrics_on_crash_native(cmake, httpserver, rerun):
431432
env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver))
432433

433434
with httpserver.wait(timeout=10):
434-
run(
435+
run_native_crash(
435436
tmp_path,
436437
"sentry_example",
437438
["log", "capture-metric", "crash"],
438-
expect_failure=True,
439439
env=env,
440440
)
441441

tests/test_integration_native.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
is_feedback_envelope,
1616
make_dsn,
1717
run,
18+
run_native_crash,
1819
Envelope,
1920
split_log_request_cond,
2021
)
@@ -64,12 +65,22 @@ def run_crash(tmp_path, exe, args, env):
6465

6566
if is_kcov:
6667
try:
67-
run(tmp_path, exe, args, expect_failure=True, env=env)
68+
run_native_crash(
69+
tmp_path,
70+
exe,
71+
args,
72+
env=env,
73+
)
6874
except AssertionError:
6975
# kcov may exit with 0 even on crash, that's acceptable
7076
pass
7177
else:
72-
run(tmp_path, exe, args, expect_failure=True, env=env)
78+
run_native_crash(
79+
tmp_path,
80+
exe,
81+
args,
82+
env=env,
83+
)
7384

7485

7586
def test_native_capture_crash(cmake, httpserver):

tests/test_integration_screenshot.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88

9-
from . import Envelope, make_dsn, run
9+
from . import Envelope, make_dsn, run, run_native_crash
1010

1111

1212
def assert_screenshot_file(database_path):
@@ -82,7 +82,12 @@ def test_capture_screenshot_native(cmake, httpserver):
8282
httpserver.expect_oneshot_request("/api/123456/envelope/").respond_with_data("OK")
8383

8484
with httpserver.wait(timeout=10) as waiting:
85-
run(tmp_path, "sentry_screenshot", ["crash"], expect_failure=True, env=env)
85+
run_native_crash(
86+
tmp_path,
87+
"sentry_screenshot",
88+
["crash"],
89+
env=env,
90+
)
8691

8792
assert waiting.result
8893

@@ -172,11 +177,10 @@ def test_before_screenshot_native(cmake, httpserver):
172177
httpserver.expect_oneshot_request("/api/123456/envelope/").respond_with_data("OK")
173178

174179
with httpserver.wait(timeout=10) as waiting:
175-
run(
180+
run_native_crash(
176181
tmp_path,
177182
"sentry_screenshot",
178183
["crash", "before-screenshot"],
179-
expect_failure=True,
180184
env=env,
181185
)
182186

tests/test_integration_tus.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from . import (
99
make_dsn,
1010
run,
11+
run_native_crash,
1112
Envelope,
1213
SENTRY_VERSION,
1314
)
@@ -522,11 +523,10 @@ def test_tus_crash_native(cmake, httpserver):
522523
env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver))
523524

524525
with httpserver.wait(timeout=15) as waiting:
525-
run(
526+
run_native_crash(
526527
tmp_path,
527528
"sentry_example",
528529
["log", "large-attachment", "crash"],
529-
expect_failure=True,
530530
env=env,
531531
)
532532
assert waiting.result

0 commit comments

Comments
 (0)