def test_decorate_basic_publish_published_message_to_queue( get_span: mock.MagicMock, ) -> None: callback = mock.MagicMock() tracer = mock.MagicMock() channel = mock.MagicMock(spec=Channel) exchange_name = "" routing_key = "test-routing-key" properties = mock.MagicMock() mock_body = b"mock_body" decorated_basic_publish = utils._decorate_basic_publish( callback, channel, tracer ) decorated_basic_publish( exchange_name, routing_key, mock_body, properties ) get_span.assert_called_once_with( tracer, channel, properties, destination=routing_key, span_kind=SpanKind.PRODUCER, task_name="(temporary)", operation=None, )
def test_decorate_basic_publish( self, use_span: mock.MagicMock, get_current: mock.MagicMock, inject: mock.MagicMock, get_span: mock.MagicMock, ) -> None: callback = mock.MagicMock() tracer = mock.MagicMock() channel = mock.MagicMock(spec=Channel) method = mock.MagicMock(spec=Basic.Deliver) properties = mock.MagicMock() mock_body = b"mock_body" decorated_basic_publish = utils._decorate_basic_publish( callback, channel, tracer) retval = decorated_basic_publish(channel, method, mock_body, properties) get_current.assert_called_once() get_span.assert_called_once_with( tracer, channel, properties, span_kind=SpanKind.PRODUCER, task_name="(temporary)", ctx=get_current.return_value, operation=None, ) use_span.assert_called_once_with(get_span.return_value, end_on_exit=True) get_span.return_value.is_recording.assert_called_once() inject.assert_called_once_with(properties.headers) callback.assert_called_once_with(channel, method, mock_body, properties, False) self.assertEqual(retval, callback.return_value)
def _instrument_basic_publish(channel: Channel, tracer: Tracer) -> None: original_function = getattr(channel, "basic_publish") decorated_function = utils._decorate_basic_publish( original_function, channel, tracer) setattr(decorated_function, "_original_function", original_function) channel.__setattr__("basic_publish", decorated_function) channel.basic_publish = decorated_function
def _instrument_basic_publish( channel: BlockingChannel, tracer: Tracer, publish_hook: utils.HookT = utils.dummy_callback, ) -> None: original_function = getattr(channel, "basic_publish") decorated_function = utils._decorate_basic_publish( original_function, channel, tracer, publish_hook) setattr(decorated_function, "_original_function", original_function) channel.__setattr__("basic_publish", decorated_function) channel.basic_publish = decorated_function
def test_decorate_basic_publish_with_hook( self, use_span: mock.MagicMock, inject: mock.MagicMock, get_span: mock.MagicMock, ) -> None: callback = mock.MagicMock() tracer = mock.MagicMock() channel = mock.MagicMock(spec=Channel) exchange_name = "test-exchange" routing_key = "test-routing-key" properties = mock.MagicMock() mock_body = b"mock_body" publish_hook = mock.MagicMock() decorated_basic_publish = utils._decorate_basic_publish( callback, channel, tracer, publish_hook ) retval = decorated_basic_publish( exchange_name, routing_key, mock_body, properties ) get_span.assert_called_once_with( tracer, channel, properties, destination=exchange_name, span_kind=SpanKind.PRODUCER, task_name="(temporary)", operation=None, ) use_span.assert_called_once_with( get_span.return_value, end_on_exit=True ) get_span.return_value.is_recording.assert_called_once() inject.assert_called_once_with(properties.headers) publish_hook.assert_called_once_with( get_span.return_value, mock_body, properties ) callback.assert_called_once_with( exchange_name, routing_key, mock_body, properties, False ) self.assertEqual(retval, callback.return_value)
def test_decorate_basic_publish_no_properties( self, basic_properties: mock.MagicMock, use_span: mock.MagicMock, inject: mock.MagicMock, get_span: mock.MagicMock, ) -> None: callback = mock.MagicMock() tracer = mock.MagicMock() channel = mock.MagicMock(spec=Channel) method = mock.MagicMock(spec=Basic.Deliver) mock_body = b"mock_body" decorated_basic_publish = utils._decorate_basic_publish( callback, channel, tracer ) retval = decorated_basic_publish(channel, method, body=mock_body) basic_properties.assert_called_once_with(BasicProperties, headers={}) use_span.assert_called_once_with( get_span.return_value, end_on_exit=True ) get_span.return_value.is_recording.assert_called_once() inject.assert_called_once_with(basic_properties.return_value.headers) self.assertEqual(retval, callback.return_value)