Browse Source

Adopted pyenv-asyncio for async unit tests

pull/1426/head
Miguel Grinberg 4 months ago
parent
commit
0b5c4638e5
Failed to extract signature
  1. 4
      pyproject.toml
  2. 6
      tests/async/helpers.py
  3. 477
      tests/async/test_client.py
  4. 336
      tests/async/test_manager.py
  5. 151
      tests/async/test_namespace.py
  6. 335
      tests/async/test_pubsub_manager.py
  7. 582
      tests/async/test_server.py
  8. 69
      tests/async/test_simple_client.py
  9. 1
      tox.ini

4
pyproject.toml

@ -54,3 +54,7 @@ namespaces = false
[build-system] [build-system]
requires = ["setuptools>=61.2"] requires = ["setuptools>=61.2"]
build-backend = "setuptools.build_meta" build-backend = "setuptools.build_meta"
[tool.pytest.ini_options]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "session"

6
tests/async/helpers.py

@ -1,6 +0,0 @@
import asyncio
def _run(coro):
"""Run the given coroutine."""
return asyncio.get_event_loop().run_until_complete(coro)

477
tests/async/test_client.py

File diff suppressed because it is too large

336
tests/async/test_manager.py

@ -2,7 +2,6 @@ from unittest import mock
from socketio import async_manager from socketio import async_manager
from socketio import packet from socketio import packet
from .helpers import _run
class TestAsyncManager: class TestAsyncManager:
@ -23,8 +22,8 @@ class TestAsyncManager:
self.bm.set_server(mock_server) self.bm.set_server(mock_server)
self.bm.initialize() self.bm.initialize()
def test_connect(self): async def test_connect(self):
sid = _run(self.bm.connect('123', '/foo')) sid = await self.bm.connect('123', '/foo')
assert None in self.bm.rooms['/foo'] assert None in self.bm.rooms['/foo']
assert sid in self.bm.rooms['/foo'] assert sid in self.bm.rooms['/foo']
assert sid in self.bm.rooms['/foo'][None] assert sid in self.bm.rooms['/foo'][None]
@ -33,9 +32,9 @@ class TestAsyncManager:
assert dict(self.bm.rooms['/foo'][sid]) == {sid: '123'} assert dict(self.bm.rooms['/foo'][sid]) == {sid: '123'}
assert self.bm.sid_from_eio_sid('123', '/foo') == sid assert self.bm.sid_from_eio_sid('123', '/foo') == sid
def test_pre_disconnect(self): async def test_pre_disconnect(self):
sid1 = _run(self.bm.connect('123', '/foo')) sid1 = await self.bm.connect('123', '/foo')
sid2 = _run(self.bm.connect('456', '/foo')) sid2 = await self.bm.connect('456', '/foo')
assert self.bm.is_connected(sid1, '/foo') assert self.bm.is_connected(sid1, '/foo')
assert self.bm.pre_disconnect(sid1, '/foo') == '123' assert self.bm.pre_disconnect(sid1, '/foo') == '123'
assert self.bm.pending_disconnect == {'/foo': [sid1]} assert self.bm.pending_disconnect == {'/foo': [sid1]}
@ -43,124 +42,124 @@ class TestAsyncManager:
assert self.bm.pre_disconnect(sid2, '/foo') == '456' assert self.bm.pre_disconnect(sid2, '/foo') == '456'
assert self.bm.pending_disconnect == {'/foo': [sid1, sid2]} assert self.bm.pending_disconnect == {'/foo': [sid1, sid2]}
assert not self.bm.is_connected(sid2, '/foo') assert not self.bm.is_connected(sid2, '/foo')
_run(self.bm.disconnect(sid1, '/foo')) await self.bm.disconnect(sid1, '/foo')
assert self.bm.pending_disconnect == {'/foo': [sid2]} assert self.bm.pending_disconnect == {'/foo': [sid2]}
_run(self.bm.disconnect(sid2, '/foo')) await self.bm.disconnect(sid2, '/foo')
assert self.bm.pending_disconnect == {} assert self.bm.pending_disconnect == {}
def test_disconnect(self): async def test_disconnect(self):
sid1 = _run(self.bm.connect('123', '/foo')) sid1 = await self.bm.connect('123', '/foo')
sid2 = _run(self.bm.connect('456', '/foo')) sid2 = await self.bm.connect('456', '/foo')
_run(self.bm.enter_room(sid1, '/foo', 'bar')) await self.bm.enter_room(sid1, '/foo', 'bar')
_run(self.bm.enter_room(sid2, '/foo', 'baz')) await self.bm.enter_room(sid2, '/foo', 'baz')
_run(self.bm.disconnect(sid1, '/foo')) await self.bm.disconnect(sid1, '/foo')
assert dict(self.bm.rooms['/foo'][None]) == {sid2: '456'} assert dict(self.bm.rooms['/foo'][None]) == {sid2: '456'}
assert dict(self.bm.rooms['/foo'][sid2]) == {sid2: '456'} assert dict(self.bm.rooms['/foo'][sid2]) == {sid2: '456'}
assert dict(self.bm.rooms['/foo']['baz']) == {sid2: '456'} assert dict(self.bm.rooms['/foo']['baz']) == {sid2: '456'}
def test_disconnect_default_namespace(self): async def test_disconnect_default_namespace(self):
sid1 = _run(self.bm.connect('123', '/')) sid1 = await self.bm.connect('123', '/')
sid2 = _run(self.bm.connect('123', '/foo')) sid2 = await self.bm.connect('123', '/foo')
sid3 = _run(self.bm.connect('456', '/')) sid3 = await self.bm.connect('456', '/')
sid4 = _run(self.bm.connect('456', '/foo')) sid4 = await self.bm.connect('456', '/foo')
assert self.bm.is_connected(sid1, '/') assert self.bm.is_connected(sid1, '/')
assert self.bm.is_connected(sid2, '/foo') assert self.bm.is_connected(sid2, '/foo')
assert not self.bm.is_connected(sid2, '/') assert not self.bm.is_connected(sid2, '/')
assert not self.bm.is_connected(sid1, '/foo') assert not self.bm.is_connected(sid1, '/foo')
_run(self.bm.disconnect(sid1, '/')) await self.bm.disconnect(sid1, '/')
assert not self.bm.is_connected(sid1, '/') assert not self.bm.is_connected(sid1, '/')
assert self.bm.is_connected(sid2, '/foo') assert self.bm.is_connected(sid2, '/foo')
_run(self.bm.disconnect(sid2, '/foo')) await self.bm.disconnect(sid2, '/foo')
assert not self.bm.is_connected(sid2, '/foo') assert not self.bm.is_connected(sid2, '/foo')
assert dict(self.bm.rooms['/'][None]) == {sid3: '456'} assert dict(self.bm.rooms['/'][None]) == {sid3: '456'}
assert dict(self.bm.rooms['/'][sid3]) == {sid3: '456'} assert dict(self.bm.rooms['/'][sid3]) == {sid3: '456'}
assert dict(self.bm.rooms['/foo'][None]) == {sid4: '456'} assert dict(self.bm.rooms['/foo'][None]) == {sid4: '456'}
assert dict(self.bm.rooms['/foo'][sid4]) == {sid4: '456'} assert dict(self.bm.rooms['/foo'][sid4]) == {sid4: '456'}
def test_disconnect_twice(self): async def test_disconnect_twice(self):
sid1 = _run(self.bm.connect('123', '/')) sid1 = await self.bm.connect('123', '/')
sid2 = _run(self.bm.connect('123', '/foo')) sid2 = await self.bm.connect('123', '/foo')
sid3 = _run(self.bm.connect('456', '/')) sid3 = await self.bm.connect('456', '/')
sid4 = _run(self.bm.connect('456', '/foo')) sid4 = await self.bm.connect('456', '/foo')
_run(self.bm.disconnect(sid1, '/')) await self.bm.disconnect(sid1, '/')
_run(self.bm.disconnect(sid2, '/foo')) await self.bm.disconnect(sid2, '/foo')
_run(self.bm.disconnect(sid1, '/')) await self.bm.disconnect(sid1, '/')
_run(self.bm.disconnect(sid2, '/foo')) await self.bm.disconnect(sid2, '/foo')
assert dict(self.bm.rooms['/'][None]) == {sid3: '456'} assert dict(self.bm.rooms['/'][None]) == {sid3: '456'}
assert dict(self.bm.rooms['/'][sid3]) == {sid3: '456'} assert dict(self.bm.rooms['/'][sid3]) == {sid3: '456'}
assert dict(self.bm.rooms['/foo'][None]) == {sid4: '456'} assert dict(self.bm.rooms['/foo'][None]) == {sid4: '456'}
assert dict(self.bm.rooms['/foo'][sid4]) == {sid4: '456'} assert dict(self.bm.rooms['/foo'][sid4]) == {sid4: '456'}
def test_disconnect_all(self): async def test_disconnect_all(self):
sid1 = _run(self.bm.connect('123', '/foo')) sid1 = await self.bm.connect('123', '/foo')
sid2 = _run(self.bm.connect('456', '/foo')) sid2 = await self.bm.connect('456', '/foo')
_run(self.bm.enter_room(sid1, '/foo', 'bar')) await self.bm.enter_room(sid1, '/foo', 'bar')
_run(self.bm.enter_room(sid2, '/foo', 'baz')) await self.bm.enter_room(sid2, '/foo', 'baz')
_run(self.bm.disconnect(sid1, '/foo')) await self.bm.disconnect(sid1, '/foo')
_run(self.bm.disconnect(sid2, '/foo')) await self.bm.disconnect(sid2, '/foo')
assert self.bm.rooms == {} assert self.bm.rooms == {}
def test_disconnect_with_callbacks(self): async def test_disconnect_with_callbacks(self):
sid1 = _run(self.bm.connect('123', '/')) sid1 = await self.bm.connect('123', '/')
sid2 = _run(self.bm.connect('123', '/foo')) sid2 = await self.bm.connect('123', '/foo')
sid3 = _run(self.bm.connect('456', '/foo')) sid3 = await self.bm.connect('456', '/foo')
self.bm._generate_ack_id(sid1, 'f') self.bm._generate_ack_id(sid1, 'f')
self.bm._generate_ack_id(sid2, 'g') self.bm._generate_ack_id(sid2, 'g')
self.bm._generate_ack_id(sid3, 'h') self.bm._generate_ack_id(sid3, 'h')
_run(self.bm.disconnect(sid2, '/foo')) await self.bm.disconnect(sid2, '/foo')
assert sid2 not in self.bm.callbacks assert sid2 not in self.bm.callbacks
_run(self.bm.disconnect(sid1, '/')) await self.bm.disconnect(sid1, '/')
assert sid1 not in self.bm.callbacks assert sid1 not in self.bm.callbacks
assert sid3 in self.bm.callbacks assert sid3 in self.bm.callbacks
def test_trigger_sync_callback(self): async def test_trigger_sync_callback(self):
sid1 = _run(self.bm.connect('123', '/')) sid1 = await self.bm.connect('123', '/')
sid2 = _run(self.bm.connect('123', '/foo')) sid2 = await self.bm.connect('123', '/foo')
cb = mock.MagicMock() cb = mock.MagicMock()
id1 = self.bm._generate_ack_id(sid1, cb) id1 = self.bm._generate_ack_id(sid1, cb)
id2 = self.bm._generate_ack_id(sid2, cb) id2 = self.bm._generate_ack_id(sid2, cb)
_run(self.bm.trigger_callback(sid1, id1, ['foo'])) await self.bm.trigger_callback(sid1, id1, ['foo'])
_run(self.bm.trigger_callback(sid2, id2, ['bar', 'baz'])) await self.bm.trigger_callback(sid2, id2, ['bar', 'baz'])
assert cb.call_count == 2 assert cb.call_count == 2
cb.assert_any_call('foo') cb.assert_any_call('foo')
cb.assert_any_call('bar', 'baz') cb.assert_any_call('bar', 'baz')
def test_trigger_async_callback(self): async def test_trigger_async_callback(self):
sid1 = _run(self.bm.connect('123', '/')) sid1 = await self.bm.connect('123', '/')
sid2 = _run(self.bm.connect('123', '/foo')) sid2 = await self.bm.connect('123', '/foo')
cb = mock.AsyncMock() cb = mock.AsyncMock()
id1 = self.bm._generate_ack_id(sid1, cb) id1 = self.bm._generate_ack_id(sid1, cb)
id2 = self.bm._generate_ack_id(sid2, cb) id2 = self.bm._generate_ack_id(sid2, cb)
_run(self.bm.trigger_callback(sid1, id1, ['foo'])) await self.bm.trigger_callback(sid1, id1, ['foo'])
_run(self.bm.trigger_callback(sid2, id2, ['bar', 'baz'])) await self.bm.trigger_callback(sid2, id2, ['bar', 'baz'])
assert cb.await_count == 2 assert cb.await_count == 2
cb.assert_any_await('foo') cb.assert_any_await('foo')
cb.assert_any_await('bar', 'baz') cb.assert_any_await('bar', 'baz')
def test_invalid_callback(self): async def test_invalid_callback(self):
sid = _run(self.bm.connect('123', '/')) sid = await self.bm.connect('123', '/')
cb = mock.MagicMock() cb = mock.MagicMock()
id = self.bm._generate_ack_id(sid, cb) id = self.bm._generate_ack_id(sid, cb)
# these should not raise an exception # these should not raise an exception
_run(self.bm.trigger_callback('xxx', id, ['foo'])) await self.bm.trigger_callback('xxx', id, ['foo'])
_run(self.bm.trigger_callback(sid, id + 1, ['foo'])) await self.bm.trigger_callback(sid, id + 1, ['foo'])
assert cb.call_count == 0 assert cb.call_count == 0
def test_get_namespaces(self): async def test_get_namespaces(self):
assert list(self.bm.get_namespaces()) == [] assert list(self.bm.get_namespaces()) == []
_run(self.bm.connect('123', '/')) await self.bm.connect('123', '/')
_run(self.bm.connect('123', '/foo')) await self.bm.connect('123', '/foo')
namespaces = list(self.bm.get_namespaces()) namespaces = list(self.bm.get_namespaces())
assert len(namespaces) == 2 assert len(namespaces) == 2
assert '/' in namespaces assert '/' in namespaces
assert '/foo' in namespaces assert '/foo' in namespaces
def test_get_participants(self): async def test_get_participants(self):
sid1 = _run(self.bm.connect('123', '/')) sid1 = await self.bm.connect('123', '/')
sid2 = _run(self.bm.connect('456', '/')) sid2 = await self.bm.connect('456', '/')
sid3 = _run(self.bm.connect('789', '/')) sid3 = await self.bm.connect('789', '/')
_run(self.bm.disconnect(sid3, '/')) await self.bm.disconnect(sid3, '/')
assert sid3 not in self.bm.rooms['/'][None] assert sid3 not in self.bm.rooms['/'][None]
participants = list(self.bm.get_participants('/', None)) participants = list(self.bm.get_participants('/', None))
assert len(participants) == 2 assert len(participants) == 2
@ -168,44 +167,42 @@ class TestAsyncManager:
assert (sid2, '456') in participants assert (sid2, '456') in participants
assert (sid3, '789') not in participants assert (sid3, '789') not in participants
def test_leave_invalid_room(self): async def test_leave_invalid_room(self):
sid = _run(self.bm.connect('123', '/foo')) sid = await self.bm.connect('123', '/foo')
_run(self.bm.leave_room(sid, '/foo', 'baz')) await self.bm.leave_room(sid, '/foo', 'baz')
_run(self.bm.leave_room(sid, '/bar', 'baz')) await self.bm.leave_room(sid, '/bar', 'baz')
def test_no_room(self): async def test_no_room(self):
rooms = self.bm.get_rooms('123', '/foo') rooms = self.bm.get_rooms('123', '/foo')
assert [] == rooms assert [] == rooms
def test_close_room(self): async def test_close_room(self):
sid = _run(self.bm.connect('123', '/foo')) sid = await self.bm.connect('123', '/foo')
_run(self.bm.connect('456', '/foo')) await self.bm.connect('456', '/foo')
_run(self.bm.connect('789', '/foo')) await self.bm.connect('789', '/foo')
_run(self.bm.enter_room(sid, '/foo', 'bar')) await self.bm.enter_room(sid, '/foo', 'bar')
_run(self.bm.enter_room(sid, '/foo', 'bar')) await self.bm.enter_room(sid, '/foo', 'bar')
_run(self.bm.close_room('bar', '/foo')) await self.bm.close_room('bar', '/foo')
from pprint import pprint from pprint import pprint
pprint(self.bm.rooms) pprint(self.bm.rooms)
assert 'bar' not in self.bm.rooms['/foo'] assert 'bar' not in self.bm.rooms['/foo']
def test_close_invalid_room(self): async def test_close_invalid_room(self):
self.bm.close_room('bar', '/foo') self.bm.close_room('bar', '/foo')
def test_rooms(self): async def test_rooms(self):
sid = _run(self.bm.connect('123', '/foo')) sid = await self.bm.connect('123', '/foo')
_run(self.bm.enter_room(sid, '/foo', 'bar')) await self.bm.enter_room(sid, '/foo', 'bar')
r = self.bm.get_rooms(sid, '/foo') r = self.bm.get_rooms(sid, '/foo')
assert len(r) == 2 assert len(r) == 2
assert sid in r assert sid in r
assert 'bar' in r assert 'bar' in r
def test_emit_to_sid(self): async def test_emit_to_sid(self):
sid = _run(self.bm.connect('123', '/foo')) sid = await self.bm.connect('123', '/foo')
_run(self.bm.connect('456', '/foo')) await self.bm.connect('456', '/foo')
_run( await self.bm.emit(
self.bm.emit( 'my event', {'foo': 'bar'}, namespace='/foo', to=sid
'my event', {'foo': 'bar'}, namespace='/foo', to=sid
)
) )
assert self.bm.server._send_eio_packet.await_count == 1 assert self.bm.server._send_eio_packet.await_count == 1
assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
@ -213,16 +210,14 @@ class TestAsyncManager:
pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1]
assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]'
def test_emit_to_room(self): async def test_emit_to_room(self):
sid1 = _run(self.bm.connect('123', '/foo')) sid1 = await self.bm.connect('123', '/foo')
_run(self.bm.enter_room(sid1, '/foo', 'bar')) await self.bm.enter_room(sid1, '/foo', 'bar')
sid2 = _run(self.bm.connect('456', '/foo')) sid2 = await self.bm.connect('456', '/foo')
_run(self.bm.enter_room(sid2, '/foo', 'bar')) await self.bm.enter_room(sid2, '/foo', 'bar')
_run(self.bm.connect('789', '/foo')) await self.bm.connect('789', '/foo')
_run( await self.bm.emit(
self.bm.emit( 'my event', {'foo': 'bar'}, namespace='/foo', room='bar'
'my event', {'foo': 'bar'}, namespace='/foo', room='bar'
)
) )
assert self.bm.server._send_eio_packet.await_count == 2 assert self.bm.server._send_eio_packet.await_count == 2
assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
@ -234,18 +229,16 @@ class TestAsyncManager:
== pkt == pkt
assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]'
def test_emit_to_rooms(self): async def test_emit_to_rooms(self):
sid1 = _run(self.bm.connect('123', '/foo')) sid1 = await self.bm.connect('123', '/foo')
_run(self.bm.enter_room(sid1, '/foo', 'bar')) await self.bm.enter_room(sid1, '/foo', 'bar')
sid2 = _run(self.bm.connect('456', '/foo')) sid2 = await self.bm.connect('456', '/foo')
_run(self.bm.enter_room(sid2, '/foo', 'bar')) await self.bm.enter_room(sid2, '/foo', 'bar')
_run(self.bm.enter_room(sid2, '/foo', 'baz')) await self.bm.enter_room(sid2, '/foo', 'baz')
sid3 = _run(self.bm.connect('789', '/foo')) sid3 = await self.bm.connect('789', '/foo')
_run(self.bm.enter_room(sid3, '/foo', 'baz')) await self.bm.enter_room(sid3, '/foo', 'baz')
_run( await self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo',
self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo', room=['bar', 'baz'])
room=['bar', 'baz'])
)
assert self.bm.server._send_eio_packet.await_count == 3 assert self.bm.server._send_eio_packet.await_count == 3
assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
== '123' == '123'
@ -260,14 +253,14 @@ class TestAsyncManager:
== pkt == pkt
assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]'
def test_emit_to_all(self): async def test_emit_to_all(self):
sid1 = _run(self.bm.connect('123', '/foo')) sid1 = await self.bm.connect('123', '/foo')
_run(self.bm.enter_room(sid1, '/foo', 'bar')) await self.bm.enter_room(sid1, '/foo', 'bar')
sid2 = _run(self.bm.connect('456', '/foo')) sid2 = await self.bm.connect('456', '/foo')
_run(self.bm.enter_room(sid2, '/foo', 'bar')) await self.bm.enter_room(sid2, '/foo', 'bar')
_run(self.bm.connect('789', '/foo')) await self.bm.connect('789', '/foo')
_run(self.bm.connect('abc', '/bar')) await self.bm.connect('abc', '/bar')
_run(self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo')) await self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo')
assert self.bm.server._send_eio_packet.await_count == 3 assert self.bm.server._send_eio_packet.await_count == 3
assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
== '123' == '123'
@ -282,17 +275,15 @@ class TestAsyncManager:
== pkt == pkt
assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]'
def test_emit_to_all_skip_one(self): async def test_emit_to_all_skip_one(self):
sid1 = _run(self.bm.connect('123', '/foo')) sid1 = await self.bm.connect('123', '/foo')
_run(self.bm.enter_room(sid1, '/foo', 'bar')) await self.bm.enter_room(sid1, '/foo', 'bar')
sid2 = _run(self.bm.connect('456', '/foo')) sid2 = await self.bm.connect('456', '/foo')
_run(self.bm.enter_room(sid2, '/foo', 'bar')) await self.bm.enter_room(sid2, '/foo', 'bar')
_run(self.bm.connect('789', '/foo')) await self.bm.connect('789', '/foo')
_run(self.bm.connect('abc', '/bar')) await self.bm.connect('abc', '/bar')
_run( await self.bm.emit(
self.bm.emit( 'my event', {'foo': 'bar'}, namespace='/foo', skip_sid=sid2
'my event', {'foo': 'bar'}, namespace='/foo', skip_sid=sid2
)
) )
assert self.bm.server._send_eio_packet.await_count == 2 assert self.bm.server._send_eio_packet.await_count == 2
assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
@ -304,20 +295,18 @@ class TestAsyncManager:
== pkt == pkt
assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]'
def test_emit_to_all_skip_two(self): async def test_emit_to_all_skip_two(self):
sid1 = _run(self.bm.connect('123', '/foo')) sid1 = await self.bm.connect('123', '/foo')
_run(self.bm.enter_room(sid1, '/foo', 'bar')) await self.bm.enter_room(sid1, '/foo', 'bar')
sid2 = _run(self.bm.connect('456', '/foo')) sid2 = await self.bm.connect('456', '/foo')
_run(self.bm.enter_room(sid2, '/foo', 'bar')) await self.bm.enter_room(sid2, '/foo', 'bar')
sid3 = _run(self.bm.connect('789', '/foo')) sid3 = await self.bm.connect('789', '/foo')
_run(self.bm.connect('abc', '/bar')) await self.bm.connect('abc', '/bar')
_run( await self.bm.emit(
self.bm.emit( 'my event',
'my event', {'foo': 'bar'},
{'foo': 'bar'}, namespace='/foo',
namespace='/foo', skip_sid=[sid1, sid3],
skip_sid=[sid1, sid3],
)
) )
assert self.bm.server._send_eio_packet.await_count == 1 assert self.bm.server._send_eio_packet.await_count == 1
assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
@ -325,14 +314,12 @@ class TestAsyncManager:
pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1]
assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]'
def test_emit_with_callback(self): async def test_emit_with_callback(self):
sid = _run(self.bm.connect('123', '/foo')) sid = await self.bm.connect('123', '/foo')
self.bm._generate_ack_id = mock.MagicMock() self.bm._generate_ack_id = mock.MagicMock()
self.bm._generate_ack_id.return_value = 11 self.bm._generate_ack_id.return_value = 11
_run( await self.bm.emit(
self.bm.emit( 'my event', {'foo': 'bar'}, namespace='/foo', callback='cb'
'my event', {'foo': 'bar'}, namespace='/foo', callback='cb'
)
) )
self.bm._generate_ack_id.assert_called_once_with(sid, 'cb') self.bm._generate_ack_id.assert_called_once_with(sid, 'cb')
assert self.bm.server._send_packet.await_count == 1 assert self.bm.server._send_packet.await_count == 1
@ -341,20 +328,17 @@ class TestAsyncManager:
pkt = self.bm.server._send_packet.await_args_list[0][0][1] pkt = self.bm.server._send_packet.await_args_list[0][0][1]
assert pkt.encode() == '2/foo,11["my event",{"foo":"bar"}]' assert pkt.encode() == '2/foo,11["my event",{"foo":"bar"}]'
def test_emit_to_invalid_room(self): async def test_emit_to_invalid_room(self):
_run( await self.bm.emit('my event', {'foo': 'bar'}, namespace='/',
self.bm.emit('my event', {'foo': 'bar'}, namespace='/', room='123') room='123')
)
def test_emit_to_invalid_namespace(self): async def test_emit_to_invalid_namespace(self):
_run(self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo')) await self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo')
def test_emit_with_tuple(self): async def test_emit_with_tuple(self):
sid = _run(self.bm.connect('123', '/foo')) sid = await self.bm.connect('123', '/foo')
_run( await self.bm.emit(
self.bm.emit( 'my event', ('foo', 'bar'), namespace='/foo', room=sid
'my event', ('foo', 'bar'), namespace='/foo', room=sid
)
) )
assert self.bm.server._send_eio_packet.await_count == 1 assert self.bm.server._send_eio_packet.await_count == 1
assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
@ -362,12 +346,10 @@ class TestAsyncManager:
pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1]
assert pkt.encode() == '42/foo,["my event","foo","bar"]' assert pkt.encode() == '42/foo,["my event","foo","bar"]'
def test_emit_with_list(self): async def test_emit_with_list(self):
sid = _run(self.bm.connect('123', '/foo')) sid = await self.bm.connect('123', '/foo')
_run( await self.bm.emit(
self.bm.emit( 'my event', ['foo', 'bar'], namespace='/foo', room=sid
'my event', ['foo', 'bar'], namespace='/foo', room=sid
)
) )
assert self.bm.server._send_eio_packet.await_count == 1 assert self.bm.server._send_eio_packet.await_count == 1
assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
@ -375,12 +357,10 @@ class TestAsyncManager:
pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1]
assert pkt.encode() == '42/foo,["my event",["foo","bar"]]' assert pkt.encode() == '42/foo,["my event",["foo","bar"]]'
def test_emit_with_none(self): async def test_emit_with_none(self):
sid = _run(self.bm.connect('123', '/foo')) sid = await self.bm.connect('123', '/foo')
_run( await self.bm.emit(
self.bm.emit( 'my event', None, namespace='/foo', room=sid
'my event', None, namespace='/foo', room=sid
)
) )
assert self.bm.server._send_eio_packet.await_count == 1 assert self.bm.server._send_eio_packet.await_count == 1
assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
@ -388,12 +368,10 @@ class TestAsyncManager:
pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1]
assert pkt.encode() == '42/foo,["my event"]' assert pkt.encode() == '42/foo,["my event"]'
def test_emit_binary(self): async def test_emit_binary(self):
sid = _run(self.bm.connect('123', '/')) sid = await self.bm.connect('123', '/')
_run( await self.bm.emit(
self.bm.emit( 'my event', b'my binary data', namespace='/', room=sid
'my event', b'my binary data', namespace='/', room=sid
)
) )
assert self.bm.server._send_eio_packet.await_count == 2 assert self.bm.server._send_eio_packet.await_count == 2
assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \

