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


Python asynctest.CoroutineMock方法代码示例

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


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

示例1: session

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def session(event_loop):
    fake_retry_policy = asynctest.MagicMock(wraps=aiozk.session.RetryPolicy.forever())
    session = aiozk.session.Session(
        'zookeeper.test',
        timeout=10,
        retry_policy=fake_retry_policy,
        allow_read_only=True,
        read_timeout=30,
        loop=asynctest.MagicMock(wraps=event_loop),
    )
    session.state.transition_to(aiozk.session.States.CONNECTED)
    session.conn = asynctest.MagicMock()
    session.conn.send = asynctest.CoroutineMock()
    session.conn.close = asynctest.CoroutineMock()
    session.ensure_safe_state = asynctest.CoroutineMock()
    session.set_heartbeat = mock.Mock()
    return session 
开发者ID:micro-fan,项目名称:aiozk,代码行数:19,代码来源:test_session.py

示例2: test_watch_timeout_with_resource_version

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_watch_timeout_with_resource_version(self):
        fake_resp = CoroutineMock()
        fake_resp.content.readline = CoroutineMock()
        fake_resp.release = Mock()

        fake_resp.content.readline.side_effect = [asyncio.TimeoutError(),
                                                  b""]

        fake_api = Mock()
        fake_api.get_namespaces = CoroutineMock(return_value=fake_resp)
        fake_api.get_namespaces.__doc__ = ':return: V1NamespaceList'

        watch = kubernetes_asyncio.watch.Watch()
        async with watch.stream(fake_api.get_namespaces, resource_version='10') as stream:
            async for e in stream: # noqa
                pass

        # all calls use the passed resource version
        fake_api.get_namespaces.assert_has_calls(
            [call(_preload_content=False, watch=True, resource_version='10'),
             call(_preload_content=False, watch=True, resource_version='10')])

        fake_resp.release.assert_called_once_with()
        self.assertEqual(watch.resource_version, '10') 
开发者ID:tomplus,项目名称:kubernetes_asyncio,代码行数:26,代码来源:watch_test.py

示例3: test_emit_writes_records_into_the_stream

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_emit_writes_records_into_the_stream(self):
        msg = self.record.msg
        formatter = Mock(format=Mock(return_value=msg))
        writer = Mock(write=Mock(), drain=CoroutineMock())

        with patch(
            "aiologger.handlers.streams.StreamWriter", return_value=writer
        ):
            handler = AsyncStreamHandler(
                level=10, stream=self.write_pipe, formatter=formatter
            )

            await handler.emit(self.record)

            writer.write.assert_called_once_with(
                (msg + handler.terminator).encode()
            )
            writer.drain.assert_awaited_once()
            await handler.close() 
开发者ID:b2wdigital,项目名称:aiologger,代码行数:21,代码来源:test_streams.py

示例4: test_emit_calls_handle_error_if_an_error_occurs

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_emit_calls_handle_error_if_an_error_occurs(self):
        writer = Mock(write=CoroutineMock(), drain=CoroutineMock())
        with patch(
            "aiologger.handlers.streams.StreamWriter", return_value=writer
        ):
            exc = Exception("XABLAU")
            handler = AsyncStreamHandler(
                level=10,
                stream=self.write_pipe,
                formatter=Mock(format=Mock(side_effect=exc)),
            )
            with asynctest.patch.object(
                handler, "handle_error"
            ) as handle_error:
                await handler.emit(self.record)

                handle_error.assert_awaited_once_with(self.record, exc)
                writer.write.assert_not_awaited()
                writer.drain.assert_not_awaited() 
开发者ID:b2wdigital,项目名称:aiologger,代码行数:21,代码来源:test_streams.py

示例5: test_emit_awaits_for_handle_error_is_an_exceptions_is_raised

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_emit_awaits_for_handle_error_is_an_exceptions_is_raised(
        self
    ):
        handler = BaseAsyncRotatingFileHandler(filename=self.temp_file.name)
        handler.should_rollover = Mock(return_value=False)
        exc = OSError()
        with patch(
            "aiologger.handlers.files.AsyncFileHandler.emit", side_effect=exc
        ), patch.object(
            handler, "handle_error", CoroutineMock()
        ) as handleError:
            log_record = LogRecord(
                name="Xablau",
                level=20,
                pathname="/aiologger/tests/test_logger.py",
                lineno=17,
                msg="Xablau!",
                exc_info=None,
                args=None,
            )
            await handler.emit(log_record)
            handleError.assert_awaited_once_with(log_record, exc) 
开发者ID:b2wdigital,项目名称:aiologger,代码行数:24,代码来源:test_files.py

