Skip to content

Commit

Permalink
fix: Updates required for httpstan 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
riddell-stan committed Dec 12, 2019
1 parent 0f8ca6e commit bf0e6cf
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pbr<6,>=5 # Apache-2.0
httpstan<0.11,>=0.10
aiohttp~=3.6
httpstan~=1.0
tqdm<5,>=4.14
requests<3,>=2.18
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ classifier =
Operating System :: POSIX :: Linux
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
license_file = LICENSE

[files]
Expand Down
30 changes: 24 additions & 6 deletions stan/common.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Common routines"""
import asyncio
import contextlib
import threading
import time
import typing

import aiohttp.web
import requests

import httpstan.main
import httpstan.app


class ServerAddress(typing.NamedTuple):
Expand All @@ -16,12 +19,23 @@ class ServerAddress(typing.NamedTuple):
@contextlib.contextmanager
def httpstan_server():
"""Manage starting and stopping an httpstan web gateway."""
host, port = "127.0.0.1", 8080
runner = aiohttp.web.AppRunner(httpstan.app.make_app())
loop = asyncio.get_event_loop()

try:
server = httpstan.main.Server()
server.start()
host, port = server.host, server.port
# After dropping Python 3.6, use `asyncio.run`
asyncio.get_event_loop().run_until_complete(runner.setup())
site = aiohttp.web.TCPSite(runner, host, port)
# After dropping Python 3.6, use `asyncio.run`
asyncio.get_event_loop().run_until_complete(site.start())
t = threading.Thread(target=loop.run_forever)
# after this call, the event loop is running in thread which is not the main
# thread. All interactions with the event loop must use thread-safe calls
t.start()

# wait until server is ready
retries = 10
# if server is not ready (thread has not started)
for _ in range(retries):
try:
r = requests.get(f"http://{host}:{port}/v1/health", timeout=0.01)
Expand All @@ -34,6 +48,10 @@ def httpstan_server():
break
else:
raise RuntimeError("Could not communicate with httpstan server.")

yield ServerAddress(host=host, port=port)

finally:
server.stop()
asyncio.run_coroutine_threadsafe(runner.cleanup(), loop)
loop.call_soon_threadsafe(loop.stop) # stops `run_forever`
t.join(timeout=1)
10 changes: 10 additions & 0 deletions tests/test_httpstan_health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import requests

import stan


def test_httpstan_health():
with stan.common.httpstan_server() as server:
host, port = server.host, server.port
response = requests.get(f"http://{host}:{port}/v1/health")
assert response.status_code == 200

0 comments on commit bf0e6cf

Please sign in to comment.