Skip to content

Commit e626e6c

Browse files
some client unit tests
1 parent 7e5c146 commit e626e6c

File tree

7 files changed

+661
-34
lines changed

7 files changed

+661
-34
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ matrix:
55
env: TOXENV=flake8
66
- python: 2.7
77
env: TOXENV=py27
8-
- python: 3.4
9-
env: TOXENV=py34
108
- python: 3.5
119
env: TOXENV=py35
1210
- python: 3.6

socketio/asyncio_namespace.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ class AsyncClientNamespace(namespace.ClientNamespace):
108108
handlers defined in this class. If this argument is
109109
omitted, the default namespace is used.
110110
"""
111+
def is_asyncio_based(self):
112+
return True
113+
111114
async def emit(self, event, data=None, namespace=None, callback=None):
112115
"""Emit a custom event to the server.
113116

socketio/client.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __init__(self, reconnection=True, reconnection_attempts=0,
9595
self.connection_namespaces = None
9696
self.socketio_path = None
9797

98-
self.namespaces = None
98+
self.namespaces = []
9999
self.handlers = {}
100100
self.namespace_handlers = {}
101101
self.callbacks = {}
@@ -245,7 +245,21 @@ def emit(self, event, data=None, namespace=None, callback=None):
245245
id = self._generate_ack_id(namespace, callback)
246246
else:
247247
id = None
248-
self._emit_internal(event, data, namespace, id)
248+
if six.PY2 and not self.binary:
249+
binary = False # pragma: nocover
250+
else:
251+
binary = None
252+
# tuples are expanded to multiple arguments, everything else is sent
253+
# as a single argument
254+
if isinstance(data, tuple):
255+
data = list(data)
256+
elif data is not None:
257+
data = [data]
258+
else:
259+
data = []
260+
self._send_packet(packet.Packet(packet.EVENT, namespace=namespace,
261+
data=[event] + data, id=id,
262+
binary=binary))
249263

250264
def send(self, data, namespace=None, callback=None):
251265
"""Send a message to one or more connected clients.
@@ -265,7 +279,8 @@ def send(self, data, namespace=None, callback=None):
265279
by the client. Callback functions can only be used
266280
when addressing an individual client.
267281
"""
268-
self.emit('message', data, namespace, callback)
282+
self.emit('message', data=data, namespace=namespace,
283+
callback=callback)
269284

270285
def disconnect(self):
271286
"""Disconnect from the server."""
@@ -311,24 +326,6 @@ def sleep(self, seconds=0):
311326
"""
312327
return self.eio.sleep(seconds)
313328

314-
def _emit_internal(self, event, data, namespace=None, id=None):
315-
"""Send a message to a client."""
316-
if six.PY2 and not self.binary:
317-
binary = False # pragma: nocover
318-
else:
319-
binary = None
320-
# tuples are expanded to multiple arguments, everything else is sent
321-
# as a single argument
322-
if isinstance(data, tuple):
323-
data = list(data)
324-
elif data is not None:
325-
data = [data]
326-
else:
327-
data = []
328-
self._send_packet(packet.Packet(packet.EVENT, namespace=namespace,
329-
data=[event] + data, id=id,
330-
binary=binary))
331-
332329
def _send_packet(self, pkt):
333330
"""Send a Socket.IO packet to the server."""
334331
encoded_packet = pkt.encode()
@@ -356,6 +353,8 @@ def _handle_connect(self, namespace):
356353
if namespace == '/':
357354
for n in self.namespaces:
358355
self._send_packet(packet.Packet(packet.CONNECT, namespace=n))
356+
elif namespace not in self.namespaces:
357+
self.namespaces.append(namespace)
359358

360359
def _handle_disconnect(self, namespace):
361360
namespace = namespace or '/'
@@ -366,9 +365,6 @@ def _handle_disconnect(self, namespace):
366365
def _handle_event(self, namespace, id, data):
367366
namespace = namespace or '/'
368367
self.logger.info('Received event "%s" [%s]', data[0], namespace)
369-
self._handle_event_internal(data, namespace, id)
370-
371-
def _handle_event_internal(self, data, namespace, id):
372368
r = self._trigger_event(data[0], namespace, *data[1:])
373369
if id is not None:
374370
# send ACK packet with the response returned by the handler
@@ -400,7 +396,7 @@ def _handle_ack(self, namespace, id, data):
400396
if callback is not None:
401397
callback(*data)
402398

403-
def _handle_error(self, namespace, data):
399+
def _handle_error(self, namespace):
404400
namespace = namespace or '/'
405401
self.logger.info('Connection to namespace {} was rejected'.format(
406402
namespace))
@@ -413,7 +409,7 @@ def _trigger_event(self, event, namespace, *args):
413409
if namespace in self.handlers and event in self.handlers[namespace]:
414410
return self.handlers[namespace][event](*args)
415411

416-
# or else, forward the event to a namepsace handler if one exists
412+
# or else, forward the event to a namespace handler if one exists
417413
elif namespace in self.namespace_handlers:
418414
return self.namespace_handlers[namespace].trigger_event(
419415
event, *args)
@@ -449,7 +445,7 @@ def _handle_reconnect(self):
449445
'Maximum reconnection attempts reached, giving up')
450446
break
451447

452-
def _handle_eio_connect(self):
448+
def _handle_eio_connect(self): # pragma: no cover
453449
"""Handle the Engine.IO connection event."""
454450
self.logger.info('Engine.IO connection established')
455451

@@ -477,7 +473,7 @@ def _handle_eio_message(self, data):
477473
pkt.packet_type == packet.BINARY_ACK:
478474
self._binary_packet = pkt
479475
elif pkt.packet_type == packet.ERROR:
480-
self._handle_error(pkt.namespace, pkt.data)
476+
self._handle_error(pkt.namespace)
481477
else:
482478
raise ValueError('Unknown packet type.')
483479

socketio/namespace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class ClientNamespace(BaseNamespace):
124124
omitted, the default namespace is used.
125125
"""
126126
def __init__(self, namespace=None):
127-
super(Namespace, self).__init__(namespace=namespace)
127+
super(ClientNamespace, self).__init__(namespace=namespace)
128128
self.client = None
129129

130130
def _set_client(self, client):

socketio/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def _trigger_event(self, event, namespace, *args):
489489
if namespace in self.handlers and event in self.handlers[namespace]:
490490
return self.handlers[namespace][event](*args)
491491

492-
# or else, forward the event to a namepsace handler if one exists
492+
# or else, forward the event to a namespace handler if one exists
493493
elif namespace in self.namespace_handlers:
494494
return self.namespace_handlers[namespace].trigger_event(
495495
event, *args)

0 commit comments

Comments
 (0)