Skip to content

Commit

Permalink
httpbin as daemon process
Browse files Browse the repository at this point in the history
  • Loading branch information
derlih committed Jun 29, 2021
1 parent 7d89e7c commit f944b59
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,23 +210,21 @@ def httpbin():
sock.bind(("127.0.0.1", 0))
available_port = sock.getsockname()[1]

print(f"MP start method: {multiprocessing.get_start_method()}")
httbbin_url = f"http://127.0.0.1:{available_port}"

server = multiprocessing.Process(
target=httpbin_app.run,
kwargs={"host": "127.0.0.1", "port": available_port, "load_dotenv": False},
)
try:
server.start()
for _ in range(10):
time.sleep(0.1)
with contextlib.closing(socket.socket()) as sock:
with contextlib.suppress(ConnectionRefusedError):
sock.connect(("127.0.0.1", available_port))
break
else:
raise RuntimeError("Can't connect to local httpbin")
server.daemon = True
server.start()
for _ in range(10):
time.sleep(0.1)
with contextlib.closing(socket.socket()) as sock:
with contextlib.suppress(ConnectionRefusedError):
sock.connect(("127.0.0.1", available_port))
break
else:
raise RuntimeError("Can't connect to local httpbin")

yield f"http://127.0.0.1:{available_port}"
finally:
server.terminate()
server.join()
return httbbin_url

0 comments on commit f944b59

Please sign in to comment.