summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Pursehouse <david.pursehouse@sonyericsson.com>2011-10-27 17:23:53 +0900
committerDavid Pursehouse <david.pursehouse@sonymobile.com>2012-07-24 14:44:36 +0900
commit5dae4a1c98ab0bc4fe52fa0e0290c7384a871235 (patch)
treef187ab3c454992a362eae9e131cbefad444196ed
parent0e343379c4e3be9b4f041181602c63e7361c7764 (diff)
downloadpygerrit-5dae4a1c98ab0bc4fe52fa0e0290c7384a871235.tar.gz
Check for callable event handler on attach
When a listener is attached to the stream object, check that its "on_gerrit_event" is callable.
-rwxr-xr-xgerrit_stream.py4
-rwxr-xr-xtests/gerrit_stream/unittest_gerrit_stream/unittests.py19
2 files changed, 22 insertions, 1 deletions
diff --git a/gerrit_stream.py b/gerrit_stream.py
index 5578f7f..00c74ed 100755
--- a/gerrit_stream.py
+++ b/gerrit_stream.py
@@ -227,11 +227,13 @@ class GerritStream:
def attach(self, listener):
''' Attach the `listener` to the list of listeners.
Raise GerritStream error if the listener does not match the
- expected signature.
+ expected signature, or if its event handler is not callable.
'''
if not hasattr(listener, "on_gerrit_event"):
raise GerritStreamError("Listener must have `on_gerrit_event` " \
"event handler")
+ if not callable(listener.on_gerrit_event):
+ raise GerritStreamError("`on_gerrit_event` must be callable")
if not listener.on_gerrit_event.func_code.co_argcount == 2:
raise GerritStreamError("`on_gerrit_event` must take 1 arg")
if not listener in self.listeners:
diff --git a/tests/gerrit_stream/unittest_gerrit_stream/unittests.py b/tests/gerrit_stream/unittest_gerrit_stream/unittests.py
index c84c932..93f9925 100755
--- a/tests/gerrit_stream/unittest_gerrit_stream/unittests.py
+++ b/tests/gerrit_stream/unittest_gerrit_stream/unittests.py
@@ -23,6 +23,17 @@ class ListenerWithInvalidHandler():
pass
+class ListenerWithInvalidHandlerNotCallable():
+ """ Dummy listener class with invalid event handler
+ that is not callable.
+ """
+
+ on_gerrit_event = "this is a string"
+
+ def __init__(self):
+ pass
+
+
class ListenerWithValidHandler():
""" Dummy listener class.
"""
@@ -53,6 +64,14 @@ class TestGerritStream(unittest.TestCase):
l = ListenerWithInvalidHandler()
self.assertRaises(GerritStreamError, g.attach, l)
+ def test_listener_non_callable_handler(self):
+ """ Test that an exception is raised if a listener with
+ non-callable event handler is added.
+ """
+ g = GerritStream()
+ l = ListenerWithInvalidHandlerNotCallable()
+ self.assertRaises(GerritStreamError, g.attach, l)
+
def test_listener_valid_handler(self):
""" Test that a valid listener can be added.
"""