Skip to content

Commit c42bbb8

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

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

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

Lines changed: 10 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,10 @@ 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(
80+
"Timeout while waiting for tasks to finish."
81+
)
7782

7883
def wait_for_metrics_until_finished(self, task_fn, *args):
7984
"""
@@ -83,11 +88,11 @@ def wait_for_metrics_until_finished(self, task_fn, *args):
8388
"""
8489

8590
result = task_fn.delay(*args)
86-
87-
timeout = time.time() + 60 * 1
91+
give_up_time = time.time() + 30
8892
while not result.ready():
89-
if time.time() > timeout:
90-
break
93+
if time.time() > give_up_time:
94+
raise TimeoutError("Timeout while waiting for task to finish.")
95+
9196
time.sleep(0.05)
9297
return self.get_sorted_metrics()
9398

0 commit comments

Comments
 (0)