From 5f2fc67c40f0cd73bee7b2cf3bc3a73d83832a50 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Tue, 7 Dec 2010 17:19:39 -0500 Subject: Added option for iq.send to accept a callhandler. The callback will be a stream level handler, and will not execute in its own thread. If you must have a thread, have the callback function raise a custom event, which can be processed by another event handler, which may run in an individual thread, like so: def handle_reply(self, iq): self.event('custom_event', iq) def do_long_operation_in_thread(self, iq): ... self.add_event_handler('custom_event', self.do_long_operation_in_thread) ...take out already prepared iq stanza... iq.send(callback=self.handle_reply) --- tests/test_stream_handlers.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'tests/test_stream_handlers.py') diff --git a/tests/test_stream_handlers.py b/tests/test_stream_handlers.py index 2b878b37..a475b36c 100644 --- a/tests/test_stream_handlers.py +++ b/tests/test_stream_handlers.py @@ -1,3 +1,5 @@ +import time + from sleekxmpp.test import * from sleekxmpp.xmlstream.handler import * from sleekxmpp.xmlstream.matcher import * @@ -108,5 +110,41 @@ class TestHandlers(SleekTest): self.failUnless(waiter_exists == False, "Waiter handler was not removed.") + def testIqCallback(self): + """Test that iq.send(callback=handle_foo) works.""" + events = [] + + def handle_foo(iq): + events.append('foo') + + iq = self.Iq() + iq['type'] = 'get' + iq['id'] = 'test-foo' + iq['to'] = 'user@localhost' + iq['query'] = 'foo' + iq.send(callback=handle_foo) + + self.send(""" + + + + """) + + self.recv(""" + + + + + + """) + + # Give event queue time to process + time.sleep(0.1) + + self.failUnless(events == ['foo'], + "Iq callback was not executed: %s" % events) + suite = unittest.TestLoader().loadTestsFromTestCase(TestHandlers) -- cgit v1.2.3