Browse Source

More tests

tags/nilmrun-0.2
Jim Paris 11 years ago
parent
commit
e2c9575937
6 changed files with 17 additions and 10 deletions
  1. +1
    -0
      .gitignore
  2. +1
    -0
      nilmrun/filters/__init__.py
  3. +2
    -1
      nilmrun/filters/dummy.py
  4. +2
    -0
      nilmrun/filters/trainola.py
  5. +7
    -5
      nilmrun/server.py
  6. +4
    -4
      tests/test_client.py

+ 1
- 0
.gitignore View File

@@ -1,3 +1,4 @@
.coverage
build/
*.pyc
dist/


+ 1
- 0
nilmrun/filters/__init__.py View File

@@ -0,0 +1 @@
# Filters

nilmrun/dummyfilter.py → nilmrun/filters/dummy.py View File

@@ -6,7 +6,8 @@ import signal
import sys

# This is just for testing the process management.
def dummy(n):
def filterfunc(n):
n = int(n)
if n < 0: # raise an exception
raise Exception("test exception")
if n == 0: # ignore SIGTERM and count to 40

nilmrun/trainola.py → nilmrun/filters/trainola.py View File

@@ -235,6 +235,8 @@ def trainola(conf):

return "done"

filterfunc = trainola

def main(argv = None):
import simplejson as json
import argparse

+ 7
- 5
nilmrun/server.py View File

@@ -25,8 +25,8 @@ from nilmdb.server.serverutil import (
cherrypy_stop,
)
import nilmrun
import nilmrun.trainola
import nilmrun.dummyfilter
import nilmrun.filters.trainola
import nilmrun.filters.dummy

# Add CORS_allow tool
cherrypy.tools.CORS_allow = cherrypy.Tool('on_start_resource', CORS_allow)
@@ -121,7 +121,8 @@ class AppFilter(object):
@exception_to_httperror(KeyError, ValueError)
@cherrypy.tools.CORS_allow(methods = ["POST"])
def trainola(self, data):
return self.manager.run("trainola", nilmrun.trainola.trainola, data)
return self.manager.run(
"trainola", nilmrun.filters.trainola.filterfunc, data)

# /filter/dummy
@cherrypy.expose
@@ -129,8 +130,9 @@ class AppFilter(object):
@cherrypy.tools.json_out()
@exception_to_httperror(KeyError, ValueError)
@cherrypy.tools.CORS_allow(methods = ["POST"])
def dummy(self, count):
return self.manager.run("dummy", nilmrun.dummyfilter.dummy, int(count))
def dummy(self, data):
return self.manager.run(
"dummy", nilmrun.filters.dummy.filterfunc, data)

class Server(object):
def __init__(self, host = '127.0.0.1', port = 8080,


+ 4
- 4
tests/test_client.py View File

@@ -73,7 +73,7 @@ class TestClient(object):
client = HTTPClient(baseurl = testurl, post_json = True)

# start dummy filter
pid = client.post("/filter/dummy", { "count": 30 })
pid = client.post("/filter/dummy", { "data": 30 })
eq_(client.get("/process/list"), [pid])
time.sleep(1)

@@ -110,7 +110,7 @@ class TestClient(object):
client = HTTPClient(baseurl = testurl, post_json = True)

# Trigger exception in filter
pid = client.post("/filter/dummy", { "count": -1 })
pid = client.post("/filter/dummy", { "data": -1 })
time.sleep(0.5)
status = client.get("/process/status", { "pid": pid })
eq_(status["alive"], False)
@@ -119,7 +119,7 @@ class TestClient(object):
client.post("/process/remove", { "pid": pid })

# Kill a running filter by removing it early
newpid = client.post("/filter/dummy", { "count": 30 })
newpid = client.post("/filter/dummy", { "data": 30 })
ne_(newpid, pid)
time.sleep(0.5)
status = client.post("/process/remove", { "pid": newpid })
@@ -131,7 +131,7 @@ class TestClient(object):

# Try to remove a running filter that ignored SIGTERM
# (can't be killed)
pid = client.post("/filter/dummy", { "count": 0 })
pid = client.post("/filter/dummy", { "data": 0 })
with assert_raises(ServerError) as e:
status = client.post("/process/remove", { "pid": pid })
in_("503 Service Unavailable", str(e.exception))


Loading…
Cancel
Save