From 4f39d1f92282f84e43df8fc026a804a6bcac13fb Mon Sep 17 00:00:00 2001 From: Roy Hyunjin Han Date: Thu, 21 Feb 2013 09:08:58 -0800 Subject: [PATCH] Added tests for Channel.message() --- socketIO_client/__init__.py | 35 ++++++++++++++++++++++------------- socketIO_client/tests.py | 28 +++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/socketIO_client/__init__.py b/socketIO_client/__init__.py index b850b1c..920fa48 100644 --- a/socketIO_client/__init__.py +++ b/socketIO_client/__init__.py @@ -87,11 +87,14 @@ class SocketIO(object): self._channelByPath[channelPath] = channel return channel + def get_channel(self, channelPath=''): + return self._channelByPath[channelPath] + def get_namespace(self, channelPath=''): - return self._channelByPath[channelPath].get_namespace() + return self.get_channel(channelPath).get_namespace() def on(self, eventName, eventCallback, channelPath=''): - return self._channelByPath[channelPath].on(eventName, eventCallback) + return self.get_channel(channelPath).on(eventName, eventCallback) def message(self, messageData, messageCallback=None, channelPath=''): self._socketIO.message(messageData, messageCallback, channelPath) @@ -166,17 +169,22 @@ class _ListenerThread(Thread): continue try: channel = self._channelByPath[channelPath] + except KeyError: + print 'Received unexpected channelPath (%s)' % channelPath + continue + try: delegate = { - 0: self.on_disconnect, - 1: self.on_connect, - 2: self.on_heartbeat, - 3: self.on_message, - 4: self.on_json, - 5: self.on_event, - 6: self.on_acknowledgment, - 7: self.on_error, + '0': self.on_disconnect, + '1': self.on_connect, + '2': self.on_heartbeat, + '3': self.on_message, + '4': self.on_json, + '5': self.on_event, + '6': self.on_acknowledgment, + '7': self.on_error, }[code] except KeyError: + print 'Received unexpected code (%s)' % code continue delegate(packetID, channel._get_eventCallback, data) @@ -250,6 +258,7 @@ class _SocketIO(object): self.disconnect(force=True) def disconnect(self, channelPath='', force=False): + 'Set force=True to skip closing the websocket' if not self.connected: return if channelPath: @@ -306,7 +315,6 @@ class _SocketIO(object): return True if self.callbackByMessageID else False def recv_packet(self): - code, packetID, channelPath, data = -1, None, None, None try: packet = self.connection.recv() except WebSocketConnectionClosedException: @@ -320,13 +328,14 @@ class _SocketIO(object): except AttributeError: raise SocketIOPacketError('Received invalid packet (%s)' % packet) packetCount = len(packetParts) + code, packetID, channelPath, data = None, None, None, None if 4 == packetCount: code, packetID, channelPath, data = packetParts elif 3 == packetCount: code, packetID, channelPath = packetParts - elif 1 == packetCount: # pragma: no cover + elif 1 == packetCount: code = packetParts[0] - return int(code), packetID, channelPath, data + return code, packetID, channelPath, data def send_packet(self, code, channelPath='', data='', messageCallback=None): callbackNumber = self.set_messageCallback(messageCallback) if messageCallback else '' diff --git a/socketIO_client/tests.py b/socketIO_client/tests.py index 6442e1e..a662c45 100644 --- a/socketIO_client/tests.py +++ b/socketIO_client/tests.py @@ -24,14 +24,14 @@ class TestSocketIO(TestCase): socketIO = SocketIO('localhost', 8000) socketIO.define(Namespace) socketIO.emit('aaa') - sleep(0.5) + sleep(0.1) self.assertEqual(socketIO.get_namespace().payload, '') def test_emit_with_payload(self): socketIO = SocketIO('localhost', 8000) socketIO.define(Namespace) socketIO.emit('aaa', PAYLOAD) - sleep(0.5) + sleep(0.1) self.assertEqual(socketIO.get_namespace().payload, PAYLOAD) def test_emit_with_callback(self): @@ -42,13 +42,21 @@ class TestSocketIO(TestCase): socketIO.wait(forCallbacks=True) self.assertEqual(ON_RESPONSE_CALLED, True) + def test_message(self): + global ON_RESPONSE_CALLED + ON_RESPONSE_CALLED = False + socketIO = SocketIO('localhost', 8000) + socketIO.message(PAYLOAD, on_response) + socketIO.wait(forCallbacks=True) + self.assertEqual(ON_RESPONSE_CALLED, True) + def test_events(self): global ON_RESPONSE_CALLED ON_RESPONSE_CALLED = False socketIO = SocketIO('localhost', 8000) socketIO.on('ddd', on_response) socketIO.emit('aaa', PAYLOAD) - sleep(0.5) + sleep(0.1) self.assertEqual(ON_RESPONSE_CALLED, True) def test_channels(self): @@ -56,12 +64,22 @@ class TestSocketIO(TestCase): mainSocket = socketIO.define(Namespace) chatSocket = socketIO.define(Namespace, '/chat') newsSocket = socketIO.define(Namespace, '/news') - newsSocket.emit('aaa', PAYLOAD) - sleep(0.5) self.assertNotEqual(mainSocket.get_namespace().payload, PAYLOAD) self.assertNotEqual(chatSocket.get_namespace().payload, PAYLOAD) + self.assertNotEqual(newsSocket.get_namespace().payload, PAYLOAD) + newsSocket.emit('aaa', PAYLOAD) + sleep(0.1) self.assertEqual(newsSocket.get_namespace().payload, PAYLOAD) + def test_channels_with_callback(self): + global ON_RESPONSE_CALLED + ON_RESPONSE_CALLED = False + socketIO = SocketIO('localhost', 8000) + mainSocket = socketIO.get_channel() + mainSocket.message(PAYLOAD, on_response) + sleep(0.1) + self.assertEqual(ON_RESPONSE_CALLED, True) + def test_delete(self): socketIO = SocketIO('localhost', 8000) childThreads = [