Skip to content

Commit

Permalink
Merge pull request #40 from sjaensch/sjaensch-add-integration-test-re…
Browse files Browse the repository at this point in the history
…quest-fired-immediately

Add test to verify request is fired on future creation
  • Loading branch information
sjaensch committed Mar 25, 2020
2 parents 54ed559 + 23a2c3a commit c9d9022
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 40 deletions.
22 changes: 19 additions & 3 deletions testing/integration_server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import argparse
import asyncio
import multiprocessing
import os.path
import sys

import umsgpack
from aiohttp import web


shm_request_received = None


async def swagger_spec(request):
with open(os.path.join(os.path.dirname(__file__), "swagger.yaml")) as f:
spec = f.read()
Expand Down Expand Up @@ -130,6 +134,11 @@ async def get_pets(request):
return web.json_response(pets)


async def ping(request):
shm_request_received.value = 1
return web.json_response({})


def check_content_type(headers, expected_content_type):
content_type = headers.get("Content-Type")
if content_type != expected_content_type:
Expand All @@ -152,6 +161,15 @@ def setup_routes(app):
app.router.add_put("/pet", update_pet)
app.router.add_delete("/pet", delete_pet)
app.router.add_get("/pets", get_pets)
app.router.add_get("/ping", ping)


def start_integration_server(port, shm_request_received_var):
global shm_request_received
shm_request_received = shm_request_received_var
app = web.Application()
setup_routes(app)
web.run_app(app, host="127.0.0.1", port=port)


if __name__ == "__main__":
Expand All @@ -166,6 +184,4 @@ def setup_routes(app):
)
args = parser.parse_args()

app = web.Application()
setup_routes(app)
web.run_app(app, host="127.0.0.1", port=args.port)
start_integration_server(args.port, multiprocessing.Value("i", 0))
12 changes: 12 additions & 0 deletions testing/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,18 @@ paths:
description: "date in UTC when token expires"
400:
description: "Invalid username/password supplied"
/ping:
get:
tags:
- "ping"
summary: "Send a ping to the server"
description: ""
operationId: "ping"
produces:
- "application/json"
responses:
200:
description: "successful operation"
definitions:
Category:
type: "object"
Expand Down
37 changes: 0 additions & 37 deletions tests/integration/conftest.py

This file was deleted.

55 changes: 55 additions & 0 deletions tests/integration/integration_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import asyncio
import io
import multiprocessing
import os.path
import time
import urllib
from asyncio import run_coroutine_threadsafe
from concurrent.futures import CancelledError

import ephemeral_port_reserve
import monotonic
import pytest
from bravado import requests_client
from bravado.client import SwaggerClient
Expand All @@ -16,6 +20,40 @@

from bravado_asyncio import http_client
from bravado_asyncio import thread_loop
from testing.integration_server import start_integration_server


shm_request_received = None


def wait_unit_service_starts(url, timeout=10):
start = time.time()
while time.time() < start + timeout:
try:
urllib.request.urlopen(url, timeout=2)
except urllib.error.HTTPError: # pragma: no cover
return
except urllib.error.URLError: # pragma: no cover
time.sleep(0.1)


@pytest.fixture(scope="module")
def integration_server():
global shm_request_received
shm_request_received = multiprocessing.Value("i", 0, lock=False)

server_port = ephemeral_port_reserve.reserve()
server_process = multiprocessing.Process(
target=start_integration_server, args=(server_port, shm_request_received)
)
server_process.daemon = True
server_process.start()
wait_unit_service_starts("http://localhost:{port}".format(port=server_port))

yield "http://localhost:{}".format(server_port)

server_process.terminate()
server_process.join(timeout=1)


@pytest.fixture(
Expand Down Expand Up @@ -230,6 +268,23 @@ def test_timeout_on_future(swagger_client):
assert isinstance(other_future.response(timeout=1).result, Model)


def test_time_until_request_done(integration_server):
swagger_client = get_swagger_client(integration_server, http_client.AsyncioClient())
start_time = monotonic.monotonic()
bravado_future = swagger_client.ping.ping()
time_waited = 0
while not shm_request_received.value and time_waited < 1:
time.sleep(0.01)
time_waited += 0.01
wait_request_received_time = monotonic.monotonic() - start_time

bravado_future.response(timeout=1)

assert time_waited < 0.2, "Waited {}s for request to be received by server,".format(
wait_request_received_time
) + "shm value is now {}".format(shm_request_received.value)


@pytest.mark.xfail(
reason="Execution time is not always below 2 seconds especially for Python 3.5"
)
Expand Down

0 comments on commit c9d9022

Please sign in to comment.