mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-23 19:19:53 +00:00
Added support for testind disconnect errors.
This commit is contained in:
parent
ec9aed5b75
commit
384e1a92b7
5 changed files with 55 additions and 2 deletions
|
@ -58,6 +58,18 @@ class TestLiveSocket(object):
|
|||
# ------------------------------------------------------------------
|
||||
# Testing Interface
|
||||
|
||||
def disconnect_errror(self):
|
||||
"""
|
||||
Used to simulate a socket disconnection error.
|
||||
|
||||
Not used by live sockets.
|
||||
"""
|
||||
try:
|
||||
self.socket.shutdown()
|
||||
self.socket.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
def next_sent(self, timeout=None):
|
||||
"""
|
||||
Get the next stanza that has been sent.
|
||||
|
|
|
@ -39,6 +39,7 @@ class TestSocket(object):
|
|||
self.recv_queue = queue.Queue()
|
||||
self.send_queue = queue.Queue()
|
||||
self.is_live = False
|
||||
self.disconnected = False
|
||||
|
||||
def __getattr__(self, name):
|
||||
"""
|
||||
|
@ -89,6 +90,13 @@ class TestSocket(object):
|
|||
"""
|
||||
self.recv_queue.put(data)
|
||||
|
||||
def disconnect_error(self):
|
||||
"""
|
||||
Simulate a disconnect error by raising a socket.error exception
|
||||
for any current or further socket operations.
|
||||
"""
|
||||
self.disconnected = True
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Socket Interface
|
||||
|
||||
|
@ -99,6 +107,8 @@ class TestSocket(object):
|
|||
Arguments:
|
||||
Placeholders. Same as for socket.Socket.recv.
|
||||
"""
|
||||
if self.disconnected:
|
||||
raise socket.error
|
||||
return self.read(block=True)
|
||||
|
||||
def send(self, data):
|
||||
|
@ -108,6 +118,8 @@ class TestSocket(object):
|
|||
Arguments:
|
||||
data -- String value to write.
|
||||
"""
|
||||
if self.disconnected:
|
||||
raise socket.error
|
||||
self.send_queue.put(data)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
|
@ -132,6 +144,8 @@ class TestSocket(object):
|
|||
timeout -- Time in seconds a block should last before
|
||||
returning None.
|
||||
"""
|
||||
if self.disconnected:
|
||||
raise socket.error
|
||||
if timeout is not None:
|
||||
block = True
|
||||
try:
|
||||
|
|
|
@ -259,6 +259,13 @@ class SleekTest(unittest.TestCase):
|
|||
# ------------------------------------------------------------------
|
||||
# Methods for simulating stanza streams.
|
||||
|
||||
def stream_disconnect(self):
|
||||
"""
|
||||
Simulate a stream disconnection.
|
||||
"""
|
||||
if self.xmpp:
|
||||
self.xmpp.socket.disconnect_error()
|
||||
|
||||
def stream_start(self, mode='client', skip=True, header=None,
|
||||
socket='mock', jid='tester@localhost',
|
||||
password='test', server='localhost',
|
||||
|
|
|
@ -978,7 +978,8 @@ class XMLStream(object):
|
|||
log.debug("SEND: %s" % data)
|
||||
try:
|
||||
self.socket.send(data.encode('utf-8'))
|
||||
except:
|
||||
except Socket.error as serr:
|
||||
self.event('socket_error', serr)
|
||||
log.warning("Failed to send %s" % data)
|
||||
self.disconnect(self.auto_reconnect)
|
||||
except KeyboardInterrupt:
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import time
|
||||
from sleekxmpp.test import *
|
||||
import sleekxmpp.plugins.xep_0033 as xep_0033
|
||||
|
||||
|
||||
class TestStreamTester(SleekTest):
|
||||
|
@ -57,4 +57,23 @@ class TestStreamTester(SleekTest):
|
|||
self.stream_start(mode='client', skip=False)
|
||||
self.send_header(sto='localhost')
|
||||
|
||||
def testStreamDisconnect(self):
|
||||
"""Test that the test socket can simulate disconnections."""
|
||||
self.stream_start()
|
||||
events = set()
|
||||
|
||||
def stream_error(event):
|
||||
events.add('socket_error')
|
||||
|
||||
self.xmpp.add_event_handler('socket_error', stream_error)
|
||||
|
||||
self.stream_disconnect()
|
||||
self.xmpp.send_raw(' ')
|
||||
|
||||
time.sleep(.1)
|
||||
|
||||
self.failUnless('socket_error' in events,
|
||||
"Stream error event not raised: %s" % events)
|
||||
|
||||
|
||||
suite = unittest.TestLoader().loadTestsFromTestCase(TestStreamTester)
|
||||
|
|
Loading…
Reference in a new issue