Skip to content

Commit f6ae194

Browse files
committed
Fix flaky crashtracker test
1 parent 8fbf836 commit f6ae194

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

tests/internal/crashtracker/test_crashtracker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def test_crashtracker_preload_disabled(ddtrace_run_python_code_in_subprocess):
330330
assert exitcode == -11
331331

332332
# No crash reports should be sent
333-
assert client.crash_reports() == []
333+
assert client.crash_messages() == []
334334

335335

336336
auto_code = """
@@ -389,7 +389,7 @@ def test_crashtracker_auto_disabled(run_python_code_in_subprocess):
389389
assert exitcode == -11
390390

391391
# No crash reports should be sent
392-
assert client.crash_reports() == []
392+
assert client.crash_messages() == []
393393

394394

395395
@pytest.mark.skipif(not sys.platform.startswith("linux"), reason="Linux only")

tests/internal/crashtracker/utils.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,35 +112,44 @@ def logs(self):
112112
return read_files([self.stdout, self.stderr])
113113

114114

115-
def wait_for_crash_reports(test_agent_client: TestAgentClient) -> List[TestAgentRequest]:
116-
crash_reports = []
117-
for _ in range(10): # 10 iterations * 0.1 second = 1 second total
118-
incoming_reports = test_agent_client.crash_reports()
119-
if incoming_reports:
120-
crash_reports.extend(incoming_reports)
115+
def wait_for_crash_messages(test_agent_client: TestAgentClient) -> List[TestAgentRequest]:
116+
seen_report_ids = set()
117+
crash_messages = []
118+
# 5 iterations * 0.2 second = 1 second total should be enough to get ping + report
119+
for _ in range(5):
120+
incoming_messages = test_agent_client.crash_messages()
121+
for message in incoming_messages:
122+
body = message.get("body", b"")
123+
if isinstance(body, str):
124+
body = body.encode("utf-8")
125+
report_id = (hash(body), frozenset(message.get("headers", {}).items()))
126+
if report_id not in seen_report_ids:
127+
seen_report_ids.add(report_id)
128+
crash_messages.append(message)
129+
121130
# If we have both crash ping and crash report (2 reports), we can return early
122-
if len(crash_reports) >= 2:
123-
return crash_reports
124-
time.sleep(0.1)
131+
if len(crash_messages) >= 2:
132+
return crash_messages
133+
time.sleep(0.2)
125134

126-
return crash_reports
135+
return crash_messages
127136

128137

129138
def get_crash_report(test_agent_client: TestAgentClient) -> TestAgentRequest:
130139
"""Wait for a crash report from the crashtracker listener socket."""
131-
crash_reports = wait_for_crash_reports(test_agent_client)
140+
crash_messages = wait_for_crash_messages(test_agent_client)
132141
# We want at least the crash report
133-
assert len(crash_reports) == 2, f"Expected at 2 messages; one ping and one report, got {len(crash_reports)}"
142+
assert len(crash_messages) == 2, f"Expected at least 2 messages; got {len(crash_messages)}"
134143

135-
# Find the actual crash report (the one with "is_crash":"true")
136-
actual_crash_report = None
137-
for report in crash_reports:
138-
if b"is_crash:true" in report["body"]:
139-
actual_crash_report = report
144+
# Find the crash report (the one with "is_crash":"true")
145+
crash_report = None
146+
for message in crash_messages:
147+
if b"is_crash:true" in message["body"]:
148+
crash_report = message
140149
break
141150

142-
assert actual_crash_report is not None, "Could not find crash report with 'is_crash:true' tag"
143-
return actual_crash_report
151+
assert crash_report is not None, "Could not find crash report with 'is_crash:true' tag"
152+
return crash_report
144153

145154

146155
@contextmanager

tests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ def telemetry_requests(self, telemetry_type: Optional[str] = None) -> List[TestA
11621162
reqs.append(req)
11631163
return reqs
11641164

1165-
def crash_reports(self) -> List[TestAgentRequest]:
1165+
def crash_messages(self) -> List[TestAgentRequest]:
11661166
reqs = []
11671167
for req in self.telemetry_requests(telemetry_type="logs"):
11681168
# Parse the json data in order to filter based on "origin" key,

0 commit comments

Comments
 (0)