Skip to content

Commit 6fe065b

Browse files
final batch of client unit tests
1 parent 16bdb42 commit 6fe065b

File tree

5 files changed

+716
-36
lines changed

5 files changed

+716
-36
lines changed

socketio/asyncio_client.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,21 @@ async def emit(self, event, data=None, namespace=None, callback=None):
143143
id = self._generate_ack_id(namespace, callback)
144144
else:
145145
id = None
146-
await self._emit_internal(event, data, namespace, id)
146+
if six.PY2 and not self.binary:
147+
binary = False # pragma: nocover
148+
else:
149+
binary = None
150+
# tuples are expanded to multiple arguments, everything else is sent
151+
# as a single argument
152+
if isinstance(data, tuple):
153+
data = list(data)
154+
elif data is not None:
155+
data = [data]
156+
else:
157+
data = []
158+
await self._send_packet(packet.Packet(
159+
packet.EVENT, namespace=namespace, data=[event] + data, id=id,
160+
binary=binary))
147161

148162
async def send(self, data, namespace=None, callback=None):
149163
"""Send a message to one or more connected clients.
@@ -165,7 +179,8 @@ async def send(self, data, namespace=None, callback=None):
165179
166180
Note: this method is a coroutine.
167181
"""
168-
await self.emit('message', data, namespace, callback)
182+
await self.emit('message', data=data, namespace=namespace,
183+
callback=callback)
169184

170185
async def disconnect(self):
171186
"""Disconnect from the server.
@@ -209,24 +224,6 @@ async def sleep(self, seconds=0):
209224
"""
210225
return await self.eio.sleep(seconds)
211226

212-
async def _emit_internal(self, event, data, namespace=None, id=None):
213-
"""Send a message to a client."""
214-
if six.PY2 and not self.binary:
215-
binary = False # pragma: nocover
216-
else:
217-
binary = None
218-
# tuples are expanded to multiple arguments, everything else is sent
219-
# as a single argument
220-
if isinstance(data, tuple):
221-
data = list(data)
222-
elif data is not None:
223-
data = [data]
224-
else:
225-
data = []
226-
await self._send_packet(packet.Packet(
227-
packet.EVENT, namespace=namespace, data=[event] + data, id=id,
228-
binary=binary))
229-
230227
async def _send_packet(self, pkt):
231228
"""Send a Socket.IO packet to the server."""
232229
encoded_packet = pkt.encode()
@@ -246,6 +243,8 @@ async def _handle_connect(self, namespace):
246243
for n in self.namespaces:
247244
await self._send_packet(packet.Packet(packet.CONNECT,
248245
namespace=n))
246+
elif namespace not in self.namespaces:
247+
self.namespaces.append(namespace)
249248

250249
async def _handle_disconnect(self, namespace):
251250
namespace = namespace or '/'
@@ -256,9 +255,6 @@ async def _handle_disconnect(self, namespace):
256255
async def _handle_event(self, namespace, id, data):
257256
namespace = namespace or '/'
258257
self.logger.info('Received event "%s" [%s]', data[0], namespace)
259-
await self._handle_event_internal(data, namespace, id)
260-
261-
async def _handle_event_internal(self, data, namespace, id):
262258
r = await self._trigger_event(data[0], namespace, *data[1:])
263259
if id is not None:
264260
# send ACK packet with the response returned by the handler
@@ -289,9 +285,12 @@ async def _handle_ack(self, namespace, id, data):
289285
else:
290286
del self.callbacks[namespace][id]
291287
if callback is not None:
292-
callback(*data)
288+
if asyncio.iscoroutinefunction(callback):
289+
await callback(*data)
290+
else:
291+
callback(*data)
293292

294-
def _handle_error(self, namespace, data):
293+
def _handle_error(self, namespace):
295294
namespace = namespace or '/'
296295
self.logger.info('Connection to namespace {} was rejected'.format(
297296
namespace))
@@ -302,8 +301,7 @@ async def _trigger_event(self, event, namespace, *args):
302301
"""Invoke an application event handler."""
303302
# first see if we have an explicit handler for the event
304303
if namespace in self.handlers and event in self.handlers[namespace]:
305-
if asyncio.iscoroutinefunction(self.handlers[namespace][event]) \
306-
is True:
304+
if asyncio.iscoroutinefunction(self.handlers[namespace][event]):
307305
try:
308306
ret = await self.handlers[namespace][event](*args)
309307
except asyncio.CancelledError: # pragma: no cover
@@ -348,7 +346,7 @@ async def _handle_reconnect(self):
348346
'Maximum reconnection attempts reached, giving up')
349347
break
350348

351-
def _handle_eio_connect(self):
349+
def _handle_eio_connect(self): # pragma: no cover
352350
"""Handle the Engine.IO connection event."""
353351
self.logger.info('Engine.IO connection established')
354352

@@ -376,7 +374,7 @@ async def _handle_eio_message(self, data):
376374
pkt.packet_type == packet.BINARY_ACK:
377375
self._binary_packet = pkt
378376
elif pkt.packet_type == packet.ERROR:
379-
self._handle_error(pkt.namespace, pkt.data)
377+
self._handle_error(pkt.namespace)
380378
else:
381379
raise ValueError('Unknown packet type.')
382380

socketio/asyncio_namespace.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,28 @@ class AsyncClientNamespace(namespace.ClientNamespace):
111111
def is_asyncio_based(self):
112112
return True
113113

114+
async def trigger_event(self, event, *args):
115+
"""Dispatch an event to the proper handler method.
116+
117+
In the most common usage, this method is not overloaded by subclasses,
118+
as it performs the routing of events to methods. However, this
119+
method can be overriden if special dispatching rules are needed, or if
120+
having a single method that catches all events is desired.
121+
122+
Note: this method is a coroutine.
123+
"""
124+
handler_name = 'on_' + event
125+
if hasattr(self, handler_name):
126+
handler = getattr(self, handler_name)
127+
if asyncio.iscoroutinefunction(handler) is True:
128+
try:
129+
ret = await handler(*args)
130+
except asyncio.CancelledError: # pragma: no cover
131+
ret = None
132+
else:
133+
ret = handler(*args)
134+
return ret
135+
114136
async def emit(self, event, data=None, namespace=None, callback=None):
115137
"""Emit a custom event to the server.
116138

0 commit comments

Comments
 (0)