示例6: test_callhandlers_calls_handlers_for_loglevel

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_callhandlers_calls_handlers_for_loglevel(self):
        level10_handler = Mock(level=10, handle=CoroutineMock())
        level30_handler = Mock(level=30, handle=CoroutineMock())

        logger = Logger.with_default_handlers()
        logger.handlers = [level10_handler, level30_handler]

        record = LogRecord(
            level=20,
            name="aiologger",
            pathname="/aiologger/tests/test_logger.py",
            lineno=17,
            msg="Xablau!",
            exc_info=None,
            args=None,
        )
        await logger.call_handlers(record)

        level10_handler.handle.assert_awaited_once_with(record)
        level30_handler.handle.assert_not_awaited() 
开发者ID:b2wdigital,项目名称:aiologger,代码行数:22,代码来源:test_logger.py

示例7: test_it_calls_multiple_handlers_if_multiple_handle_matches_are_found_for_record

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_it_calls_multiple_handlers_if_multiple_handle_matches_are_found_for_record(
        self
    ):
        level10_handler = Mock(level=10, handle=CoroutineMock())
        level20_handler = Mock(level=20, handle=CoroutineMock())

        logger = Logger.with_default_handlers()
        logger.handlers = [level10_handler, level20_handler]

        record = LogRecord(
            level=30,
            name="aiologger",
            pathname="/aiologger/tests/test_logger.py",
            lineno=17,
            msg="Xablau!",
            exc_info=None,
            args=None,
        )

        await logger.call_handlers(record)

        level10_handler.handle.assert_awaited_once_with(record)
        level20_handler.handle.assert_awaited_once_with(record) 
开发者ID:b2wdigital,项目名称:aiologger,代码行数:25,代码来源:test_logger.py

示例8: test_check_mesos_active_frameworks_fails

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_check_mesos_active_frameworks_fails(capfd):
    with mock.patch(
        "paasta_tools.monitoring.check_mesos_active_frameworks.parse_args",
        autospec=True,
    ) as mock_parse_args, mock.patch(
        "paasta_tools.monitoring.check_mesos_active_frameworks.get_mesos_master",
        autospec=True,
    ) as mock_get_mesos_master:
        mock_opts = mock.MagicMock()
        mock_opts.expected = "foo,bar"
        mock_parse_args.return_value = mock_opts
        mock_master = mock.MagicMock()
        mock_master.state = CoroutineMock(
            func=CoroutineMock(),  # https://github.com/notion/a_sync/pull/40
            return_value={"frameworks": [{"name": "foo"}]},
        )
        mock_get_mesos_master.return_value = mock_master

        with pytest.raises(SystemExit) as error:
            check_mesos_active_frameworks()
        out, err = capfd.readouterr()
        assert "CRITICAL" in out
        assert "bar" in out
        assert "foo" not in out
        assert error.value.code == 2 
开发者ID:Yelp,项目名称:paasta,代码行数:27,代码来源:test_check_mesos_active_frameworks.py

示例9: test_get_cpu_usage_bad

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_get_cpu_usage_bad():
    fake_task = mock.create_autospec(mesos.task.Task)
    fake_task.cpu_limit = asynctest.CoroutineMock(return_value=1.1)
    fake_duration = 100
    fake_task.stats = asynctest.CoroutineMock(
        return_value={"cpus_system_time_secs": 50.0, "cpus_user_time_secs": 50.0}
    )
    current_time = datetime.datetime.now()
    fake_task.__getitem__.return_value = [
        {
            "state": "TASK_RUNNING",
            "timestamp": int(current_time.strftime("%s")) - fake_duration,
        }
    ]
    with asynctest.patch(
        "paasta_tools.mesos_tools.datetime.datetime", autospec=True
    ) as mock_datetime:
        mock_datetime.now.return_value = current_time
        actual = await mesos_tools.get_cpu_usage(fake_task)
    assert PaastaColors.red("100.0%") in actual 
开发者ID:Yelp,项目名称:paasta,代码行数:22,代码来源:test_mesos_tools.py

示例10: test_get_all_running_tasks

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_get_all_running_tasks():
    with asynctest.patch(
        "paasta_tools.mesos_tools.get_current_tasks", autospec=True
    ) as mock_get_current_tasks, asynctest.patch(
        "paasta_tools.mesos_tools.filter_running_tasks", autospec=True
    ) as mock_filter_running_tasks, asynctest.patch(
        "paasta_tools.mesos_tools.get_mesos_master", autospec=True
    ) as mock_get_mesos_master:
        mock_task_1 = mock.Mock()
        mock_task_2 = mock.Mock()
        mock_task_3 = mock.Mock()

        mock_get_current_tasks.return_value = [mock_task_1, mock_task_2]
        mock_orphan_tasks = asynctest.CoroutineMock(return_value=[mock_task_3])
        mock_mesos_master = mock.Mock(orphan_tasks=mock_orphan_tasks)
        mock_get_mesos_master.return_value = mock_mesos_master

        ret = await mesos_tools.get_all_running_tasks()
        mock_get_current_tasks.assert_called_with("")
        mock_filter_running_tasks.assert_called_with(
            [mock_task_1, mock_task_2, mock_task_3]
        )
        assert ret == mock_filter_running_tasks.return_value 
