Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit dfb039e

Browse files
authored
Fix hanging async tests (#382)
1 parent d3b2897 commit dfb039e

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

tests/unit/common/transports/test_async.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
from opencensus.common.transports import async_
2020

21+
22+
# Don't let workers wait between exports in testing
23+
wait_period_patch = mock.patch(
24+
'opencensus.common.transports.async_._WAIT_PERIOD', 0)
25+
26+
2127
class Test_Worker(unittest.TestCase):
2228

2329
def _start_worker(self, worker):
@@ -35,7 +41,7 @@ def test_constructor(self):
3541
max_batch_size = 20
3642

3743
worker = async_._Worker(exporter, grace_period=grace_period,
38-
max_batch_size=max_batch_size)
44+
max_batch_size=max_batch_size)
3945

4046
self.assertEqual(worker.exporter, exporter)
4147
self.assertEqual(worker._grace_period, grace_period)
@@ -67,8 +73,6 @@ def test_stop(self):
6773

6874
mock_thread, mock_atexit = self._start_worker(worker)
6975

70-
thread = worker._thread
71-
7276
worker.stop()
7377

7478
self.assertEqual(worker._queue.qsize(), 1)
@@ -129,7 +133,8 @@ def test__thread_main(self):
129133
worker.enqueue(trace2)
130134
worker._queue.put_nowait(async_._WORKER_TERMINATOR)
131135

132-
worker._thread_main()
136+
with wait_period_patch:
137+
worker._thread_main()
133138

134139
self.assertTrue(worker.exporter.emit.called)
135140
self.assertEqual(worker._queue.qsize(), 0)
@@ -164,7 +169,8 @@ def test__thread_main_batches(self):
164169

165170
worker._queue.put_nowait(async_._WORKER_TERMINATOR)
166171

167-
worker._thread_main()
172+
with wait_period_patch:
173+
worker._thread_main()
168174

169175
self.assertEqual(worker._queue.qsize(), 0)
170176

@@ -193,7 +199,8 @@ def emit(self, span):
193199
worker.enqueue(span_data1)
194200
worker.enqueue(span_data2)
195201

196-
worker._thread_main()
202+
with wait_period_patch:
203+
worker._thread_main()
197204

198205
self.assertEqual(exporter.exported, [span_data1])
199206

@@ -225,7 +232,8 @@ def emit(self, span):
225232
worker.enqueue(span_data2)
226233
worker.enqueue(async_._WORKER_TERMINATOR)
227234

228-
worker._thread_main()
235+
with wait_period_patch:
236+
worker._thread_main()
229237

230238
# Span 2 should throw an exception, only span 0 and 1 are left
231239
self.assertEqual(exporter.exported, span_data0 + span_data1)
@@ -238,7 +246,6 @@ def emit(self, span):
238246
# and the data was dropped.
239247
self.assertEqual(worker._queue.qsize(), 0)
240248

241-
242249
def test_flush(self):
243250
from six.moves import queue
244251

@@ -259,7 +266,7 @@ def test_constructor(self):
259266
autospec=True)
260267
exporter = mock.Mock()
261268

262-
with patch_worker as mock_worker:
269+
with patch_worker:
263270
transport = async_.AsyncTransport(exporter)
264271

265272
self.assertTrue(transport.worker.start.called)
@@ -271,7 +278,7 @@ def test_export(self):
271278
autospec=True)
272279
exporter = mock.Mock()
273280

274-
with patch_worker as mock_worker:
281+
with patch_worker:
275282
transport = async_.AsyncTransport(exporter)
276283

277284
trace = {
@@ -289,7 +296,7 @@ def test_flush(self):
289296
autospec=True)
290297
exporter = mock.Mock()
291298

292-
with patch_worker as mock_worker:
299+
with patch_worker:
293300
transport = async_.AsyncTransport(exporter)
294301

295302
transport.flush()

0 commit comments

Comments
 (0)