Fixed some live stream test errors.

Added test demonstrating using multiple stream clients
in a single test.
This commit is contained in:
Lance Stout 2010-11-17 15:45:16 -05:00
parent 6ee8a2980c
commit ea48bb5ac5
4 changed files with 101 additions and 11 deletions

View file

@ -7,6 +7,7 @@
""" """
import socket import socket
import threading
try: try:
import queue import queue
except ImportError: except ImportError:
@ -40,6 +41,8 @@ class TestLiveSocket(object):
self.recv_buffer = [] self.recv_buffer = []
self.recv_queue = queue.Queue() self.recv_queue = queue.Queue()
self.send_queue = queue.Queue() self.send_queue = queue.Queue()
self.send_queue_lock = threading.Lock()
self.recv_queue_lock = threading.Lock()
self.is_live = True self.is_live = True
def __getattr__(self, name): def __getattr__(self, name):
@ -108,7 +111,8 @@ class TestLiveSocket(object):
Placeholders. Same as for socket.recv. Placeholders. Same as for socket.recv.
""" """
data = self.socket.recv(*args, **kwargs) data = self.socket.recv(*args, **kwargs)
self.recv_queue.put(data) with self.recv_queue_lock:
self.recv_queue.put(data)
return data return data
def send(self, data): def send(self, data):
@ -120,7 +124,8 @@ class TestLiveSocket(object):
Arguments: Arguments:
data -- String value to write. data -- String value to write.
""" """
self.send_queue.put(data) with self.send_queue_lock:
self.send_queue.put(data)
self.socket.send(data) self.socket.send(data)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
@ -143,3 +148,15 @@ class TestLiveSocket(object):
Placeholders, same as socket.recv() Placeholders, same as socket.recv()
""" """
return self.recv(*args, **kwargs) return self.recv(*args, **kwargs)
def clear(self):
"""
Empty the send queue, typically done once the session has started to
remove the feature negotiation and log in stanzas.
"""
with self.send_queue_lock:
for i in range(0, self.send_queue.qsize()):
self.send_queue.get(block=False)
with self.recv_queue_lock:
for i in range(0, self.recv_queue.qsize()):
self.recv_queue.get(block=False)

View file

@ -7,6 +7,10 @@
""" """
import unittest import unittest
try:
import Queue as queue
except:
import queue
import sleekxmpp import sleekxmpp
from sleekxmpp import ClientXMPP, ComponentXMPP from sleekxmpp import ClientXMPP, ComponentXMPP
@ -279,6 +283,10 @@ class SleekTest(unittest.TestCase):
else: else:
raise ValueError("Unknown XMPP connection mode.") raise ValueError("Unknown XMPP connection mode.")
# We will use this to wait for the session_start event
# for live connections.
skip_queue = queue.Queue()
if socket == 'mock': if socket == 'mock':
self.xmpp.set_socket(TestSocket()) self.xmpp.set_socket(TestSocket())
@ -293,6 +301,10 @@ class SleekTest(unittest.TestCase):
self.xmpp.socket.recv_data(header) self.xmpp.socket.recv_data(header)
elif socket == 'live': elif socket == 'live':
self.xmpp.socket_class = TestLiveSocket self.xmpp.socket_class = TestLiveSocket
def wait_for_session(x):
self.xmpp.socket.clear()
skip_queue.put('started')
self.xmpp.add_event_handler('session_start', wait_for_session)
self.xmpp.connect() self.xmpp.connect()
else: else:
raise ValueError("Unknown socket type.") raise ValueError("Unknown socket type.")
@ -300,10 +312,13 @@ class SleekTest(unittest.TestCase):
self.xmpp.register_plugins() self.xmpp.register_plugins()
self.xmpp.process(threaded=True) self.xmpp.process(threaded=True)
if skip: if skip:
# Clear startup stanzas if socket != 'live':
self.xmpp.socket.next_sent(timeout=1) # Clear startup stanzas
if mode == 'component':
self.xmpp.socket.next_sent(timeout=1) self.xmpp.socket.next_sent(timeout=1)
if mode == 'component':
self.xmpp.socket.next_sent(timeout=1)
else:
skip_queue.get(block=True, timeout=10)
def make_header(self, sto='', def make_header(self, sto='',
sfrom='', sfrom='',
@ -573,11 +588,13 @@ class SleekTest(unittest.TestCase):
Defaults to the value of self.match_method. Defaults to the value of self.match_method.
""" """
sent = self.xmpp.socket.next_sent(timeout) sent = self.xmpp.socket.next_sent(timeout)
if isinstance(data, str): if sent is None:
xml = self.parse_xml(data) return False
self.fix_namespaces(xml, 'jabber:client') print sent
data = self.xmpp._build_stanza(xml, 'jabber:client') xml = self.parse_xml(sent)
self.check(data, sent, self.fix_namespaces(xml, 'jabber:client')
sent = self.xmpp._build_stanza(xml, 'jabber:client')
self.check(sent, data,
method=method, method=method,
defaults=defaults, defaults=defaults,
use_values=use_values) use_values=use_values)

View file

@ -0,0 +1,57 @@
import logging
from sleekxmpp.test import *
class TestMultipleStreams(SleekTest):
"""
Test that we can test a live stanza stream.
"""
def setUp(self):
self.client1 = SleekTest()
self.client2 = SleekTest()
def tearDown(self):
self.client1.stream_close()
self.client2.stream_close()
def testMultipleStreams(self):
"""Test that we can interact with multiple live ClientXMPP instance."""
client1 = self.client1
client2 = self.client2
client1.stream_start(mode='client',
socket='live',
skip=True,
jid='user@localhost/test1',
password='user')
client2.stream_start(mode='client',
socket='live',
skip=True,
jid='user@localhost/test2',
password='user')
client1.xmpp.send_message(mto='user@localhost/test2',
mbody='test')
client1.send('message@body=test', method='stanzapath')
client2.recv('message@body=test', method='stanzapath')
suite = unittest.TestLoader().loadTestsFromTestCase(TestMultipleStreams)
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG,
format='%(levelname)-8s %(message)s')
tests = unittest.TestSuite([suite])
result = unittest.TextTestRunner(verbosity=2).run(tests)
test_ns = 'http://andyet.net/protocol/tests'
print("<tests xmlns='%s' %s %s %s %s />" % (
test_ns,
'ran="%s"' % result.testsRun,
'errors="%s"' % len(result.errors),
'fails="%s"' % len(result.failures),
'success="%s"' % result.wasSuccessful()))

View file

@ -1,7 +1,6 @@
import logging import logging
from sleekxmpp.test import * from sleekxmpp.test import *
import sleekxmpp.plugins.xep_0033 as xep_0033
class TestLiveStream(SleekTest): class TestLiveStream(SleekTest):