Skip to content

Commit e3a2c0f

Browse files
committed
Add timeout for waiting functions
1 parent 0686674 commit e3a2c0f

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

instrumentation/opentelemetry-instrumentation-celery/tests/test_metrics.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ def tearDown(self):
5656
self._worker.stop()
5757
self._thread.join()
5858

59-
def wait_for_tasks_to_finish(self):
59+
def wait_for_tasks_to_finish(self, timeout=30):
6060
"""Blocks until all tasks in Celery worker are finished"""
6161

6262
inspect = app.control.inspect()
63+
start_time = time.time()
6364
while True:
6465
counter = 0
6566
for state in (
@@ -74,6 +75,9 @@ def wait_for_tasks_to_finish(self):
7475
break
7576

7677
time.sleep(0.5)
78+
if time.time() - start_time > timeout:
79+
raise TimeoutError("Timeout while waiting for tasks to finish.")
80+
7781

7882
def wait_for_metrics_until_finished(self, task_fn, *args):
7983
"""
@@ -83,11 +87,11 @@ def wait_for_metrics_until_finished(self, task_fn, *args):
8387
"""
8488

8589
result = task_fn.delay(*args)
86-
87-
timeout = time.time() + 60 * 1
90+
give_up_time = time.time() + 30
8891
while not result.ready():
89-
if time.time() > timeout:
90-
break
92+
if time.time() > give_up_time:
93+
raise TimeoutError("Timeout while waiting for task to finish.")
94+
9195
time.sleep(0.05)
9296
return self.get_sorted_metrics()
9397

0 commit comments

Comments
 (0)