mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-27 19:19:54 +00:00
Modified event handling to use the event queue.
Updated tests to match. (Needed to add a small wait to make sure the event got through the queue before checking the results.)
This commit is contained in:
parent
2662131124
commit
9a34c9a9a1
2 changed files with 91 additions and 38 deletions
|
@ -398,6 +398,11 @@ class XMLStream(object):
|
||||||
stream processing and not during normal event
|
stream processing and not during normal event
|
||||||
processing.
|
processing.
|
||||||
"""
|
"""
|
||||||
|
# To prevent circular dependencies, we must load the matcher
|
||||||
|
# and handler classes here.
|
||||||
|
from sleekxmpp.xmlstream.matcher import MatchXMLMask
|
||||||
|
from sleekxmpp.xmlstream.handler import XMLCallback
|
||||||
|
|
||||||
if name is None:
|
if name is None:
|
||||||
name = 'add_handler_%s' % self.getNewId()
|
name = 'add_handler_%s' % self.getNewId()
|
||||||
self.registerHandler(XMLCallback(name, MatchXMLMask(mask), pointer,
|
self.registerHandler(XMLCallback(name, MatchXMLMask(mask), pointer,
|
||||||
|
@ -478,19 +483,13 @@ class XMLStream(object):
|
||||||
Defaults to an empty dictionary.
|
Defaults to an empty dictionary.
|
||||||
"""
|
"""
|
||||||
for handler in self.__event_handlers.get(name, []):
|
for handler in self.__event_handlers.get(name, []):
|
||||||
func, threaded, disposable = handler
|
self.event_queue.put(('event', handler, copy.copy(data)))
|
||||||
|
if handler[2]:
|
||||||
handler_data = copy.copy(data)
|
# If the handler is disposable, we will go ahead and
|
||||||
if threaded:
|
# remove it now instead of waiting for it to be
|
||||||
x = threading.Thread(name="Event_%s" % str(func),
|
# processed in the queue.
|
||||||
target=func,
|
|
||||||
args=(handler_data,))
|
|
||||||
x.start()
|
|
||||||
else:
|
|
||||||
func(handler_data)
|
|
||||||
if disposable:
|
|
||||||
with self.__event_handlers_lock:
|
with self.__event_handlers_lock:
|
||||||
handler_index = self.event_handlers[name].index(handler)
|
handler_index = self.__event_handlers[name].index(handler)
|
||||||
self.__event_handlers[name].pop(handler_index)
|
self.__event_handlers[name].pop(handler_index)
|
||||||
|
|
||||||
def schedule(self, name, seconds, callback, args=None,
|
def schedule(self, name, seconds, callback, args=None,
|
||||||
|
@ -522,7 +521,7 @@ class XMLStream(object):
|
||||||
"""
|
"""
|
||||||
return xml
|
return xml
|
||||||
|
|
||||||
def send(self, data, mask, timeout=RESPONSE_TIMEOUT):
|
def send(self, data, mask=None, timeout=RESPONSE_TIMEOUT):
|
||||||
"""
|
"""
|
||||||
A wrapper for send_raw for sending stanza objects.
|
A wrapper for send_raw for sending stanza objects.
|
||||||
|
|
||||||
|
@ -772,7 +771,7 @@ class XMLStream(object):
|
||||||
try:
|
try:
|
||||||
handler.run(args[0])
|
handler.run(args[0])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_msg = 'Error processing event handler: %s'
|
error_msg = 'Error processing stream handler: %s'
|
||||||
logging.exception(error_msg % handler.name)
|
logging.exception(error_msg % handler.name)
|
||||||
args[0].exception(e)
|
args[0].exception(e)
|
||||||
elif etype == 'schedule':
|
elif etype == 'schedule':
|
||||||
|
@ -781,6 +780,18 @@ class XMLStream(object):
|
||||||
handler(*args[0])
|
handler(*args[0])
|
||||||
except:
|
except:
|
||||||
logging.exception('Error processing scheduled task')
|
logging.exception('Error processing scheduled task')
|
||||||
|
elif etype == 'event':
|
||||||
|
func, threaded, disposable = handler
|
||||||
|
try:
|
||||||
|
if threaded:
|
||||||
|
x = threading.Thread(name="Event_%s" % str(func),
|
||||||
|
target=func,
|
||||||
|
args=args)
|
||||||
|
x.start()
|
||||||
|
else:
|
||||||
|
func(*args)
|
||||||
|
except:
|
||||||
|
logging.exception('Error processing event handler: %s')
|
||||||
elif etype == 'quit':
|
elif etype == 'quit':
|
||||||
logging.debug("Quitting event runner thread")
|
logging.debug("Quitting event runner thread")
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -1,31 +1,73 @@
|
||||||
import sleekxmpp
|
import sleekxmpp
|
||||||
|
import time
|
||||||
from . sleektest import *
|
from . sleektest import *
|
||||||
|
|
||||||
|
|
||||||
class TestEvents(SleekTest):
|
class TestEvents(SleekTest):
|
||||||
|
|
||||||
def testEventHappening(self):
|
def setUp(self):
|
||||||
"Test handler working"
|
self.streamStart()
|
||||||
c = sleekxmpp.ClientXMPP('crap@wherever', 'password')
|
|
||||||
happened = []
|
|
||||||
def handletestevent(event):
|
|
||||||
happened.append(True)
|
|
||||||
c.add_event_handler("test_event", handletestevent)
|
|
||||||
c.event("test_event", {})
|
|
||||||
c.event("test_event", {})
|
|
||||||
self.failUnless(happened == [True, True], "event did not get triggered twice")
|
|
||||||
|
|
||||||
def testDelEvent(self):
|
def tearDown(self):
|
||||||
"Test handler working, then deleted and not triggered"
|
self.streamClose()
|
||||||
c = sleekxmpp.ClientXMPP('crap@wherever', 'password')
|
|
||||||
happened = []
|
def testEventHappening(self):
|
||||||
def handletestevent(event):
|
"""Test handler working"""
|
||||||
happened.append(True)
|
happened = []
|
||||||
c.add_event_handler("test_event", handletestevent)
|
|
||||||
c.event("test_event", {})
|
def handletestevent(event):
|
||||||
c.del_event_handler("test_event", handletestevent)
|
happened.append(True)
|
||||||
c.event("test_event", {}) # should not trigger because it was deleted
|
|
||||||
self.failUnless(happened == [True], "event did not get triggered the correct number of times")
|
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)
|
||||||
|
|
||||||
|
|
||||||
suite = unittest.TestLoader().loadTestsFromTestCase(TestEvents)
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestEvents)
|
||||||
|
|
Loading…
Reference in a new issue