|
| 1 | +# coding=utf-8 |
| 2 | +from __future__ import absolute_import, division, print_function, unicode_literals |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from scout_apm.api import BackgroundTransaction, WebTransaction, instrument |
| 7 | +from tests.tools import async_test |
| 8 | + |
| 9 | + |
| 10 | +@async_test |
| 11 | +async def test_instrument_decorator_async(tracked_request): |
| 12 | + @instrument.async_("Foo") |
| 13 | + async def foo(): |
| 14 | + pass |
| 15 | + |
| 16 | + @instrument.async_("Bar") |
| 17 | + async def example(): |
| 18 | + await foo() |
| 19 | + |
| 20 | + await example() |
| 21 | + |
| 22 | + assert len(tracked_request.active_spans) == 0 |
| 23 | + assert len(tracked_request.complete_spans) == 2 |
| 24 | + assert tracked_request.complete_spans[0].operation == "Custom/Foo" |
| 25 | + assert tracked_request.complete_spans[1].operation == "Custom/Bar" |
| 26 | + |
| 27 | + |
| 28 | +def test_instrument_decorator_async_for_sync_function(tracked_request): |
| 29 | + @instrument.async_("Bar") |
| 30 | + def example(): |
| 31 | + pass |
| 32 | + |
| 33 | + with pytest.warns(RuntimeWarning): |
| 34 | + example() |
| 35 | + |
| 36 | + assert len(tracked_request.active_spans) == 0 |
| 37 | + assert len(tracked_request.complete_spans) == 0 |
| 38 | + |
| 39 | + |
| 40 | +@async_test |
| 41 | +async def test_instrument_decorator_async_misconfigured(tracked_request): |
| 42 | + """Test case where .async_ isn't used from parent instrument""" |
| 43 | + |
| 44 | + @instrument.async_("Foo") |
| 45 | + async def foo(): |
| 46 | + pass |
| 47 | + |
| 48 | + @instrument("Bar") |
| 49 | + async def example(): |
| 50 | + await foo() |
| 51 | + |
| 52 | + await example() |
| 53 | + |
| 54 | + assert len(tracked_request.active_spans) == 0 |
| 55 | + assert len(tracked_request.complete_spans) == 1 |
| 56 | + assert tracked_request.complete_spans[0].operation == "Custom/Bar" |
| 57 | + |
| 58 | + |
| 59 | +@async_test |
| 60 | +async def test_instrument_decorator_async_classmethod(tracked_request): |
| 61 | + class Example(object): |
| 62 | + @classmethod |
| 63 | + @instrument.async_("Test Decorator") |
| 64 | + async def method(cls): |
| 65 | + pass |
| 66 | + |
| 67 | + await Example.method() |
| 68 | + |
| 69 | + assert len(tracked_request.active_spans) == 0 |
| 70 | + assert len(tracked_request.complete_spans) == 1 |
| 71 | + assert tracked_request.complete_spans[0].operation == "Custom/Test Decorator" |
| 72 | + |
| 73 | + |
| 74 | +@async_test |
| 75 | +async def test_instrument_decorator_async_staticmethod(tracked_request): |
| 76 | + class Example(object): |
| 77 | + @staticmethod |
| 78 | + @instrument.async_("Test Decorator") |
| 79 | + async def method(): |
| 80 | + pass |
| 81 | + |
| 82 | + await Example.method() |
| 83 | + |
| 84 | + assert len(tracked_request.active_spans) == 0 |
| 85 | + assert len(tracked_request.complete_spans) == 1 |
| 86 | + assert tracked_request.complete_spans[0].operation == "Custom/Test Decorator" |
| 87 | + |
| 88 | + |
| 89 | +@async_test |
| 90 | +async def test_instrument_decorator_async_return_awaitable(tracked_request): |
| 91 | + @instrument.async_("Foo") |
| 92 | + async def foo(): |
| 93 | + pass |
| 94 | + |
| 95 | + @instrument.async_("Bar") |
| 96 | + def return_awaitable(): |
| 97 | + return foo() |
| 98 | + |
| 99 | + await return_awaitable() |
| 100 | + |
| 101 | + assert len(tracked_request.active_spans) == 0 |
| 102 | + assert len(tracked_request.complete_spans) == 2 |
| 103 | + assert tracked_request.complete_spans[0].operation == "Custom/Foo" |
| 104 | + assert tracked_request.complete_spans[1].operation == "Custom/Bar" |
| 105 | + |
| 106 | + |
| 107 | +@async_test |
| 108 | +async def test_instrument_decorator_async_return_awaitable_misconfigured( |
| 109 | + tracked_request, |
| 110 | +): |
| 111 | + """Test case where .async_ isn't used from parent instrument""" |
| 112 | + |
| 113 | + @instrument.async_("Foo") |
| 114 | + async def foo(): |
| 115 | + pass |
| 116 | + |
| 117 | + @instrument("Bar") |
| 118 | + def return_awaitable(): |
| 119 | + return foo() |
| 120 | + |
| 121 | + await return_awaitable() |
| 122 | + |
| 123 | + assert len(tracked_request.active_spans) == 0 |
| 124 | + assert len(tracked_request.complete_spans) == 1 |
| 125 | + assert tracked_request.complete_spans[0].operation == "Custom/Bar" |
| 126 | + |
| 127 | + |
| 128 | +@async_test |
| 129 | +async def test_instrument_context_manager_async_await_later(tracked_request): |
| 130 | + """ |
| 131 | + Test proving that if an awaitable goes unawaited in a context manager, |
| 132 | + the spans are lost. |
| 133 | + """ |
| 134 | + |
| 135 | + @instrument.async_("Outer") |
| 136 | + async def foo(): |
| 137 | + with instrument("Inner"): |
| 138 | + pass |
| 139 | + |
| 140 | + async def example(): |
| 141 | + await foo() |
| 142 | + |
| 143 | + with instrument("Test Decorator"): |
| 144 | + awaitable = example() |
| 145 | + |
| 146 | + await awaitable |
| 147 | + |
| 148 | + assert len(tracked_request.active_spans) == 0 |
| 149 | + assert len(tracked_request.complete_spans) == 1 |
| 150 | + assert tracked_request.complete_spans[0].operation == "Custom/Test Decorator" |
| 151 | + |
| 152 | + |
| 153 | +@async_test |
| 154 | +async def test_web_transaction_decorator_async(tracked_request): |
| 155 | + @instrument.async_("Foo") |
| 156 | + async def foo(): |
| 157 | + pass |
| 158 | + |
| 159 | + @WebTransaction.async_("Bar") |
| 160 | + async def my_transaction(): |
| 161 | + await foo() |
| 162 | + |
| 163 | + await my_transaction() |
| 164 | + |
| 165 | + assert len(tracked_request.active_spans) == 0 |
| 166 | + assert len(tracked_request.complete_spans) == 2 |
| 167 | + assert tracked_request.complete_spans[0].operation == "Custom/Foo" |
| 168 | + assert tracked_request.complete_spans[1].operation == "Controller/Bar" |
| 169 | + |
| 170 | + |
| 171 | +@async_test |
| 172 | +async def test_web_transaction_decorator_async_misconfigured(tracked_request): |
| 173 | + """Test case where .async_ isn't used from WebTransaction""" |
| 174 | + |
| 175 | + @instrument.async_("Foo") |
| 176 | + async def foo(): |
| 177 | + pass |
| 178 | + |
| 179 | + @WebTransaction("Bar") |
| 180 | + async def my_transaction(): |
| 181 | + await foo() |
| 182 | + |
| 183 | + await my_transaction() |
| 184 | + |
| 185 | + assert len(tracked_request.active_spans) == 0 |
| 186 | + assert len(tracked_request.complete_spans) == 1 |
| 187 | + assert tracked_request.complete_spans[0].operation == "Controller/Bar" |
| 188 | + |
| 189 | + |
| 190 | +def test_web_transaction_decorator_async_for_sync_function(tracked_request): |
| 191 | + @WebTransaction.async_("Bar") |
| 192 | + def example(): |
| 193 | + pass |
| 194 | + |
| 195 | + with pytest.warns(RuntimeWarning): |
| 196 | + example() |
| 197 | + |
| 198 | + assert len(tracked_request.active_spans) == 0 |
| 199 | + assert len(tracked_request.complete_spans) == 0 |
| 200 | + |
| 201 | + |
| 202 | +@async_test |
| 203 | +async def test_background_transaction_decorator_async(tracked_request): |
| 204 | + @instrument.async_("Foo") |
| 205 | + async def foo(): |
| 206 | + pass |
| 207 | + |
| 208 | + @BackgroundTransaction.async_("Bar") |
| 209 | + async def my_transaction(): |
| 210 | + await foo() |
| 211 | + |
| 212 | + await my_transaction() |
| 213 | + |
| 214 | + assert len(tracked_request.active_spans) == 0 |
| 215 | + assert len(tracked_request.complete_spans) == 2 |
| 216 | + assert tracked_request.complete_spans[0].operation == "Custom/Foo" |
| 217 | + assert tracked_request.complete_spans[1].operation == "Job/Bar" |
| 218 | + |
| 219 | + |
| 220 | +@async_test |
| 221 | +async def test_background_transaction_decorator_async_misconfigured(tracked_request): |
| 222 | + """Test case where .async_ isn't used from BackgroundTransaction""" |
| 223 | + |
| 224 | + @instrument.async_("Foo") |
| 225 | + async def foo(): |
| 226 | + pass |
| 227 | + |
| 228 | + @BackgroundTransaction("Bar") |
| 229 | + async def my_transaction(): |
| 230 | + await foo() |
| 231 | + |
| 232 | + await my_transaction() |
| 233 | + |
| 234 | + assert len(tracked_request.active_spans) == 0 |
| 235 | + assert len(tracked_request.complete_spans) == 1 |
| 236 | + assert tracked_request.complete_spans[0].operation == "Job/Bar" |
| 237 | + |
| 238 | + |
| 239 | +def test_background_transaction_decorator_async_for_sync_function(tracked_request): |
| 240 | + @BackgroundTransaction.async_("Bar") |
| 241 | + def example(): |
| 242 | + pass |
| 243 | + |
| 244 | + with pytest.warns(RuntimeWarning): |
| 245 | + example() |
| 246 | + |
| 247 | + assert len(tracked_request.active_spans) == 0 |
| 248 | + assert len(tracked_request.complete_spans) == 0 |
0 commit comments