当前位置: 首页>>代码示例>>Python>>正文


Python Mock.service_name方法代码示例

本文整理汇总了Python中mock.Mock.service_name方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.service_name方法的具体用法?Python Mock.service_name怎么用?Python Mock.service_name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mock.Mock的用法示例。


在下文中一共展示了Mock.service_name方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: install_three_tasks_test

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
    def install_three_tasks_test(self, proxy_getter):
        """Install with three tasks."""
        observers = []

        observer = Mock()
        observer.is_service_available = True
        observer.service_name = "A"
        observer.proxy.InstallWithTasks.return_value = ["/A/1"]

        observers.append(observer)

        observer = Mock()
        observer.is_service_available = True
        observer.service_name = "B"
        observer.proxy.InstallWithTasks.return_value = ["/B/1", "/B/2"]

        observers.append(observer)

        task_proxy = Mock()
        task_proxy.Steps = 1
        proxy_getter.return_value = task_proxy

        install_manager = InstallManager()
        install_manager.module_observers = observers
        main_task = install_manager.install_system_with_task()

        proxy_getter.assert_has_calls([
            call("A", "/A/1"),
            call("B", "/B/1"),
            call("B", "/B/2")
        ])
        self.assertIsInstance(main_task, SystemInstallationTask)
        self.assertEqual(main_task._subtasks, [task_proxy, task_proxy, task_proxy])
开发者ID:rvykydal,项目名称:anaconda,代码行数:35,代码来源:install_manager_test.py

示例2: test_list_services

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
 def test_list_services(self):
     sms = ServiceManagementService('secret', 'cert.pem')
     hs1 = Mock()
     hs1.service_name = 'juju-upgrade-foo'
     hs2 = Mock()
     hs2.service_name = 'juju-deploy-foo'
     services = [hs1, hs2]
     with patch.object(sms, 'list_hosted_services', autospec=True,
                       return_value=services) as ls_mock:
         services = list_services(sms, 'juju-deploy-*', verbose=False)
     ls_mock.assert_called_once_with()
     self.assertEqual([hs2], services)
开发者ID:mjs,项目名称:juju,代码行数:14,代码来源:test_winazure.py

示例3: test_consume_from_rabbit

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
def test_consume_from_rabbit(rabbit_manager, rabbit_config):

    vhost = rabbit_config['vhost']

    container = Mock(spec=ServiceContainer)
    container.worker_ctx_cls = CustomWorkerContext
    container.service_name = "service"
    container.config = rabbit_config
    container.max_workers = 10

    def spawn_thread(method, protected):
        return eventlet.spawn(method)

    container.spawn_managed_thread = spawn_thread

    worker_ctx = CustomWorkerContext(container, None, None)

    factory = DependencyFactory(
        ConsumeProvider, queue=foobar_queue, requeue_on_error=False)
    consumer = factory.create_and_bind_instance("injection_name", container)

    # prepare and start dependencies
    consumer.prepare()
    consumer.queue_consumer.prepare()
    consumer.start()
    consumer.queue_consumer.start()

    # test queue, exchange and binding created in rabbit
    exchanges = rabbit_manager.get_exchanges(vhost)
    queues = rabbit_manager.get_queues(vhost)
    bindings = rabbit_manager.get_queue_bindings(vhost, foobar_queue.name)

    assert "foobar_ex" in [exchange['name'] for exchange in exchanges]
    assert "foobar_queue" in [queue['name'] for queue in queues]
    assert "foobar_ex" in [binding['source'] for binding in bindings]

    # test message consumed from queue
    container.spawn_worker.return_value = worker_ctx

    headers = {'nameko.language': 'en', 'nameko.customheader': 'customvalue'}
    rabbit_manager.publish(
        vhost, foobar_ex.name, '', 'msg', properties=dict(headers=headers))

    ctx_data = {
        'language': 'en',
        'customheader': 'customvalue',
    }
    with wait_for_call(CONSUME_TIMEOUT, container.spawn_worker) as method:
        method.assert_called_once_with(
            consumer, ('msg', ), {},
            context_data=ctx_data,
            handle_result=ANY_PARTIAL)
        handle_result = method.call_args[1]['handle_result']

    # ack message
    handle_result(worker_ctx, 'result')

    # stop will hang if the consumer hasn't acked or requeued messages
    with eventlet.timeout.Timeout(CONSUME_TIMEOUT):
        consumer.stop()
开发者ID:pombredanne,项目名称:nameko,代码行数:62,代码来源:test_messaging.py

示例4: test_publish_to_queue

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
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)
开发者ID:pombredanne,项目名称:nameko,代码行数:36,代码来源:test_messaging.py