开发者ID:Yelp,项目名称:paasta,代码行数:25,代码来源:test_mesos_tools.py

示例11: test_resources_utilization_nothing_special

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_resources_utilization_nothing_special(
    mock_get_mesos_master, mock_get_resource_utilization_by_grouping
):
    request = testing.DummyRequest()
    request.swagger_data = {"groupings": None, "filter": None}
    mock_mesos_state = mock.Mock()
    mock_master = mock.Mock(
        state=asynctest.CoroutineMock(return_value=mock_mesos_state)
    )
    mock_get_mesos_master.return_value = mock_master

    mock_get_resource_utilization_by_grouping.return_value = {
        frozenset([("superregion", "unknown")]): {
            "total": metastatus_lib.ResourceInfo(cpus=10.0, mem=512.0, disk=100.0),
            "free": metastatus_lib.ResourceInfo(cpus=8.0, mem=312.0, disk=20.0),
        }
    }

    resp = resources_utilization(request)
    body = json.loads(resp.body.decode("utf-8"))

    assert resp.status_int == 200
    assert len(body) == 1
    assert set(body[0].keys()) == {"disk", "mem", "groupings", "cpus", "gpus"} 
开发者ID:Yelp,项目名称:paasta,代码行数:26,代码来源:test_resources.py

示例12: test_resources_utilization_with_grouping

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_resources_utilization_with_grouping(mock_get_mesos_master):
    request = testing.DummyRequest()
    request.swagger_data = {"groupings": ["region", "pool"], "filter": None}
    mock_master = mock.Mock(
        state=asynctest.CoroutineMock(
            func=asynctest.CoroutineMock(),  # https://github.com/notion/a_sync/pull/40
            return_value=mock_mesos_state,
        )
    )
    mock_get_mesos_master.return_value = mock_master

    resp = resources_utilization(request)
    body = json.loads(resp.body.decode("utf-8"))

    assert resp.status_int == 200
    # 4 groupings, 2x2 attrs for 5 slaves
    assert len(body) == 4 
开发者ID:Yelp,项目名称:paasta,代码行数:19,代码来源:test_resources.py

示例13: test_is_draining_yes

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_is_draining_yes(self):
        fake_response = mock.Mock(
            status=503,
            text=asynctest.CoroutineMock(
                return_value="Service service in down state since 1435694078.778886 "
                "until 1435694178.780000: Drained by Paasta"
            ),
        )
        fake_task = mock.Mock(host="fake_host", ports=[54321])
        with mock_ClientSession(
            get=mock.Mock(
                return_value=asynctest.MagicMock(
                    __aenter__=asynctest.CoroutineMock(return_value=fake_response)
                )
            )
        ):
            assert await self.drain_method.is_draining(fake_task) is True 
开发者ID:Yelp,项目名称:paasta,代码行数:19,代码来源:test_drain_lib.py

示例14: test_service_can_be_unregistered_by_id

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_service_can_be_unregistered_by_id():
    with mock.patch('bookshelf.discolib.client') as client_func:
        client = asynctest.CoroutineMock()
        svc = mock.MagicMock()
        
        client_func.return_value = client
        client.agent.services.register.return_value = svc
        
        res = await discolib.register_service(id="svc1", name="svc",
                                              tags=["a", "b"],
                                              port=9000,
                                              address="localhost")
        assert res == svc

        client.agent.services.deregister.return_value = True
        res = await discolib.deregister_service("svc1")
        client.agent.services.deregister.assert_called_with("svc1")
        assert res is True 
开发者ID:Lawouach,项目名称:event-driven-microservice,代码行数:20,代码来源:test_discolib.py

示例15: test_service_can_be_unregistered_by_instance

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import CoroutineMock [as 别名]
def test_service_can_be_unregistered_by_instance():
    with mock.patch('bookshelf.discolib.client') as client_func:
        client = asynctest.CoroutineMock()
        svc = mock.MagicMock()
        
        client_func.return_value = client
        client.agent.services.register.return_value = svc
        
        res = await discolib.register_service(id="svc1", name="svc",
                                              tags=["a", "b"],
                                              port=9000,
                                              address="localhost")
        assert res == svc

        client.agent.services.deregister.return_value = True
        res = await discolib.deregister_service(svc)
        client.agent.services.deregister.assert_called_with(svc)
        assert res is True 
开发者ID:Lawouach,项目名称:event-driven-microservice,代码行数:20,代码来源:test_discolib.py


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