def test_regular_parameters(self, parameter, mock_container, producer): """ Verify that most parameters can be specified at RpcProxy instantiation time. """ mock_container.config = {'AMQP_URI': 'memory://localhost'} mock_container.shared_extensions = {} mock_container.service_name = "service" worker_ctx = Mock() worker_ctx.container = mock_container worker_ctx.context_data = {} value = Mock() rpc_proxy = RpcProxy("service-name", **{ parameter: value }).bind(mock_container, "service_rpc") rpc_proxy.setup() rpc_proxy.rpc_reply_listener.setup() service_rpc = rpc_proxy.get_dependency(worker_ctx) service_rpc.method.call_async() assert producer.publish.call_args[1][parameter] == value
def test_headers(self, mock_container, producer): """ Headers can be provided at instantiation time, and are merged with Nameko headers. """ mock_container.config = {'AMQP_URI': 'memory://localhost'} mock_container.shared_extensions = {} mock_container.service_name = "service-name" # use a real worker context so nameko headers are generated service = Mock() entrypoint = Mock(method_name="method") worker_ctx = WorkerContext(mock_container, service, entrypoint, data={'context': 'data'}) nameko_headers = { 'nameko.context': 'data', 'nameko.call_id_stack': ['service-name.method.0'], } value = {'foo': Mock()} rpc_proxy = RpcProxy("service-name", **{ 'headers': value }).bind(mock_container, "service_rpc") rpc_proxy.setup() rpc_proxy.rpc_reply_listener.setup() service_rpc = rpc_proxy.get_dependency(worker_ctx) def merge_dicts(base, *updates): merged = base.copy() [merged.update(update) for update in updates] return merged service_rpc.method.call_async() assert producer.publish.call_args[1]['headers'] == merge_dicts( nameko_headers, value)
def test_restricted_parameters(self, patch_uuid, mock_container, producer): """ Verify that providing routing parameters at instantiation time has no effect. """ mock_container.config = {'AMQP_URI': 'memory://localhost'} mock_container.shared_extensions = {} mock_container.service_name = "service" worker_ctx = Mock() worker_ctx.container = mock_container worker_ctx.context_data = {} uuid1 = uuid.uuid4() uuid2 = uuid.uuid4() patch_uuid.uuid4.side_effect = [uuid1, uuid2] restricted_params = ('exchange', 'routing_key', 'mandatory', 'correlation_id', 'reply_to') rpc_proxy = RpcProxy("service", **{param: Mock() for param in restricted_params }).bind(mock_container, "service_rpc") rpc_proxy.setup() rpc_proxy.rpc_reply_listener.setup() service_rpc = rpc_proxy.get_dependency(worker_ctx) service_rpc.method.call_async() publish_params = producer.publish.call_args[1] assert publish_params['exchange'].name == "nameko-rpc" assert publish_params['routing_key'] == 'service.method' assert publish_params['mandatory'] is True assert publish_params['reply_to'] == str(uuid1) assert publish_params['correlation_id'] == str(uuid2)