Skip to content

Commit

Permalink
Expose the ignore_queue option in namespaces (Fixes #1103)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jan 21, 2023
1 parent de4d5b5 commit 1cadada
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 26 deletions.
15 changes: 9 additions & 6 deletions src/socketio/asyncio_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def trigger_event(self, event, *args):
return ret

async def emit(self, event, data=None, to=None, room=None, skip_sid=None,
namespace=None, callback=None):
namespace=None, callback=None, ignore_queue=False):
"""Emit a custom event to one or more connected clients.
The only difference with the :func:`socketio.Server.emit` method is
Expand All @@ -54,10 +54,11 @@ async def emit(self, event, data=None, to=None, room=None, skip_sid=None,
return await self.server.emit(event, data=data, to=to, room=room,
skip_sid=skip_sid,
namespace=namespace or self.namespace,
callback=callback)
callback=callback,
ignore_queue=ignore_queue)

async def send(self, data, to=None, room=None, skip_sid=None,
namespace=None, callback=None):
namespace=None, callback=None, ignore_queue=False):
"""Send a message to one or more connected clients.
The only difference with the :func:`socketio.Server.send` method is
Expand All @@ -69,10 +70,11 @@ async def send(self, data, to=None, room=None, skip_sid=None,
return await self.server.send(data, to=to, room=room,
skip_sid=skip_sid,
namespace=namespace or self.namespace,
callback=callback)
callback=callback,
ignore_queue=ignore_queue)

async def call(self, event, data=None, to=None, sid=None, namespace=None,
timeout=None):
timeout=None, ignore_queue=False):
"""Emit a custom event to a client and wait for the response.
The only difference with the :func:`socketio.Server.call` method is
Expand All @@ -81,7 +83,8 @@ async def call(self, event, data=None, to=None, sid=None, namespace=None,
"""
return await self.server.call(event, data=data, to=to, sid=sid,
namespace=namespace or self.namespace,
timeout=timeout)
timeout=timeout,
ignore_queue=ignore_queue)

async def close_room(self, room, namespace=None):
"""Close a room.
Expand Down
12 changes: 6 additions & 6 deletions src/socketio/asyncio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def attach(self, app, socketio_path='socket.io'):
self.eio.attach(app, socketio_path)

async def emit(self, event, data=None, to=None, room=None, skip_sid=None,
namespace=None, callback=None, **kwargs):
namespace=None, callback=None, ignore_queue=False):
"""Emit a custom event to one or more connected clients.
:param event: The event name. It can be any string. The event names
Expand Down Expand Up @@ -167,10 +167,10 @@ async def emit(self, event, data=None, to=None, room=None, skip_sid=None,
room or 'all', namespace)
await self.manager.emit(event, data, namespace, room=room,
skip_sid=skip_sid, callback=callback,
**kwargs)
ignore_queue=ignore_queue)

async def send(self, data, to=None, room=None, skip_sid=None,
namespace=None, callback=None, **kwargs):
namespace=None, callback=None, ignore_queue=False):
"""Send a message to one or more connected clients.
This function emits an event with the name ``'message'``. Use
Expand Down Expand Up @@ -210,10 +210,10 @@ async def send(self, data, to=None, room=None, skip_sid=None,
"""
await self.emit('message', data=data, to=to, room=room,
skip_sid=skip_sid, namespace=namespace,
callback=callback, **kwargs)
callback=callback, ignore_queue=ignore_queue)

async def call(self, event, data=None, to=None, sid=None, namespace=None,
timeout=60, **kwargs):
timeout=60, ignore_queue=False):
"""Emit a custom event to a client and wait for the response.
This method issues an emit with a callback and waits for the callback
Expand Down Expand Up @@ -266,7 +266,7 @@ def event_callback(*args):
callback_event.set()

await self.emit(event, data=data, room=to or sid, namespace=namespace,
callback=event_callback, **kwargs)
callback=event_callback, ignore_queue=ignore_queue)
try:
await asyncio.wait_for(callback_event.wait(), timeout)
except asyncio.TimeoutError:
Expand Down
12 changes: 6 additions & 6 deletions src/socketio/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _set_server(self, server):
self.server = server

def emit(self, event, data=None, to=None, room=None, skip_sid=None,
namespace=None, callback=None):
namespace=None, callback=None, ignore_queue=False):
"""Emit a custom event to one or more connected clients.
The only difference with the :func:`socketio.Server.emit` method is
Expand All @@ -48,10 +48,10 @@ def emit(self, event, data=None, to=None, room=None, skip_sid=None,
return self.server.emit(event, data=data, to=to, room=room,
skip_sid=skip_sid,
namespace=namespace or self.namespace,
callback=callback)
callback=callback, ignore_queue=ignore_queue)

