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):
print '[Disconnected]'
def on_error(self, name, message):
print '[Error] %s: %s' % (name, message)
def on_error(self, reason, advice):
print '[Error] %s' % advice
def on_message(self, id, message):
print '[Message] %s: %s' % (id, message)
def on_message(self, messageData):
print '[Message] %s' % messageData
socketIO = SocketIO('localhost', 8000)
socketIO.define(Namespace)
@ -100,17 +100,17 @@ Define different namespaces on a single socket. ::
from socketIO_client import SocketIO, BaseNamespace
class MainNamespace(Channel):
class MainNamespace(BaseNamespace):
def on_aaa(self, *args):
print 'aaa', args
class ChatNamespace(Channel):
class ChatNamespace(BaseNamespace):
def on_bbb(self, *args):
print 'bbb', args
class NewsNamespace(Channel):
class NewsNamespace(BaseNamespace):
def on_ccc(self, *args):
print 'ccc', args
@ -129,6 +129,7 @@ Open secure websockets (HTTPS / WSS) behind a proxy. ::
secure=True,
proxies={'http': 'http://proxy.example.com:8080'})
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'
import sys
try:
from socketio import socketio_manage
from socketio.namespace import BaseNamespace
@ -8,17 +7,13 @@ except ImportError:
from setuptools.command import easy_install
easy_install.main(['-U', 'gevent-socketio'])
print('\nPlease run the script again to launch the test server.')
sys.exit(1)
import sys; sys.exit(1)
class Namespace(BaseNamespace):
def on_aaa(self, *args):
self.socket.send_packet(dict(
type='event',
name='ddd',
args=args,
endpoint=self.ns_name))
self.emit('aaa_response', *args)
class Application(object):
@ -32,7 +27,7 @@ class Application(object):
if __name__ == '__main__':
port = 8000
print 'Starting server at port %s' % port
socketIOServer = SocketIOServer(('0.0.0.0', port), Application())
from socketIO_client.tests import PORT
print 'Starting server at port %s' % PORT
socketIOServer = SocketIOServer(('0.0.0.0', PORT), Application())
socketIOServer.serve_forever()

View file

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

View file

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