def setUp(self): self.agent = mock.Mock() self.tracer = RawRESTkinHTTPTracer(self.agent, 'http://trace.it') self.trace = Trace('test', 1, 2, tracers=[self.tracer])
class RawRESTkinHTTPTracerTests(TestCase): def assertBodyEquals(self, bodyProducer, expectedOutput): output = StringIO() consumer = FileConsumer(output) def _check_body(_): self.assertEqual( json.loads(output.getvalue()), expectedOutput ) d = bodyProducer.startProducing(consumer) d.addCallback(_check_body) return d def setUp(self): self.agent = mock.Mock() self.tracer = RawRESTkinHTTPTracer(self.agent, 'http://trace.it') self.trace = Trace('test', 1, 2, tracers=[self.tracer]) def test_verifyObject(self): verifyObject(ITracer, self.tracer) def test_posts_immediately(self): self.trace.record(Annotation.client_send(1)) self.assertEqual(self.agent.request.call_count, 1) args = self.agent.request.mock_calls[0][1] self.assertEqual(('POST', 'http://trace.it', Headers({})), args[:3]) bodyProducer = args[3] return self.assertBodyEquals( bodyProducer, [{'trace_id': '0000000000000001', 'span_id': '0000000000000002', 'name': 'test', 'annotations': [ {'type': 'timestamp', 'value': 1, 'key': 'cs'} ]}]) def test_handles_batched_traces(self): t1 = self.trace t2 = Trace('test2', 3, 4) cs1 = Annotation.client_send(1) cs2 = Annotation.client_send(2) cr1 = Annotation.client_recv(3) cr2 = Annotation.client_recv(4) self.tracer.record([(t1, [cs1, cr1]), (t2, [cs2, cr2])]) self.assertEqual(self.agent.request.call_count, 1) args = self.agent.request.mock_calls[0][1] self.assertEqual(('POST', 'http://trace.it', Headers({})), args[:3]) bodyProducer = args[3] return self.assertBodyEquals( bodyProducer, [{'trace_id': '0000000000000001', 'span_id': '0000000000000002', 'name': 'test', 'annotations': [ {'type': 'timestamp', 'value': 1, 'key': 'cs'}, {'type': 'timestamp', 'value': 3, 'key': 'cr'} ]}, {'trace_id': '0000000000000003', 'span_id': '0000000000000004', 'name': 'test2', 'annotations': [ {'type': 'timestamp', 'value': 2, 'key': 'cs'}, {'type': 'timestamp', 'value': 4, 'key': 'cr'} ]}])
class RawRESTkinHTTPTracerTests(TestCase): def assertBodyEquals(self, bodyProducer, expectedOutput): output = StringIO() def _check_body(_): self.assertEqual(json.loads(output.getvalue()), expectedOutput) d = bodyProducer.startProducing(output) d.addCallback(_check_body) return d def setUp(self): self.agent = mock.Mock() self.tracer = RawRESTkinHTTPTracer(self.agent, 'http://trace.it') self.trace = Trace('test', 1, 2, tracers=[self.tracer]) def test_verifyObject(self): verifyObject(ITracer, self.tracer) def test_posts_immediately(self): self.trace.record(Annotation.client_send(1)) self.assertEqual(self.agent.request.call_count, 1) args = self.agent.request.mock_calls[0][1] self.assertEqual(('POST', 'http://trace.it', Headers({})), args[:3]) bodyProducer = args[3] return self.assertBodyEquals( bodyProducer, [{ 'trace_id': '0000000000000001', 'span_id': '0000000000000002', 'name': 'test', 'annotations': [{ 'type': 'timestamp', 'value': 1, 'key': 'cs' }] }]) def test_handles_batched_traces(self): t1 = self.trace t2 = Trace('test2', 3, 4) cs1 = Annotation.client_send(1) cs2 = Annotation.client_send(2) cr1 = Annotation.client_recv(3) cr2 = Annotation.client_recv(4) self.tracer.record([(t1, [cs1, cr1]), (t2, [cs2, cr2])]) self.assertEqual(self.agent.request.call_count, 1) args = self.agent.request.mock_calls[0][1] self.assertEqual(('POST', 'http://trace.it', Headers({})), args[:3]) bodyProducer = args[3] return self.assertBodyEquals(bodyProducer, [{ 'trace_id': '0000000000000001', 'span_id': '0000000000000002', 'name': 'test', 'annotations': [{ 'type': 'timestamp', 'value': 1, 'key': 'cs' }, { 'type': 'timestamp', 'value': 3, 'key': 'cr' }] }, { 'trace_id': '0000000000000003', 'span_id': '0000000000000004', 'name': 'test2', 'annotations': [{ 'type': 'timestamp', 'value': 2, 'key': 'cs' }, { 'type': 'timestamp', 'value': 4, 'key': 'cr' }] }])