| 1 |
# Copyright (c) The OpenTracing Authors. |
| 2 |
# |
| 3 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
# of this software and associated documentation files (the "Software"), to deal |
| 5 |
# in the Software without restriction, including without limitation the rights |
| 6 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
# copies of the Software, and to permit persons to whom the Software is |
| 8 |
# furnished to do so, subject to the following conditions: |
| 9 |
# |
| 10 |
# The above copyright notice and this permission notice shall be included in |
| 11 |
# all copies or substantial portions of the Software. |
| 12 |
# |
| 13 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 |
# THE SOFTWARE. |
| 20 |
from __future__ import absolute_import |
| 21 |
|
| •22 |
from unittest import mock |
| 23 |
|
| 24 |
from opentracing.span import Span |
| 25 |
|
| 26 |
|
| 27 |
class ScopeCompatibilityCheckMixin(object): |
| 28 |
""" |
| 29 |
A mixin class for validation that a given scope manager implementation |
| 30 |
satisfies the requirements of the OpenTracing API. |
| 31 |
""" |
| 32 |
|
| 33 |
def scope_manager(self): |
| 34 |
raise NotImplementedError('Subclass must implement scope_manager()') |
| 35 |
|
| 36 |
def run_test(self, test_fn): |
| 37 |
""" |
| 38 |
Utility method that can be optionally defined by ScopeManager |
| 39 |
implementers to run the passed test_fn() function |
| 40 |
in a given environment, such as a coroutine or greenlet. |
| 41 |
By default, it simply runs the passed test_fn() function |
| 42 |
in the current thread. |
| 43 |
""" |
| 44 |
|
| 45 |
test_fn() |
| 46 |
|
| 47 |
def test_missing_active_external(self): |
| 48 |
# test that 'active' does not fail outside the run_test() |
| 49 |
# implementation (greenlet or coroutine). |
| 50 |
scope_manager = self.scope_manager() |
| 51 |
assert scope_manager.active is None |
| 52 |
|
| 53 |
def test_missing_active(self): |
| 54 |
def fn(): |
| 55 |
scope_manager = self.scope_manager() |
| 56 |
assert scope_manager.active is None |
| 57 |
|
| 58 |
self.run_test(fn) |
| 59 |
|
| 60 |
def test_activate(self): |
| 61 |
def fn(): |
| 62 |
scope_manager = self.scope_manager() |
| 63 |
span = mock.MagicMock(spec=Span) |
| 64 |
|
| 65 |
scope = scope_manager.activate(span, False) |
| 66 |
assert scope is not None |
| 67 |
assert scope_manager.active is scope |
| 68 |
|
| 69 |
scope.close() |
| 70 |
assert span.finish.call_count == 0 |
| 71 |
assert scope_manager.active is None |
| 72 |
|
| 73 |
self.run_test(fn) |
| 74 |
|
| 75 |
def test_activate_external(self): |
| 76 |
# test that activate() does not fail outside the run_test() |
| 77 |
# implementation (greenlet or corotuine). |
| 78 |
scope_manager = self.scope_manager() |
| 79 |
span = mock.MagicMock(spec=Span) |
| 80 |
|
| 81 |
scope = scope_manager.activate(span, False) |
| 82 |
assert scope is not None |
| 83 |
assert scope_manager.active is scope |
| 84 |
|
| 85 |
scope.close() |
| 86 |
assert span.finish.call_count == 0 |
| 87 |
assert scope_manager.active is None |
| 88 |
|
| 89 |
def test_activate_finish_on_close(self): |
| 90 |
def fn(): |
| 91 |
scope_manager = self.scope_manager() |
| 92 |
span = mock.MagicMock(spec=Span) |
| 93 |
|
| 94 |
scope = scope_manager.activate(span, True) |
| 95 |
assert scope is not None |
| 96 |
assert scope_manager.active is scope |
| 97 |
|
| 98 |
scope.close() |
| 99 |
assert span.finish.call_count == 1 |
| 100 |
assert scope_manager.active is None |
| 101 |
|
| 102 |
self.run_test(fn) |
| 103 |
|
| 104 |
def test_activate_nested(self): |
| 105 |
def fn(): |
| 106 |
# when a Scope is closed, the previous one must be re-activated. |
| 107 |
scope_manager = self.scope_manager() |
| 108 |
parent_span = mock.MagicMock(spec=Span) |
| 109 |
child_span = mock.MagicMock(spec=Span) |
| 110 |
|
| 111 |
with scope_manager.activate(parent_span, True) as parent: |
| 112 |
assert parent is not None |
| 113 |
assert scope_manager.active is parent |
| 114 |
|
| 115 |
with scope_manager.activate(child_span, True) as child: |
| 116 |
assert child is not None |
| 117 |
assert scope_manager.active is child |
| 118 |
|
| 119 |
assert scope_manager.active is parent |
| 120 |
|
| 121 |
assert parent_span.finish.call_count == 1 |
| 122 |
assert child_span.finish.call_count == 1 |
| 123 |
|
| 124 |
assert scope_manager.active is None |
| 125 |
|
| 126 |
self.run_test(fn) |
| 127 |
|
| 128 |
def test_activate_finish_on_close_nested(self): |
| 129 |
def fn(): |
| 130 |
# finish_on_close must be correctly handled |
| 131 |
scope_manager = self.scope_manager() |
| 132 |
parent_span = mock.MagicMock(spec=Span) |
| 133 |
child_span = mock.MagicMock(spec=Span) |
| 134 |
|
| 135 |
parent = scope_manager.activate(parent_span, False) |
| 136 |
with scope_manager.activate(child_span, True): |
| 137 |
pass |
| 138 |
parent.close() |
| 139 |
|
| 140 |
assert parent_span.finish.call_count == 0 |
| 141 |
assert child_span.finish.call_count == 1 |
| 142 |
assert scope_manager.active is None |
| 143 |
|
| 144 |
self.run_test(fn) |
| 145 |
|
| 146 |
def test_close_wrong_order(self): |
| 147 |
def fn(): |
| 148 |
# only the active `Scope` can be closed |
| 149 |
scope_manager = self.scope_manager() |
| 150 |
parent_span = mock.MagicMock(spec=Span) |
| 151 |
child_span = mock.MagicMock(spec=Span) |
| 152 |
|
| 153 |
parent = scope_manager.activate(parent_span, True) |
| 154 |
child = scope_manager.activate(child_span, True) |
| 155 |
parent.close() |
| 156 |
|
| 157 |
assert parent_span.finish.call_count == 0 |
| 158 |
assert scope_manager.active == child |
| 159 |
|
| 160 |
self.run_test(fn) |