-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
""" | ||
Simple echo http server | ||
""" | ||
import time | ||
import os | ||
import sys | ||
from aiohttp import web | ||
|
||
wait_time = float(sys.argv[1]) | ||
print(f'waiting {wait_time}') | ||
time.sleep(wait_time) | ||
|
||
PORT = os.environ['PORT'] | ||
|
||
routes = web.RouteTableDef() | ||
|
||
@routes.get('/') | ||
async def hello(request): | ||
return web.Response(text="Hello, world") | ||
|
||
app = web.Application() | ||
app.add_routes(routes) | ||
web.run_app(app, port=PORT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import sys | ||
import time | ||
import pytest | ||
import os | ||
from simpervisor import SupervisedProcess | ||
import aiohttp | ||
import logging | ||
|
||
@pytest.mark.asyncio | ||
async def test_ready(): | ||
""" | ||
Test web app's readyness | ||
""" | ||
httpserver_file = os.path.join( | ||
os.path.dirname(os.path.abspath(__file__)), | ||
'child_scripts', | ||
'simplehttpserver.py' | ||
) | ||
|
||
port = '9005' | ||
# We tell our server to wait this many seconds before it starts serving | ||
ready_time = 3.0 | ||
|
||
async def _ready_func(p): | ||
url = f'http://localhost:{port}' | ||
async with aiohttp.ClientSession() as session: | ||
try: | ||
async with session.get(url) as resp: | ||
logging.debug(f'Got code {resp.status} back from {url}') | ||
return resp.status == 200 | ||
except aiohttp.ClientConnectionError: | ||
logging.debug(f'Connection to {url} refused') | ||
return False | ||
|
||
proc = SupervisedProcess( | ||
'socketserver', | ||
sys.executable, httpserver_file, str(ready_time), | ||
ready_func=_ready_func, | ||
env={'PORT': port} | ||
) | ||
|
||
try: | ||
await proc.start() | ||
start_time = time.time() | ||
assert (await proc.ready()) | ||
assert time.time() - start_time > ready_time | ||
finally: | ||
# Clean up our process after ourselves | ||
await proc.kill() |