| 1 |
from __future__ import absolute_import, print_function |
| 2 |
|
| 3 |
import functools |
| 4 |
|
| 5 |
from tornado import gen, ioloop |
| 6 |
|
| 7 |
from opentracing.mocktracer import MockTracer |
| 8 |
from opentracing.scope_managers.tornado import TornadoScopeManager, \ |
| 9 |
tracer_stack_context |
| 10 |
from ..testcase import OpenTracingTestCase |
| 11 |
|
| 12 |
|
| 13 |
class TestTornado(OpenTracingTestCase): |
| 14 |
def setUp(self): |
| 15 |
self.tracer = MockTracer(TornadoScopeManager()) |
| 16 |
self.loop = ioloop.IOLoop.current() |
| 17 |
|
| 18 |
def test_main(self): |
| 19 |
parent_task = functools.partial(self.parent_task, 'message') |
| 20 |
with tracer_stack_context(): |
| 21 |
res = self.loop.run_sync(parent_task) |
| 22 |
self.assertEqual(res, 'message::response') |
| 23 |
|
| 24 |
spans = self.tracer.finished_spans() |
| 25 |
self.assertEqual(len(spans), 2) |
| 26 |
self.assertNamesEqual(spans, ['child', 'parent']) |
| 27 |
self.assertIsChildOf(spans[0], spans[1]) |
| 28 |
|
| 29 |
@gen.coroutine |
| 30 |
def parent_task(self, message): |
| 31 |
with self.tracer.start_active_span('parent'): |
| 32 |
res = yield self.child_task(message) |
| 33 |
|
| 34 |
raise gen.Return(res) |
| 35 |
|
| 36 |
@gen.coroutine |
| 37 |
def child_task(self, message): |
| 38 |
|
| 39 |
|
| 40 |
with self.tracer.start_active_span('child'): |
| 41 |
raise gen.Return('%s::response' % message) |