示例#1
0
async def test_register_app_connections(monkeypatch, mocker,
                                        mock_client_app_config):  # noqa: F811
    await register_app_connections(mock_client_app_config)
    context = create_test_context(mock_client_app_config, "mock_client_event")

    client = app_client("test_app_connection", context)

    assert client.app_config == mock_client_app_config
    assert client.app_connection == "test_app_connection"
    assert client.started

    await stop_app_connections(mock_client_app_config.app_key())
    assert client.stopped
    with pytest.raises(AppConnectionNotFound):
        client = app_client("test_app_connection", context)
示例#2
0
async def test_load_balancer_cb_open(monkeypatch, mock_client_app_config,
                                     mock_auth):
    async with MockClientSession.lock:
        set_settings(mock_client_app_config,
                     retries=1,
                     circuit_breaker_open_failures=10,
                     circuit_breaker_failure_reset_seconds=60,
                     circuit_breaker_open_seconds=60)
        await init_mock_client_app(apps_client_module, monkeypatch, mock_auth,
                                   mock_client_app_config, "test-event-get",
                                   "ok")
        context = create_test_context(mock_client_app_config,
                                      "mock_client_event")
        client = app_client("test_app_connection", context)

        MockClientSession.set_failure("http://test-host1", 500)

        for _ in range(20):
            result = await client.call("test_event_get",
                                       datatype=MockResponseData,
                                       payload=None,
                                       context=context,
                                       test_param="test_param_value")
        assert result == [
            MockResponseData(value="ok",
                             param="test_param_value",
                             host="http://test-host2",
                             log={
                                 "http://test-host1": 10,
                                 "http://test-host2": 20
                             })
        ]
示例#3
0
async def test_client_session_lifecycle(monkeypatch, mock_client_app_config,
                                        mock_auth):
    async with MockClientSession.lock:
        await init_mock_client_app(apps_client_module, monkeypatch, mock_auth,
                                   mock_client_app_config, "test-event-get",
                                   "ok")
        assert MockClientSession.session_open
        context = create_test_context(mock_client_app_config,
                                      "mock_client_event")
        client = app_client("test_app_connection", context)
        await client.stop()
        assert MockClientSession.session_open is False
示例#4
0
async def test_load_balancer_no_hosts_available(monkeypatch,
                                                mock_client_app_config,
                                                mock_auth):
    async with MockClientSession.lock:
        set_settings(mock_client_app_config,
                     retries=1,
                     circuit_breaker_open_failures=10,
                     circuit_breaker_failure_reset_seconds=60,
                     circuit_breaker_open_seconds=60)
        await init_mock_client_app(apps_client_module, monkeypatch, mock_auth,
                                   mock_client_app_config, "test-event-get",
                                   "ok")
        context = create_test_context(mock_client_app_config,
                                      "mock_client_event")
        MockClientSession.set_failure("http://test-host1", 500)
        MockClientSession.set_failure("http://test-host2", 500)

        client = app_client("test_app_connection", context)

        for _ in range(10):
            try:
                await client.call("test_event_get",
                                  datatype=MockResponseData,
                                  payload=None,
                                  context=context,
                                  test_param="test_param_value")
            except:  # noqa: E722
                pass  # Opening circuit breakers

        assert MockClientSession.call_log == {
            "http://test-host1": 10,
            "http://test-host2": 10
        }
        with pytest.raises(ClientLoadBalancerException):
            await client.call("test_event_get",
                              datatype=MockResponseData,
                              payload=None,
                              context=context,
                              test_param="test_param_value")
            assert MockClientSession.call_log == {
                "http://test-host1": 10,
                "http://test-host2": 10
            }