1e2665df19
- monkey-patch our own monkey-patched idle_call to run events immediatly rather than adding them to the event queue, and add a fake transport with a fake socket. - remove the test file related to xep_0059 as it relies on blocking behavior, and comment out one xep_0030 test uses xep_0059 - remove many instances of threading and sleep()s because they do nothing except waste time and introduce race conditions. - keep exactly two sleep() in IoT xeps because they rely on timeouts
84 lines
2.5 KiB
Python
84 lines
2.5 KiB
Python
import time
|
|
import unittest
|
|
from slixmpp.test import SlixTest
|
|
|
|
|
|
class TestEvents(SlixTest):
|
|
|
|
def setUp(self):
|
|
self.stream_start()
|
|
|
|
def tearDown(self):
|
|
self.stream_close()
|
|
|
|
def testEventHappening(self):
|
|
"""Test handler working"""
|
|
happened = []
|
|
|
|
def handletestevent(event):
|
|
happened.append(True)
|
|
|
|
self.xmpp.add_event_handler("test_event", handletestevent)
|
|
self.xmpp.event("test_event")
|
|
self.xmpp.event("test_event")
|
|
|
|
msg = "Event was not triggered the correct number of times: %s"
|
|
self.failUnless(happened == [True, True], msg)
|
|
|
|
def testDelEvent(self):
|
|
"""Test handler working, then deleted and not triggered"""
|
|
happened = []
|
|
|
|
def handletestevent(event):
|
|
happened.append(True)
|
|
|
|
self.xmpp.add_event_handler("test_event", handletestevent)
|
|
self.xmpp.event("test_event", {})
|
|
|
|
self.xmpp.del_event_handler("test_event", handletestevent)
|
|
|
|
# Should not trigger because it was deleted
|
|
self.xmpp.event("test_event", {})
|
|
|
|
msg = "Event was not triggered the correct number of times: %s"
|
|
self.failUnless(happened == [True], msg % happened)
|
|
|
|
def testAddDelAddEvent(self):
|
|
"""Test adding, then removing, then adding an event handler."""
|
|
happened = []
|
|
|
|
def handletestevent(event):
|
|
happened.append(True)
|
|
|
|
self.xmpp.add_event_handler("test_event", handletestevent)
|
|
self.xmpp.event("test_event", {})
|
|
|
|
self.xmpp.del_event_handler("test_event", handletestevent)
|
|
# Should not trigger because it was deleted
|
|
self.xmpp.event("test_event", {})
|
|
|
|
self.xmpp.add_event_handler("test_event", handletestevent)
|
|
self.xmpp.event("test_event", {})
|
|
|
|
msg = "Event was not triggered the correct number of times: %s"
|
|
self.failUnless(happened == [True, True], msg % happened)
|
|
|
|
def testDisposableEvent(self):
|
|
"""Test disposable handler working, then not being triggered again."""
|
|
happened = []
|
|
|
|
def handletestevent(event):
|
|
happened.append(True)
|
|
|
|
self.xmpp.add_event_handler("test_event", handletestevent,
|
|
disposable=True)
|
|
self.xmpp.event("test_event", {})
|
|
|
|
# Should not trigger because it was deleted
|
|
self.xmpp.event("test_event", {})
|
|
|
|
msg = "Event was not triggered the correct number of times: %s"
|
|
self.failUnless(happened == [True], msg % happened)
|
|
|
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestEvents)
|