diff options
Diffstat (limited to 'tests/test_twisted.py')
-rw-r--r-- | tests/test_twisted.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/tests/test_twisted.py b/tests/test_twisted.py new file mode 100644 index 0000000..6a667ed --- /dev/null +++ b/tests/test_twisted.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- + +from mock import Mock +from twisted.internet.defer import inlineCallbacks +from twisted.python.failure import Failure + +from pyee import TwistedEventEmitter + + +class PyeeTestError(Exception): + pass + + +def test_propagates_failure(): + """Test that TwistedEventEmitters can propagate failures + from twisted Deferreds + """ + ee = TwistedEventEmitter() + + should_call = Mock() + + @ee.on("event") + @inlineCallbacks + def event_handler(): + yield Failure(PyeeTestError()) + + @ee.on("failure") + def handle_failure(f): + assert isinstance(f, Failure) + should_call(f) + + ee.emit("event") + + should_call.assert_called_once() + + +def test_propagates_sync_failure(): + """Test that TwistedEventEmitters can propagate failures + from twisted Deferreds + """ + ee = TwistedEventEmitter() + + should_call = Mock() + + @ee.on("event") + def event_handler(): + raise PyeeTestError() + + @ee.on("failure") + def handle_failure(f): + assert isinstance(f, Failure) + should_call(f) + + ee.emit("event") + + should_call.assert_called_once() + + +def test_propagates_exception(): + """Test that TwistedEventEmitters propagate failures as exceptions to + the error event when no failure handler + """ + + ee = TwistedEventEmitter() + + should_call = Mock() + + @ee.on("event") + @inlineCallbacks + def event_handler(): + yield Failure(PyeeTestError()) + + @ee.on("error") + def handle_error(exc): + assert isinstance(exc, Exception) + should_call(exc) + + ee.emit("event") + + should_call.assert_called_once() |