Пример #1
0
def test_keep_alive_server_timeout():
    """If the client keep-alive timeout is longer than the server
    keep-alive timeout, the client will either a 'Connection reset' error
    _or_ a new connection. Depending on how the event-loop handles the
    broken server connection."""
    try:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        client = ReuseableSanicTestClient(keep_alive_app_server_timeout, loop)
        headers = {"Connection": "keep-alive"}
        try:
            request, response = client.get(
                "/1", headers=headers, request_keepalive=60
            )
            assert response.status == 200
            assert response.text == "OK"
            loop.run_until_complete(aio_sleep(3))
            exception = None
            request, response = client.get("/1", request_keepalive=60)
        except ValueError as e:
            exception = e
        assert exception is not None
        assert isinstance(exception, ValueError)
        assert (
            "Connection reset" in exception.args[0]
            or "got a new connection" in exception.args[0]
        )
    finally:
        client.kill_server()
Пример #2
0
def test_keep_alive_server_timeout():
    """If the client keep-alive timeout is longer than the server
    keep-alive timeout, the client will either a 'Connection reset' error
    _or_ a new connection. Depending on how the event-loop handles the
    broken server connection."""
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    client = ReuseableSanicTestClient(keep_alive_app_server_timeout, loop)
    headers = {"Connection": "keep-alive"}
    request, response = client.get("/1", headers=headers, request_keepalive=60)
    assert response.status == 200
    assert response.text == "OK"
    loop.run_until_complete(aio_sleep(3))
    exception = None
    try:
        request, response = client.get(
            "/1", request_keepalive=60, end_server=True
        )
    except ValueError as e:
        exception = e
    assert exception is not None
    assert isinstance(exception, ValueError)
    assert (
        "Connection reset" in exception.args[0]
        or "got a new connection" in exception.args[0]
    )
Пример #3
0
def test_keep_alive_timeout_reuse():
    """If the server keep-alive timeout and client keep-alive timeout are
     both longer than the delay, the client _and_ server will successfully
     reuse the existing connection."""
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    client = ReuseableSanicTestClient(keep_alive_timeout_app_reuse, loop)
    headers = {'Connection': 'keep-alive'}
    request, response = client.get('/1', headers=headers)
    assert response.status == 200
    assert response.text == 'OK'
    loop.run_until_complete(aio_sleep(1))
    request, response = client.get('/1', end_server=True)
    assert response.status == 200
    assert response.text == 'OK'
Пример #4
0
def test_keep_alive_timeout_reuse():
    """If the server keep-alive timeout and client keep-alive timeout are
     both longer than the delay, the client _and_ server will successfully
     reuse the existing connection."""
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    client = ReuseableSanicTestClient(keep_alive_timeout_app_reuse, loop)
    headers = {"Connection": "keep-alive"}
    request, response = client.get("/1", headers=headers)
    assert response.status == 200
    assert response.text == "OK"
    loop.run_until_complete(aio_sleep(1))
    request, response = client.get("/1", end_server=True)
    assert response.status == 200
    assert response.text == "OK"
Пример #5
0
def test_keep_alive_connection_context():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    client = ReusableClient(keep_alive_app_context, loop=loop, port=PORT)
    with client:
        headers = {"Connection": "keep-alive"}
        request1, _ = client.post("/ctx", headers=headers)

        loop.run_until_complete(aio_sleep(1))
        request2, response = client.get("/ctx")

        assert response.text == "hello"
        assert id(request1.conn_info.ctx) == id(request2.conn_info.ctx)
        assert (request1.conn_info.ctx.foo == request2.conn_info.ctx.foo ==
                "hello")
        assert request2.protocol.state["requests_count"] == 2
Пример #6
0
def test_keep_alive_connection_context():
    try:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        client = ReuseableSanicTestClient(keep_alive_app_context, loop)
        headers = {"Connection": "keep-alive"}
        request1, _ = client.post("/ctx", headers=headers)

        loop.run_until_complete(aio_sleep(1))
        request2, response = client.get("/ctx")

        assert response.text == "hello"
        assert id(request1.conn_info.ctx) == id(request2.conn_info.ctx)
        assert (request1.conn_info.ctx.foo == request2.conn_info.ctx.foo ==
                "hello")
    finally:
        client.kill_server()
Пример #7
0
def test_keep_alive_timeout_reuse():
    """If the server keep-alive timeout and client keep-alive timeout are
     both longer than the delay, the client _and_ server will successfully
     reuse the existing connection."""
    try:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        client = ReuseableSanicTestClient(keep_alive_timeout_app_reuse, loop)
        headers = {"Connection": "keep-alive"}
        request, response = client.get("/1", headers=headers)
        assert response.status == 200
        assert response.text == "OK"
        loop.run_until_complete(aio_sleep(1))
        request, response = client.get("/1")
        assert response.status == 200
        assert response.text == "OK"
    finally:
        client.kill_server()
