Skip to content

Commit

Permalink
tests: Move the fake webapp's server code to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfehler committed Jun 26, 2024
1 parent 57a39bd commit 7cf96bc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 55 deletions.
57 changes: 2 additions & 55 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import os
import sys
from multiprocessing import Process
from urllib import parse
from urllib.request import urlopen

import pytest

from tests.fake_webapp import EXAMPLE_APP
from tests.fake_webapp import start_flask_app
from tests.fake_webapp_server import start_server, stop_server

import splinter
from splinter import Browser
Expand All @@ -30,57 +26,8 @@ def selenium_server_is_running():
return "WebDriver Hub" in page_contents


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():
sys.stderr = open(os.devnull, "w")
env.process = Process(target=start_flask_app, args=(env.host, env.port))
env.process.daemon = True
env.process.start()
wait_until_start()


def stop_server():
env.process.terminate()
env.process.join()
wait_until_stop()


def pytest_configure(config):
try:
start_server()
except Exception as e:
sys.stdout.write("Failed to start test server: %s\n\n" % e)
sys.exit(1)
start_server()


def pytest_unconfigure(config):
Expand Down
58 changes: 58 additions & 0 deletions tests/fake_webapp_server.py
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()

0 comments on commit 7cf96bc

Please sign in to comment.