From c15b53924d6f579a2aa64c57057e62ecc2e1ff1e Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Tue, 4 Aug 2020 23:34:13 -0400 Subject: [PATCH] Use multiprocessing to start multiple servers in tests Cherrypy uses a bunch of globals, so use multiprocessing to let us spawn a few that are totally independent. --- .gitignore | 2 +- tests/runtests.py | 5 +++-- tests/test.py | 38 ++++++++++++++++++++++++++++++-------- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 4bcef61..74f6f24 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,5 @@ build/ dist/ *.egg-info/ .eggs/ -tests/testdb +tests/testdb* MANIFEST diff --git a/tests/runtests.py b/tests/runtests.py index 8c25399..9c277c5 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -45,5 +45,6 @@ class JimOrderPlugin(nose.plugins.Plugin): # Use setup.cfg for most of the test configuration. Adding # --with-jimorder here means that a normal "nosetests" run will # still work, it just won't support test.order. -nose.main(addplugins = [ JimOrderPlugin() ], - argv = sys.argv + ["--with-jimorder"]) +if __name__ == "__main__": + nose.main(addplugins = [ JimOrderPlugin() ], + argv = sys.argv + ["--with-jimorder"]) diff --git a/tests/test.py b/tests/test.py index d829d35..5335f0a 100644 --- a/tests/test.py +++ b/tests/test.py @@ -17,26 +17,48 @@ from nose.tools import assert_raises import unittest from testutil.helpers import * +import multiprocessing +import traceback +from urllib.request import urlopen from nilmtools.filter import ArgumentError +def run_cherrypy_server(path, port, event): + db = nilmdb.utils.serializer_proxy(nilmdb.server.NilmDB)(path) + server = nilmdb.server.Server(db, host="127.0.0.1", + port=port, stoppable=True) + server.start(blocking = True, event = event) + db.close() + class CommandTester(): url = "http://localhost:32182/" + url2 = "http://localhost:32183/" @classmethod def setup_class(cls): - path = "tests/testdb" - recursive_unlink(path) - cls.db = nilmdb.utils.serializer_proxy(nilmdb.server.NilmDB)(path) - cls.server = nilmdb.server.Server(cls.db, host="127.0.0.1", - port=32182, stoppable=True) - cls.server.start(blocking=False) + # Use multiprocessing with "spawn" method, so that we can + # start two fully independent cherrypy instances + # (needed for copy-wildcard) + multiprocessing.set_start_method('spawn') + + events = [] + for (path, port) in (("tests/testdb1", 32182), + ("tests/testdb2", 32183)): + recursive_unlink(path) + event = multiprocessing.Event() + proc = multiprocessing.Process(target=run_cherrypy_server, + args=(path, port, event)) + proc.start() + events.append(event) + for event in events: + if not event.wait(timeout = 10): + raise AssertionError("server didn't start") @classmethod def teardown_class(cls): - cls.server.stop() - cls.db.close() + urlopen("http://127.0.0.1:32182/exit/", timeout = 1) + urlopen("http://127.0.0.1:32183/exit/", timeout = 1) def run(self, arg_string, infile=None, outfile=None): """Run a cmdline client with the specified argument string,