Explicitly list all events

This commit is contained in:
Aldo Cortesi 2016-05-29 12:15:52 +12:00
parent 9a32402a39
commit 8bb9285678
2 changed files with 25 additions and 2 deletions

View File

@ -6,6 +6,27 @@ import sys
from . import exceptions
Events = frozenset([
"clientconnect",
"clientdisconnect",
"serverconnect",
"serverdisconnect",
"tcp_open",
"tcp_message",
"tcp_error",
"tcp_close",
"request",
"response",
"responseheaders",
"next_layer",
"error",
"log",
])
class ControlError(Exception):
pass
@ -43,6 +64,8 @@ class Master(object):
# exception is thrown.
while True:
mtype, obj = self.event_queue.get(timeout=timeout)
if mtype not in Events:
raise ControlError("Unknown event %s"%repr(mtype))
handle_func = getattr(self, "handle_" + mtype)
if not handle_func.func_dict.get("handler"):
raise ControlError(

View File

@ -18,7 +18,7 @@ class TestMaster(object):
def test_simple(self):
class DummyMaster(controller.Master):
@controller.handler
def handle_panic(self, _):
def handle_log(self, _):
m.should_exit.set()
def tick(self, timeout):
@ -29,7 +29,7 @@ class TestMaster(object):
assert not m.should_exit.is_set()
msg = TMsg()
msg.reply = controller.DummyReply()
m.event_queue.put(("panic", msg))
m.event_queue.put(("log", msg))
m.run()
assert m.should_exit.is_set()