151
tests/async/test_namespace.py

@ -1,11 +1,10 @@
from unittest import mock from unittest import mock
from socketio import async_namespace from socketio import async_namespace
from .helpers import _run
class TestAsyncNamespace: class TestAsyncNamespace:
def test_connect_event(self): async def test_connect_event(self):
result = {} result = {}
class MyNamespace(async_namespace.AsyncNamespace): class MyNamespace(async_namespace.AsyncNamespace):
@ -14,10 +13,10 @@ class TestAsyncNamespace:
ns = MyNamespace('/foo') ns = MyNamespace('/foo')
ns._set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
_run(ns.trigger_event('connect', 'sid', {'foo': 'bar'})) await ns.trigger_event('connect', 'sid', {'foo': 'bar'})
assert result['result'] == ('sid', {'foo': 'bar'}) assert result['result'] == ('sid', {'foo': 'bar'})
def test_disconnect_event(self): async def test_disconnect_event(self):
result = {} result = {}
class MyNamespace(async_namespace.AsyncNamespace): class MyNamespace(async_namespace.AsyncNamespace):
@ -26,10 +25,10 @@ class TestAsyncNamespace:
ns = MyNamespace('/foo') ns = MyNamespace('/foo')
ns._set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
_run(ns.trigger_event('disconnect', 'sid')) await ns.trigger_event('disconnect', 'sid')
assert result['result'] == 'sid' assert result['result'] == 'sid'
def test_sync_event(self): async def test_sync_event(self):
result = {} result = {}
class MyNamespace(async_namespace.AsyncNamespace): class MyNamespace(async_namespace.AsyncNamespace):
@ -38,10 +37,10 @@ class TestAsyncNamespace:
ns = MyNamespace('/foo') ns = MyNamespace('/foo')
ns._set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
_run(ns.trigger_event('custom_message', 'sid', {'data': 'data'})) await ns.trigger_event('custom_message', 'sid', {'data': 'data'})
assert result['result'] == ('sid', {'data': 'data'}) assert result['result'] == ('sid', {'data': 'data'})
def test_async_event(self): async def test_async_event(self):
result = {} result = {}
class MyNamespace(async_namespace.AsyncNamespace): class MyNamespace(async_namespace.AsyncNamespace):
@ -50,10 +49,10 @@ class TestAsyncNamespace:
ns = MyNamespace('/foo') ns = MyNamespace('/foo')
ns._set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
_run(ns.trigger_event('custom_message', 'sid', {'data': 'data'})) await ns.trigger_event('custom_message', 'sid', {'data': 'data'})
assert result['result'] == ('sid', {'data': 'data'}) assert result['result'] == ('sid', {'data': 'data'})
def test_event_not_found(self): async def test_event_not_found(self):
result = {} result = {}
class MyNamespace(async_namespace.AsyncNamespace): class MyNamespace(async_namespace.AsyncNamespace):
@ -62,20 +61,17 @@ class TestAsyncNamespace:
ns = MyNamespace('/foo') ns = MyNamespace('/foo')
ns._set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
_run( await ns.trigger_event('another_custom_message', 'sid',
ns.trigger_event('another_custom_message', 'sid', {'data': 'data'}) {'data': 'data'})
)
assert result == {} assert result == {}
def test_emit(self): async def test_emit(self):
ns = async_namespace.AsyncNamespace('/foo') ns = async_namespace.AsyncNamespace('/foo')
mock_server = mock.MagicMock() mock_server = mock.MagicMock()
mock_server.emit = mock.AsyncMock() mock_server.emit = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run( await ns.emit(
ns.emit( 'ev', data='data', to='room', skip_sid='skip', callback='cb'
'ev', data='data', to='room', skip_sid='skip', callback='cb'
)
) )
ns.server.emit.assert_awaited_with( ns.server.emit.assert_awaited_with(
'ev', 'ev',
@ -87,16 +83,14 @@ class TestAsyncNamespace:
callback='cb', callback='cb',
ignore_queue=False, ignore_queue=False,
) )
_run( await ns.emit(
ns.emit( 'ev',
'ev', data='data',
data='data', room='room',
room='room', skip_sid='skip',
skip_sid='skip', namespace='/bar',
namespace='/bar', callback='cb',
callback='cb', ignore_queue=True,
ignore_queue=True,
)
) )
ns.server.emit.assert_awaited_with( ns.server.emit.assert_awaited_with(
'ev', 'ev',
@ -109,12 +103,12 @@ class TestAsyncNamespace:
ignore_queue=True, ignore_queue=True,
) )
def test_send(self): async def test_send(self):
ns = async_namespace.AsyncNamespace('/foo') ns = async_namespace.AsyncNamespace('/foo')
mock_server = mock.MagicMock() mock_server = mock.MagicMock()
mock_server.send = mock.AsyncMock() mock_server.send = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run(ns.send(data='data', to='room', skip_sid='skip', callback='cb')) await ns.send(data='data', to='room', skip_sid='skip', callback='cb')
ns.server.send.assert_awaited_with( ns.server.send.assert_awaited_with(
'data', 'data',
to='room', to='room',
@ -124,15 +118,13 @@ class TestAsyncNamespace:
callback='cb', callback='cb',
ignore_queue=False, ignore_queue=False,
) )
_run( await ns.send(
ns.send( data='data',
data='data', room='room',
room='room', skip_sid='skip',
skip_sid='skip', namespace='/bar',
namespace='/bar', callback='cb',
callback='cb', ignore_queue=True,
ignore_queue=True,
)
) )
ns.server.send.assert_awaited_with( ns.server.send.assert_awaited_with(
'data', 'data',
@ -144,12 +136,12 @@ class TestAsyncNamespace:
ignore_queue=True, ignore_queue=True,
) )
def test_call(self): async def test_call(self):
ns = async_namespace.AsyncNamespace('/foo') ns = async_namespace.AsyncNamespace('/foo')
mock_server = mock.MagicMock() mock_server = mock.MagicMock()
mock_server.call = mock.AsyncMock() mock_server.call = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run(ns.call('ev', data='data', to='sid')) await ns.call('ev', data='data', to='sid')
ns.server.call.assert_awaited_with( ns.server.call.assert_awaited_with(
'ev', 'ev',
data='data', data='data',
@ -159,8 +151,8 @@ class TestAsyncNamespace:
timeout=None, timeout=None,
ignore_queue=False, ignore_queue=False,
) )
_run(ns.call('ev', data='data', sid='sid', namespace='/bar', await ns.call('ev', data='data', sid='sid', namespace='/bar',
timeout=45, ignore_queue=True)) timeout=45, ignore_queue=True)
ns.server.call.assert_awaited_with( ns.server.call.assert_awaited_with(
'ev', 'ev',
data='data', data='data',
@ -171,45 +163,45 @@ class TestAsyncNamespace:
ignore_queue=True, ignore_queue=True,
) )
def test_enter_room(self): async def test_enter_room(self):
ns = async_namespace.AsyncNamespace('/foo') ns = async_namespace.AsyncNamespace('/foo')
mock_server = mock.MagicMock() mock_server = mock.MagicMock()
mock_server.enter_room = mock.AsyncMock() mock_server.enter_room = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run(ns.enter_room('sid', 'room')) await ns.enter_room('sid', 'room')
ns.server.enter_room.assert_awaited_with( ns.server.enter_room.assert_awaited_with(
'sid', 'room', namespace='/foo' 'sid', 'room', namespace='/foo'
) )
_run(ns.enter_room('sid', 'room', namespace='/bar')) await ns.enter_room('sid', 'room', namespace='/bar')
ns.server.enter_room.assert_awaited_with( ns.server.enter_room.assert_awaited_with(
'sid', 'room', namespace='/bar' 'sid', 'room', namespace='/bar'
) )
def test_leave_room(self): async def test_leave_room(self):
ns = async_namespace.AsyncNamespace('/foo') ns = async_namespace.AsyncNamespace('/foo')
mock_server = mock.MagicMock() mock_server = mock.MagicMock()
mock_server.leave_room = mock.AsyncMock() mock_server.leave_room = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run(ns.leave_room('sid', 'room')) await ns.leave_room('sid', 'room')
ns.server.leave_room.assert_awaited_with( ns.server.leave_room.assert_awaited_with(
'sid', 'room', namespace='/foo' 'sid', 'room', namespace='/foo'
) )
_run(ns.leave_room('sid', 'room', namespace='/bar')) await ns.leave_room('sid', 'room', namespace='/bar')
ns.server.leave_room.assert_awaited_with( ns.server.leave_room.assert_awaited_with(
'sid', 'room', namespace='/bar' 'sid', 'room', namespace='/bar'
) )
def test_close_room(self): async def test_close_room(self):
ns = async_namespace.AsyncNamespace('/foo') ns = async_namespace.AsyncNamespace('/foo')
mock_server = mock.MagicMock() mock_server = mock.MagicMock()
mock_server.close_room = mock.AsyncMock() mock_server.close_room = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run(ns.close_room('room')) await ns.close_room('room')
ns.server.close_room.assert_awaited_with('room', namespace='/foo') ns.server.close_room.assert_awaited_with('room', namespace='/foo')
_run(ns.close_room('room', namespace='/bar')) await ns.close_room('room', namespace='/bar')
ns.server.close_room.assert_awaited_with('room', namespace='/bar') ns.server.close_room.assert_awaited_with('room', namespace='/bar')
def test_rooms(self): async def test_rooms(self):
ns = async_namespace.AsyncNamespace('/foo') ns = async_namespace.AsyncNamespace('/foo')
ns._set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
ns.rooms('sid') ns.rooms('sid')
@ -217,21 +209,21 @@ class TestAsyncNamespace:
ns.rooms('sid', namespace='/bar') ns.rooms('sid', namespace='/bar')
ns.server.rooms.assert_called_with('sid', namespace='/bar') ns.server.rooms.assert_called_with('sid', namespace='/bar')
def test_session(self): async def test_session(self):
ns = async_namespace.AsyncNamespace('/foo') ns = async_namespace.AsyncNamespace('/foo')
mock_server = mock.MagicMock() mock_server = mock.MagicMock()
mock_server.get_session = mock.AsyncMock() mock_server.get_session = mock.AsyncMock()
mock_server.save_session = mock.AsyncMock() mock_server.save_session = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run(ns.get_session('sid')) await ns.get_session('sid')
ns.server.get_session.assert_awaited_with('sid', namespace='/foo') ns.server.get_session.assert_awaited_with('sid', namespace='/foo')
_run(ns.get_session('sid', namespace='/bar')) await ns.get_session('sid', namespace='/bar')
ns.server.get_session.assert_awaited_with('sid', namespace='/bar') ns.server.get_session.assert_awaited_with('sid', namespace='/bar')
_run(ns.save_session('sid', {'a': 'b'})) await ns.save_session('sid', {'a': 'b'})
ns.server.save_session.assert_awaited_with( ns.server.save_session.assert_awaited_with(
'sid', {'a': 'b'}, namespace='/foo' 'sid', {'a': 'b'}, namespace='/foo'
) )
_run(ns.save_session('sid', {'a': 'b'}, namespace='/bar')) await ns.save_session('sid', {'a': 'b'}, namespace='/bar')
ns.server.save_session.assert_awaited_with( ns.server.save_session.assert_awaited_with(
'sid', {'a': 'b'}, namespace='/bar' 'sid', {'a': 'b'}, namespace='/bar'
) )
@ -240,17 +232,17 @@ class TestAsyncNamespace:
ns.session('sid', namespace='/bar') ns.session('sid', namespace='/bar')
ns.server.session.assert_called_with('sid', namespace='/bar') ns.server.session.assert_called_with('sid', namespace='/bar')
def test_disconnect(self): async def test_disconnect(self):
ns = async_namespace.AsyncNamespace('/foo') ns = async_namespace.AsyncNamespace('/foo')
mock_server = mock.MagicMock() mock_server = mock.MagicMock()
mock_server.disconnect = mock.AsyncMock() mock_server.disconnect = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run(ns.disconnect('sid')) await ns.disconnect('sid')
ns.server.disconnect.assert_awaited_with('sid', namespace='/foo') ns.server.disconnect.assert_awaited_with('sid', namespace='/foo')
_run(ns.disconnect('sid', namespace='/bar')) await ns.disconnect('sid', namespace='/bar')
ns.server.disconnect.assert_awaited_with('sid', namespace='/bar') ns.server.disconnect.assert_awaited_with('sid', namespace='/bar')
def test_sync_event_client(self): async def test_sync_event_client(self):
result = {} result = {}
class MyNamespace(async_namespace.AsyncClientNamespace): class MyNamespace(async_namespace.AsyncClientNamespace):
@ -259,10 +251,10 @@ class TestAsyncNamespace:
ns = MyNamespace('/foo') ns = MyNamespace('/foo')
ns._set_client(mock.MagicMock()) ns._set_client(mock.MagicMock())
_run(ns.trigger_event('custom_message', 'sid', {'data': 'data'})) await ns.trigger_event('custom_message', 'sid', {'data': 'data'})
assert result['result'] == ('sid', {'data': 'data'}) assert result['result'] == ('sid', {'data': 'data'})
def test_async_event_client(self): async def test_async_event_client(self):
result = {} result = {}
class MyNamespace(async_namespace.AsyncClientNamespace): class MyNamespace(async_namespace.AsyncClientNamespace):
@ -271,10 +263,10 @@ class TestAsyncNamespace:
ns = MyNamespace('/foo') ns = MyNamespace('/foo')
ns._set_client(mock.MagicMock()) ns._set_client(mock.MagicMock())
_run(ns.trigger_event('custom_message', 'sid', {'data': 'data'})) await ns.trigger_event('custom_message', 'sid', {'data': 'data'})
assert result['result'] == ('sid', {'data': 'data'}) assert result['result'] == ('sid', {'data': 'data'})
def test_event_not_found_client(self): async def test_event_not_found_client(self):
result = {} result = {}
class MyNamespace(async_namespace.AsyncClientNamespace): class MyNamespace(async_namespace.AsyncClientNamespace):
@ -283,57 +275,56 @@ class TestAsyncNamespace:
ns = MyNamespace('/foo') ns = MyNamespace('/foo')
ns._set_client(mock.MagicMock()) ns._set_client(mock.MagicMock())
_run( await ns.trigger_event('another_custom_message', 'sid',
ns.trigger_event('another_custom_message', 'sid', {'data': 'data'}) {'data': 'data'})
)
assert result == {} assert result == {}
def test_emit_client(self): async def test_emit_client(self):
ns = async_namespace.AsyncClientNamespace('/foo') ns = async_namespace.AsyncClientNamespace('/foo')
mock_client = mock.MagicMock() mock_client = mock.MagicMock()
mock_client.emit = mock.AsyncMock() mock_client.emit = mock.AsyncMock()
ns._set_client(mock_client) ns._set_client(mock_client)
_run(ns.emit('ev', data='data', callback='cb')) await ns.emit('ev', data='data', callback='cb')
ns.client.emit.assert_awaited_with( ns.client.emit.assert_awaited_with(
'ev', data='data', namespace='/foo', callback='cb' 'ev', data='data', namespace='/foo', callback='cb'
) )
_run(ns.emit('ev', data='data', namespace='/bar', callback='cb')) await ns.emit('ev', data='data', namespace='/bar', callback='cb')
ns.client.emit.assert_awaited_with( ns.client.emit.assert_awaited_with(
'ev', data='data', namespace='/bar', callback='cb' 'ev', data='data', namespace='/bar', callback='cb'
) )
def test_send_client(self): async def test_send_client(self):
ns = async_namespace.AsyncClientNamespace('/foo') ns = async_namespace.AsyncClientNamespace('/foo')
mock_client = mock.MagicMock() mock_client = mock.MagicMock()
mock_client.send = mock.AsyncMock() mock_client.send = mock.AsyncMock()
ns._set_client(mock_client) ns._set_client(mock_client)
_run(ns.send(data='data', callback='cb')) await ns.send(data='data', callback='cb')
ns.client.send.assert_awaited_with( ns.client.send.assert_awaited_with(
'data', namespace='/foo', callback='cb' 'data', namespace='/foo', callback='cb'
) )
_run(ns.send(data='data', namespace='/bar', callback='cb')) await ns.send(data='data', namespace='/bar', callback='cb')
ns.client.send.assert_awaited_with( ns.client.send.assert_awaited_with(
'data', namespace='/bar', callback='cb' 'data', namespace='/bar', callback='cb'
) )
def test_call_client(self): async def test_call_client(self):
ns = async_namespace.AsyncClientNamespace('/foo') ns = async_namespace.AsyncClientNamespace('/foo')
mock_client = mock.MagicMock() mock_client = mock.MagicMock()
mock_client.call = mock.AsyncMock() mock_client.call = mock.AsyncMock()
ns._set_client(mock_client) ns._set_client(mock_client)
_run(ns.call('ev', data='data')) await ns.call('ev', data='data')
ns.client.call.assert_awaited_with( ns.client.call.assert_awaited_with(
'ev', data='data', namespace='/foo', timeout=None 'ev', data='data', namespace='/foo', timeout=None
) )
_run(ns.call('ev', data='data', namespace='/bar', timeout=45)) await ns.call('ev', data='data', namespace='/bar', timeout=45)
ns.client.call.assert_awaited_with( ns.client.call.assert_awaited_with(
'ev', data='data', namespace='/bar', timeout=45 'ev', data='data', namespace='/bar', timeout=45
) )
def test_disconnect_client(self): async def test_disconnect_client(self):
ns = async_namespace.AsyncClientNamespace('/foo') ns = async_namespace.AsyncClientNamespace('/foo')
mock_client = mock.MagicMock() mock_client = mock.MagicMock()
mock_client.disconnect = mock.AsyncMock() mock_client.disconnect = mock.AsyncMock()
ns._set_client(mock_client) ns._set_client(mock_client)
_run(ns.disconnect()) await ns.disconnect()
ns.client.disconnect.assert_awaited_with() ns.client.disconnect.assert_awaited_with()

