Added support for acks thanks to zratic
This commit is contained in:
parent
61918597b7
commit
18d2a1ea8c
2 changed files with 138 additions and 109 deletions
|
|
@ -1,9 +1,8 @@
|
|||
from socketIO_client import SocketIO, BaseNamespace
|
||||
from socketIO_client import SocketIO, BaseNamespace, find_callback
|
||||
from time import sleep
|
||||
from unittest import TestCase
|
||||
|
||||
|
||||
ON_RESPONSE_CALLED = False
|
||||
PORT = 8000
|
||||
PAYLOAD = {'xxx': 'yyy'}
|
||||
|
||||
|
|
@ -11,13 +10,28 @@ PAYLOAD = {'xxx': 'yyy'}
|
|||
class TestSocketIO(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
global ON_RESPONSE_CALLED
|
||||
ON_RESPONSE_CALLED = False
|
||||
self.socketIO = SocketIO('localhost', PORT)
|
||||
self.called_on_response = False
|
||||
|
||||
def tearDown(self):
|
||||
del self.socketIO
|
||||
|
||||
def on_response(self, *args):
|
||||
self.called_on_response = True
|
||||
callback, args = find_callback(args)
|
||||
if callback:
|
||||
callback(*args)
|
||||
|
||||
def test_disconnect(self):
|
||||
childThreads = [
|
||||
self.socketIO._rhythmicThread,
|
||||
self.socketIO._listenerThread,
|
||||
]
|
||||
self.socketIO.disconnect()
|
||||
for childThread in childThreads:
|
||||
self.assertEqual(True, childThread.done.is_set())
|
||||
self.assertEqual(False, self.socketIO.connected)
|
||||
|
||||
def test_emit(self):
|
||||
self.socketIO.define(Namespace)
|
||||
self.socketIO.emit('aaa')
|
||||
|
|
@ -31,20 +45,26 @@ class TestSocketIO(TestCase):
|
|||
self.assertEqual(self.socketIO.get_namespace().payload, PAYLOAD)
|
||||
|
||||
def test_emit_with_callback(self):
|
||||
self.socketIO.emit('aaa', PAYLOAD, on_response)
|
||||
self.socketIO.wait(forCallbacks=True)
|
||||
self.assertEqual(ON_RESPONSE_CALLED, True)
|
||||
self.socketIO.emit('aaa', PAYLOAD, self.on_response)
|
||||
self.socketIO.wait(seconds=0.1, forCallbacks=True)
|
||||
self.assertEqual(self.called_on_response, True)
|
||||
|
||||
def test_message(self):
|
||||
self.socketIO.message(PAYLOAD, on_response)
|
||||
self.socketIO.wait(forCallbacks=True)
|
||||
self.assertEqual(ON_RESPONSE_CALLED, True)
|
||||
|
||||
def test_events(self):
|
||||
self.socketIO.on('aaa_response', on_response)
|
||||
def test_emit_with_event(self):
|
||||
self.socketIO.on('aaa_response', self.on_response)
|
||||
self.socketIO.emit('aaa', PAYLOAD)
|
||||
sleep(0.1)
|
||||
self.assertEqual(ON_RESPONSE_CALLED, True)
|
||||
self.assertEqual(self.called_on_response, True)
|
||||
|
||||
def test_message(self):
|
||||
self.socketIO.message(PAYLOAD, self.on_response)
|
||||
self.socketIO.wait(seconds=0.1, forCallbacks=True)
|
||||
self.assertEqual(self.called_on_response, True)
|
||||
|
||||
def test_ack(self):
|
||||
self.socketIO.on('bbb_response', self.on_response)
|
||||
self.socketIO.emit('bbb', PAYLOAD)
|
||||
sleep(0.1)
|
||||
self.assertEqual(self.called_on_response, True)
|
||||
|
||||
def test_namespaces(self):
|
||||
mainNamespace = self.socketIO.define(Namespace)
|
||||
|
|
@ -59,19 +79,9 @@ class TestSocketIO(TestCase):
|
|||
|
||||
def test_namespaces_with_callback(self):
|
||||
mainNamespace = self.socketIO.get_namespace()
|
||||
mainNamespace.message(PAYLOAD, on_response)
|
||||
mainNamespace.message(PAYLOAD, self.on_response)
|
||||
sleep(0.1)
|
||||
self.assertEqual(ON_RESPONSE_CALLED, True)
|
||||
|
||||
def test_disconnect(self):
|
||||
childThreads = [
|
||||
self.socketIO._rhythmicThread,
|
||||
self.socketIO._listenerThread,
|
||||
]
|
||||
self.socketIO.disconnect()
|
||||
for childThread in childThreads:
|
||||
self.assertEqual(True, childThread.done.is_set())
|
||||
self.assertEqual(False, self.socketIO.connected)
|
||||
self.assertEqual(self.called_on_response, True)
|
||||
|
||||
|
||||
class Namespace(BaseNamespace):
|
||||
|
|
@ -81,8 +91,3 @@ class Namespace(BaseNamespace):
|
|||
def on_aaa_response(self, data=''):
|
||||
print '[Event] aaa_response(%s)' % data
|
||||
self.payload = data
|
||||
|
||||
|
||||
def on_response(*args):
|
||||
global ON_RESPONSE_CALLED
|
||||
ON_RESPONSE_CALLED = True
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue