|
10 | 10 | from ddtrace.api import API |
11 | 11 | from ddtrace.span import Span |
12 | 12 | from ddtrace.tracer import Tracer |
13 | | -from ddtrace.encoding import JSONEncoder, MsgpackEncoder |
| 13 | +from ddtrace.encoding import JSONEncoder, MsgpackEncoder, get_encoder |
14 | 14 | from tests.test_tracer import get_dummy_tracer |
15 | 15 |
|
16 | 16 |
|
@@ -278,3 +278,44 @@ def test_send_service_called_multiple_times(self): |
278 | 278 | response = self.api_msgpack.send_services(services) |
279 | 279 | ok_(response) |
280 | 280 | eq_(response.status, 200) |
| 281 | + |
| 282 | +@skipUnless( |
| 283 | + os.environ.get('TEST_DATADOG_INTEGRATION', False), |
| 284 | + 'You should have a running trace agent and set TEST_DATADOG_INTEGRATION=1 env variable' |
| 285 | +) |
| 286 | +class TestAPIDowngrade(TestCase): |
| 287 | + """ |
| 288 | + Ensures that if the tracing client found an earlier trace agent, |
| 289 | + it will downgrade the current connection to a stable API version |
| 290 | + """ |
| 291 | + def test_get_encoder_default(self): |
| 292 | + # get_encoder should return MsgpackEncoder instance if |
| 293 | + # msgpack and the CPP implementaiton are available |
| 294 | + encoder = get_encoder() |
| 295 | + ok_(isinstance(encoder, MsgpackEncoder)) |
| 296 | + |
| 297 | + @mock.patch('ddtrace.encoding.MSGPACK_ENCODING', False) |
| 298 | + def test_get_encoder_fallback(self): |
| 299 | + # get_encoder should return JSONEncoder instance if |
| 300 | + # msgpack or the CPP implementaiton, are not available |
| 301 | + encoder = get_encoder() |
| 302 | + ok_(isinstance(encoder, JSONEncoder)) |
| 303 | + |
| 304 | + def test_downgrade_api(self): |
| 305 | + # make a call to a not existing endpoint, downgrades |
| 306 | + # the current API to a stable one |
| 307 | + tracer = get_dummy_tracer() |
| 308 | + tracer.trace('client.testing').finish() |
| 309 | + trace = tracer.writer.pop() |
| 310 | + |
| 311 | + # the encoder is right but we're targeting an API |
| 312 | + # endpoint that is not available |
| 313 | + api = API('localhost', 7777) |
| 314 | + api._traces = '/v0.0/traces' |
| 315 | + ok_(isinstance(api._encoder, MsgpackEncoder)) |
| 316 | + |
| 317 | + # after the call, we downgrade to a working endpoint |
| 318 | + response = api.send_traces([trace]) |
| 319 | + ok_(response) |
| 320 | + eq_(response.status, 200) |
| 321 | + ok_(isinstance(api._encoder, JSONEncoder)) |
0 commit comments