def send(self, data, to=None, room=None, skip_sid=None, namespace=None,
callback=None):
callback=None, ignore_queue=False):
"""Send a message to one or more connected clients.
The only difference with the :func:`socketio.Server.send` method is
Expand All @@ -60,10 +60,10 @@ def send(self, data, to=None, room=None, skip_sid=None, namespace=None,
"""
return self.server.send(data, to=to, room=room, skip_sid=skip_sid,
namespace=namespace or self.namespace,
callback=callback)
callback=callback, ignore_queue=ignore_queue)

def call(self, event, data=None, to=None, sid=None, namespace=None,
timeout=None):
timeout=None, ignore_queue=False):
"""Emit a custom event to a client and wait for the response.
The only difference with the :func:`socketio.Server.call` method is
Expand All @@ -72,7 +72,7 @@ def call(self, event, data=None, to=None, sid=None, namespace=None,
"""
return self.server.call(event, data=data, to=to, sid=sid,
namespace=namespace or self.namespace,
timeout=timeout)
timeout=timeout, ignore_queue=ignore_queue)

def enter_room(self, sid, room, namespace=None):
"""Enter a room.
Expand Down
14 changes: 8 additions & 6 deletions src/socketio/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def register_namespace(self, namespace_handler):
namespace_handler

def emit(self, event, data=None, to=None, room=None, skip_sid=None,
namespace=None, callback=None, **kwargs):
namespace=None, callback=None, ignore_queue=False):
"""Emit a custom event to one or more connected clients.
:param event: The event name. It can be any string. The event names
Expand Down Expand Up @@ -317,10 +317,11 @@ def emit(self, event, data=None, to=None, room=None, skip_sid=None,
self.logger.info('emitting event "%s" to %s [%s]', event,
room or 'all', namespace)
self.manager.emit(event, data, namespace, room=room,
skip_sid=skip_sid, callback=callback, **kwargs)
skip_sid=skip_sid, callback=callback,
ignore_queue=ignore_queue)

def send(self, data, to=None, room=None, skip_sid=None, namespace=None,
callback=None, **kwargs):
callback=None, ignore_queue=False):
"""Send a message to one or more connected clients.
This function emits an event with the name ``'message'``. Use
Expand Down Expand Up @@ -358,10 +359,11 @@ def send(self, data, to=None, room=None, skip_sid=None, namespace=None,
value of ``False``.
"""
self.emit('message', data=data, to=to, room=room, skip_sid=skip_sid,
namespace=namespace, callback=callback, **kwargs)
namespace=namespace, callback=callback,
ignore_queue=ignore_queue)

