Skip to content

Commit b9e9ef2

Browse files
authored
test: robust gitter setup (google#5134)
This PR improves the previous setup logic for gitter in `osv/tests.py`: - Implements a 60-second maximum wait time to accommodate compilation/startup in CI environments. - Uses `socket.connect_ex` to verify the port is open before proceeding. - Prints the exact time taken for the service to become ready.
1 parent 812734f commit b9e9ef2

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

osv/tests.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import difflib
1818
import os
1919
import shutil
20+
import socket
2021
import pprint
2122
import signal
2223
import time
@@ -295,17 +296,23 @@ def setup_gitter():
295296
)
296297

297298
try:
298-
# Wait a bit for it to start (optional, but good for stability)
299-
# Basic check to see if it crashed immediately
300-
try:
301-
proc.wait(timeout=1.0)
302-
# If it returns, it exited
303-
raise RuntimeError(
304-
f'Gitter exited early:\n{proc.stdout.read().decode()}\n\n'
305-
f'{proc.stderr.read().decode()}')
306-
except subprocess.TimeoutExpired:
307-
# Process is still running
308-
pass
299+
# Wait for it to start
300+
start = time.time()
301+
while time.time() - start < 60:
302+
if proc.poll() is not None:
303+
# If it returns, it exited
304+
raise RuntimeError(
305+
f'Gitter exited early:\n{proc.stdout.read().decode()}\n\n'
306+
f'{proc.stderr.read().decode()}')
307+
308+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
309+
if s.connect_ex(('localhost', int(gitter_port))) == 0:
310+
print(f'Gitter ready in {time.time() - start:.2f}s.')
311+
break
312+
time.sleep(0.5)
313+
else:
314+
os.killpg(os.getpgid(proc.pid), signal.SIGKILL)
315+
raise RuntimeError('Gitter did not get ready in time.')
309316

310317
yield
311318

0 commit comments

Comments
 (0)