def test_publish_to_queue(empty_config, maybe_declare, patch_publisher): container = Mock(spec=ServiceContainer) container.service_name = "srcservice" container.config = empty_config ctx_data = {'language': 'en'} service = Mock() worker_ctx = WorkerContext(container, service, "publish", data=ctx_data) publisher = PublishProvider(queue=foobar_queue) publisher.bind("publish", container) producer = Mock() connection = Mock() get_connection, get_producer = patch_publisher(publisher) get_connection.return_value = as_context_manager(connection) get_producer.return_value = as_context_manager(producer) # test declarations publisher.prepare() maybe_declare.assert_called_once_with(foobar_queue, connection) # test publish msg = "msg" headers = { 'nameko.language': 'en', 'nameko.call_id_stack': ['srcservice.publish.0'], } publisher.inject(worker_ctx) service.publish(msg) producer.publish.assert_called_once_with( msg, headers=headers, exchange=foobar_ex)
def test_publish_to_exchange(empty_config, maybe_declare, patch_publisher): container = Mock(spec=ServiceContainer) container.service_name = "srcservice" container.config = empty_config service = Mock() worker_ctx = WorkerContext(container, service, DummyProvider("publish")) publisher = PublishProvider(exchange=foobar_ex) publisher.bind("publish", container) producer = Mock() connection = Mock() get_connection, get_producer = patch_publisher(publisher) get_connection.return_value = as_context_manager(connection) get_producer.return_value = as_context_manager(producer) # test declarations publisher.prepare() maybe_declare.assert_called_once_with(foobar_ex, connection) # test publish msg = "msg" publisher.inject(worker_ctx) service.publish(msg, publish_kwarg="value") headers = {'nameko.call_id_stack': ['srcservice.publish.0']} producer.publish.assert_called_once_with(msg, headers=headers, exchange=foobar_ex, retry=True, retry_policy=DEFAULT_RETRY_POLICY, publish_kwarg="value")
def test_publish_to_queue(empty_config, maybe_declare, patch_publisher): container = Mock(spec=ServiceContainer) container.shared_extensions = {} container.service_name = "srcservice" container.config = empty_config ctx_data = {"language": "en"} service = Mock() worker_ctx = WorkerContext(container, service, DummyProvider("publish"), data=ctx_data) publisher = Publisher(queue=foobar_queue).bind(container, "publish") producer = Mock() connection = Mock() get_connection, get_producer = patch_publisher(publisher) get_connection.return_value = as_context_manager(connection) get_producer.return_value = as_context_manager(producer) # test declarations publisher.setup() maybe_declare.assert_called_once_with(foobar_queue, connection) # test publish msg = "msg" headers = {"nameko.language": "en", "nameko.call_id_stack": ["srcservice.publish.0"]} service.publish = publisher.get_dependency(worker_ctx) service.publish(msg, publish_kwarg="value") producer.publish.assert_called_once_with( msg, headers=headers, exchange=foobar_ex, retry=True, retry_policy=DEFAULT_RETRY_POLICY, publish_kwarg="value" )
def test_publish_to_exchange(maybe_declare, patch_publisher, mock_container): container = mock_container container.service_name = "srcservice" service = Mock() worker_ctx = WorkerContext(container, service, DummyProvider("publish")) publisher = Publisher(exchange=foobar_ex).bind(container, "publish") producer = Mock() connection = Mock() get_connection, get_producer = patch_publisher(publisher) get_connection.return_value = as_context_manager(connection) get_producer.return_value = as_context_manager(producer) # test declarations publisher.setup() maybe_declare.assert_called_once_with(foobar_ex, connection) # test publish msg = "msg" service.publish = publisher.get_dependency(worker_ctx) service.publish(msg, publish_kwarg="value") headers = { 'nameko.call_id_stack': ['srcservice.publish.0'] } producer.publish.assert_called_once_with( msg, headers=headers, exchange=foobar_ex, retry=True, serializer=container.serializer, retry_policy=DEFAULT_RETRY_POLICY, publish_kwarg="value")
def test_publish_to_exchange(empty_config, maybe_declare, patch_publisher): container = Mock(spec=ServiceContainer) container.service_name = "srcservice" container.config = empty_config service = Mock() worker_ctx = WorkerContext(container, service, DummyProvider("publish")) publisher = PublishProvider(exchange=foobar_ex) publisher.bind("publish", container) producer = Mock() connection = Mock() get_connection, get_producer = patch_publisher(publisher) get_connection.return_value = as_context_manager(connection) get_producer.return_value = as_context_manager(producer) # test declarations publisher.prepare() maybe_declare.assert_called_once_with(foobar_ex, connection) # test publish msg = "msg" publisher.inject(worker_ctx) service.publish(msg, publish_kwarg="value") headers = { 'nameko.call_id_stack': ['srcservice.publish.0'] } producer.publish.assert_called_once_with( msg, headers=headers, exchange=foobar_ex, retry=True, retry_policy=DEFAULT_RETRY_POLICY, publish_kwarg="value")
def test_event_dispatcher(empty_config): container = Mock(spec=ServiceContainer) container.service_name = "srcservice" container.config = empty_config service = Mock() worker_ctx = WorkerContext(container, service, "dispatch") event_dispatcher = EventDispatcher() event_dispatcher.bind("dispatch", container) path = 'nameko.messaging.PublishProvider.prepare' with patch(path, autospec=True) as prepare: # test start method event_dispatcher.prepare() assert event_dispatcher.exchange.name == "srcservice.events" assert prepare.called evt = Mock(type="eventtype", data="msg") event_dispatcher.inject(worker_ctx) producer = Mock() with patch.object( event_dispatcher, 'get_producer', autospec=True) as get_producer: get_producer.return_value = as_context_manager(producer) # test dispatch service.dispatch(evt) headers = event_dispatcher.get_message_headers(worker_ctx) producer.publish.assert_called_once_with( evt.data, exchange=event_dispatcher.exchange, headers=headers, routing_key=evt.type)
def test_publish_custom_headers(empty_config, maybe_declare, patch_publisher): container = Mock(spec=ServiceContainer) container.service_name = "srcservice" container.config = empty_config ctx_data = {'language': 'en', 'customheader': 'customvalue'} service = Mock() worker_ctx = CustomWorkerContext(container, service, DummyProvider('method'), data=ctx_data) publisher = PublishProvider(queue=foobar_queue) publisher.bind("publish", container) producer = Mock() connection = Mock() get_connection, get_producer = patch_publisher(publisher) get_connection.return_value = as_context_manager(connection) get_producer.return_value = as_context_manager(producer) # test declarations publisher.prepare() maybe_declare.assert_called_once_with(foobar_queue, connection) # test publish msg = "msg" headers = { 'nameko.language': 'en', 'nameko.customheader': 'customvalue', 'nameko.call_id_stack': ['srcservice.method.0'] } publisher.inject(worker_ctx) service.publish(msg, publish_kwarg="value") producer.publish.assert_called_once_with(msg, headers=headers, exchange=foobar_ex, retry=True, retry_policy=DEFAULT_RETRY_POLICY, publish_kwarg="value")
def test_publish_custom_headers(mock_container, maybe_declare, patch_publisher): container = mock_container container.service_name = "srcservice" ctx_data = {'language': 'en', 'customheader': 'customvalue'} service = Mock() worker_ctx = WorkerContext(container, service, DummyProvider('method'), data=ctx_data) publisher = Publisher(queue=foobar_queue).bind(container, "publish") producer = Mock() connection = Mock() get_connection, get_producer = patch_publisher(publisher) get_connection.return_value = as_context_manager(connection) get_producer.return_value = as_context_manager(producer) # test declarations publisher.setup() maybe_declare.assert_called_once_with(foobar_queue, connection) # test publish msg = "msg" headers = { 'nameko.language': 'en', 'nameko.customheader': 'customvalue', 'nameko.call_id_stack': ['srcservice.method.0'] } service.publish = publisher.get_dependency(worker_ctx) service.publish(msg, publish_kwarg="value") producer.publish.assert_called_once_with(msg, headers=headers, exchange=foobar_ex, retry=True, serializer=container.serializer, retry_policy=DEFAULT_RETRY_POLICY, publish_kwarg="value")
def test_publish_to_queue(empty_config, maybe_declare, patch_publisher): container = Mock(spec=ServiceContainer) container.shared_extensions = {} container.service_name = "srcservice" container.config = empty_config ctx_data = {'language': 'en'} service = Mock() worker_ctx = WorkerContext(container, service, DummyProvider("publish"), data=ctx_data) publisher = Publisher(queue=foobar_queue).bind(container, "publish") producer = Mock() connection = Mock() get_connection, get_producer = patch_publisher(publisher) get_connection.return_value = as_context_manager(connection) get_producer.return_value = as_context_manager(producer) # test declarations publisher.setup() maybe_declare.assert_called_once_with(foobar_queue, connection) # test publish msg = "msg" headers = { 'nameko.language': 'en', 'nameko.call_id_stack': ['srcservice.publish.0'], } service.publish = publisher.get_dependency(worker_ctx) service.publish(msg, publish_kwarg="value") producer.publish.assert_called_once_with(msg, headers=headers, exchange=foobar_ex, retry=True, retry_policy=DEFAULT_RETRY_POLICY, publish_kwarg="value")
def test_publish_custom_headers(mock_container, maybe_declare, patch_publisher): container = mock_container container.service_name = "srcservice" ctx_data = {'language': 'en', 'customheader': 'customvalue'} service = Mock() worker_ctx = WorkerContext( container, service, DummyProvider('method'), data=ctx_data ) publisher = Publisher(queue=foobar_queue).bind(container, "publish") producer = Mock() connection = Mock() get_connection, get_producer = patch_publisher(publisher) get_connection.return_value = as_context_manager(connection) get_producer.return_value = as_context_manager(producer) # test declarations publisher.setup() maybe_declare.assert_called_once_with(foobar_queue, connection) # test publish msg = "msg" headers = {'nameko.language': 'en', 'nameko.customheader': 'customvalue', 'nameko.call_id_stack': ['srcservice.method.0']} service.publish = publisher.get_dependency(worker_ctx) service.publish(msg, publish_kwarg="value") producer.publish.assert_called_once_with( msg, headers=headers, exchange=foobar_ex, retry=True, serializer=container.serializer, retry_policy=DEFAULT_RETRY_POLICY, publish_kwarg="value")
def test_publish_custom_headers(empty_config, maybe_declare, patch_publisher): container = Mock(spec=ServiceContainer) container.service_name = "srcservice" container.config = empty_config ctx_data = {'language': 'en', 'customheader': 'customvalue'} service = Mock() worker_ctx = CustomWorkerContext(container, service, DummyProvider('method'), data=ctx_data) publisher = PublishProvider(queue=foobar_queue) publisher.bind("publish", container) producer = Mock() connection = Mock() get_connection, get_producer = patch_publisher(publisher) get_connection.return_value = as_context_manager(connection) get_producer.return_value = as_context_manager(producer) # test declarations publisher.prepare() maybe_declare.assert_called_once_with(foobar_queue, connection) # test publish msg = "msg" headers = {'nameko.language': 'en', 'nameko.customheader': 'customvalue', 'nameko.call_id_stack': ['srcservice.method.0']} publisher.inject(worker_ctx) service.publish(msg, publish_kwarg="value") producer.publish.assert_called_once_with( msg, headers=headers, exchange=foobar_ex, retry=True, retry_policy=DEFAULT_RETRY_POLICY, publish_kwarg="value")
def test_event_dispatcher(empty_config): container = Mock(spec=ServiceContainer) container.service_name = "srcservice" container.config = empty_config service = Mock() worker_ctx = WorkerContext(container, service, DummyProvider("dispatch")) event_dispatcher = EventDispatcher() event_dispatcher.bind("dispatch", container) path = 'nameko.messaging.PublishProvider.prepare' with patch(path, autospec=True) as prepare: # test start method event_dispatcher.prepare() assert event_dispatcher.exchange.name == "srcservice.events" assert prepare.called evt = Mock(type="eventtype", data="msg") event_dispatcher.inject(worker_ctx) producer = Mock() with patch.object(event_dispatcher, 'get_producer', autospec=True) as get_producer: get_producer.return_value = as_context_manager(producer) # test dispatch service.dispatch(evt, retry_policy={'max_retries': 5}) headers = event_dispatcher.get_message_headers(worker_ctx) producer.publish.assert_called_once_with( evt.data, exchange=event_dispatcher.exchange, headers=headers, routing_key=evt.type, retry=True, retry_policy={'max_retries': 5})