2008-06-19 01:58:19 +02:00
|
|
|
'''
|
2008-07-14 00:22:58 +02:00
|
|
|
Unit test for NonBlockingTCP tranport.
|
2008-06-19 01:58:19 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
import unittest
|
2008-12-24 15:46:21 +01:00
|
|
|
from xmpp_mocks import IdleQueueThread, IdleMock
|
|
|
|
import sys, os.path
|
2008-06-19 01:58:19 +02:00
|
|
|
|
|
|
|
gajim_root = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
|
|
|
|
|
|
|
|
sys.path.append(gajim_root + '/src/common/xmpp')
|
|
|
|
sys.path.append(gajim_root + '/src/common')
|
|
|
|
|
2008-06-30 02:02:32 +02:00
|
|
|
import transports_nb
|
2008-06-19 01:58:19 +02:00
|
|
|
from client import *
|
|
|
|
|
2008-12-24 15:46:21 +01:00
|
|
|
xmpp_server = ('gajim.org', 5222)
|
2008-06-19 01:58:19 +02:00
|
|
|
'''
|
|
|
|
2-tuple - (XMPP server hostname, c2s port)
|
|
|
|
Script will connect to the machine.
|
|
|
|
'''
|
|
|
|
|
2008-06-30 02:02:32 +02:00
|
|
|
import socket
|
2008-12-24 15:46:21 +01:00
|
|
|
ips = socket.getaddrinfo(xmpp_server[0], xmpp_server[1],
|
|
|
|
socket.AF_UNSPEC,socket.SOCK_STREAM)
|
2008-06-30 02:02:32 +02:00
|
|
|
|
|
|
|
# change xmpp_server on real values
|
|
|
|
ip = ips[0]
|
|
|
|
|
2008-06-19 01:58:19 +02:00
|
|
|
class MockClient(IdleMock):
|
2008-12-24 15:46:21 +01:00
|
|
|
|
2008-06-30 02:02:32 +02:00
|
|
|
def __init__(self, idlequeue):
|
2008-12-24 15:46:21 +01:00
|
|
|
self.idlequeue = idlequeue
|
2008-06-19 01:58:19 +02:00
|
|
|
IdleMock.__init__(self)
|
2008-06-30 02:02:32 +02:00
|
|
|
|
|
|
|
def do_connect(self):
|
2008-12-24 15:46:21 +01:00
|
|
|
self.socket = transports_nb.NonBlockingTCP(
|
2008-12-15 22:06:08 +01:00
|
|
|
lambda(event_type, data): sys.stdout.write('raising event %s: %s' % (
|
|
|
|
event_type, data)), lambda: self.on_success(mode='SocketDisconnect'),
|
|
|
|
self.idlequeue, False, None)
|
2008-06-19 01:58:19 +02:00
|
|
|
|
2008-06-30 02:02:32 +02:00
|
|
|
self.socket.PlugIn(self)
|
2008-06-19 01:58:19 +02:00
|
|
|
|
2008-12-15 22:06:08 +01:00
|
|
|
self.socket.connect(conn_5tuple=ip,
|
2008-06-30 02:02:32 +02:00
|
|
|
on_connect=lambda: self.on_success(mode='TCPconnect'),
|
2008-12-15 22:06:08 +01:00
|
|
|
on_connect_failure=self.on_failure)
|
2008-06-19 01:58:19 +02:00
|
|
|
self.wait()
|
|
|
|
|
2008-06-30 02:02:32 +02:00
|
|
|
def do_disconnect(self):
|
|
|
|
self.socket.disconnect()
|
|
|
|
self.wait()
|
2008-06-19 01:58:19 +02:00
|
|
|
|
|
|
|
def on_failure(self, data):
|
|
|
|
print 'Error: %s' % data
|
|
|
|
self.set_event()
|
|
|
|
|
2008-06-30 02:02:32 +02:00
|
|
|
def on_success(self, mode, data=None):
|
|
|
|
if mode == "TCPconnect":
|
|
|
|
pass
|
|
|
|
if mode == "SocketDisconnect":
|
2008-06-19 01:58:19 +02:00
|
|
|
pass
|
|
|
|
self.set_event()
|
|
|
|
|
2008-07-14 00:22:58 +02:00
|
|
|
class TestNonBlockingTCP(unittest.TestCase):
|
2008-06-19 01:58:19 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.idlequeue_thread = IdleQueueThread()
|
|
|
|
self.idlequeue_thread.start()
|
2008-12-15 22:06:08 +01:00
|
|
|
self.client = MockClient(idlequeue=self.idlequeue_thread.iq)
|
2008-06-19 01:58:19 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.idlequeue_thread.stop_thread()
|
|
|
|
self.idlequeue_thread.join()
|
|
|
|
|
2008-12-24 15:46:21 +01:00
|
|
|
def test_connect_disconnect(self):
|
2008-06-30 02:02:32 +02:00
|
|
|
self.client.do_connect()
|
|
|
|
self.assert_(self.client.socket.state == 'CONNECTED')
|
|
|
|
self.client.do_disconnect()
|
|
|
|
self.assert_(self.client.socket.state == 'DISCONNECTED')
|
|
|
|
|
2008-12-24 15:46:21 +01:00
|
|
|
|
2008-06-19 01:58:19 +02:00
|
|
|
if __name__ == '__main__':
|
2008-12-24 15:46:21 +01:00
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
# vim: se ts=3:
|