Preparing to merge changes from zratic

This commit is contained in:
Roy Hyunjin Han 2013-04-15 01:15:21 -07:00
commit ee91f82171
4 changed files with 52 additions and 74 deletions

View file

@ -86,11 +86,11 @@ Define standard events. ::
def on_disconnect(self): def on_disconnect(self):
print '[Disconnected]' print '[Disconnected]'
def on_error(self, name, message): def on_error(self, reason, advice):
print '[Error] %s: %s' % (name, message) print '[Error] %s' % advice
def on_message(self, id, message): def on_message(self, messageData):
print '[Message] %s: %s' % (id, message) print '[Message] %s' % messageData
socketIO = SocketIO('localhost', 8000) socketIO = SocketIO('localhost', 8000)
socketIO.define(Namespace) socketIO.define(Namespace)
@ -100,17 +100,17 @@ Define different namespaces on a single socket. ::
from socketIO_client import SocketIO, BaseNamespace from socketIO_client import SocketIO, BaseNamespace
class MainNamespace(Channel): class MainNamespace(BaseNamespace):
def on_aaa(self, *args): def on_aaa(self, *args):
print 'aaa', args print 'aaa', args
class ChatNamespace(Channel): class ChatNamespace(BaseNamespace):
def on_bbb(self, *args): def on_bbb(self, *args):
print 'bbb', args print 'bbb', args
class NewsNamespace(Channel): class NewsNamespace(BaseNamespace):
def on_ccc(self, *args): def on_ccc(self, *args):
print 'ccc', args print 'ccc', args
@ -129,6 +129,7 @@ Open secure websockets (HTTPS / WSS) behind a proxy. ::
secure=True, secure=True,
proxies={'http': 'http://proxy.example.com:8080'}) proxies={'http': 'http://proxy.example.com:8080'})
License License
------- -------
This software is available under the MIT License. This software is available under the MIT License.

View file

@ -1,5 +1,4 @@
'Launch this server in another terminal window before running tests' 'Launch this server in another terminal window before running tests'
import sys
try: try:
from socketio import socketio_manage from socketio import socketio_manage
from socketio.namespace import BaseNamespace from socketio.namespace import BaseNamespace
@ -8,17 +7,13 @@ except ImportError:
from setuptools.command import easy_install from setuptools.command import easy_install
easy_install.main(['-U', 'gevent-socketio']) easy_install.main(['-U', 'gevent-socketio'])
print('\nPlease run the script again to launch the test server.') print('\nPlease run the script again to launch the test server.')
sys.exit(1) import sys; sys.exit(1)
class Namespace(BaseNamespace): class Namespace(BaseNamespace):
def on_aaa(self, *args): def on_aaa(self, *args):
self.socket.send_packet(dict( self.emit('aaa_response', *args)
type='event',
name='ddd',
args=args,
endpoint=self.ns_name))
class Application(object): class Application(object):
@ -32,7 +27,7 @@ class Application(object):
if __name__ == '__main__': if __name__ == '__main__':
port = 8000 from socketIO_client.tests import PORT
print 'Starting server at port %s' % port print 'Starting server at port %s' % PORT
socketIOServer = SocketIOServer(('0.0.0.0', port), Application()) socketIOServer = SocketIOServer(('0.0.0.0', PORT), Application())
socketIOServer.serve_forever() socketIOServer.serve_forever()

View file

@ -286,7 +286,6 @@ class _SocketIO(object):
self.send_packet(code, channelPath, data, messageCallback) self.send_packet(code, channelPath, data, messageCallback)
def emit(self, eventName, *eventArguments, **eventKeywords): def emit(self, eventName, *eventArguments, **eventKeywords):
code = 5
if eventArguments and callable(eventArguments[-1]): if eventArguments and callable(eventArguments[-1]):
messageCallback = eventArguments[-1] messageCallback = eventArguments[-1]
eventArguments = eventArguments[:-1] eventArguments = eventArguments[:-1]
@ -294,7 +293,7 @@ class _SocketIO(object):
messageCallback = None messageCallback = None
channelPath = eventKeywords.get('channelPath', '') channelPath = eventKeywords.get('channelPath', '')
data = dumps(dict(name=eventName, args=eventArguments)) data = dumps(dict(name=eventName, args=eventArguments))
self.send_packet(code, channelPath, data, messageCallback) self.send_packet(5, channelPath, data, messageCallback)
def set_messageCallback(self, callback): def set_messageCallback(self, callback):
'Set callback that will be called after receiving an acknowledgment' 'Set callback that will be called after receiving an acknowledgment'

View file

