diff options
author | Andy Hayden <andyhayden1@gmail.com> | 2019-05-30 13:40:40 -0700 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-05-30 16:40:40 -0400 |
commit | 8fb44eba5bb9862de5fcc6c785eb6f21ecbd0aea (patch) | |
tree | 9c866283b36d8388af4fd0e72d2bf2da4b8b70e4 /tools/http_server.py | |
parent | 1540b36ce7a4740f4a87c564becca65ed8d97255 (diff) |
chore: refactor python tests to use unittest (#2414)
Move every test to a method on DenoTestCase.
test.py is a single TestSuite of every TestCase.
Add a Spawn context manager for http_server,
this is explicitly used where it's needed.
Each python test file can now be run independently
without needing to manually run http_server.
Add --help and consistent flags using argparse for
each python test, including --failfast.
Use ColorTextTestRunner so that '... ok' is green.
Diffstat (limited to 'tools/http_server.py')
-rwxr-xr-x | tools/http_server.py | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/tools/http_server.py b/tools/http_server.py index e44a86fc7..7415ee47c 100755 --- a/tools/http_server.py +++ b/tools/http_server.py @@ -2,13 +2,15 @@ # Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. # Many tests expect there to be an http server on port 4545 servering the deno # root directory. +from collections import namedtuple +from contextlib import contextmanager import os -import sys -from threading import Thread import SimpleHTTPServer import SocketServer -from util import root_path +import sys from time import sleep +from threading import Thread +from util import root_path PORT = 4545 REDIRECT_PORT = 4546 @@ -87,6 +89,9 @@ class ContentTypeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): return SimpleHTTPServer.SimpleHTTPRequestHandler.guess_type(self, path) +RunningServer = namedtuple("RunningServer", ["server", "thread"]) + + def server(): os.chdir(root_path) # Hopefully the main thread doesn't also chdir. Handler = ContentTypeHandler @@ -98,7 +103,7 @@ def server(): SocketServer.TCPServer.allow_reuse_address = True s = SocketServer.TCPServer(("", PORT), Handler) print "Deno test server http://localhost:%d/" % PORT - return s + return RunningServer(s, start(s)) def base_redirect_server(host_port, target_port, extra_path_segment=""): @@ -117,7 +122,7 @@ def base_redirect_server(host_port, target_port, extra_path_segment=""): s = SocketServer.TCPServer(("", host_port), Handler) print "redirect server http://localhost:%d/ -> http://localhost:%d/" % ( host_port, target_port) - return s + return RunningServer(s, start(s)) # redirect server @@ -137,35 +142,30 @@ def double_redirects_server(): return base_redirect_server(DOUBLE_REDIRECTS_PORT, REDIRECT_PORT) -def spawn(): - # Main http server - s = server() - thread = Thread(target=s.serve_forever) +def start(s): + thread = Thread(target=s.serve_forever, kwargs={"poll_interval": 0.05}) thread.daemon = True thread.start() - # Redirect server - rs = redirect_server() - r_thread = Thread(target=rs.serve_forever) - r_thread.daemon = True - r_thread.start() - # Another redirect server - ars = another_redirect_server() - ar_thread = Thread(target=ars.serve_forever) - ar_thread.daemon = True - ar_thread.start() - # Double redirects server - drs = double_redirects_server() - dr_thread = Thread(target=drs.serve_forever) - dr_thread.daemon = True - dr_thread.start() - sleep(1) # TODO I'm too lazy to figure out how to do this properly. return thread +@contextmanager +def spawn(): + servers = (server(), redirect_server(), another_redirect_server(), + double_redirects_server()) + sleep(1) # TODO I'm too lazy to figure out how to do this properly. + try: + yield + finally: + for s in servers: + s.server.shutdown() + + def main(): + servers = (server(), redirect_server(), another_redirect_server(), + double_redirects_server()) try: - thread = spawn() - while thread.is_alive(): + while all(s.thread.is_alive() for s in servers): sleep(10) except KeyboardInterrupt: pass |