def call(self, event, data=None, to=None, sid=None, namespace=None,
timeout=60, **kwargs):
timeout=60, ignore_queue=False):
"""Emit a custom event to a client and wait for the response.
This method issues an emit with a callback and waits for the callback
Expand Down Expand Up @@ -412,7 +414,7 @@ def event_callback(*args):
callback_event.set()

self.emit(event, data=data, room=to or sid, namespace=namespace,
callback=event_callback, **kwargs)
callback=event_callback, ignore_queue=ignore_queue)
if not callback_event.wait(timeout=timeout):
raise exceptions.TimeoutError()
return callback_args[0] if len(callback_args[0]) > 1 \
Expand Down
10 changes: 9 additions & 1 deletion tests/asyncio/test_asyncio_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def test_emit(self):
skip_sid='skip',
namespace='/foo',
callback='cb',
ignore_queue=False,
)
_run(
ns.emit(
Expand All @@ -113,6 +114,7 @@ def test_emit(self):
skip_sid='skip',
namespace='/bar',
callback='cb',
ignore_queue=True,
)
)
ns.server.emit.mock.assert_called_with(
Expand All @@ -123,6 +125,7 @@ def test_emit(self):
skip_sid='skip',
namespace='/bar',
callback='cb',
ignore_queue=True,
)

def test_send(self):
Expand All @@ -138,6 +141,7 @@ def test_send(self):
skip_sid='skip',
namespace='/foo',
callback='cb',
ignore_queue=False,
)
_run(
ns.send(
Expand All @@ -146,6 +150,7 @@ def test_send(self):
skip_sid='skip',
namespace='/bar',
callback='cb',
ignore_queue=True,
)
)
ns.server.send.mock.assert_called_with(
Expand All @@ -155,6 +160,7 @@ def test_send(self):
skip_sid='skip',
namespace='/bar',
callback='cb',
ignore_queue=True,
)

def test_call(self):
Expand All @@ -170,16 +176,18 @@ def test_call(self):
sid=None,
namespace='/foo',
timeout=None,
ignore_queue=False,
)
_run(ns.call('ev', data='data', sid='sid', namespace='/bar',
timeout=45))
timeout=45, ignore_queue=True))
ns.server.call.mock.assert_called_with(
'ev',
data='data',
to=None,
sid='sid',
namespace='/bar',
timeout=45,
ignore_queue=True,
)

def test_enter_room(self):
Expand Down
9 changes: 9 additions & 0 deletions tests/asyncio/test_asyncio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def test_emit(self, eio):
room='room',
skip_sid='123',
callback='cb',
ignore_queue=False,
)
_run(
s.emit(
Expand All @@ -110,6 +111,7 @@ def test_emit(self, eio):
skip_sid='123',
namespace='/foo',
callback='cb',
ignore_queue=True,
)
)
s.manager.emit.mock.assert_called_with(
Expand All @@ -119,6 +121,7 @@ def test_emit(self, eio):
room='room',
skip_sid='123',
callback='cb',
ignore_queue=True,
)

def test_emit_default_namespace(self, eio):
Expand All @@ -140,6 +143,7 @@ def test_emit_default_namespace(self, eio):
room='room',
skip_sid='123',
callback='cb',
ignore_queue=False,
)
_run(
s.emit(
Expand All @@ -148,6 +152,7 @@ def test_emit_default_namespace(self, eio):
room='room',
skip_sid='123',
callback='cb',
ignore_queue=True,
)
)
s.manager.emit.mock.assert_called_with(
Expand All @@ -157,6 +162,7 @@ def test_emit_default_namespace(self, eio):
room='room',
skip_sid='123',
callback='cb',
ignore_queue=True,
)

def test_send(self, eio):
Expand All @@ -178,6 +184,7 @@ def test_send(self, eio):
room='room',
skip_sid='123',
callback='cb',
ignore_queue=False,
)
_run(
s.send(
Expand All @@ -186,6 +193,7 @@ def test_send(self, eio):
skip_sid='123',
namespace='/foo',
callback='cb',
ignore_queue=True,
)
)
s.manager.emit.mock.assert_called_with(
Expand All @@ -195,6 +203,7 @@ def test_send(self, eio):
room='room',
skip_sid='123',
callback='cb',
ignore_queue=True,
)

def test_call(self, eio):
Expand Down
9 changes: 9 additions & 0 deletions tests/common/test_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def test_emit(self):
skip_sid='skip',
namespace='/foo',
callback='cb',
ignore_queue=False,
)
ns.emit(
'ev',
Expand All @@ -73,6 +74,7 @@ def test_emit(self):
skip_sid='skip',
namespace='/bar',
callback='cb',
ignore_queue=True,
)
ns.server.emit.assert_called_with(
'ev',
Expand All @@ -82,6 +84,7 @@ def test_emit(self):
skip_sid='skip',
namespace='/bar',
callback='cb',
ignore_queue=True,
)

def test_send(self):
Expand All @@ -95,13 +98,15 @@ def test_send(self):
skip_sid='skip',
namespace='/foo',
callback='cb',
ignore_queue=False,
)
ns.send(
data='data',
room='room',
skip_sid='skip',
namespace='/bar',
callback='cb',
ignore_queue=True,
)
ns.server.send.assert_called_with(
'data',
Expand All @@ -110,6 +115,7 @@ def test_send(self):
skip_sid='skip',
namespace='/bar',
callback='cb',
ignore_queue=True,
)

def test_call(self):
Expand All @@ -123,13 +129,15 @@ def test_call(self):
sid=None,
namespace='/foo',
timeout=None,
ignore_queue=False,
)
ns.call(
'ev',
data='data',
sid='sid',
namespace='/bar',
timeout=45,
ignore_queue=True,
)
ns.server.call.assert_called_with(
'ev',
Expand All @@ -138,6 +146,7 @@ def test_call(self):
sid='sid',
namespace='/bar',
timeout=45,
ignore_queue=True,
)

def test_enter_room(self):
Expand Down
Loading

0 comments on commit 1cadada

Please sign in to comment.