-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: Move the fake webapp's server code to a separate file
- Loading branch information
Showing
2 changed files
with
60 additions
and
55 deletions.
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,58 @@ | ||
import multiprocessing | ||
import os | ||
import sys | ||
from urllib.request import urlopen | ||
|
||
from tests.fake_webapp import EXAMPLE_APP | ||
from tests.fake_webapp import start_flask_app | ||
|
||
|
||
class Env: | ||
def __init__(self): | ||
self.process = None | ||
self.host = "localhost" | ||
self.port = 5000 | ||
|
||
|
||
env = Env() | ||
|
||
|
||
def wait_until_start(): | ||
while True: | ||
try: | ||
results = urlopen(EXAMPLE_APP) | ||
if results.code == 404: | ||
raise Exception("%s returned unexpected 404" % EXAMPLE_APP) | ||
break | ||
except OSError: | ||
pass | ||
|
||
|
||
def wait_until_stop(): | ||
while True: | ||
try: | ||
results = urlopen(EXAMPLE_APP) | ||
if results.code == 404: | ||
break | ||
except OSError: | ||
break | ||
|
||
|
||
def start_server(): | ||
"""Start the Flask app used for integration testing.""" | ||
try: | ||
sys.stderr = open(os.devnull, "w") | ||
env.process = multiprocessing.Process(target=start_flask_app, args=(env.host, env.port)) | ||
env.process.daemon = True | ||
env.process.start() | ||
wait_until_start() | ||
except Exception as e: | ||
sys.stdout.write("Failed to start test server: %s\n\n" % e) | ||
sys.exit(1) | ||
|
||
|
||
def stop_server(): | ||
"""Stop the Flask app used for integration testing.""" | ||
env.process.terminate() | ||
env.process.join() | ||
wait_until_stop() |