Browse Source

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.
tags/nilmtools-2.0.0
Jim Paris 9 months ago
parent
commit
c15b53924d
3 changed files with 34 additions and 11 deletions
  1. +1
    -1
      .gitignore
  2. +3
    -2
      tests/runtests.py
  3. +30
    -8
      tests/test.py

+ 1
- 1
.gitignore View File

@@ -7,5 +7,5 @@ build/
dist/
*.egg-info/
.eggs/
tests/testdb
tests/testdb*
MANIFEST

+ 3
- 2
tests/runtests.py View File

@@ -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"])

+ 30
- 8
tests/test.py View File

@@ -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,


Loading…
Cancel
Save