From 670473f29b7678bfcaa4e7a389dc482376b81227 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 9 Jul 2018 17:15:04 -0400 Subject: [PATCH] Fix webserver --- Makefile | 6 +++++- test/conftest.py | 37 ++++++++++++++++++++++++++++--------- test/test_python.py | 2 +- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 11f33f893..b46055b4e 100644 --- a/Makefile +++ b/Makefile @@ -96,10 +96,14 @@ build/renderedhtml.css: src/renderedhtml.less lessc $< $@ -test: all build/test.html +test: all build/test.html build/test_data.txt py.test test -v +build/test_data.txt: test/data.txt + cp test/data.txt build/test_data.txt + + lint: flake8 src flake8 test diff --git a/test/conftest.py b/test/conftest.py index 64f0654a6..302d61641 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -6,7 +6,8 @@ import atexit import multiprocessing import os import pathlib -import time +import queue +import sys try: import pytest @@ -38,7 +39,7 @@ class SeleniumWrapper: driver = self.get_driver() wait = WebDriverWait(driver, timeout=20) - driver.get('http://127.0.0.1:8000/test.html') + driver.get(f'http://127.0.0.1:{PORT}/test.html') wait.until(PyodideInited()) self.wait = wait self.driver = driver @@ -104,27 +105,33 @@ if pytest is not None: selenium.driver.quit() +PORT = 0 + + def spawn_web_server(): + global PORT + print("Spawning webserver...") - p = multiprocessing.Process(target=run_web_server) + q = multiprocessing.Queue() + p = multiprocessing.Process(target=run_web_server, args=(q,)) def shutdown_webserver(): - p.terminate() + q.put("TERMINATE") + p.join() atexit.register(shutdown_webserver) p.start() - time.sleep(2) + PORT = q.get() -def run_web_server(): +def run_web_server(q): import http.server import socketserver print("Running webserver...") os.chdir(BUILD_PATH) - PORT = 8000 Handler = http.server.SimpleHTTPRequestHandler Handler.extensions_map['.wasm'] = 'application/wasm' @@ -132,8 +139,20 @@ def run_web_server(): pass Handler.log_message = dummy_log - with socketserver.TCPServer(("", PORT), Handler) as httpd: - print("serving at port", PORT) + with socketserver.TCPServer(("", 0), Handler) as httpd: + host, port = httpd.server_address + print("serving at port", port) + q.put(port) + + def service_actions(): + try: + if q.get(False) == "TERMINATE": + sys.exit(0) + httpd.shutdown() + except queue.Empty: + pass + + httpd.service_actions = service_actions httpd.serve_forever() diff --git a/test/test_python.py b/test/test_python.py index fd30db8cb..5d61dc7a8 100644 --- a/test/test_python.py +++ b/test/test_python.py @@ -177,7 +177,7 @@ def test_jsproxy(selenium): def test_open_url(selenium): assert selenium.run( "import pyodide\n" - "pyodide.open_url('../test/data.txt').read()\n") == 'HELLO\n' + "pyodide.open_url('test_data.txt').read()\n") == 'HELLO\n' def test_run_core_python_test(python_test, selenium):