mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-30 19:19:55 +00:00
Begin experimental use of exceptions.
Provides IqTimeout and IqError which are raised when an Iq response does not arrive in time, or it arrives with type='error'.
This commit is contained in:
parent
1469323350
commit
8aa4396e44
6 changed files with 32 additions and 21 deletions
|
@ -231,8 +231,8 @@ class ClientXMPP(BaseXMPP):
|
||||||
'subscription': subscription,
|
'subscription': subscription,
|
||||||
'groups': groups}}
|
'groups': groups}}
|
||||||
response = iq.send(block, timeout, callback)
|
response = iq.send(block, timeout, callback)
|
||||||
if response in [False, None] or not isinstance(response, Iq):
|
if response is None:
|
||||||
return response
|
return None
|
||||||
return response['type'] == 'result'
|
return response['type'] == 'result'
|
||||||
|
|
||||||
def del_roster_item(self, jid):
|
def del_roster_item(self, jid):
|
||||||
|
@ -265,12 +265,7 @@ class ClientXMPP(BaseXMPP):
|
||||||
iq.enable('roster')
|
iq.enable('roster')
|
||||||
response = iq.send(block, timeout, callback)
|
response = iq.send(block, timeout, callback)
|
||||||
|
|
||||||
if response == False:
|
if callback is None:
|
||||||
self.event('roster_timeout')
|
|
||||||
|
|
||||||
if response in [False, None] or not isinstance(response, Iq):
|
|
||||||
return response
|
|
||||||
else:
|
|
||||||
return self._handle_roster(response, request=True)
|
return self._handle_roster(response, request=True)
|
||||||
|
|
||||||
def _handle_stream_features(self, features):
|
def _handle_stream_features(self, features):
|
||||||
|
|
|
@ -52,3 +52,16 @@ class XMPPError(Exception):
|
||||||
self.extension = extension
|
self.extension = extension
|
||||||
self.extension_ns = extension_ns
|
self.extension_ns = extension_ns
|
||||||
self.extension_args = extension_args
|
self.extension_args = extension_args
|
||||||
|
|
||||||
|
|
||||||
|
class IqTimeout(Exception):
|
||||||
|
|
||||||
|
"""
|
||||||
|
An exception which indicates that an IQ request response has not been
|
||||||
|
received within the alloted time window.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class IqError(Exception):
|
||||||
|
|
||||||
|
def __init__(self, iq):
|
||||||
|
self.iq = iq
|
||||||
|
|
|
@ -11,6 +11,7 @@ from sleekxmpp.stanza.rootstanza import RootStanza
|
||||||
from sleekxmpp.xmlstream import StanzaBase, ET
|
from sleekxmpp.xmlstream import StanzaBase, ET
|
||||||
from sleekxmpp.xmlstream.handler import Waiter, Callback
|
from sleekxmpp.xmlstream.handler import Waiter, Callback
|
||||||
from sleekxmpp.xmlstream.matcher import MatcherId
|
from sleekxmpp.xmlstream.matcher import MatcherId
|
||||||
|
from sleekxmpp.exceptions import IqTimeout, IqError
|
||||||
|
|
||||||
|
|
||||||
class Iq(RootStanza):
|
class Iq(RootStanza):
|
||||||
|
@ -197,7 +198,12 @@ class Iq(RootStanza):
|
||||||
waitfor = Waiter('IqWait_%s' % self['id'], MatcherId(self['id']))
|
waitfor = Waiter('IqWait_%s' % self['id'], MatcherId(self['id']))
|
||||||
self.stream.register_handler(waitfor)
|
self.stream.register_handler(waitfor)
|
||||||
StanzaBase.send(self, now=now)
|
StanzaBase.send(self, now=now)
|
||||||
return waitfor.wait(timeout)
|
result = waitfor.wait(timeout)
|
||||||
|
if not result:
|
||||||
|
raise IqTimeout()
|
||||||
|
if result['type'] == 'error':
|
||||||
|
raise IqError(result)
|
||||||
|
return result
|
||||||
else:
|
else:
|
||||||
return StanzaBase.send(self, now=now)
|
return StanzaBase.send(self, now=now)
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ import sleekxmpp
|
||||||
from sleekxmpp import ClientXMPP, ComponentXMPP
|
from sleekxmpp import ClientXMPP, ComponentXMPP
|
||||||
from sleekxmpp.stanza import Message, Iq, Presence
|
from sleekxmpp.stanza import Message, Iq, Presence
|
||||||
from sleekxmpp.test import TestSocket, TestLiveSocket
|
from sleekxmpp.test import TestSocket, TestLiveSocket
|
||||||
|
from sleekxmpp.exceptions import XMPPError, IqTimeout, IqError
|
||||||
from sleekxmpp.xmlstream import ET, register_stanza_plugin
|
from sleekxmpp.xmlstream import ET, register_stanza_plugin
|
||||||
from sleekxmpp.xmlstream import ElementBase, StanzaBase
|
from sleekxmpp.xmlstream import ElementBase, StanzaBase
|
||||||
from sleekxmpp.xmlstream.tostring import tostring
|
from sleekxmpp.xmlstream.tostring import tostring
|
||||||
|
|
|
@ -90,7 +90,10 @@ class TestHandlers(SleekTest):
|
||||||
iq['id'] = 'test2'
|
iq['id'] = 'test2'
|
||||||
iq['type'] = 'set'
|
iq['type'] = 'set'
|
||||||
iq['query'] = 'test2'
|
iq['query'] = 'test2'
|
||||||
|
try:
|
||||||
reply = iq.send(block=True, timeout=0)
|
reply = iq.send(block=True, timeout=0)
|
||||||
|
except IqTimeout:
|
||||||
|
pass
|
||||||
|
|
||||||
self.xmpp.add_event_handler('message', waiter_handler, threaded=True)
|
self.xmpp.add_event_handler('message', waiter_handler, threaded=True)
|
||||||
|
|
||||||
|
|
|
@ -111,19 +111,12 @@ class TestStreamRoster(SleekTest):
|
||||||
def testRosterTimeout(self):
|
def testRosterTimeout(self):
|
||||||
"""Test handling a timed out roster request."""
|
"""Test handling a timed out roster request."""
|
||||||
self.stream_start()
|
self.stream_start()
|
||||||
events = []
|
|
||||||
|
|
||||||
def roster_timeout(event):
|
def do_test():
|
||||||
events.append('roster_timeout')
|
|
||||||
|
|
||||||
self.xmpp.add_event_handler('roster_timeout', roster_timeout)
|
|
||||||
self.xmpp.get_roster(timeout=0)
|
self.xmpp.get_roster(timeout=0)
|
||||||
|
|
||||||
# Give the event queue time to process.
|
|
||||||
time.sleep(.1)
|
time.sleep(.1)
|
||||||
|
|
||||||
self.failUnless(events == ['roster_timeout'],
|
self.assertRaises(IqTimeout, do_test)
|
||||||
"Roster timeout event not triggered: %s." % events)
|
|
||||||
|
|
||||||
def testRosterCallback(self):
|
def testRosterCallback(self):
|
||||||
"""Test handling a roster request callback."""
|
"""Test handling a roster request callback."""
|
||||||
|
|
Loading…
Reference in a new issue