@ -3,67 +3,53 @@ from time import sleep
from unittest import TestCase from unittest import TestCase
PAYLOAD = {'bbb': 'ccc'}
ON_RESPONSE_CALLED = False ON_RESPONSE_CALLED = False
PORT = 8000
PAYLOAD = {'xxx': 'yyy'}
class TestSocketIO(TestCase): class TestSocketIO(TestCase):
def test_disconnect(self): def setUp(self):
socketIO = SocketIO('localhost', 8000)
socketIO.disconnect()
self.assertEqual(False, socketIO.connected)
childThreads = [
socketIO._rhythmicThread,
socketIO._listenerThread,
]
for childThread in childThreads:
self.assertEqual(True, childThread.done.is_set())
def test_emit(self):
socketIO = SocketIO('localhost', 8000)
socketIO.define(Namespace)
socketIO.emit('aaa')
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.1)
self.assertEqual(socketIO.get_namespace().payload, PAYLOAD)
def test_emit_with_callback(self):
global ON_RESPONSE_CALLED global ON_RESPONSE_CALLED
ON_RESPONSE_CALLED = False ON_RESPONSE_CALLED = False
socketIO = SocketIO('localhost', 8000) self.socketIO = SocketIO('localhost', PORT)
socketIO.emit('aaa', PAYLOAD, on_response)
socketIO.wait(forCallbacks=True) def tearDown(self):
del self.socketIO
def test_emit(self):
self.socketIO.define(Namespace)
self.socketIO.emit('aaa')
sleep(0.1)
self.assertEqual(self.socketIO.get_namespace().payload, '')
def test_emit_with_payload(self):
self.socketIO.define(Namespace)
self.socketIO.emit('aaa', PAYLOAD)
sleep(0.1)
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.assertEqual(ON_RESPONSE_CALLED, True)
def test_message(self): def test_message(self):
global ON_RESPONSE_CALLED self.socketIO.message(PAYLOAD, on_response)
ON_RESPONSE_CALLED = False self.socketIO.wait(forCallbacks=True)
socketIO = SocketIO('localhost', 8000)
socketIO.message(PAYLOAD, on_response)
socketIO.wait(forCallbacks=True)
self.assertEqual(ON_RESPONSE_CALLED, True) self.assertEqual(ON_RESPONSE_CALLED, True)
def test_events(self): def test_events(self):
global ON_RESPONSE_CALLED self.socketIO.on('aaa_response', on_response)
ON_RESPONSE_CALLED = False self.socketIO.emit('aaa', PAYLOAD)
socketIO = SocketIO('localhost', 8000)
socketIO.on('ddd', on_response)
socketIO.emit('aaa', PAYLOAD)
sleep(0.1) sleep(0.1)
self.assertEqual(ON_RESPONSE_CALLED, True) self.assertEqual(ON_RESPONSE_CALLED, True)
def test_channels(self): def test_channels(self):
socketIO = SocketIO('localhost', 8000) mainSocket = self.socketIO.define(Namespace)
mainSocket = socketIO.define(Namespace) chatSocket = self.socketIO.define(Namespace, '/chat')
chatSocket = socketIO.define(Namespace, '/chat') newsSocket = self.socketIO.define(Namespace, '/news')
newsSocket = socketIO.define(Namespace, '/news')
self.assertNotEqual(mainSocket.get_namespace().payload, PAYLOAD) self.assertNotEqual(mainSocket.get_namespace().payload, PAYLOAD)
self.assertNotEqual(chatSocket.get_namespace().payload, PAYLOAD) self.assertNotEqual(chatSocket.get_namespace().payload, PAYLOAD)
self.assertNotEqual(newsSocket.get_namespace().payload, PAYLOAD) self.assertNotEqual(newsSocket.get_namespace().payload, PAYLOAD)
@ -72,31 +58,28 @@ class TestSocketIO(TestCase):
self.assertEqual(newsSocket.get_namespace().payload, PAYLOAD) self.assertEqual(newsSocket.get_namespace().payload, PAYLOAD)
def test_channels_with_callback(self): def test_channels_with_callback(self):
global ON_RESPONSE_CALLED mainSocket = self.socketIO.get_channel()
ON_RESPONSE_CALLED = False
socketIO = SocketIO('localhost', 8000)
mainSocket = socketIO.get_channel()
mainSocket.message(PAYLOAD, on_response) mainSocket.message(PAYLOAD, on_response)
sleep(0.1) sleep(0.1)
self.assertEqual(ON_RESPONSE_CALLED, True) self.assertEqual(ON_RESPONSE_CALLED, True)
def test_delete(self): def test_disconnect(self):
socketIO = SocketIO('localhost', 8000)
childThreads = [ childThreads = [
socketIO._rhythmicThread, self.socketIO._rhythmicThread,
socketIO._listenerThread, self.socketIO._listenerThread,
] ]
del socketIO self.socketIO.disconnect()
for childThread in childThreads: for childThread in childThreads:
self.assertEqual(True, childThread.done.is_set()) self.assertEqual(True, childThread.done.is_set())
self.assertEqual(False, self.socketIO.connected)
class Namespace(BaseNamespace): class Namespace(BaseNamespace):
payload = None payload = None
def on_ddd(self, data=''): def on_aaa_response(self, data=''):
print '[Event] ddd(%s)' % data print '[Event] aaa_response(%s)' % data
self.payload = data self.payload = data