示例5: test_nova_rpc_provider

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
def test_nova_rpc_provider(empty_config):

    rpc_consumer = Mock()
    message = Mock(headers={})

    message_body = {
        'method': 'method',
        'args': {"arg": "arg_value"},
        'msg_id': 'msg_id',
        '_context_user_id': 'user_id'
    }

    class Service(object):
        def method(self, arg):
            pass

    container = Mock(spec=ServiceContainer)
    container.shared_extensions = {}
    container.service_cls = Service
    container.worker_ctx_cls = WorkerContext
    container.service_name = "service"
    container.config = empty_config

    entrypoint = NovaRpc().bind(container, "method")
    entrypoint.setup()
    entrypoint.rpc_consumer = rpc_consumer

    container.spawn_worker.side_effect = ContainerBeingKilled()
    entrypoint.handle_message(message_body, message)
    assert rpc_consumer.requeue_message.called
开发者ID:pombredanne,项目名称:nameko-nova-compat,代码行数:32,代码来源:test_dependencies.py

示例6: test_event_dispatcher

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
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(retry_policy={'max_retries': 5}).bind(
        container, attr_name="dispatch")
    event_dispatcher.setup()

    service.dispatch = event_dispatcher.get_dependency(worker_ctx)

    from mock import ANY
    with patch('nameko.standalone.events.producers') as mock_producers:
        with mock_producers[ANY].acquire() as mock_producer:

            service.dispatch('eventtype', 'msg')
            headers = event_dispatcher.get_message_headers(worker_ctx)
    mock_producer.publish.assert_called_once_with(
        'msg', exchange=ANY, headers=headers,
        routing_key='eventtype', retry=True, retry_policy={'max_retries': 5})
    _, call_kwargs = mock_producer.publish.call_args
    exchange = call_kwargs['exchange']
    assert exchange.name == 'srcservice.events'
开发者ID:SivagnanamCiena,项目名称:nameko,代码行数:29,代码来源:test_events.py

示例7: test_publish_to_queue

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
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"
    )
开发者ID:mamachanko,项目名称:nameko,代码行数:34,代码来源:test_messaging.py

示例8: test_unserialisable_headers

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
def test_unserialisable_headers(rabbit_manager, rabbit_config):

    vhost = rabbit_config["vhost"]

    container = Mock(spec=ServiceContainer)
    container.service_name = "service"
    container.config = rabbit_config
    container.spawn_managed_thread = eventlet.spawn

    ctx_data = {"language": "en", "customheader": None}
    service = Mock()
    worker_ctx = CustomWorkerContext(container, service, DummyProvider("method"), data=ctx_data)

    publisher = Publisher(exchange=foobar_ex, queue=foobar_queue).bind(container, "publish")

    publisher.setup()
    publisher.start()

    service.publish = publisher.get_dependency(worker_ctx)
    service.publish("msg")
    messages = rabbit_manager.get_messages(vhost, foobar_queue.name)

    assert messages[0]["properties"]["headers"] == {
        "nameko.language": "en",
        "nameko.call_id_stack": ["service.method.0"],
        # no `customheader`
    }
开发者ID:mamachanko,项目名称:nameko,代码行数:29,代码来源:test_messaging.py

示例9: test_publish_to_exchange

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
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")
开发者ID:ahmb,项目名称:nameko,代码行数:35,代码来源:test_messaging.py

示例10: test_dispatch_to_rabbit

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
def test_dispatch_to_rabbit(rabbit_manager, rabbit_config):

    vhost = rabbit_config['vhost']

    container = Mock(spec=ServiceContainer)
    container.service_name = "srcservice"
    container.config = rabbit_config

    service = Mock()
    worker_ctx = WorkerContext(container, service, None)

    dispatcher = EventDispatcher()
    dispatcher.bind("dispatch", container)
    dispatcher.prepare()
    dispatcher.start()

    # we should have an exchange but no queues
    exchanges = rabbit_manager.get_exchanges(vhost)
    queues = rabbit_manager.get_queues(vhost)
    assert "srcservice.events" in [exchange['name'] for exchange in exchanges]
    assert queues == []

    # manually add a queue to capture the events
    rabbit_manager.create_queue(vhost, "event-sink", auto_delete=True)
    rabbit_manager.create_binding(vhost, "srcservice.events", "event-sink",
                                  rt_key=ExampleEvent.type)

    dispatcher.inject(worker_ctx)
    service.dispatch(ExampleEvent("msg"))

    # test event receieved on manually added queue
    messages = rabbit_manager.get_messages(vhost, "event-sink")
    assert ['msg'] == [msg['payload'] for msg in messages]
开发者ID:pombredanne,项目名称:nameko,代码行数:35,代码来源:test_events.py

示例11: test_event_dispatcher

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
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)
开发者ID:pombredanne,项目名称:nameko,代码行数:37,代码来源:test_events.py

