| 1 | from __future__ import absolute_import, print_function |
| 2 | |
| 3 | from concurrent.futures import ThreadPoolExecutor |
| 4 | |
| 5 | from opentracing.mocktracer import MockTracer |
| 6 | from ..testcase import OpenTracingTestCase |
| 7 | |
| 8 | |
| 9 | class TestThreads(OpenTracingTestCase): |
| 10 | def setUp(self): |
| 11 | self.tracer = MockTracer() |
| 12 | self.executor = ThreadPoolExecutor(max_workers=3) |
| 13 | |
| 14 | def test_main(self): |
| 15 | res = self.executor.submit(self.parent_task, 'message').result() |
| 16 | self.assertEqual(res, 'message::response') |
| 17 | |
| 18 | spans = self.tracer.finished_spans() |
| 19 | self.assertEqual(len(spans), 2) |
| 20 | self.assertNamesEqual(spans, ['child', 'parent']) |
| 21 | self.assertIsChildOf(spans[0], spans[1]) |
| 22 | |
| 23 | def parent_task(self, message): |
| 24 | with self.tracer.start_active_span('parent') as scope: |
| 25 | f = self.executor.submit(self.child_task, message, scope.span) |
| 26 | res = f.result() |
| 27 | |
| 28 | return res |
| 29 | |
| 30 | def child_task(self, message, span): |
| 31 | with self.tracer.scope_manager.activate(span, False): |
| 32 | with self.tracer.start_active_span('child'): |
| 33 | return '%s::response' % message |