Browse Source

Improve test coverage and remove the last "#pragma: no cover"

We now have full coverage of the main code.
tags/nilmdb-2.0.0
Jim Paris 2 years ago
parent
commit
b20bb92988
2 changed files with 30 additions and 19 deletions
  1. +4
    -7
      nilmdb/server/serverutil.py
  2. +26
    -12
      tests/test_nilmdb.py

+ 4
- 7
nilmdb/server/serverutil.py View File

@@ -186,14 +186,11 @@ def cherrypy_start(blocking = False, event = False):
try:
cherrypy.engine.wait(cherrypy.engine.states.EXITING,
interval = 0.1, channel = 'main')
except (KeyboardInterrupt, IOError): # pragma: no cover (hard to invoke
# from tests, standalone server
# isn't used in production)
cherrypy.engine.log('Keyboard Interrupt: shutting down bus')
except (KeyboardInterrupt, IOError):
cherrypy.engine.log('Keyboard Interrupt: shutting down')
cherrypy.engine.exit()
except SystemExit: # pragma: no cover (hard to invoke from tests;
# standalone server isn't used in production)
cherrypy.engine.log('SystemExit raised: shutting down bus')
except SystemExit:
cherrypy.engine.log('SystemExit raised: shutting down')
cherrypy.engine.exit()
raise



+ 26
- 12
tests/test_nilmdb.py View File

@@ -16,6 +16,7 @@ import time
import requests
import socket
import sqlite3
import cherrypy

from nilmdb.utils import serializer_proxy
from nilmdb.server.interval import Interval
@@ -163,21 +164,34 @@ class TestBlockingServer(object):
self.server = nilmdb.server.Server(self.db, host = "127.0.0.1",
port = 32180, stoppable = True)

# Run it
event = threading.Event()
def run_server():
self.server.start(blocking = True, event = event)
thread = threading.Thread(target = run_server)
thread.start()
if not event.wait(timeout = 10):
raise AssertionError("server didn't start in 10 seconds")

# Send request to exit.
def start_server():
# Run it
event = threading.Event()
def run_server():
self.server.start(blocking = True, event = event)
thread = threading.Thread(target = run_server)
thread.start()
if not event.wait(timeout = 10):
raise AssertionError("server didn't start in 10 seconds")
return thread

# Start server and request for it to exit
thread = start_server()
req = urlopen("http://127.0.0.1:32180/exit/", timeout = 1)

# Wait for it
thread.join()

# Mock some signals that should kill the server
def try_signal(sig):
old = cherrypy.engine.wait
def raise_sig(*args, **kwargs):
raise sig()
cherrypy.engine.wait = raise_sig
thread = start_server()
thread.join()
cherrypy.engine.wait = old
try_signal(SystemExit)
try_signal(KeyboardInterrupt)

def geturl(path):
resp = urlopen("http://127.0.0.1:32180" + path, timeout = 10)
body = resp.read()


Loading…
Cancel
Save