Skip to content

Commit cbba7b3

Browse files
committed
test(ray): Fix flaky ray tests
1 parent fe4a09e commit cbba7b3

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

tests/integrations/ray/test_ray.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import os
33
import shutil
4+
import time
45
import uuid
56

67
import pytest
@@ -89,7 +90,7 @@ def read_error_from_log(job_id, ray_temp_dir):
8990
return None
9091

9192

92-
def read_spans_from_log(job_id, ray_temp_dir):
93+
def _parse_spans_from_log(job_id, ray_temp_dir):
9394
# Find the actual session directory that Ray created
9495
session_dirs = [d for d in os.listdir(ray_temp_dir) if d.startswith("session_")]
9596
if not session_dirs:
@@ -101,15 +102,17 @@ def read_spans_from_log(job_id, ray_temp_dir):
101102
if not os.path.exists(log_dir):
102103
raise FileNotFoundError(f"No logs directory found at {log_dir}")
103104

104-
log_file = [
105+
log_files = [
105106
f
106107
for f in os.listdir(log_dir)
107108
if "worker" in f and job_id in f and f.endswith(".out")
108-
][0]
109+
]
110+
if not log_files:
111+
return []
109112

110113
spans = []
111114
next_line_is_span_payload = False
112-
with open(os.path.join(log_dir, log_file), "r") as file:
115+
with open(os.path.join(log_dir, log_files[0]), "r") as file:
113116
for line in file:
114117
try:
115118
payload = json.loads(line)
@@ -125,6 +128,19 @@ def read_spans_from_log(job_id, ray_temp_dir):
125128
return spans
126129

127130

131+
def read_spans_from_log(job_id, ray_temp_dir, min_spans=1, timeout=10):
132+
deadline = time.monotonic() + timeout
133+
spans = []
134+
while True:
135+
try:
136+
spans = _parse_spans_from_log(job_id, ray_temp_dir)
137+
except FileNotFoundError:
138+
spans = []
139+
if len(spans) >= min_spans or time.monotonic() >= deadline:
140+
return spans
141+
time.sleep(0.1)
142+
143+
128144
def example_task(span_streaming: bool):
129145
if span_streaming:
130146
with sentry_sdk.traces.start_span(
@@ -205,7 +221,7 @@ def test_tracing_in_ray_tasks(task_options, task, span_streaming):
205221
ray.get(future)
206222

207223
job_id = future.job_id().hex()
208-
worker_spans = read_spans_from_log(job_id, ray_temp_dir)
224+
worker_spans = read_spans_from_log(job_id, ray_temp_dir, min_spans=2)
209225
finally:
210226
if os.path.exists(ray_temp_dir):
211227
shutil.rmtree(ray_temp_dir, ignore_errors=True)

0 commit comments

Comments
 (0)