| 1 |
from __future__ import absolute_import, print_function |
| 2 |
|
| 3 |
import asyncio |
| 4 |
|
| 5 |
from opentracing.mocktracer import MockTracer |
| 6 |
from opentracing.scope_managers.asyncio import AsyncioScopeManager |
| 7 |
from ..testcase import OpenTracingTestCase |
| 8 |
|
| 9 |
|
| 10 |
class TestAsyncio(OpenTracingTestCase): |
| 11 |
def setUp(self): |
| 12 |
self.tracer = MockTracer(AsyncioScopeManager()) |
| 13 |
self.loop = asyncio.get_event_loop() |
| 14 |
|
| 15 |
def test_main(self): |
| 16 |
res = self.loop.run_until_complete(self.parent_task('message')) |
| 17 |
self.assertEqual(res, 'message::response') |
| 18 |
|
| 19 |
spans = self.tracer.finished_spans() |
| 20 |
self.assertEqual(len(spans), 2) |
| 21 |
self.assertNamesEqual(spans, ['child', 'parent']) |
| 22 |
self.assertIsChildOf(spans[0], spans[1]) |
| 23 |
|
| 24 |
async def parent_task(self, message): |
| 25 |
with self.tracer.start_active_span('parent') as scope: |
| 26 |
res = await self.child_task(message, scope.span) |
| 27 |
|
| 28 |
return res |
| 29 |
|
| 30 |
async 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 |