示例12: test_unserialisable_headers

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
def test_unserialisable_headers(rabbit_manager, rabbit_config):

    vhost = rabbit_config['vhost']

    container = Mock(spec=ServiceContainer)
    container.service_name = "service"
    container.config = rabbit_config
    container.spawn_managed_thread = eventlet.spawn

    ctx_data = {'language': 'en', 'customheader': None}
    service = Mock()
    worker_ctx = CustomWorkerContext(
        container, service, 'method', data=ctx_data)

    publisher = PublishProvider(exchange=foobar_ex, queue=foobar_queue)
    publisher.bind("publish", container)

    publisher.prepare()
    publisher.start()

    publisher.inject(worker_ctx)
    service.publish("msg")
    messages = rabbit_manager.get_messages(vhost, foobar_queue.name)

    assert messages[0]['properties']['headers'] == {
        'nameko.language': 'en',
        'nameko.call_id_stack': ['service.method.0'],
        # no `customheader`
    }
开发者ID:pombredanne,项目名称:nameko,代码行数:31,代码来源:test_messaging.py

示例13: test_dispatch_to_rabbit

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
def test_dispatch_to_rabbit(rabbit_manager, rabbit_config):

    vhost = rabbit_config['vhost']

    container = Mock(spec=ServiceContainer)
    container.shared_extensions = {}
    container.service_name = "srcservice"
    container.config = rabbit_config

    service = Mock()
    worker_ctx = WorkerContext(container, service, DummyProvider())

    dispatcher = EventDispatcher().bind(container, 'dispatch')
    dispatcher.setup()
    dispatcher.start()

    # we should have an exchange but no queues
    exchanges = rabbit_manager.get_exchanges(vhost)
    queues = rabbit_manager.get_queues(vhost)
    assert "srcservice.events" in [exchange['name'] for exchange in exchanges]
    assert queues == []

    # manually add a queue to capture the events
    rabbit_manager.create_queue(vhost, "event-sink", auto_delete=True)
    rabbit_manager.create_queue_binding(
        vhost, "srcservice.events", "event-sink", routing_key="eventtype")

    service.dispatch = dispatcher.get_dependency(worker_ctx)
    service.dispatch("eventtype", "msg")

    # test event receieved on manually added queue
    messages = rabbit_manager.get_messages(vhost, "event-sink")
    assert ['msg'] == [msg['payload'] for msg in messages]
开发者ID:SivagnanamCiena,项目名称:nameko,代码行数:35,代码来源:test_events.py

示例14: test_stringify

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
def test_stringify():
    dep = DependencyProvider()
    assert str(dep).startswith("<DependencyProvider [unbound] at")

    container = Mock()
    container.service_name = "foobar"
    dep.bind("spam", container)
    assert str(dep).startswith("<DependencyProvider [foobar.spam] at")
开发者ID:pombredanne,项目名称:nameko,代码行数:10,代码来源:test_dependencies.py

示例15: test_consume_provider

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import service_name [as 别名]
def test_consume_provider(empty_config):

    container = Mock(spec=ServiceContainer)
    container.shared_extensions = {}
    container.worker_ctx_cls = WorkerContext
    container.service_name = "service"
    container.config = empty_config

    worker_ctx = WorkerContext(container, None, DummyProvider())

    spawn_worker = container.spawn_worker
    spawn_worker.return_value = worker_ctx

    queue_consumer = Mock()

    consume_provider = Consumer(queue=foobar_queue, requeue_on_error=False).bind(container, "consume")
    consume_provider.queue_consumer = queue_consumer

    message = Mock(headers={})

    # test lifecycle
    consume_provider.setup()
    queue_consumer.register_provider.assert_called_once_with(consume_provider)

    consume_provider.stop()
    queue_consumer.unregister_provider.assert_called_once_with(consume_provider)

    # test handling successful call
    queue_consumer.reset_mock()
    consume_provider.handle_message("body", message)
    handle_result = spawn_worker.call_args[1]["handle_result"]
    handle_result(worker_ctx, "result")
    queue_consumer.ack_message.assert_called_once_with(message)

    # test handling failed call without requeue
    queue_consumer.reset_mock()
    consume_provider.requeue_on_error = False
    consume_provider.handle_message("body", message)
    handle_result = spawn_worker.call_args[1]["handle_result"]
    handle_result(worker_ctx, None, (Exception, Exception("Error"), "tb"))
    queue_consumer.ack_message.assert_called_once_with(message)

    # test handling failed call with requeue
    queue_consumer.reset_mock()
    consume_provider.requeue_on_error = True
    consume_provider.handle_message("body", message)
    handle_result = spawn_worker.call_args[1]["handle_result"]
    handle_result(worker_ctx, None, (Exception, Exception("Error"), "tb"))
    assert not queue_consumer.ack_message.called
    queue_consumer.requeue_message.assert_called_once_with(message)

    # test requeueing on ContainerBeingKilled (even without requeue_on_error)
    queue_consumer.reset_mock()
    consume_provider.requeue_on_error = False
    spawn_worker.side_effect = ContainerBeingKilled()
    consume_provider.handle_message("body", message)
    assert not queue_consumer.ack_message.called
    queue_consumer.requeue_message.assert_called_once_with(message)
开发者ID:mamachanko,项目名称:nameko,代码行数:60,代码来源:test_messaging.py


注:本文中的mock.Mock.service_name方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。