335
tests/async/test_pubsub_manager.py

@ -7,7 +7,6 @@ import pytest
from socketio import async_manager from socketio import async_manager
from socketio import async_pubsub_manager from socketio import async_pubsub_manager
from socketio import packet from socketio import packet
from .helpers import _run
class TestAsyncPubSubManager: class TestAsyncPubSubManager:
@ -31,18 +30,18 @@ class TestAsyncPubSubManager:
self.pm.host_id = '123456' self.pm.host_id = '123456'
self.pm.initialize() self.pm.initialize()
def test_default_init(self): async def test_default_init(self):
assert self.pm.channel == 'socketio' assert self.pm.channel == 'socketio'
self.pm.server.start_background_task.assert_called_once_with( self.pm.server.start_background_task.assert_called_once_with(
self.pm._thread self.pm._thread
) )
def test_custom_init(self): async def test_custom_init(self):
pubsub = async_pubsub_manager.AsyncPubSubManager(channel='foo') pubsub = async_pubsub_manager.AsyncPubSubManager(channel='foo')
assert pubsub.channel == 'foo' assert pubsub.channel == 'foo'
assert len(pubsub.host_id) == 32 assert len(pubsub.host_id) == 32
def test_write_only_init(self): async def test_write_only_init(self):
mock_server = mock.MagicMock() mock_server = mock.MagicMock()
pm = async_pubsub_manager.AsyncPubSubManager(write_only=True) pm = async_pubsub_manager.AsyncPubSubManager(write_only=True)
pm.set_server(mock_server) pm.set_server(mock_server)
@ -51,8 +50,8 @@ class TestAsyncPubSubManager:
assert len(pm.host_id) == 32 assert len(pm.host_id) == 32
assert pm.server.start_background_task.call_count == 0 assert pm.server.start_background_task.call_count == 0
def test_emit(self): async def test_emit(self):
_run(self.pm.emit('foo', 'bar')) await self.pm.emit('foo', 'bar')
self.pm._publish.assert_awaited_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'emit', 'method': 'emit',
@ -66,9 +65,9 @@ class TestAsyncPubSubManager:
} }
) )
def test_emit_with_to(self): async def test_emit_with_to(self):
sid = 'room-mate' sid = 'room-mate'
_run(self.pm.emit('foo', 'bar', to=sid)) await self.pm.emit('foo', 'bar', to=sid)
self.pm._publish.assert_awaited_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'emit', 'method': 'emit',
@ -82,8 +81,8 @@ class TestAsyncPubSubManager:
} }
) )
def test_emit_with_namespace(self): async def test_emit_with_namespace(self):
_run(self.pm.emit('foo', 'bar', namespace='/baz')) await self.pm.emit('foo', 'bar', namespace='/baz')
self.pm._publish.assert_awaited_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'emit', 'method': 'emit',
@ -97,8 +96,8 @@ class TestAsyncPubSubManager:
} }
) )
def test_emit_with_room(self): async def test_emit_with_room(self):
_run(self.pm.emit('foo', 'bar', room='baz')) await self.pm.emit('foo', 'bar', room='baz')
self.pm._publish.assert_awaited_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'emit', 'method': 'emit',
@ -112,8 +111,8 @@ class TestAsyncPubSubManager:
} }
) )
def test_emit_with_skip_sid(self): async def test_emit_with_skip_sid(self):
_run(self.pm.emit('foo', 'bar', skip_sid='baz')) await self.pm.emit('foo', 'bar', skip_sid='baz')
self.pm._publish.assert_awaited_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'emit', 'method': 'emit',
@ -127,11 +126,11 @@ class TestAsyncPubSubManager:
} }
) )
def test_emit_with_callback(self): async def test_emit_with_callback(self):
with mock.patch.object( with mock.patch.object(
self.pm, '_generate_ack_id', return_value='123' self.pm, '_generate_ack_id', return_value='123'
): ):
_run(self.pm.emit('foo', 'bar', room='baz', callback='cb')) await self.pm.emit('foo', 'bar', room='baz', callback='cb')
self.pm._publish.assert_awaited_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'emit', 'method': 'emit',
@ -145,24 +144,22 @@ class TestAsyncPubSubManager:
} }
) )
def test_emit_with_callback_without_server(self): async def test_emit_with_callback_without_server(self):
standalone_pm = async_pubsub_manager.AsyncPubSubManager() standalone_pm = async_pubsub_manager.AsyncPubSubManager()
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
_run(standalone_pm.emit('foo', 'bar', callback='cb')) await standalone_pm.emit('foo', 'bar', callback='cb')
def test_emit_with_callback_missing_room(self): async def test_emit_with_callback_missing_room(self):
with mock.patch.object( with mock.patch.object(
self.pm, '_generate_ack_id', return_value='123' self.pm, '_generate_ack_id', return_value='123'
): ):
with pytest.raises(ValueError): with pytest.raises(ValueError):
_run(self.pm.emit('foo', 'bar', callback='cb')) await self.pm.emit('foo', 'bar', callback='cb')
def test_emit_with_ignore_queue(self): async def test_emit_with_ignore_queue(self):
sid = _run(self.pm.connect('123', '/')) sid = await self.pm.connect('123', '/')
_run( await self.pm.emit(
self.pm.emit( 'foo', 'bar', room=sid, namespace='/', ignore_queue=True
'foo', 'bar', room=sid, namespace='/', ignore_queue=True
)
) )
self.pm._publish.assert_not_awaited() self.pm._publish.assert_not_awaited()
assert self.pm.server._send_eio_packet.await_count == 1 assert self.pm.server._send_eio_packet.await_count == 1
@ -171,33 +168,33 @@ class TestAsyncPubSubManager:
pkt = self.pm.server._send_eio_packet.await_args_list[0][0][1] pkt = self.pm.server._send_eio_packet.await_args_list[0][0][1]
assert pkt.encode() == '42["foo","bar"]' assert pkt.encode() == '42["foo","bar"]'
def test_can_disconnect(self): async def test_can_disconnect(self):
sid = _run(self.pm.connect('123', '/')) sid = await self.pm.connect('123', '/')
assert _run(self.pm.can_disconnect(sid, '/')) is True assert await self.pm.can_disconnect(sid, '/') is True
_run(self.pm.can_disconnect(sid, '/foo')) await self.pm.can_disconnect(sid, '/foo')
self.pm._publish.assert_awaited_once_with( self.pm._publish.assert_awaited_once_with(
{'method': 'disconnect', 'sid': sid, 'namespace': '/foo', {'method': 'disconnect', 'sid': sid, 'namespace': '/foo',
'host_id': '123456'} 'host_id': '123456'}
) )
def test_disconnect(self): async def test_disconnect(self):
_run(self.pm.disconnect('foo', '/')) await self.pm.disconnect('foo', '/')
self.pm._publish.assert_awaited_once_with( self.pm._publish.assert_awaited_once_with(
{'method': 'disconnect', 'sid': 'foo', 'namespace': '/', {'method': 'disconnect', 'sid': 'foo', 'namespace': '/',
'host_id': '123456'} 'host_id': '123456'}
) )
def test_disconnect_ignore_queue(self): async def test_disconnect_ignore_queue(self):
sid = _run(self.pm.connect('123', '/')) sid = await self.pm.connect('123', '/')
self.pm.pre_disconnect(sid, '/') self.pm.pre_disconnect(sid, '/')
_run(self.pm.disconnect(sid, '/', ignore_queue=True)) await self.pm.disconnect(sid, '/', ignore_queue=True)
self.pm._publish.assert_not_awaited() self.pm._publish.assert_not_awaited()
assert self.pm.is_connected(sid, '/') is False assert self.pm.is_connected(sid, '/') is False
def test_enter_room(self): async def test_enter_room(self):
sid = _run(self.pm.connect('123', '/')) sid = await self.pm.connect('123', '/')
_run(self.pm.enter_room(sid, '/', 'foo')) await self.pm.enter_room(sid, '/', 'foo')
_run(self.pm.enter_room('456', '/', 'foo')) await self.pm.enter_room('456', '/', 'foo')
assert sid in self.pm.rooms['/']['foo'] assert sid in self.pm.rooms['/']['foo']
assert self.pm.rooms['/']['foo'][sid] == '123' assert self.pm.rooms['/']['foo'][sid] == '123'
self.pm._publish.assert_awaited_once_with( self.pm._publish.assert_awaited_once_with(
@ -205,35 +202,35 @@ class TestAsyncPubSubManager:
'namespace': '/', 'host_id': '123456'} 'namespace': '/', 'host_id': '123456'}
) )
def test_leave_room(self): async def test_leave_room(self):
sid = _run(self.pm.connect('123', '/')) sid = await self.pm.connect('123', '/')
_run(self.pm.leave_room(sid, '/', 'foo')) await self.pm.leave_room(sid, '/', 'foo')
_run(self.pm.leave_room('456', '/', 'foo')) await self.pm.leave_room('456', '/', 'foo')
assert 'foo' not in self.pm.rooms['/'] assert 'foo' not in self.pm.rooms['/']
self.pm._publish.assert_awaited_once_with( self.pm._publish.assert_awaited_once_with(
{'method': 'leave_room', 'sid': '456', 'room': 'foo', {'method': 'leave_room', 'sid': '456', 'room': 'foo',
'namespace': '/', 'host_id': '123456'} 'namespace': '/', 'host_id': '123456'}
) )
def test_close_room(self): async def test_close_room(self):
_run(self.pm.close_room('foo')) await self.pm.close_room('foo')
self.pm._publish.assert_awaited_once_with( self.pm._publish.assert_awaited_once_with(
{'method': 'close_room', 'room': 'foo', 'namespace': '/', {'method': 'close_room', 'room': 'foo', 'namespace': '/',
'host_id': '123456'} 'host_id': '123456'}
) )
def test_close_room_with_namespace(self): async def test_close_room_with_namespace(self):
_run(self.pm.close_room('foo', '/bar')) await self.pm.close_room('foo', '/bar')
self.pm._publish.assert_awaited_once_with( self.pm._publish.assert_awaited_once_with(
{'method': 'close_room', 'room': 'foo', 'namespace': '/bar', {'method': 'close_room', 'room': 'foo', 'namespace': '/bar',
'host_id': '123456'} 'host_id': '123456'}
) )
def test_handle_emit(self): async def test_handle_emit(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'emit' async_manager.AsyncManager, 'emit'
) as super_emit: ) as super_emit:
_run(self.pm._handle_emit({'event': 'foo', 'data': 'bar'})) await self.pm._handle_emit({'event': 'foo', 'data': 'bar'})
super_emit.assert_awaited_once_with( super_emit.assert_awaited_once_with(
'foo', 'foo',
'bar', 'bar',
@ -243,14 +240,12 @@ class TestAsyncPubSubManager:
callback=None, callback=None,
) )
def test_handle_emit_with_namespace(self): async def test_handle_emit_with_namespace(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'emit' async_manager.AsyncManager, 'emit'
) as super_emit: ) as super_emit:
_run( await self.pm._handle_emit(
self.pm._handle_emit( {'event': 'foo', 'data': 'bar', 'namespace': '/baz'}
{'event': 'foo', 'data': 'bar', 'namespace': '/baz'}
)
) )
super_emit.assert_awaited_once_with( super_emit.assert_awaited_once_with(
'foo', 'foo',
@ -261,14 +256,12 @@ class TestAsyncPubSubManager:
callback=None, callback=None,
) )
def test_handle_emit_with_room(self): async def test_handle_emit_with_room(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'emit' async_manager.AsyncManager, 'emit'
) as super_emit: ) as super_emit:
_run( await self.pm._handle_emit(
self.pm._handle_emit( {'event': 'foo', 'data': 'bar', 'room': 'baz'}
{'event': 'foo', 'data': 'bar', 'room': 'baz'}
)
) )
super_emit.assert_awaited_once_with( super_emit.assert_awaited_once_with(
'foo', 'foo',
@ -279,14 +272,12 @@ class TestAsyncPubSubManager:
callback=None, callback=None,
) )
def test_handle_emit_with_skip_sid(self): async def test_handle_emit_with_skip_sid(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'emit' async_manager.AsyncManager, 'emit'
) as super_emit: ) as super_emit:
_run( await self.pm._handle_emit(
self.pm._handle_emit( {'event': 'foo', 'data': 'bar', 'skip_sid': '123'}
{'event': 'foo', 'data': 'bar', 'skip_sid': '123'}
)
) )
super_emit.assert_awaited_once_with( super_emit.assert_awaited_once_with(
'foo', 'foo',
@ -297,20 +288,18 @@ class TestAsyncPubSubManager:
callback=None, callback=None,
) )
def test_handle_emit_with_remote_callback(self): async def test_handle_emit_with_remote_callback(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'emit' async_manager.AsyncManager, 'emit'
) as super_emit: ) as super_emit:
_run( await self.pm._handle_emit(
self.pm._handle_emit( {
{ 'event': 'foo',
'event': 'foo', 'data': 'bar',
'data': 'bar', 'namespace': '/baz',
'namespace': '/baz', 'callback': ('sid', '/baz', 123),
'callback': ('sid', '/baz', 123), 'host_id': 'x',
'host_id': 'x', }
}
)
) )
assert super_emit.await_count == 1 assert super_emit.await_count == 1
assert super_emit.await_args[0] == ('foo', 'bar') assert super_emit.await_args[0] == ('foo', 'bar')
@ -320,7 +309,7 @@ class TestAsyncPubSubManager:
assert isinstance( assert isinstance(
super_emit.await_args[1]['callback'], functools.partial super_emit.await_args[1]['callback'], functools.partial
) )
_run(super_emit.await_args[1]['callback']('one', 2, 'three')) await super_emit.await_args[1]['callback']('one', 2, 'three')
self.pm._publish.assert_awaited_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'callback', 'method': 'callback',
@ -332,20 +321,18 @@ class TestAsyncPubSubManager:
} }
) )
def test_handle_emit_with_local_callback(self): async def test_handle_emit_with_local_callback(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'emit' async_manager.AsyncManager, 'emit'
) as super_emit: ) as super_emit:
_run( await self.pm._handle_emit(
self.pm._handle_emit( {
{ 'event': 'foo',
'event': 'foo', 'data': 'bar',
'data': 'bar', 'namespace': '/baz',
'namespace': '/baz', 'callback': ('sid', '/baz', 123),
'callback': ('sid', '/baz', 123), 'host_id': self.pm.host_id,
'host_id': self.pm.host_id, }
}
)
) )
assert super_emit.await_count == 1 assert super_emit.await_count == 1
assert super_emit.await_args[0] == ('foo', 'bar') assert super_emit.await_args[0] == ('foo', 'bar')
@ -355,163 +342,137 @@ class TestAsyncPubSubManager:
assert isinstance( assert isinstance(
super_emit.await_args[1]['callback'], functools.partial super_emit.await_args[1]['callback'], functools.partial
) )
_run(super_emit.await_args[1]['callback']('one', 2, 'three')) await super_emit.await_args[1]['callback']('one', 2, 'three')
self.pm._publish.assert_not_awaited() self.pm._publish.assert_not_awaited()
def test_handle_callback(self): async def test_handle_callback(self):
host_id = self.pm.host_id host_id = self.pm.host_id
with mock.patch.object( with mock.patch.object(
self.pm, 'trigger_callback' self.pm, 'trigger_callback'
) as trigger: ) as trigger:
_run( await self.pm._handle_callback(
self.pm._handle_callback( {
{ 'method': 'callback',
'method': 'callback', 'host_id': host_id,
'host_id': host_id, 'sid': 'sid',
'sid': 'sid', 'namespace': '/',
'namespace': '/', 'id': 123,
'id': 123, 'args': ('one', 2),
'args': ('one', 2), }
}
)
) )
trigger.assert_awaited_once_with('sid', 123, ('one', 2)) trigger.assert_awaited_once_with('sid', 123, ('one', 2))
def test_handle_callback_bad_host_id(self): async def test_handle_callback_bad_host_id(self):
with mock.patch.object( with mock.patch.object(
self.pm, 'trigger_callback' self.pm, 'trigger_callback'
) as trigger: ) as trigger:
_run( await self.pm._handle_callback(
self.pm._handle_callback( {
{ 'method': 'callback',
'method': 'callback', 'host_id': 'bad',
'host_id': 'bad', 'sid': 'sid',
'sid': 'sid', 'namespace': '/',
'namespace': '/', 'id': 123,
'id': 123, 'args': ('one', 2),
'args': ('one', 2), }
}
)
) )
assert trigger.await_count == 0 assert trigger.await_count == 0
def test_handle_callback_missing_args(self): async def test_handle_callback_missing_args(self):
host_id = self.pm.host_id host_id = self.pm.host_id
with mock.patch.object( with mock.patch.object(
self.pm, 'trigger_callback' self.pm, 'trigger_callback'
) as trigger: ) as trigger:
_run( await self.pm._handle_callback(
self.pm._handle_callback( {
{ 'method': 'callback',
'method': 'callback', 'host_id': host_id,
'host_id': host_id, 'sid': 'sid',
'sid': 'sid', 'namespace': '/',
'namespace': '/', 'id': 123,
'id': 123, }
}
)
) )
_run( await self.pm._handle_callback(
self.pm._handle_callback( {
{ 'method': 'callback',
'method': 'callback', 'host_id': host_id,
'host_id': host_id, 'sid': 'sid',
'sid': 'sid', 'namespace': '/',
'namespace': '/', }
}
)
) )
_run( await self.pm._handle_callback(
self.pm._handle_callback( {'method': 'callback', 'host_id': host_id, 'sid': 'sid'}
{'method': 'callback', 'host_id': host_id, 'sid': 'sid'}
)
) )
_run( await self.pm._handle_callback(
self.pm._handle_callback( {'method': 'callback', 'host_id': host_id}
{'method': 'callback', 'host_id': host_id}
)
) )
assert trigger.await_count == 0 assert trigger.await_count == 0
def test_handle_disconnect(self): async def test_handle_disconnect(self):
_run( await self.pm._handle_disconnect(
self.pm._handle_disconnect( {'method': 'disconnect', 'sid': '123', 'namespace': '/foo'}
{'method': 'disconnect', 'sid': '123', 'namespace': '/foo'}
)
) )
self.pm.server.disconnect.assert_awaited_once_with( self.pm.server.disconnect.assert_awaited_once_with(
sid='123', namespace='/foo', ignore_queue=True sid='123', namespace='/foo', ignore_queue=True
) )
def test_handle_enter_room(self): async def test_handle_enter_room(self):
sid = _run(self.pm.connect('123', '/')) sid = await self.pm.connect('123', '/')
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'enter_room' async_manager.AsyncManager, 'enter_room'
) as super_enter_room: ) as super_enter_room:
_run( await self.pm._handle_enter_room(
self.pm._handle_enter_room( {'method': 'enter_room', 'sid': sid, 'namespace': '/',
{'method': 'enter_room', 'sid': sid, 'namespace': '/', 'room': 'foo'}
'room': 'foo'}
)
) )
_run( await self.pm._handle_enter_room(
self.pm._handle_enter_room( {'method': 'enter_room', 'sid': '456', 'namespace': '/',
{'method': 'enter_room', 'sid': '456', 'namespace': '/', 'room': 'foo'}
'room': 'foo'}
)
) )
super_enter_room.assert_awaited_once_with(sid, '/', 'foo') super_enter_room.assert_awaited_once_with(sid, '/', 'foo')
def test_handle_leave_room(self): async def test_handle_leave_room(self):
sid = _run(self.pm.connect('123', '/')) sid = await self.pm.connect('123', '/')
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'leave_room' async_manager.AsyncManager, 'leave_room'
) as super_leave_room: ) as super_leave_room:
_run( await self.pm._handle_leave_room(
self.pm._handle_leave_room( {'method': 'leave_room', 'sid': sid, 'namespace': '/',
{'method': 'leave_room', 'sid': sid, 'namespace': '/', 'room': 'foo'}
'room': 'foo'}
)
) )
_run( await self.pm._handle_leave_room(
self.pm._handle_leave_room( {'method': 'leave_room', 'sid': '456', 'namespace': '/',
{'method': 'leave_room', 'sid': '456', 'namespace': '/', 'room': 'foo'}
'room': 'foo'}
)
) )
super_leave_room.assert_awaited_once_with(sid, '/', 'foo') super_leave_room.assert_awaited_once_with(sid, '/', 'foo')
def test_handle_close_room(self): async def test_handle_close_room(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'close_room' async_manager.AsyncManager, 'close_room'
) as super_close_room: ) as super_close_room:
_run( await self.pm._handle_close_room(
self.pm._handle_close_room( {'method': 'close_room', 'room': 'foo'}
{'method': 'close_room', 'room': 'foo'}
)
) )
super_close_room.assert_awaited_once_with( super_close_room.assert_awaited_once_with(
room='foo', namespace=None room='foo', namespace=None
) )
def test_handle_close_room_with_namespace(self): async def test_handle_close_room_with_namespace(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'close_room' async_manager.AsyncManager, 'close_room'
) as super_close_room: ) as super_close_room:
_run( await self.pm._handle_close_room(
self.pm._handle_close_room( {
{ 'method': 'close_room',
'method': 'close_room', 'room': 'foo',
'room': 'foo', 'namespace': '/bar',
'namespace': '/bar', }
}
)
) )
super_close_room.assert_awaited_once_with( super_close_room.assert_awaited_once_with(
room='foo', namespace='/bar' room='foo', namespace='/bar'
) )
def test_background_thread(self): async def test_background_thread(self):
self.pm._handle_emit = mock.AsyncMock() self.pm._handle_emit = mock.AsyncMock()
self.pm._handle_callback = mock.AsyncMock() self.pm._handle_callback = mock.AsyncMock()
self.pm._handle_disconnect = mock.AsyncMock() self.pm._handle_disconnect = mock.AsyncMock()
@ -548,7 +509,7 @@ class TestAsyncPubSubManager:
'host_id': host_id}) 'host_id': host_id})
self.pm._listen = messages self.pm._listen = messages
_run(self.pm._thread()) await self.pm._thread()
self.pm._handle_emit.assert_awaited_once_with( self.pm._handle_emit.assert_awaited_once_with(
{'method': 'emit', 'value': 'foo', 'host_id': 'x'} {'method': 'emit', 'value': 'foo', 'host_id': 'x'}
@ -575,7 +536,7 @@ class TestAsyncPubSubManager:
{'method': 'close_room', 'value': 'baz', 'host_id': 'x'} {'method': 'close_room', 'value': 'baz', 'host_id': 'x'}
) )
def test_background_thread_exception(self): async def test_background_thread_exception(self):
self.pm._handle_emit = mock.AsyncMock(side_effect=[ self.pm._handle_emit = mock.AsyncMock(side_effect=[
ValueError(), asyncio.CancelledError]) ValueError(), asyncio.CancelledError])
@ -584,7 +545,7 @@ class TestAsyncPubSubManager:
yield {'method': 'emit', 'value': 'bar', 'host_id': 'x'} yield {'method': 'emit', 'value': 'bar', 'host_id': 'x'}
self.pm._listen = messages self.pm._listen = messages
_run(self.pm._thread()) await self.pm._thread()
self.pm._handle_emit.assert_any_await( self.pm._handle_emit.assert_any_await(
{'method': 'emit', 'value': 'foo', 'host_id': 'x'} {'method': 'emit', 'value': 'foo', 'host_id': 'x'}

582
tests/async/test_server.py

File diff suppressed because it is too large

69
tests/async/test_simple_client.py

@ -4,11 +4,10 @@ import pytest
from socketio import AsyncSimpleClient from socketio import AsyncSimpleClient
from socketio.exceptions import SocketIOError, TimeoutError, DisconnectedError from socketio.exceptions import SocketIOError, TimeoutError, DisconnectedError
from .helpers import _run
class TestAsyncAsyncSimpleClient: class TestAsyncAsyncSimpleClient:
def test_constructor(self): async def test_constructor(self):
client = AsyncSimpleClient(1, '2', a='3', b=4) client = AsyncSimpleClient(1, '2', a='3', b=4)
assert client.client_args == (1, '2') assert client.client_args == (1, '2')
assert client.client_kwargs == {'a': '3', 'b': 4} assert client.client_kwargs == {'a': '3', 'b': 4}
@ -16,15 +15,15 @@ class TestAsyncAsyncSimpleClient:
assert client.input_buffer == [] assert client.input_buffer == []
assert not client.connected assert not client.connected
def test_connect(self): async def test_connect(self):
client = AsyncSimpleClient(123, a='b') client = AsyncSimpleClient(123, a='b')
with mock.patch('socketio.async_simple_client.AsyncClient') \ with mock.patch('socketio.async_simple_client.AsyncClient') \
as mock_client: as mock_client:
mock_client.return_value.connect = mock.AsyncMock() mock_client.return_value.connect = mock.AsyncMock()
_run(client.connect('url', headers='h', auth='a', transports='t', await client.connect('url', headers='h', auth='a', transports='t',
namespace='n', socketio_path='s', namespace='n', socketio_path='s',
wait_timeout='w')) wait_timeout='w')
mock_client.assert_called_once_with(123, a='b') mock_client.assert_called_once_with(123, a='b')
assert client.client == mock_client() assert client.client == mock_client()
mock_client().connect.assert_awaited_once_with( mock_client().connect.assert_awaited_once_with(
@ -35,7 +34,7 @@ class TestAsyncAsyncSimpleClient:
assert client.namespace == 'n' assert client.namespace == 'n'
assert not client.input_event.is_set() assert not client.input_event.is_set()
def test_connect_context_manager(self): async def test_connect_context_manager(self):
async def _t(): async def _t():
async with AsyncSimpleClient(123, a='b') as client: async with AsyncSimpleClient(123, a='b') as client:
with mock.patch('socketio.async_simple_client.AsyncClient') \ with mock.patch('socketio.async_simple_client.AsyncClient') \
@ -56,17 +55,17 @@ class TestAsyncAsyncSimpleClient:
assert client.namespace == 'n' assert client.namespace == 'n'
assert not client.input_event.is_set() assert not client.input_event.is_set()
_run(_t()) await _t()
def test_connect_twice(self): async def test_connect_twice(self):
client = AsyncSimpleClient(123, a='b') client = AsyncSimpleClient(123, a='b')
client.client = mock.MagicMock() client.client = mock.MagicMock()
client.connected = True client.connected = True
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
_run(client.connect('url')) await client.connect('url')
def test_properties(self): async def test_properties(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
client.client = mock.MagicMock(transport='websocket') client.client = mock.MagicMock(transport='websocket')
client.client.get_sid.return_value = 'sid' client.client.get_sid.return_value = 'sid'
@ -76,7 +75,7 @@ class TestAsyncAsyncSimpleClient:
assert client.sid == 'sid' assert client.sid == 'sid'
assert client.transport == 'websocket' assert client.transport == 'websocket'
def test_emit(self): async def test_emit(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
client.client = mock.MagicMock() client.client = mock.MagicMock()
client.client.emit = mock.AsyncMock() client.client.emit = mock.AsyncMock()
@ -84,18 +83,18 @@ class TestAsyncAsyncSimpleClient:
client.connected_event.set() client.connected_event.set()
client.connected = True client.connected = True
_run(client.emit('foo', 'bar')) await client.emit('foo', 'bar')
client.client.emit.assert_awaited_once_with('foo', 'bar', client.client.emit.assert_awaited_once_with('foo', 'bar',
namespace='/ns') namespace='/ns')
def test_emit_disconnected(self): async def test_emit_disconnected(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
client.connected_event.set() client.connected_event.set()
client.connected = False client.connected = False
with pytest.raises(DisconnectedError): with pytest.raises(DisconnectedError):
_run(client.emit('foo', 'bar')) await client.emit('foo', 'bar')
def test_emit_retries(self): async def test_emit_retries(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
client.connected_event.set() client.connected_event.set()
client.connected = True client.connected = True
@ -103,10 +102,10 @@ class TestAsyncAsyncSimpleClient:
client.client.emit = mock.AsyncMock() client.client.emit = mock.AsyncMock()
client.client.emit.side_effect = [SocketIOError(), None] client.client.emit.side_effect = [SocketIOError(), None]
_run(client.emit('foo', 'bar')) await client.emit('foo', 'bar')
client.client.emit.assert_awaited_with('foo', 'bar', namespace='/') client.client.emit.assert_awaited_with('foo', 'bar', namespace='/')
def test_call(self): async def test_call(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
client.client = mock.MagicMock() client.client = mock.MagicMock()
client.client.call = mock.AsyncMock() client.client.call = mock.AsyncMock()
@ -115,18 +114,18 @@ class TestAsyncAsyncSimpleClient:
client.connected_event.set() client.connected_event.set()
client.connected = True client.connected = True
assert _run(client.call('foo', 'bar')) == 'result' assert await client.call('foo', 'bar') == 'result'
client.client.call.assert_awaited_once_with( client.client.call.assert_awaited_once_with(
'foo', 'bar', namespace='/ns', timeout=60) 'foo', 'bar', namespace='/ns', timeout=60)
def test_call_disconnected(self): async def test_call_disconnected(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
client.connected_event.set() client.connected_event.set()
client.connected = False client.connected = False
with pytest.raises(DisconnectedError): with pytest.raises(DisconnectedError):
_run(client.call('foo', 'bar')) await client.call('foo', 'bar')
def test_call_retries(self): async def test_call_retries(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
client.connected_event.set() client.connected_event.set()
client.connected = True client.connected = True
@ -134,17 +133,17 @@ class TestAsyncAsyncSimpleClient:
client.client.call = mock.AsyncMock() client.client.call = mock.AsyncMock()
client.client.call.side_effect = [SocketIOError(), 'result'] client.client.call.side_effect = [SocketIOError(), 'result']
assert _run(client.call('foo', 'bar')) == 'result' assert await client.call('foo', 'bar') == 'result'
client.client.call.assert_awaited_with('foo', 'bar', namespace='/', client.client.call.assert_awaited_with('foo', 'bar', namespace='/',
timeout=60) timeout=60)
def test_receive_with_input_buffer(self): async def test_receive_with_input_buffer(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
client.input_buffer = ['foo', 'bar'] client.input_buffer = ['foo', 'bar']
assert _run(client.receive()) == 'foo' assert await client.receive() == 'foo'
assert _run(client.receive()) == 'bar' assert await client.receive() == 'bar'
def test_receive_without_input_buffer(self): async def test_receive_without_input_buffer(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
client.connected_event.set() client.connected_event.set()
client.connected = True client.connected = True
@ -155,9 +154,9 @@ class TestAsyncAsyncSimpleClient:
return True return True
client.input_event.wait = fake_wait client.input_event.wait = fake_wait
assert _run(client.receive()) == 'foo' assert await client.receive() == 'foo'
def test_receive_with_timeout(self): async def test_receive_with_timeout(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
client.connected_event.set() client.connected_event.set()
client.connected = True client.connected = True
@ -168,22 +167,22 @@ class TestAsyncAsyncSimpleClient:
client.input_event.wait = fake_wait client.input_event.wait = fake_wait
with pytest.raises(TimeoutError): with pytest.raises(TimeoutError):
_run(client.receive(timeout=0.01)) await client.receive(timeout=0.01)
def test_receive_disconnected(self): async def test_receive_disconnected(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
client.connected_event.set() client.connected_event.set()
client.connected = False client.connected = False
with pytest.raises(DisconnectedError): with pytest.raises(DisconnectedError):
_run(client.receive()) await client.receive()
def test_disconnect(self): async def test_disconnect(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
mc = mock.MagicMock() mc = mock.MagicMock()
mc.disconnect = mock.AsyncMock() mc.disconnect = mock.AsyncMock()
client.client = mc client.client = mc
client.connected = True client.connected = True
_run(client.disconnect()) await client.disconnect()
_run(client.disconnect()) await client.disconnect()
mc.disconnect.assert_awaited_once_with() mc.disconnect.assert_awaited_once_with()
assert client.client is None assert client.client is None

1
tox.ini

@ -24,6 +24,7 @@ deps=
aiohttp aiohttp
msgpack msgpack
pytest pytest
pytest-asyncio
pytest-timeout pytest-timeout
pytest-cov pytest-cov

Loading…
Cancel
Save