2010-10-01 17:49:58 +00:00
|
|
|
import time
|
2010-10-07 14:58:13 +00:00
|
|
|
from sleekxmpp.test import *
|
2010-05-20 20:09:04 +00:00
|
|
|
|
|
|
|
|
2010-07-20 16:22:25 +00:00
|
|
|
class TestEvents(SleekTest):
|
2010-10-01 17:49:58 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2010-10-07 13:22:27 +00:00
|
|
|
self.stream_start()
|
2010-10-01 17:49:58 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2010-10-07 13:22:27 +00:00
|
|
|
self.stream_close()
|
2010-10-01 17:49:58 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
# Give the event queue time to process.
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
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", {})
|
|
|
|
|
|
|
|
# Give the event queue time to process.
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
msg = "Event was not triggered the correct number of times: %s"
|
|
|
|
self.failUnless(happened == [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", {})
|
|
|
|
|
|
|
|
# Give the event queue time to process.
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
msg = "Event was not triggered the correct number of times: %s"
|
|
|
|
self.failUnless(happened == [True], msg % happened)
|
|
|
|
|
2010-05-20 20:09:04 +00:00
|
|
|
|
2010-07-20 16:22:25 +00:00
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestEvents)
|