Пример #8
0
def test_keep_alive_client_timeout():
    """If the server keep-alive timeout is longer than the client
    keep-alive timeout, client will try to create a new connection here."""
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    client = ReusableClient(keep_alive_app_client_timeout,
                            loop=loop,
                            port=PORT)
    with client:
        headers = {"Connection": "keep-alive"}
        request, response = client.get("/1", headers=headers, timeout=1)

        assert response.status == 200
        assert response.text == "OK"
        assert request.protocol.state["requests_count"] == 1

        loop.run_until_complete(aio_sleep(2))
        request, response = client.get("/1", timeout=1)
        assert request.protocol.state["requests_count"] == 1
Пример #9
0
def test_keep_alive_client_timeout():
    """If the server keep-alive timeout is longer than the client
    keep-alive timeout, client will try to create a new connection here."""
    try:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        client = ReuseableSanicTestClient(keep_alive_app_client_timeout, loop)
        headers = {"Connection": "keep-alive"}
        _, response = client.get("/1", headers=headers, request_keepalive=1)

        assert response.status == 200
        assert response.text == "OK"

        loop.run_until_complete(aio_sleep(2))
        _, response = client.get("/1", request_keepalive=1)

        assert ReusableSanicConnectionPool.last_reused_connection is None
    finally:
        client.kill_server()
Пример #10
0
def test_keep_alive_timeout_reuse():
    """If the server keep-alive timeout and client keep-alive timeout are
    both longer than the delay, the client _and_ server will successfully
    reuse the existing connection."""
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    client = ReusableClient(keep_alive_timeout_app_reuse, loop=loop, port=PORT)
    with client:
        headers = {"Connection": "keep-alive"}
        request, response = client.get("/1", headers=headers)
        assert response.status == 200
        assert response.text == "OK"
        assert request.protocol.state["requests_count"] == 1

        loop.run_until_complete(aio_sleep(1))

        request, response = client.get("/1")
        assert response.status == 200
        assert response.text == "OK"
        assert request.protocol.state["requests_count"] == 2
Пример #11
0
def test_keep_alive_client_timeout():
    """If the server keep-alive timeout is longer than the client
    keep-alive timeout, client will try to create a new connection here."""
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    client = ReuseableSanicTestClient(keep_alive_app_client_timeout, loop)
    headers = {'Connection': 'keep-alive'}
    request, response = client.get('/1', headers=headers, request_keepalive=1)
    assert response.status == 200
    assert response.text == 'OK'
    loop.run_until_complete(aio_sleep(2))
    exception = None
    try:
        request, response = client.get('/1',
                                       end_server=True,
                                       request_keepalive=1)
    except ValueError as e:
        exception = e
    assert exception is not None
    assert isinstance(exception, ValueError)
    assert "got a new connection" in exception.args[0]
Пример #12
0
def test_keep_alive_server_timeout():
    """If the client keep-alive timeout is longer than the server
    keep-alive timeout, the client will either a 'Connection reset' error
    _or_ a new connection. Depending on how the event-loop handles the
    broken server connection."""
    try:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        client = ReuseableSanicTestClient(keep_alive_app_server_timeout, loop)
        headers = {"Connection": "keep-alive"}
        _, response = client.get("/1", headers=headers, request_keepalive=60)

        assert response.status == 200
        assert response.text == "OK"

        loop.run_until_complete(aio_sleep(3))
        _, response = client.get("/1", request_keepalive=60)

        assert ReusableSanicConnectionPool.last_reused_connection is None
    finally:
        client.kill_server()
Пример #13
0
def test_keep_alive_client_timeout():
    """If the server keep-alive timeout is longer than the client
    keep-alive timeout, client will try to create a new connection here."""
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    client = ReuseableSanicTestClient(keep_alive_app_client_timeout, loop)
    headers = {"Connection": "keep-alive"}
    request, response = client.get("/1", headers=headers, request_keepalive=1)
    assert response.status == 200
    assert response.text == "OK"
    loop.run_until_complete(aio_sleep(2))
    exception = None
    try:
        request, response = client.get(
            "/1", end_server=True, request_keepalive=1
        )
    except ValueError as e:
        exception = e
    assert exception is not None
    assert isinstance(exception, ValueError)
    assert "got a new connection" in exception.args[0]
Пример #14
0
def test_keep_alive_server_timeout():
    """If the client keep-alive timeout is longer than the server
    keep-alive timeout, the client will either a 'Connection reset' error
    _or_ a new connection. Depending on how the event-loop handles the
    broken server connection."""
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    client = ReusableClient(keep_alive_app_server_timeout,
                            loop=loop,
                            port=PORT)
    with client:
        headers = {"Connection": "keep-alive"}
        request, response = client.get("/1", headers=headers, timeout=60)

        assert response.status == 200
        assert response.text == "OK"
        assert request.protocol.state["requests_count"] == 1

        loop.run_until_complete(aio_sleep(3))
        request, response = client.get("/1", timeout=60)

        assert request.protocol.state["requests_count"] == 1