This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
/
test_tornado.py
65 lines (49 loc) · 1.99 KB
/
test_tornado.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from __future__ import print_function
import random
from tornado import gen, ioloop
from opentracing.mocktracer import MockTracer
from opentracing.scope_managers.tornado import TornadoScopeManager, \
tracer_stack_context
from ..testcase import OpenTracingTestCase
from ..utils import get_logger, stop_loop_when
random.seed()
logger = get_logger(__name__)
class TestTornado(OpenTracingTestCase):
def setUp(self):
self.tracer = MockTracer(TornadoScopeManager())
self.loop = ioloop.IOLoop.current()
def test_main(self):
@gen.coroutine
def main_task():
with self.tracer.start_active_span('parent'):
tasks = self.submit_callbacks()
yield tasks
with tracer_stack_context():
self.loop.add_callback(main_task)
stop_loop_when(self.loop,
lambda: len(self.tracer.finished_spans()) == 4)
self.loop.start()
spans = self.tracer.finished_spans()
self.assertEquals(len(spans), 4)
self.assertNamesEqual(spans, ['task', 'task', 'task', 'parent'])
for i in range(3):
self.assertSameTrace(spans[i], spans[-1])
self.assertIsChildOf(spans[i], spans[-1])
@gen.coroutine
def task(self, interval, parent_span):
logger.info('Starting task')
# NOTE: No need to reactivate the parent_span, as TracerStackContext
# keeps track of it, BUT a limitation is that, yielding
# upon multiple coroutines, we cannot mess with the context,
# so no active span set here.
assert self.tracer.active_span is not None
with self.tracer.start_span('task'):
yield gen.sleep(interval)
def submit_callbacks(self):
parent_span = self.tracer.scope_manager.active.span
tasks = []
for i in range(3):
interval = 0.1 + random.randint(200, 500) * 0.001
t = self.task(interval, parent_span)
tasks.append(t)
return tasks