def test_clear(self): annotations = [Mock(spec=Annotation), Mock(spec=Annotation)] binary_annotations = [ Mock(spec=BinaryAnnotation), Mock(spec=BinaryAnnotation) ] store = ThreadLocalDataStore() store.set(ZipkinData(sampled=True, trace_id=Mock())) store.set_rpc_name(Mock()) for annotation in annotations + binary_annotations: store.record(annotation) store.clear() self.assertListEqual([], store.get_annotations()) self.assertListEqual([], store.get_binary_annotations()) self.assertZipkinDataEquals(ZipkinData(), store.get()) self.assertIsNone(store.get_rpc_name())
def test_record_noop_if_not_sampled(self): self.store.get = lambda: ZipkinData(sampled=False) self.store._record_annotation = Mock() self.store._record_binary_annotation = Mock() self.store.record(Mock()) self.assertFalse(self.store._record_annotation.called) self.assertFalse(self.store._record_binary_annotation.called)
def test_record_delegates_if_sampled(self): self.store.get = lambda: ZipkinData(sampled=True) annotation = Mock(spec=Annotation) binary_annotation = Mock(spec=BinaryAnnotation) self.store.record(annotation) self.store.record(binary_annotation) self.store._record_annotation.assert_called_once_with(annotation) self.store._record_binary_annotation.assert_called_once_with( binary_annotation)
def test_annotations(self): annotations = [Mock(spec=Annotation), Mock(spec=Annotation)] binary_annotations = [ Mock(spec=BinaryAnnotation), Mock(spec=BinaryAnnotation) ] store = ThreadLocalDataStore() store.clear() store.set(ZipkinData(sampled=True)) for annotation in annotations + binary_annotations: store.record(annotation) self.assertListEqual(annotations, store.get_annotations()) self.assertListEqual(binary_annotations, store.get_binary_annotations())
def test_nonascii_input(self): timestamp, duration = Mock(), Mock() uri_in = u'\ufffd\ufffd/\x01' uri_out = uri_in.encode('utf-8') self.store.get.return_value = ZipkinData(trace_id=ZipkinId(42), span_id=ZipkinId(4242), parent_span_id=ZipkinId(1773), sampled=True) self.store.get_binary_annotations.return_value = [ self.api._build_binary_annotation('http.uri', uri_in) ] self.store.get_annotations.return_value = [ self.api._build_annotation(uri_in) ] self.store.get_rpc_name.return_value = 'test-rpc-name' self.assertEqual( self.api._build_span(timestamp, duration).annotations[0].value, uri_out) self.assertEqual( self.api._build_span(timestamp, duration).binary_annotations[0].value, uri_out)
def test_integration(self): self.api.endpoint.ipv4 = 2130706433 binary_annotations = [ self.api._build_binary_annotation('awesome', True) ] annotations = [ self.api._build_annotation('sr'), self.api._build_annotation('ss'), ] self.store.get_annotations.return_value = annotations self.store.get_binary_annotations.return_value = binary_annotations self.store.get.return_value = ZipkinData(trace_id=ZipkinId(42), span_id=ZipkinId(4242), parent_span_id=ZipkinId(1773), sampled=True) self.store.get_rpc_name.return_value = 'test-name' self.mock_time.time.return_value = 1024 self.assertEqual(self.api.build_log_message(), self.api.build_log_message()) self.assertEqual( self.api.build_log_message(), '''CgABAAAAAAAAACoLAAMAAAAJdGVzdC1uYW1lCgAEAAAAAAAAEJIKAAUAAAAAAAAG7Q8AB''' '''gwAAAACCgABAAAAAAAAAAELAAIAAAACc3IMAAMIAAF/AAABAAAKAAEAAAAAAAAAAQsAAgAAAAJzcwwAAwgAAX8AAAEAAA8ACAwAAAABCwABAAAAB''' '''2F3ZXNvbWULAAIAAAABMQgAAwAAAAAMAAQIAAF/AAABAAACAAkAAA==''')
def test_get_without_set_returns_empty_zipkin_data(self): store = ThreadLocalDataStore() store.clear() self.assertZipkinDataEquals(ZipkinData(), store.get())
def test_dont_freak_out_if_thread_local_store_is_gone(self): store = ThreadLocalDataStore() ThreadLocalDataStore.thread_local_data = object() self.assertIsNone(store.get_rpc_name()) self.assertZipkinDataEquals(ZipkinData(), store.get())