Browse Source

Adopted unittest.mock.AsyncMock in async unit tests

pull/1409/head
Miguel Grinberg 4 months ago
parent
commit
8f0e66c1cd
Failed to extract signature
  1. 12
      tests/async/helpers.py
  2. 3
      tests/async/test_admin.py
  3. 455
      tests/async/test_client.py
  4. 110
      tests/async/test_manager.py
  5. 78
      tests/async/test_namespace.py
  6. 172
      tests/async/test_pubsub_manager.py
  7. 240
      tests/async/test_server.py
  8. 40
      tests/async/test_simple_client.py

12
tests/async/helpers.py

@ -1,16 +1,4 @@
import asyncio import asyncio
from unittest import mock
def AsyncMock(*args, **kwargs):
"""Return a mock asynchronous function."""
m = mock.MagicMock(*args, **kwargs)
async def mock_coro(*args, **kwargs):
return m(*args, **kwargs)
mock_coro.mock = m
return mock_coro
def _run(coro): def _run(coro):

3
tests/async/test_admin.py

@ -10,7 +10,6 @@ except ImportError:
import socketio import socketio
from socketio.exceptions import ConnectionError from socketio.exceptions import ConnectionError
from tests.asyncio_web_server import SocketIOWebServer from tests.asyncio_web_server import SocketIOWebServer
from .helpers import AsyncMock
def with_instrumented_server(auth=False, **ikwargs): def with_instrumented_server(auth=False, **ikwargs):
@ -211,7 +210,7 @@ class TestAsyncAdmin:
sid1 = client1.sid sid1 = client1.sid
saved_check_for_upgrade = self.isvr._check_for_upgrade saved_check_for_upgrade = self.isvr._check_for_upgrade
self.isvr._check_for_upgrade = AsyncMock() self.isvr._check_for_upgrade = mock.AsyncMock()
client2.connect('http://localhost:8900', namespace='/foo', client2.connect('http://localhost:8900', namespace='/foo',
transports=['polling']) transports=['polling'])
sid2 = client2.sid sid2 = client2.sid

455
tests/async/test_client.py

File diff suppressed because it is too large

110
tests/async/test_manager.py

@ -2,7 +2,7 @@ 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 AsyncMock, _run from .helpers import _run
class TestAsyncManager: class TestAsyncManager:
@ -15,8 +15,8 @@ class TestAsyncManager:
return str(id) return str(id)
mock_server = mock.MagicMock() mock_server = mock.MagicMock()
mock_server._send_packet = AsyncMock() mock_server._send_packet = mock.AsyncMock()
mock_server._send_eio_packet = AsyncMock() mock_server._send_eio_packet = mock.AsyncMock()
mock_server.eio.generate_id = generate_id mock_server.eio.generate_id = generate_id
mock_server.packet_class = packet.Packet mock_server.packet_class = packet.Packet
self.bm = async_manager.AsyncManager() self.bm = async_manager.AsyncManager()
@ -128,14 +128,14 @@ class TestAsyncManager:
def test_trigger_async_callback(self): def test_trigger_async_callback(self):
sid1 = _run(self.bm.connect('123', '/')) sid1 = _run(self.bm.connect('123', '/'))
sid2 = _run(self.bm.connect('123', '/foo')) sid2 = _run(self.bm.connect('123', '/foo'))
cb = 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'])) _run(self.bm.trigger_callback(sid1, id1, ['foo']))
_run(self.bm.trigger_callback(sid2, id2, ['bar', 'baz'])) _run(self.bm.trigger_callback(sid2, id2, ['bar', 'baz']))
assert cb.mock.call_count == 2 assert cb.await_count == 2
cb.mock.assert_any_call('foo') cb.assert_any_await('foo')
cb.mock.assert_any_call('bar', 'baz') cb.assert_any_await('bar', 'baz')
def test_invalid_callback(self): def test_invalid_callback(self):
sid = _run(self.bm.connect('123', '/')) sid = _run(self.bm.connect('123', '/'))
@ -145,7 +145,7 @@ class TestAsyncManager:
# these should not raise an exception # these should not raise an exception
_run(self.bm.trigger_callback('xxx', id, ['foo'])) _run(self.bm.trigger_callback('xxx', id, ['foo']))
_run(self.bm.trigger_callback(sid, id + 1, ['foo'])) _run(self.bm.trigger_callback(sid, id + 1, ['foo']))
assert cb.mock.call_count == 0 assert cb.call_count == 0
def test_get_namespaces(self): def test_get_namespaces(self):
assert list(self.bm.get_namespaces()) == [] assert list(self.bm.get_namespaces()) == []
@ -207,10 +207,10 @@ class TestAsyncManager:
'my event', {'foo': 'bar'}, namespace='/foo', to=sid 'my event', {'foo': 'bar'}, namespace='/foo', to=sid
) )
) )
assert self.bm.server._send_eio_packet.mock.call_count == 1 assert self.bm.server._send_eio_packet.await_count == 1
assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
== '123' == '123'
pkt = self.bm.server._send_eio_packet.mock.call_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): def test_emit_to_room(self):
@ -224,13 +224,13 @@ class TestAsyncManager:
'my event', {'foo': 'bar'}, namespace='/foo', room='bar' 'my event', {'foo': 'bar'}, namespace='/foo', room='bar'
) )
) )
assert self.bm.server._send_eio_packet.mock.call_count == 2 assert self.bm.server._send_eio_packet.await_count == 2
assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
== '123' == '123'
assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[1][0][0] \
== '456' == '456'
pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1]
assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][1] \ assert self.bm.server._send_eio_packet.await_args_list[1][0][1] \
== pkt == pkt
assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]'
@ -246,17 +246,17 @@ class TestAsyncManager:
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.mock.call_count == 3 assert self.bm.server._send_eio_packet.await_count == 3
assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
== '123' == '123'
assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[1][0][0] \
== '456' == '456'
assert self.bm.server._send_eio_packet.mock.call_args_list[2][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[2][0][0] \
== '789' == '789'
pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1]
assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][1] \ assert self.bm.server._send_eio_packet.await_args_list[1][0][1] \
== pkt == pkt
assert self.bm.server._send_eio_packet.mock.call_args_list[2][0][1] \ assert self.bm.server._send_eio_packet.await_args_list[2][0][1] \
== pkt == pkt
assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]'
@ -268,17 +268,17 @@ class TestAsyncManager:
_run(self.bm.connect('789', '/foo')) _run(self.bm.connect('789', '/foo'))
_run(self.bm.connect('abc', '/bar')) _run(self.bm.connect('abc', '/bar'))
_run(self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo')) _run(self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo'))
assert self.bm.server._send_eio_packet.mock.call_count == 3 assert self.bm.server._send_eio_packet.await_count == 3
assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
== '123' == '123'
assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[1][0][0] \
== '456' == '456'
assert self.bm.server._send_eio_packet.mock.call_args_list[2][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[2][0][0] \
== '789' == '789'
pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1]
assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][1] \ assert self.bm.server._send_eio_packet.await_args_list[1][0][1] \
== pkt == pkt
assert self.bm.server._send_eio_packet.mock.call_args_list[2][0][1] \ assert self.bm.server._send_eio_packet.await_args_list[2][0][1] \
== pkt == pkt
assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]'
@ -294,13 +294,13 @@ class TestAsyncManager:
'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.mock.call_count == 2 assert self.bm.server._send_eio_packet.await_count == 2
assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
== '123' == '123'
assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[1][0][0] \
== '789' == '789'
pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1]
assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][1] \ assert self.bm.server._send_eio_packet.await_args_list[1][0][1] \
== pkt == pkt
assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]'
@ -319,10 +319,10 @@ class TestAsyncManager:
skip_sid=[sid1, sid3], skip_sid=[sid1, sid3],
) )
) )
assert self.bm.server._send_eio_packet.mock.call_count == 1 assert self.bm.server._send_eio_packet.await_count == 1
assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
== '456' == '456'
pkt = self.bm.server._send_eio_packet.mock.call_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): def test_emit_with_callback(self):
@ -335,10 +335,10 @@ class TestAsyncManager:
) )
) )
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.mock.call_count == 1 assert self.bm.server._send_packet.await_count == 1
assert self.bm.server._send_packet.mock.call_args_list[0][0][0] \ assert self.bm.server._send_packet.await_args_list[0][0][0] \
== '123' == '123'
pkt = self.bm.server._send_packet.mock.call_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): def test_emit_to_invalid_room(self):
@ -356,10 +356,10 @@ class TestAsyncManager:
'my event', ('foo', 'bar'), namespace='/foo', room=sid 'my event', ('foo', 'bar'), namespace='/foo', room=sid
) )
) )
assert self.bm.server._send_eio_packet.mock.call_count == 1 assert self.bm.server._send_eio_packet.await_count == 1
assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
== '123' == '123'
pkt = self.bm.server._send_eio_packet.mock.call_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): def test_emit_with_list(self):
@ -369,10 +369,10 @@ class TestAsyncManager:
'my event', ['foo', 'bar'], namespace='/foo', room=sid 'my event', ['foo', 'bar'], namespace='/foo', room=sid
) )
) )
assert self.bm.server._send_eio_packet.mock.call_count == 1 assert self.bm.server._send_eio_packet.await_count == 1
assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
== '123' == '123'
pkt = self.bm.server._send_eio_packet.mock.call_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): def test_emit_with_none(self):
@ -382,10 +382,10 @@ class TestAsyncManager:
'my event', None, namespace='/foo', room=sid 'my event', None, namespace='/foo', room=sid
) )
) )
assert self.bm.server._send_eio_packet.mock.call_count == 1 assert self.bm.server._send_eio_packet.await_count == 1
assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
== '123' == '123'
pkt = self.bm.server._send_eio_packet.mock.call_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): def test_emit_binary(self):
@ -395,12 +395,12 @@ class TestAsyncManager:
u'my event', b'my binary data', namespace='/', room=sid u'my event', b'my binary data', namespace='/', room=sid
) )
) )
assert self.bm.server._send_eio_packet.mock.call_count == 2 assert self.bm.server._send_eio_packet.await_count == 2
assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \
== '123' == '123'
pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1]
assert pkt.encode() == '451-["my event",{"_placeholder":true,"num":0}]' assert pkt.encode() == '451-["my event",{"_placeholder":true,"num":0}]'
assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][0] \ assert self.bm.server._send_eio_packet.await_args_list[1][0][0] \
== '123' == '123'
pkt = self.bm.server._send_eio_packet.mock.call_args_list[1][0][1] pkt = self.bm.server._send_eio_packet.await_args_list[1][0][1]
assert pkt.encode() == b'my binary data' assert pkt.encode() == b'my binary data'

78
tests/async/test_namespace.py

@ -1,7 +1,7 @@
from unittest import mock from unittest import mock
from socketio import async_namespace from socketio import async_namespace
from .helpers import AsyncMock, _run from .helpers import _run
class TestAsyncNamespace: class TestAsyncNamespace:
@ -70,14 +70,14 @@ class TestAsyncNamespace:
def test_emit(self): 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 = AsyncMock() mock_server.emit = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run( _run(
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.mock.assert_called_with( ns.server.emit.assert_awaited_with(
'ev', 'ev',
data='data', data='data',
to='room', to='room',
@ -98,7 +98,7 @@ class TestAsyncNamespace:
ignore_queue=True, ignore_queue=True,
) )
) )
ns.server.emit.mock.assert_called_with( ns.server.emit.assert_awaited_with(
'ev', 'ev',
data='data', data='data',
to=None, to=None,
@ -112,10 +112,10 @@ class TestAsyncNamespace:
def test_send(self): 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 = 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')) _run(ns.send(data='data', to='room', skip_sid='skip', callback='cb'))
ns.server.send.mock.assert_called_with( ns.server.send.assert_awaited_with(
'data', 'data',
to='room', to='room',
room=None, room=None,
@ -134,7 +134,7 @@ class TestAsyncNamespace:
ignore_queue=True, ignore_queue=True,
) )
) )
ns.server.send.mock.assert_called_with( ns.server.send.assert_awaited_with(
'data', 'data',
to=None, to=None,
room='room', room='room',
@ -147,10 +147,10 @@ class TestAsyncNamespace:
def test_call(self): 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 = AsyncMock() mock_server.call = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run(ns.call('ev', data='data', to='sid')) _run(ns.call('ev', data='data', to='sid'))
ns.server.call.mock.assert_called_with( ns.server.call.assert_awaited_with(
'ev', 'ev',
data='data', data='data',
to='sid', to='sid',
@ -161,7 +161,7 @@ class TestAsyncNamespace:
) )
_run(ns.call('ev', data='data', sid='sid', namespace='/bar', _run(ns.call('ev', data='data', sid='sid', namespace='/bar',
timeout=45, ignore_queue=True)) timeout=45, ignore_queue=True))
ns.server.call.mock.assert_called_with( ns.server.call.assert_awaited_with(
'ev', 'ev',
data='data', data='data',
to=None, to=None,
@ -174,40 +174,40 @@ class TestAsyncNamespace:
def test_enter_room(self): 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 = AsyncMock() mock_server.enter_room = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run(ns.enter_room('sid', 'room')) _run(ns.enter_room('sid', 'room'))
ns.server.enter_room.mock.assert_called_with( ns.server.enter_room.assert_awaited_with(
'sid', 'room', namespace='/foo' 'sid', 'room', namespace='/foo'
) )
_run(ns.enter_room('sid', 'room', namespace='/bar')) _run(ns.enter_room('sid', 'room', namespace='/bar'))
ns.server.enter_room.mock.assert_called_with( ns.server.enter_room.assert_awaited_with(
'sid', 'room', namespace='/bar' 'sid', 'room', namespace='/bar'
) )
def test_leave_room(self): 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 = AsyncMock() mock_server.leave_room = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run(ns.leave_room('sid', 'room')) _run(ns.leave_room('sid', 'room'))
ns.server.leave_room.mock.assert_called_with( ns.server.leave_room.assert_awaited_with(
'sid', 'room', namespace='/foo' 'sid', 'room', namespace='/foo'
) )
_run(ns.leave_room('sid', 'room', namespace='/bar')) _run(ns.leave_room('sid', 'room', namespace='/bar'))
ns.server.leave_room.mock.assert_called_with( ns.server.leave_room.assert_awaited_with(
'sid', 'room', namespace='/bar' 'sid', 'room', namespace='/bar'
) )
def test_close_room(self): 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 = AsyncMock() mock_server.close_room = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run(ns.close_room('room')) _run(ns.close_room('room'))
ns.server.close_room.mock.assert_called_with('room', namespace='/foo') ns.server.close_room.assert_awaited_with('room', namespace='/foo')
_run(ns.close_room('room', namespace='/bar')) _run(ns.close_room('room', namespace='/bar'))
ns.server.close_room.mock.assert_called_with('room', namespace='/bar') ns.server.close_room.assert_awaited_with('room', namespace='/bar')
def test_rooms(self): def test_rooms(self):
ns = async_namespace.AsyncNamespace('/foo') ns = async_namespace.AsyncNamespace('/foo')
@ -220,19 +220,19 @@ class TestAsyncNamespace:
def test_session(self): 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 = AsyncMock() mock_server.get_session = mock.AsyncMock()
mock_server.save_session = AsyncMock() mock_server.save_session = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run(ns.get_session('sid')) _run(ns.get_session('sid'))
ns.server.get_session.mock.assert_called_with('sid', namespace='/foo') ns.server.get_session.assert_awaited_with('sid', namespace='/foo')
_run(ns.get_session('sid', namespace='/bar')) _run(ns.get_session('sid', namespace='/bar'))
ns.server.get_session.mock.assert_called_with('sid', namespace='/bar') ns.server.get_session.assert_awaited_with('sid', namespace='/bar')
_run(ns.save_session('sid', {'a': 'b'})) _run(ns.save_session('sid', {'a': 'b'}))
ns.server.save_session.mock.assert_called_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')) _run(ns.save_session('sid', {'a': 'b'}, namespace='/bar'))
ns.server.save_session.mock.assert_called_with( ns.server.save_session.assert_awaited_with(
'sid', {'a': 'b'}, namespace='/bar' 'sid', {'a': 'b'}, namespace='/bar'
) )
ns.session('sid') ns.session('sid')
@ -243,12 +243,12 @@ class TestAsyncNamespace:
def test_disconnect(self): 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 = AsyncMock() mock_server.disconnect = mock.AsyncMock()
ns._set_server(mock_server) ns._set_server(mock_server)
_run(ns.disconnect('sid')) _run(ns.disconnect('sid'))
ns.server.disconnect.mock.assert_called_with('sid', namespace='/foo') ns.server.disconnect.assert_awaited_with('sid', namespace='/foo')
_run(ns.disconnect('sid', namespace='/bar')) _run(ns.disconnect('sid', namespace='/bar'))
ns.server.disconnect.mock.assert_called_with('sid', namespace='/bar') ns.server.disconnect.assert_awaited_with('sid', namespace='/bar')
def test_sync_event_client(self): def test_sync_event_client(self):
result = {} result = {}
@ -291,49 +291,49 @@ class TestAsyncNamespace:
def test_emit_client(self): 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 = AsyncMock() mock_client.emit = mock.AsyncMock()
ns._set_client(mock_client) ns._set_client(mock_client)
_run(ns.emit('ev', data='data', callback='cb')) _run(ns.emit('ev', data='data', callback='cb'))
ns.client.emit.mock.assert_called_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')) _run(ns.emit('ev', data='data', namespace='/bar', callback='cb'))
ns.client.emit.mock.assert_called_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): 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 = AsyncMock() mock_client.send = mock.AsyncMock()
ns._set_client(mock_client) ns._set_client(mock_client)
_run(ns.send(data='data', callback='cb')) _run(ns.send(data='data', callback='cb'))
ns.client.send.mock.assert_called_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')) _run(ns.send(data='data', namespace='/bar', callback='cb'))
ns.client.send.mock.assert_called_with( ns.client.send.assert_awaited_with(
'data', namespace='/bar', callback='cb' 'data', namespace='/bar', callback='cb'
) )
def test_call_client(self): 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 = AsyncMock() mock_client.call = mock.AsyncMock()
ns._set_client(mock_client) ns._set_client(mock_client)
_run(ns.call('ev', data='data')) _run(ns.call('ev', data='data'))
ns.client.call.mock.assert_called_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)) _run(ns.call('ev', data='data', namespace='/bar', timeout=45))
ns.client.call.mock.assert_called_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): 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 = AsyncMock() mock_client.disconnect = mock.AsyncMock()
ns._set_client(mock_client) ns._set_client(mock_client)
_run(ns.disconnect()) _run(ns.disconnect())
ns.client.disconnect.mock.assert_called_with() ns.client.disconnect.assert_awaited_with()

172
tests/async/test_pubsub_manager.py

@ -7,7 +7,7 @@ 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 AsyncMock, _run from .helpers import _run
class TestAsyncPubSubManager: class TestAsyncPubSubManager:
@ -22,11 +22,11 @@ class TestAsyncPubSubManager:
mock_server = mock.MagicMock() mock_server = mock.MagicMock()
mock_server.eio.generate_id = generate_id mock_server.eio.generate_id = generate_id
mock_server.packet_class = packet.Packet mock_server.packet_class = packet.Packet
mock_server._send_packet = AsyncMock() mock_server._send_packet = mock.AsyncMock()
mock_server._send_eio_packet = AsyncMock() mock_server._send_eio_packet = mock.AsyncMock()
mock_server.disconnect = AsyncMock() mock_server.disconnect = mock.AsyncMock()
self.pm = async_pubsub_manager.AsyncPubSubManager() self.pm = async_pubsub_manager.AsyncPubSubManager()
self.pm._publish = AsyncMock() self.pm._publish = mock.AsyncMock()
self.pm.set_server(mock_server) self.pm.set_server(mock_server)
self.pm.host_id = '123456' self.pm.host_id = '123456'
self.pm.initialize() self.pm.initialize()
@ -53,7 +53,7 @@ class TestAsyncPubSubManager:
def test_emit(self): def test_emit(self):
_run(self.pm.emit('foo', 'bar')) _run(self.pm.emit('foo', 'bar'))
self.pm._publish.mock.assert_called_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'emit', 'method': 'emit',
'event': 'foo', 'event': 'foo',
@ -69,7 +69,7 @@ class TestAsyncPubSubManager:
def test_emit_with_to(self): def test_emit_with_to(self):
sid = 'room-mate' sid = 'room-mate'
_run(self.pm.emit('foo', 'bar', to=sid)) _run(self.pm.emit('foo', 'bar', to=sid))
self.pm._publish.mock.assert_called_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'emit', 'method': 'emit',
'event': 'foo', 'event': 'foo',
@ -84,7 +84,7 @@ class TestAsyncPubSubManager:
def test_emit_with_namespace(self): def test_emit_with_namespace(self):
_run(self.pm.emit('foo', 'bar', namespace='/baz')) _run(self.pm.emit('foo', 'bar', namespace='/baz'))
self.pm._publish.mock.assert_called_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'emit', 'method': 'emit',
'event': 'foo', 'event': 'foo',
@ -99,7 +99,7 @@ class TestAsyncPubSubManager:
def test_emit_with_room(self): def test_emit_with_room(self):
_run(self.pm.emit('foo', 'bar', room='baz')) _run(self.pm.emit('foo', 'bar', room='baz'))
self.pm._publish.mock.assert_called_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'emit', 'method': 'emit',
'event': 'foo', 'event': 'foo',
@ -114,7 +114,7 @@ class TestAsyncPubSubManager:
def test_emit_with_skip_sid(self): def test_emit_with_skip_sid(self):
_run(self.pm.emit('foo', 'bar', skip_sid='baz')) _run(self.pm.emit('foo', 'bar', skip_sid='baz'))
self.pm._publish.mock.assert_called_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'emit', 'method': 'emit',
'event': 'foo', 'event': 'foo',
@ -132,7 +132,7 @@ class TestAsyncPubSubManager:
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')) _run(self.pm.emit('foo', 'bar', room='baz', callback='cb'))
self.pm._publish.mock.assert_called_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'emit', 'method': 'emit',
'event': 'foo', 'event': 'foo',
@ -164,25 +164,25 @@ class TestAsyncPubSubManager:
'foo', 'bar', room=sid, namespace='/', ignore_queue=True 'foo', 'bar', room=sid, namespace='/', ignore_queue=True
) )
) )
self.pm._publish.mock.assert_not_called() self.pm._publish.assert_not_awaited()
assert self.pm.server._send_eio_packet.mock.call_count == 1 assert self.pm.server._send_eio_packet.await_count == 1
assert self.pm.server._send_eio_packet.mock.call_args_list[0][0][0] \ assert self.pm.server._send_eio_packet.await_args_list[0][0][0] \
== '123' == '123'
pkt = self.pm.server._send_eio_packet.mock.call_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): def test_can_disconnect(self):
sid = _run(self.pm.connect('123', '/')) sid = _run(self.pm.connect('123', '/'))
assert _run(self.pm.can_disconnect(sid, '/')) is True assert _run(self.pm.can_disconnect(sid, '/')) is True
_run(self.pm.can_disconnect(sid, '/foo')) _run(self.pm.can_disconnect(sid, '/foo'))
self.pm._publish.mock.assert_called_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): def test_disconnect(self):
_run(self.pm.disconnect('foo', '/')) _run(self.pm.disconnect('foo', '/'))
self.pm._publish.mock.assert_called_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'}
) )
@ -191,7 +191,7 @@ class TestAsyncPubSubManager:
sid = _run(self.pm.connect('123', '/')) sid = _run(self.pm.connect('123', '/'))
self.pm.pre_disconnect(sid, '/') self.pm.pre_disconnect(sid, '/')
_run(self.pm.disconnect(sid, '/', ignore_queue=True)) _run(self.pm.disconnect(sid, '/', ignore_queue=True))
self.pm._publish.mock.assert_not_called() 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): def test_enter_room(self):
@ -200,7 +200,7 @@ class TestAsyncPubSubManager:
_run(self.pm.enter_room('456', '/', 'foo')) _run(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.mock.assert_called_once_with( self.pm._publish.assert_awaited_once_with(
{'method': 'enter_room', 'sid': '456', 'room': 'foo', {'method': 'enter_room', 'sid': '456', 'room': 'foo',
'namespace': '/', 'host_id': '123456'} 'namespace': '/', 'host_id': '123456'}
) )
@ -210,32 +210,31 @@ class TestAsyncPubSubManager:
_run(self.pm.leave_room(sid, '/', 'foo')) _run(self.pm.leave_room(sid, '/', 'foo'))
_run(self.pm.leave_room('456', '/', 'foo')) _run(self.pm.leave_room('456', '/', 'foo'))
assert 'foo' not in self.pm.rooms['/'] assert 'foo' not in self.pm.rooms['/']
self.pm._publish.mock.assert_called_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): def test_close_room(self):
_run(self.pm.close_room('foo')) _run(self.pm.close_room('foo'))
self.pm._publish.mock.assert_called_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): def test_close_room_with_namespace(self):
_run(self.pm.close_room('foo', '/bar')) _run(self.pm.close_room('foo', '/bar'))
self.pm._publish.mock.assert_called_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): def test_handle_emit(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'emit', new=AsyncMock() async_manager.AsyncManager, 'emit'
) as super_emit: ) as super_emit:
_run(self.pm._handle_emit({'event': 'foo', 'data': 'bar'})) _run(self.pm._handle_emit({'event': 'foo', 'data': 'bar'}))
super_emit.mock.assert_called_once_with( super_emit.assert_awaited_once_with(
self.pm,
'foo', 'foo',
'bar', 'bar',
namespace=None, namespace=None,
@ -246,15 +245,14 @@ class TestAsyncPubSubManager:
def test_handle_emit_with_namespace(self): def test_handle_emit_with_namespace(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'emit', new=AsyncMock() async_manager.AsyncManager, 'emit'
) as super_emit: ) as super_emit:
_run( _run(
self.pm._handle_emit( self.pm._handle_emit(
{'event': 'foo', 'data': 'bar', 'namespace': '/baz'} {'event': 'foo', 'data': 'bar', 'namespace': '/baz'}
) )
) )
super_emit.mock.assert_called_once_with( super_emit.assert_awaited_once_with(
self.pm,
'foo', 'foo',
'bar', 'bar',
namespace='/baz', namespace='/baz',
@ -265,15 +263,14 @@ class TestAsyncPubSubManager:
def test_handle_emit_with_room(self): def test_handle_emit_with_room(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'emit', new=AsyncMock() async_manager.AsyncManager, 'emit'
) as super_emit: ) as super_emit:
_run( _run(
self.pm._handle_emit( self.pm._handle_emit(
{'event': 'foo', 'data': 'bar', 'room': 'baz'} {'event': 'foo', 'data': 'bar', 'room': 'baz'}
) )
) )
super_emit.mock.assert_called_once_with( super_emit.assert_awaited_once_with(
self.pm,
'foo', 'foo',
'bar', 'bar',
namespace=None, namespace=None,
@ -284,15 +281,14 @@ class TestAsyncPubSubManager:
def test_handle_emit_with_skip_sid(self): def test_handle_emit_with_skip_sid(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'emit', new=AsyncMock() async_manager.AsyncManager, 'emit'
) as super_emit: ) as super_emit:
_run( _run(
self.pm._handle_emit( self.pm._handle_emit(
{'event': 'foo', 'data': 'bar', 'skip_sid': '123'} {'event': 'foo', 'data': 'bar', 'skip_sid': '123'}
) )
) )
super_emit.mock.assert_called_once_with( super_emit.assert_awaited_once_with(
self.pm,
'foo', 'foo',
'bar', 'bar',
namespace=None, namespace=None,
@ -303,7 +299,7 @@ class TestAsyncPubSubManager:
def test_handle_emit_with_remote_callback(self): def test_handle_emit_with_remote_callback(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'emit', new=AsyncMock() async_manager.AsyncManager, 'emit'
) as super_emit: ) as super_emit:
_run( _run(
self.pm._handle_emit( self.pm._handle_emit(
@ -316,16 +312,16 @@ class TestAsyncPubSubManager:
} }
) )
) )
assert super_emit.mock.call_count == 1 assert super_emit.await_count == 1
assert super_emit.mock.call_args[0] == (self.pm, 'foo', 'bar') assert super_emit.await_args[0] == ('foo', 'bar')
assert super_emit.mock.call_args[1]['namespace'] == '/baz' assert super_emit.await_args[1]['namespace'] == '/baz'
assert super_emit.mock.call_args[1]['room'] is None assert super_emit.await_args[1]['room'] is None
assert super_emit.mock.call_args[1]['skip_sid'] is None assert super_emit.await_args[1]['skip_sid'] is None
assert isinstance( assert isinstance(
super_emit.mock.call_args[1]['callback'], functools.partial super_emit.await_args[1]['callback'], functools.partial
) )
_run(super_emit.mock.call_args[1]['callback']('one', 2, 'three')) _run(super_emit.await_args[1]['callback']('one', 2, 'three'))
self.pm._publish.mock.assert_called_once_with( self.pm._publish.assert_awaited_once_with(
{ {
'method': 'callback', 'method': 'callback',
'host_id': 'x', 'host_id': 'x',
@ -338,7 +334,7 @@ class TestAsyncPubSubManager:
def test_handle_emit_with_local_callback(self): def test_handle_emit_with_local_callback(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'emit', new=AsyncMock() async_manager.AsyncManager, 'emit'
) as super_emit: ) as super_emit:
_run( _run(
self.pm._handle_emit( self.pm._handle_emit(
@ -351,21 +347,21 @@ class TestAsyncPubSubManager:
} }
) )
) )
assert super_emit.mock.call_count == 1 assert super_emit.await_count == 1
assert super_emit.mock.call_args[0] == (self.pm, 'foo', 'bar') assert super_emit.await_args[0] == ('foo', 'bar')
assert super_emit.mock.call_args[1]['namespace'] == '/baz' assert super_emit.await_args[1]['namespace'] == '/baz'
assert super_emit.mock.call_args[1]['room'] is None assert super_emit.await_args[1]['room'] is None
assert super_emit.mock.call_args[1]['skip_sid'] is None assert super_emit.await_args[1]['skip_sid'] is None
assert isinstance( assert isinstance(
super_emit.mock.call_args[1]['callback'], functools.partial super_emit.await_args[1]['callback'], functools.partial
) )
_run(super_emit.mock.call_args[1]['callback']('one', 2, 'three')) _run(super_emit.await_args[1]['callback']('one', 2, 'three'))
self.pm._publish.mock.assert_not_called() self.pm._publish.assert_not_awaited()
def test_handle_callback(self): 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', new=AsyncMock() self.pm, 'trigger_callback'
) as trigger: ) as trigger:
_run( _run(
self.pm._handle_callback( self.pm._handle_callback(
@ -379,11 +375,11 @@ class TestAsyncPubSubManager:
} }
) )
) )
trigger.mock.assert_called_once_with('sid', 123, ('one', 2)) trigger.assert_awaited_once_with('sid', 123, ('one', 2))
def test_handle_callback_bad_host_id(self): def test_handle_callback_bad_host_id(self):
with mock.patch.object( with mock.patch.object(
self.pm, 'trigger_callback', new=AsyncMock() self.pm, 'trigger_callback'
) as trigger: ) as trigger:
_run( _run(
self.pm._handle_callback( self.pm._handle_callback(
@ -397,12 +393,12 @@ class TestAsyncPubSubManager:
} }
) )
) )
assert trigger.mock.call_count == 0 assert trigger.await_count == 0
def test_handle_callback_missing_args(self): 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', new=AsyncMock() self.pm, 'trigger_callback'
) as trigger: ) as trigger:
_run( _run(
self.pm._handle_callback( self.pm._handle_callback(
@ -435,7 +431,7 @@ class TestAsyncPubSubManager:
{'method': 'callback', 'host_id': host_id} {'method': 'callback', 'host_id': host_id}
) )
) )
assert trigger.mock.call_count == 0 assert trigger.await_count == 0
def test_handle_disconnect(self): def test_handle_disconnect(self):
_run( _run(
@ -443,14 +439,14 @@ class TestAsyncPubSubManager:
{'method': 'disconnect', 'sid': '123', 'namespace': '/foo'} {'method': 'disconnect', 'sid': '123', 'namespace': '/foo'}
) )
) )
self.pm.server.disconnect.mock.assert_called_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): def test_handle_enter_room(self):
sid = _run(self.pm.connect('123', '/')) sid = _run(self.pm.connect('123', '/'))
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'enter_room', new=AsyncMock() async_manager.AsyncManager, 'enter_room'
) as super_enter_room: ) as super_enter_room:
_run( _run(
self.pm._handle_enter_room( self.pm._handle_enter_room(
@ -464,14 +460,12 @@ class TestAsyncPubSubManager:
'room': 'foo'} 'room': 'foo'}
) )
) )
super_enter_room.mock.assert_called_once_with( super_enter_room.assert_awaited_once_with(sid, '/', 'foo')
self.pm, sid, '/', 'foo'
)
def test_handle_leave_room(self): def test_handle_leave_room(self):
sid = _run(self.pm.connect('123', '/')) sid = _run(self.pm.connect('123', '/'))
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'leave_room', new=AsyncMock() async_manager.AsyncManager, 'leave_room'
) as super_leave_room: ) as super_leave_room:
_run( _run(
self.pm._handle_leave_room( self.pm._handle_leave_room(
@ -485,26 +479,24 @@ class TestAsyncPubSubManager:
'room': 'foo'} 'room': 'foo'}
) )
) )
super_leave_room.mock.assert_called_once_with( super_leave_room.assert_awaited_once_with(sid, '/', 'foo')
self.pm, sid, '/', 'foo'
)
def test_handle_close_room(self): def test_handle_close_room(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'close_room', new=AsyncMock() async_manager.AsyncManager, 'close_room'
) as super_close_room: ) as super_close_room:
_run( _run(
self.pm._handle_close_room( self.pm._handle_close_room(
{'method': 'close_room', 'room': 'foo'} {'method': 'close_room', 'room': 'foo'}
) )
) )
super_close_room.mock.assert_called_once_with( super_close_room.assert_awaited_once_with(
self.pm, room='foo', namespace=None room='foo', namespace=None
) )
def test_handle_close_room_with_namespace(self): def test_handle_close_room_with_namespace(self):
with mock.patch.object( with mock.patch.object(
async_manager.AsyncManager, 'close_room', new=AsyncMock() async_manager.AsyncManager, 'close_room'
) as super_close_room: ) as super_close_room:
_run( _run(
self.pm._handle_close_room( self.pm._handle_close_room(
@ -515,17 +507,17 @@ class TestAsyncPubSubManager:
} }
) )
) )
super_close_room.mock.assert_called_once_with( super_close_room.assert_awaited_once_with(
self.pm, room='foo', namespace='/bar' room='foo', namespace='/bar'
) )
def test_background_thread(self): def test_background_thread(self):
self.pm._handle_emit = AsyncMock() self.pm._handle_emit = mock.AsyncMock()
self.pm._handle_callback = AsyncMock() self.pm._handle_callback = mock.AsyncMock()
self.pm._handle_disconnect = AsyncMock() self.pm._handle_disconnect = mock.AsyncMock()
self.pm._handle_enter_room = AsyncMock() self.pm._handle_enter_room = mock.AsyncMock()
self.pm._handle_leave_room = AsyncMock() self.pm._handle_leave_room = mock.AsyncMock()
self.pm._handle_close_room = AsyncMock() self.pm._handle_close_room = mock.AsyncMock()
host_id = self.pm.host_id host_id = self.pm.host_id
async def messages(): async def messages():
@ -558,34 +550,34 @@ class TestAsyncPubSubManager:
self.pm._listen = messages self.pm._listen = messages
_run(self.pm._thread()) _run(self.pm._thread())
self.pm._handle_emit.mock.assert_called_once_with( self.pm._handle_emit.assert_awaited_once_with(
{'method': 'emit', 'value': 'foo', 'host_id': 'x'} {'method': 'emit', 'value': 'foo', 'host_id': 'x'}
) )
self.pm._handle_callback.mock.assert_any_call( self.pm._handle_callback.assert_any_await(
{'method': 'callback', 'value': 'bar', 'host_id': 'x'} {'method': 'callback', 'value': 'bar', 'host_id': 'x'}
) )
self.pm._handle_callback.mock.assert_any_call( self.pm._handle_callback.assert_any_await(
{'method': 'callback', 'value': 'bar', 'host_id': host_id} {'method': 'callback', 'value': 'bar', 'host_id': host_id}
) )
self.pm._handle_disconnect.mock.assert_called_once_with( self.pm._handle_disconnect.assert_awaited_once_with(
{'method': 'disconnect', 'sid': '123', 'namespace': '/foo', {'method': 'disconnect', 'sid': '123', 'namespace': '/foo',
'host_id': 'x'} 'host_id': 'x'}
) )
self.pm._handle_enter_room.mock.assert_called_once_with( self.pm._handle_enter_room.assert_awaited_once_with(
{'method': 'enter_room', 'sid': '123', 'namespace': '/foo', {'method': 'enter_room', 'sid': '123', 'namespace': '/foo',
'room': 'room', 'host_id': 'x'} 'room': 'room', 'host_id': 'x'}
) )
self.pm._handle_leave_room.mock.assert_called_once_with( self.pm._handle_leave_room.assert_awaited_once_with(
{'method': 'leave_room', 'sid': '123', 'namespace': '/foo', {'method': 'leave_room', 'sid': '123', 'namespace': '/foo',
'room': 'room', 'host_id': 'x'} 'room': 'room', 'host_id': 'x'}
) )
self.pm._handle_close_room.mock.assert_called_once_with( self.pm._handle_close_room.assert_awaited_once_with(
{'method': 'close_room', 'value': 'baz', 'host_id': 'x'} {'method': 'close_room', 'value': 'baz', 'host_id': 'x'}
) )
def test_background_thread_exception(self): def test_background_thread_exception(self):
self.pm._handle_emit = AsyncMock(side_effect=[ValueError(), self.pm._handle_emit = mock.AsyncMock(side_effect=[
asyncio.CancelledError]) ValueError(), asyncio.CancelledError])
async def messages(): async def messages():
yield {'method': 'emit', 'value': 'foo', 'host_id': 'x'} yield {'method': 'emit', 'value': 'foo', 'host_id': 'x'}
@ -594,9 +586,9 @@ class TestAsyncPubSubManager:
self.pm._listen = messages self.pm._listen = messages
_run(self.pm._thread()) _run(self.pm._thread())
self.pm._handle_emit.mock.assert_any_call( self.pm._handle_emit.assert_any_await(
{'method': 'emit', 'value': 'foo', 'host_id': 'x'} {'method': 'emit', 'value': 'foo', 'host_id': 'x'}
) )
self.pm._handle_emit.mock.assert_called_with( self.pm._handle_emit.assert_awaited_with(
{'method': 'emit', 'value': 'bar', 'host_id': 'x'} {'method': 'emit', 'value': 'bar', 'host_id': 'x'}
) )

240
tests/async/test_server.py

@ -11,12 +11,12 @@ from socketio import async_namespace
from socketio import exceptions from socketio import exceptions
from socketio import namespace from socketio import namespace
from socketio import packet from socketio import packet
from .helpers import AsyncMock, _run from .helpers import _run
@mock.patch('socketio.server.engineio.AsyncServer', **{ @mock.patch('socketio.server.engineio.AsyncServer', **{
'return_value.generate_id.side_effect': [str(i) for i in range(1, 10)], 'return_value.generate_id.side_effect': [str(i) for i in range(1, 10)],
'return_value.send_packet': AsyncMock()}) 'return_value.send_packet': mock.AsyncMock()})
class TestAsyncServer: class TestAsyncServer:
def teardown_method(self): def teardown_method(self):
# restore JSON encoder, in case a test changed it # restore JSON encoder, in case a test changed it
@ -24,16 +24,16 @@ class TestAsyncServer:
def _get_mock_manager(self): def _get_mock_manager(self):
mgr = mock.MagicMock() mgr = mock.MagicMock()
mgr.can_disconnect = AsyncMock() mgr.can_disconnect = mock.AsyncMock()
mgr.emit = AsyncMock() mgr.emit = mock.AsyncMock()
mgr.enter_room = AsyncMock() mgr.enter_room = mock.AsyncMock()
mgr.leave_room = AsyncMock() mgr.leave_room = mock.AsyncMock()
mgr.close_room = AsyncMock() mgr.close_room = mock.AsyncMock()
mgr.trigger_callback = AsyncMock() mgr.trigger_callback = mock.AsyncMock()
return mgr return mgr
def test_create(self, eio): def test_create(self, eio):
eio.return_value.handle_request = AsyncMock() eio.return_value.handle_request = mock.AsyncMock()
mgr = self._get_mock_manager() mgr = self._get_mock_manager()
s = async_server.AsyncServer( s = async_server.AsyncServer(
client_manager=mgr, async_handlers=True, foo='bar' client_manager=mgr, async_handlers=True, foo='bar'
@ -80,7 +80,7 @@ class TestAsyncServer:
callback='cb', callback='cb',
) )
) )
s.manager.emit.mock.assert_called_once_with( s.manager.emit.assert_awaited_once_with(
'my event', 'my event',
{'foo': 'bar'}, {'foo': 'bar'},
'/foo', '/foo',
@ -100,7 +100,7 @@ class TestAsyncServer:
ignore_queue=True, ignore_queue=True,
) )
) )
s.manager.emit.mock.assert_called_with( s.manager.emit.assert_awaited_with(
'my event', 'my event',
{'foo': 'bar'}, {'foo': 'bar'},
'/foo', '/foo',
@ -122,7 +122,7 @@ class TestAsyncServer:
callback='cb', callback='cb',
) )
) )
s.manager.emit.mock.assert_called_once_with( s.manager.emit.assert_awaited_once_with(
'my event', 'my event',
{'foo': 'bar'}, {'foo': 'bar'},
'/', '/',
@ -141,7 +141,7 @@ class TestAsyncServer:
ignore_queue=True, ignore_queue=True,
) )
) )
s.manager.emit.mock.assert_called_with( s.manager.emit.assert_awaited_with(
'my event', 'my event',
{'foo': 'bar'}, {'foo': 'bar'},
'/', '/',
@ -163,7 +163,7 @@ class TestAsyncServer:
callback='cb', callback='cb',
) )
) )
s.manager.emit.mock.assert_called_once_with( s.manager.emit.assert_awaited_once_with(
'message', 'message',
'foo', 'foo',
'/foo', '/foo',
@ -182,7 +182,7 @@ class TestAsyncServer:
ignore_queue=True, ignore_queue=True,
) )
) )
s.manager.emit.mock.assert_called_with( s.manager.emit.assert_awaited_with(
'message', 'message',
'foo', 'foo',
'/foo', '/foo',
@ -197,7 +197,7 @@ class TestAsyncServer:
s = async_server.AsyncServer(client_manager=mgr) s = async_server.AsyncServer(client_manager=mgr)
async def fake_event_wait(): async def fake_event_wait():
s.manager.emit.mock.call_args_list[0][1]['callback']('foo', 321) s.manager.emit.await_args_list[0][1]['callback']('foo', 321)
return True return True
s.eio.create_event.return_value.wait = fake_event_wait s.eio.create_event.return_value.wait = fake_event_wait
@ -231,39 +231,37 @@ class TestAsyncServer:
mgr = self._get_mock_manager() mgr = self._get_mock_manager()
s = async_server.AsyncServer(client_manager=mgr) s = async_server.AsyncServer(client_manager=mgr)
_run(s.enter_room('123', 'room', namespace='/foo')) _run(s.enter_room('123', 'room', namespace='/foo'))
s.manager.enter_room.mock.assert_called_once_with('123', '/foo', s.manager.enter_room.assert_awaited_once_with('123', '/foo', 'room')
'room')
def test_enter_room_default_namespace(self, eio): def test_enter_room_default_namespace(self, eio):
mgr = self._get_mock_manager() mgr = self._get_mock_manager()
s = async_server.AsyncServer(client_manager=mgr) s = async_server.AsyncServer(client_manager=mgr)
_run(s.enter_room('123', 'room')) _run(s.enter_room('123', 'room'))
s.manager.enter_room.mock.assert_called_once_with('123', '/', 'room') s.manager.enter_room.assert_awaited_once_with('123', '/', 'room')
def test_leave_room(self, eio): def test_leave_room(self, eio):
mgr = self._get_mock_manager() mgr = self._get_mock_manager()
s = async_server.AsyncServer(client_manager=mgr) s = async_server.AsyncServer(client_manager=mgr)
_run(s.leave_room('123', 'room', namespace='/foo')) _run(s.leave_room('123', 'room', namespace='/foo'))
s.manager.leave_room.mock.assert_called_once_with('123', '/foo', s.manager.leave_room.assert_awaited_once_with('123', '/foo', 'room')
'room')
def test_leave_room_default_namespace(self, eio): def test_leave_room_default_namespace(self, eio):
mgr = self._get_mock_manager() mgr = self._get_mock_manager()
s = async_server.AsyncServer(client_manager=mgr) s = async_server.AsyncServer(client_manager=mgr)
_run(s.leave_room('123', 'room')) _run(s.leave_room('123', 'room'))
s.manager.leave_room.mock.assert_called_once_with('123', '/', 'room') s.manager.leave_room.assert_awaited_once_with('123', '/', 'room')
def test_close_room(self, eio): def test_close_room(self, eio):
mgr = self._get_mock_manager() mgr = self._get_mock_manager()
s = async_server.AsyncServer(client_manager=mgr) s = async_server.AsyncServer(client_manager=mgr)
_run(s.close_room('room', namespace='/foo')) _run(s.close_room('room', namespace='/foo'))
s.manager.close_room.mock.assert_called_once_with('room', '/foo') s.manager.close_room.assert_awaited_once_with('room', '/foo')
def test_close_room_default_namespace(self, eio): def test_close_room_default_namespace(self, eio):
mgr = self._get_mock_manager() mgr = self._get_mock_manager()
s = async_server.AsyncServer(client_manager=mgr) s = async_server.AsyncServer(client_manager=mgr)
_run(s.close_room('room')) _run(s.close_room('room'))
s.manager.close_room.mock.assert_called_once_with('room', '/') s.manager.close_room.assert_awaited_once_with('room', '/')
def test_rooms(self, eio): def test_rooms(self, eio):
mgr = self._get_mock_manager() mgr = self._get_mock_manager()
@ -278,32 +276,32 @@ class TestAsyncServer:
s.manager.get_rooms.assert_called_once_with('123', '/') s.manager.get_rooms.assert_called_once_with('123', '/')
def test_handle_request(self, eio): def test_handle_request(self, eio):
eio.return_value.handle_request = AsyncMock() eio.return_value.handle_request = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
_run(s.handle_request('environ')) _run(s.handle_request('environ'))
s.eio.handle_request.mock.assert_called_once_with('environ') s.eio.handle_request.assert_awaited_once_with('environ')
def test_send_packet(self, eio): def test_send_packet(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
_run(s._send_packet('123', packet.Packet( _run(s._send_packet('123', packet.Packet(
packet.EVENT, ['my event', 'my data'], namespace='/foo'))) packet.EVENT, ['my event', 'my data'], namespace='/foo')))
s.eio.send.mock.assert_called_once_with( s.eio.send.assert_awaited_once_with(
'123', '2/foo,["my event","my data"]' '123', '2/foo,["my event","my data"]'
) )
def test_send_eio_packet(self, eio): def test_send_eio_packet(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
_run(s._send_eio_packet('123', eio_packet.Packet( _run(s._send_eio_packet('123', eio_packet.Packet(
eio_packet.MESSAGE, 'hello'))) eio_packet.MESSAGE, 'hello')))
assert s.eio.send_packet.mock.call_count == 1 assert s.eio.send_packet.await_count == 1
assert s.eio.send_packet.mock.call_args_list[0][0][0] == '123' assert s.eio.send_packet.await_args_list[0][0][0] == '123'
pkt = s.eio.send_packet.mock.call_args_list[0][0][1] pkt = s.eio.send_packet.await_args_list[0][0][1]
assert pkt.encode() == '4hello' assert pkt.encode() == '4hello'
def test_transport(self, eio): def test_transport(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
s.eio.transport = mock.MagicMock(return_value='polling') s.eio.transport = mock.MagicMock(return_value='polling')
sid_foo = _run(s.manager.connect('123', '/foo')) sid_foo = _run(s.manager.connect('123', '/foo'))
@ -311,7 +309,7 @@ class TestAsyncServer:
s.eio.transport.assert_called_once_with('123') s.eio.transport.assert_called_once_with('123')
def test_handle_connect(self, eio): def test_handle_connect(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
s.manager.initialize = mock.MagicMock() s.manager.initialize = mock.MagicMock()
handler = mock.MagicMock() handler = mock.MagicMock()
@ -320,14 +318,14 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
assert s.manager.is_connected('1', '/') assert s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ') handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with('123', '0{"sid":"1"}') s.eio.send.assert_awaited_once_with('123', '0{"sid":"1"}')
assert s.manager.initialize.call_count == 1 assert s.manager.initialize.call_count == 1
_run(s._handle_eio_connect('456', 'environ')) _run(s._handle_eio_connect('456', 'environ'))
_run(s._handle_eio_message('456', '0')) _run(s._handle_eio_message('456', '0'))
assert s.manager.initialize.call_count == 1 assert s.manager.initialize.call_count == 1
def test_handle_connect_with_auth(self, eio): def test_handle_connect_with_auth(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
s.manager.initialize = mock.MagicMock() s.manager.initialize = mock.MagicMock()
handler = mock.MagicMock() handler = mock.MagicMock()
@ -336,14 +334,14 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '0{"token":"abc"}')) _run(s._handle_eio_message('123', '0{"token":"abc"}'))
assert s.manager.is_connected('1', '/') assert s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ', {'token': 'abc'}) handler.assert_called_once_with('1', 'environ', {'token': 'abc'})
s.eio.send.mock.assert_called_once_with('123', '0{"sid":"1"}') s.eio.send.assert_awaited_once_with('123', '0{"sid":"1"}')
assert s.manager.initialize.call_count == 1 assert s.manager.initialize.call_count == 1
_run(s._handle_eio_connect('456', 'environ')) _run(s._handle_eio_connect('456', 'environ'))
_run(s._handle_eio_message('456', '0')) _run(s._handle_eio_message('456', '0'))
assert s.manager.initialize.call_count == 1 assert s.manager.initialize.call_count == 1
def test_handle_connect_with_auth_none(self, eio): def test_handle_connect_with_auth_none(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
s.manager.initialize = mock.MagicMock() s.manager.initialize = mock.MagicMock()
handler = mock.MagicMock(side_effect=[TypeError, None, None]) handler = mock.MagicMock(side_effect=[TypeError, None, None])
@ -352,30 +350,30 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
assert s.manager.is_connected('1', '/') assert s.manager.is_connected('1', '/')
handler.assert_called_with('1', 'environ', None) handler.assert_called_with('1', 'environ', None)
s.eio.send.mock.assert_called_once_with('123', '0{"sid":"1"}') s.eio.send.assert_awaited_once_with('123', '0{"sid":"1"}')
assert s.manager.initialize.call_count == 1 assert s.manager.initialize.call_count == 1
_run(s._handle_eio_connect('456', 'environ')) _run(s._handle_eio_connect('456', 'environ'))
_run(s._handle_eio_message('456', '0')) _run(s._handle_eio_message('456', '0'))
assert s.manager.initialize.call_count == 1 assert s.manager.initialize.call_count == 1
def test_handle_connect_async(self, eio): def test_handle_connect_async(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
s.manager.initialize = mock.MagicMock() s.manager.initialize = mock.MagicMock()
handler = AsyncMock() handler = mock.AsyncMock()
s.on('connect', handler) s.on('connect', handler)
_run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_connect('123', 'environ'))
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
assert s.manager.is_connected('1', '/') assert s.manager.is_connected('1', '/')
handler.mock.assert_called_once_with('1', 'environ') handler.assert_awaited_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with('123', '0{"sid":"1"}') s.eio.send.assert_awaited_once_with('123', '0{"sid":"1"}')
assert s.manager.initialize.call_count == 1 assert s.manager.initialize.call_count == 1
_run(s._handle_eio_connect('456', 'environ')) _run(s._handle_eio_connect('456', 'environ'))
_run(s._handle_eio_message('456', '0')) _run(s._handle_eio_message('456', '0'))
assert s.manager.initialize.call_count == 1 assert s.manager.initialize.call_count == 1
def test_handle_connect_with_default_implied_namespaces(self, eio): def test_handle_connect_with_default_implied_namespaces(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
_run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_connect('123', 'environ'))
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
@ -384,7 +382,7 @@ class TestAsyncServer:
assert not s.manager.is_connected('2', '/foo') assert not s.manager.is_connected('2', '/foo')
def test_handle_connect_with_implied_namespaces(self, eio): def test_handle_connect_with_implied_namespaces(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(namespaces=['/foo']) s = async_server.AsyncServer(namespaces=['/foo'])
_run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_connect('123', 'environ'))
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
@ -393,7 +391,7 @@ class TestAsyncServer:
assert s.manager.is_connected('1', '/foo') assert s.manager.is_connected('1', '/foo')
def test_handle_connect_with_all_implied_namespaces(self, eio): def test_handle_connect_with_all_implied_namespaces(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(namespaces='*') s = async_server.AsyncServer(namespaces='*')
_run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_connect('123', 'environ'))
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
@ -402,7 +400,7 @@ class TestAsyncServer:
assert s.manager.is_connected('2', '/foo') assert s.manager.is_connected('2', '/foo')
def test_handle_connect_namespace(self, eio): def test_handle_connect_namespace(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
handler = mock.MagicMock() handler = mock.MagicMock()
s.on('connect', handler, namespace='/foo') s.on('connect', handler, namespace='/foo')
@ -410,10 +408,10 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '0/foo,')) _run(s._handle_eio_message('123', '0/foo,'))
assert s.manager.is_connected('1', '/foo') assert s.manager.is_connected('1', '/foo')
handler.assert_called_once_with('1', 'environ') handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with('123', '0/foo,{"sid":"1"}') s.eio.send.assert_awaited_once_with('123', '0/foo,{"sid":"1"}')
def test_handle_connect_always_connect(self, eio): def test_handle_connect_always_connect(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(always_connect=True) s = async_server.AsyncServer(always_connect=True)
s.manager.initialize = mock.MagicMock() s.manager.initialize = mock.MagicMock()
handler = mock.MagicMock() handler = mock.MagicMock()
@ -422,14 +420,14 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
assert s.manager.is_connected('1', '/') assert s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ') handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with('123', '0{"sid":"1"}') s.eio.send.assert_awaited_once_with('123', '0{"sid":"1"}')
assert s.manager.initialize.call_count == 1 assert s.manager.initialize.call_count == 1
_run(s._handle_eio_connect('456', 'environ')) _run(s._handle_eio_connect('456', 'environ'))
_run(s._handle_eio_message('456', '0')) _run(s._handle_eio_message('456', '0'))
assert s.manager.initialize.call_count == 1 assert s.manager.initialize.call_count == 1
def test_handle_connect_rejected(self, eio): def test_handle_connect_rejected(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
handler = mock.MagicMock(return_value=False) handler = mock.MagicMock(return_value=False)
s.on('connect', handler) s.on('connect', handler)
@ -437,12 +435,12 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
assert not s.manager.is_connected('1', '/foo') assert not s.manager.is_connected('1', '/foo')
handler.assert_called_once_with('1', 'environ') handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with( s.eio.send.assert_awaited_once_with(
'123', '4{"message":"Connection rejected by server"}') '123', '4{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'} assert s.environ == {'123': 'environ'}
def test_handle_connect_namespace_rejected(self, eio): def test_handle_connect_namespace_rejected(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
handler = mock.MagicMock(return_value=False) handler = mock.MagicMock(return_value=False)
s.on('connect', handler, namespace='/foo') s.on('connect', handler, namespace='/foo')
@ -450,12 +448,12 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '0/foo,')) _run(s._handle_eio_message('123', '0/foo,'))
assert not s.manager.is_connected('1', '/foo') assert not s.manager.is_connected('1', '/foo')
handler.assert_called_once_with('1', 'environ') handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_any_call( s.eio.send.assert_any_await(
'123', '4/foo,{"message":"Connection rejected by server"}') '123', '4/foo,{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'} assert s.environ == {'123': 'environ'}
def test_handle_connect_rejected_always_connect(self, eio): def test_handle_connect_rejected_always_connect(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(always_connect=True) s = async_server.AsyncServer(always_connect=True)
handler = mock.MagicMock(return_value=False) handler = mock.MagicMock(return_value=False)
s.on('connect', handler) s.on('connect', handler)
@ -463,13 +461,13 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
assert not s.manager.is_connected('1', '/') assert not s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ') handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_any_call('123', '0{"sid":"1"}') s.eio.send.assert_any_await('123', '0{"sid":"1"}')
s.eio.send.mock.assert_any_call( s.eio.send.assert_any_await(
'123', '1{"message":"Connection rejected by server"}') '123', '1{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'} assert s.environ == {'123': 'environ'}
def test_handle_connect_namespace_rejected_always_connect(self, eio): def test_handle_connect_namespace_rejected_always_connect(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(always_connect=True) s = async_server.AsyncServer(always_connect=True)
handler = mock.MagicMock(return_value=False) handler = mock.MagicMock(return_value=False)
s.on('connect', handler, namespace='/foo') s.on('connect', handler, namespace='/foo')
@ -477,13 +475,13 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '0/foo,')) _run(s._handle_eio_message('123', '0/foo,'))
assert not s.manager.is_connected('1', '/foo') assert not s.manager.is_connected('1', '/foo')
handler.assert_called_once_with('1', 'environ') handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_any_call('123', '0/foo,{"sid":"1"}') s.eio.send.assert_any_await('123', '0/foo,{"sid":"1"}')
s.eio.send.mock.assert_any_call( s.eio.send.assert_any_await(
'123', '1/foo,{"message":"Connection rejected by server"}') '123', '1/foo,{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'} assert s.environ == {'123': 'environ'}
def test_handle_connect_rejected_with_exception(self, eio): def test_handle_connect_rejected_with_exception(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
handler = mock.MagicMock( handler = mock.MagicMock(
side_effect=exceptions.ConnectionRefusedError('fail_reason') side_effect=exceptions.ConnectionRefusedError('fail_reason')
@ -493,12 +491,12 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
assert not s.manager.is_connected('1', '/') assert not s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ') handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with( s.eio.send.assert_awaited_once_with(
'123', '4{"message":"fail_reason"}') '123', '4{"message":"fail_reason"}')
assert s.environ == {'123': 'environ'} assert s.environ == {'123': 'environ'}
def test_handle_connect_rejected_with_empty_exception(self, eio): def test_handle_connect_rejected_with_empty_exception(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
handler = mock.MagicMock( handler = mock.MagicMock(
side_effect=exceptions.ConnectionRefusedError() side_effect=exceptions.ConnectionRefusedError()
@ -508,12 +506,12 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
assert not s.manager.is_connected('1', '/') assert not s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ') handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with( s.eio.send.assert_awaited_once_with(
'123', '4{"message":"Connection rejected by server"}') '123', '4{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'} assert s.environ == {'123': 'environ'}
def test_handle_connect_namespace_rejected_with_exception(self, eio): def test_handle_connect_namespace_rejected_with_exception(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
handler = mock.MagicMock( handler = mock.MagicMock(
side_effect=exceptions.ConnectionRefusedError( side_effect=exceptions.ConnectionRefusedError(
@ -524,12 +522,12 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '0/foo,')) _run(s._handle_eio_message('123', '0/foo,'))
assert not s.manager.is_connected('1', '/foo') assert not s.manager.is_connected('1', '/foo')
handler.assert_called_once_with('1', 'environ') handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with( s.eio.send.assert_awaited_once_with(
'123', '4/foo,{"message":"fail_reason","data":[1,"2"]}') '123', '4/foo,{"message":"fail_reason","data":[1,"2"]}')
assert s.environ == {'123': 'environ'} assert s.environ == {'123': 'environ'}
def test_handle_connect_namespace_rejected_with_empty_exception(self, eio): def test_handle_connect_namespace_rejected_with_empty_exception(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
handler = mock.MagicMock( handler = mock.MagicMock(
side_effect=exceptions.ConnectionRefusedError() side_effect=exceptions.ConnectionRefusedError()
@ -539,26 +537,26 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '0/foo,')) _run(s._handle_eio_message('123', '0/foo,'))
assert not s.manager.is_connected('1', '/foo') assert not s.manager.is_connected('1', '/foo')
handler.assert_called_once_with('1', 'environ') handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with( s.eio.send.assert_awaited_once_with(
'123', '4/foo,{"message":"Connection rejected by server"}') '123', '4/foo,{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'} assert s.environ == {'123': 'environ'}
def test_handle_disconnect(self, eio): def test_handle_disconnect(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
s.manager.disconnect = AsyncMock() s.manager.disconnect = mock.AsyncMock()
handler = mock.MagicMock() handler = mock.MagicMock()
s.on('disconnect', handler) s.on('disconnect', handler)
_run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_connect('123', 'environ'))
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
_run(s._handle_eio_disconnect('123')) _run(s._handle_eio_disconnect('123'))
handler.assert_called_once_with('1') handler.assert_called_once_with('1')
s.manager.disconnect.mock.assert_called_once_with( s.manager.disconnect.assert_awaited_once_with(
'1', '/', ignore_queue=True) '1', '/', ignore_queue=True)
assert s.environ == {} assert s.environ == {}
def test_handle_disconnect_namespace(self, eio): def test_handle_disconnect_namespace(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
handler = mock.MagicMock() handler = mock.MagicMock()
s.on('disconnect', handler) s.on('disconnect', handler)
@ -572,7 +570,7 @@ class TestAsyncServer:
assert s.environ == {} assert s.environ == {}
def test_handle_disconnect_only_namespace(self, eio): def test_handle_disconnect_only_namespace(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
handler = mock.MagicMock() handler = mock.MagicMock()
s.on('disconnect', handler) s.on('disconnect', handler)
@ -591,21 +589,21 @@ class TestAsyncServer:
_run(s._handle_eio_disconnect('123')) _run(s._handle_eio_disconnect('123'))
def test_handle_event(self, eio): def test_handle_event(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(async_handlers=False) s = async_server.AsyncServer(async_handlers=False)
sid = _run(s.manager.connect('123', '/')) sid = _run(s.manager.connect('123', '/'))
handler = AsyncMock() handler = mock.AsyncMock()
catchall_handler = AsyncMock() catchall_handler = mock.AsyncMock()
s.on('msg', handler) s.on('msg', handler)
s.on('*', catchall_handler) s.on('*', catchall_handler)
_run(s._handle_eio_message('123', '2["msg","a","b"]')) _run(s._handle_eio_message('123', '2["msg","a","b"]'))
_run(s._handle_eio_message('123', '2["my message","a","b","c"]')) _run(s._handle_eio_message('123', '2["my message","a","b","c"]'))
handler.mock.assert_called_once_with(sid, 'a', 'b') handler.assert_awaited_once_with(sid, 'a', 'b')
catchall_handler.mock.assert_called_once_with( catchall_handler.assert_awaited_once_with(
'my message', sid, 'a', 'b', 'c') 'my message', sid, 'a', 'b', 'c')
def test_handle_event_with_namespace(self, eio): def test_handle_event_with_namespace(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(async_handlers=False) s = async_server.AsyncServer(async_handlers=False)
sid = _run(s.manager.connect('123', '/foo')) sid = _run(s.manager.connect('123', '/foo'))
handler = mock.MagicMock() handler = mock.MagicMock()
@ -619,7 +617,7 @@ class TestAsyncServer:
'my message', sid, 'a', 'b', 'c') 'my message', sid, 'a', 'b', 'c')
def test_handle_event_with_catchall_namespace(self, eio): def test_handle_event_with_catchall_namespace(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(async_handlers=False) s = async_server.AsyncServer(async_handlers=False)
sid_foo = _run(s.manager.connect('123', '/foo')) sid_foo = _run(s.manager.connect('123', '/foo'))
sid_bar = _run(s.manager.connect('123', '/bar')) sid_bar = _run(s.manager.connect('123', '/bar'))
@ -648,7 +646,7 @@ class TestAsyncServer:
'my message', '/bar', sid_bar, 'a', 'b', 'c') 'my message', '/bar', sid_bar, 'a', 'b', 'c')
def test_handle_event_with_disconnected_namespace(self, eio): def test_handle_event_with_disconnected_namespace(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(async_handlers=False) s = async_server.AsyncServer(async_handlers=False)
_run(s.manager.connect('123', '/foo')) _run(s.manager.connect('123', '/foo'))
handler = mock.MagicMock() handler = mock.MagicMock()
@ -657,7 +655,7 @@ class TestAsyncServer:
handler.assert_not_called() handler.assert_not_called()
def test_handle_event_binary(self, eio): def test_handle_event_binary(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(async_handlers=False) s = async_server.AsyncServer(async_handlers=False)
sid = _run(s.manager.connect('123', '/')) sid = _run(s.manager.connect('123', '/'))
handler = mock.MagicMock() handler = mock.MagicMock()
@ -675,9 +673,9 @@ class TestAsyncServer:
handler.assert_called_once_with(sid, 'a', b'bar', b'foo') handler.assert_called_once_with(sid, 'a', b'bar', b'foo')
def test_handle_event_binary_ack(self, eio): def test_handle_event_binary_ack(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(async_handlers=False) s = async_server.AsyncServer(async_handlers=False)
s.manager.trigger_callback = AsyncMock() s.manager.trigger_callback = mock.AsyncMock()
sid = _run(s.manager.connect('123', '/')) sid = _run(s.manager.connect('123', '/'))
_run( _run(
s._handle_eio_message( s._handle_eio_message(
@ -686,67 +684,67 @@ class TestAsyncServer:
) )
) )
_run(s._handle_eio_message('123', b'foo')) _run(s._handle_eio_message('123', b'foo'))
s.manager.trigger_callback.mock.assert_called_once_with( s.manager.trigger_callback.assert_awaited_once_with(
sid, 321, ['my message', 'a', b'foo'] sid, 321, ['my message', 'a', b'foo']
) )
def test_handle_event_with_ack(self, eio): def test_handle_event_with_ack(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(async_handlers=False) s = async_server.AsyncServer(async_handlers=False)
sid = _run(s.manager.connect('123', '/')) sid = _run(s.manager.connect('123', '/'))
handler = mock.MagicMock(return_value='foo') handler = mock.MagicMock(return_value='foo')
s.on('my message', handler) s.on('my message', handler)
_run(s._handle_eio_message('123', '21000["my message","foo"]')) _run(s._handle_eio_message('123', '21000["my message","foo"]'))
handler.assert_called_once_with(sid, 'foo') handler.assert_called_once_with(sid, 'foo')
s.eio.send.mock.assert_called_once_with( s.eio.send.assert_awaited_once_with(
'123', '31000["foo"]' '123', '31000["foo"]'
) )
def test_handle_unknown_event_with_ack(self, eio): def test_handle_unknown_event_with_ack(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(async_handlers=False) s = async_server.AsyncServer(async_handlers=False)
_run(s.manager.connect('123', '/')) _run(s.manager.connect('123', '/'))
handler = mock.MagicMock(return_value='foo') handler = mock.MagicMock(return_value='foo')
s.on('my message', handler) s.on('my message', handler)
_run(s._handle_eio_message('123', '21000["another message","foo"]')) _run(s._handle_eio_message('123', '21000["another message","foo"]'))
s.eio.send.mock.assert_not_called() s.eio.send.assert_not_awaited()
def test_handle_event_with_ack_none(self, eio): def test_handle_event_with_ack_none(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(async_handlers=False) s = async_server.AsyncServer(async_handlers=False)
sid = _run(s.manager.connect('123', '/')) sid = _run(s.manager.connect('123', '/'))
handler = mock.MagicMock(return_value=None) handler = mock.MagicMock(return_value=None)
s.on('my message', handler) s.on('my message', handler)
_run(s._handle_eio_message('123', '21000["my message","foo"]')) _run(s._handle_eio_message('123', '21000["my message","foo"]'))
handler.assert_called_once_with(sid, 'foo') handler.assert_called_once_with(sid, 'foo')
s.eio.send.mock.assert_called_once_with('123', '31000[]') s.eio.send.assert_awaited_once_with('123', '31000[]')
def test_handle_event_with_ack_tuple(self, eio): def test_handle_event_with_ack_tuple(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(async_handlers=False) s = async_server.AsyncServer(async_handlers=False)
sid = _run(s.manager.connect('123', '/')) sid = _run(s.manager.connect('123', '/'))
handler = mock.MagicMock(return_value=(1, '2', True)) handler = mock.MagicMock(return_value=(1, '2', True))
s.on('my message', handler) s.on('my message', handler)
_run(s._handle_eio_message('123', '21000["my message","a","b","c"]')) _run(s._handle_eio_message('123', '21000["my message","a","b","c"]'))
handler.assert_called_once_with(sid, 'a', 'b', 'c') handler.assert_called_once_with(sid, 'a', 'b', 'c')
s.eio.send.mock.assert_called_once_with( s.eio.send.assert_awaited_once_with(
'123', '31000[1,"2",true]' '123', '31000[1,"2",true]'
) )
def test_handle_event_with_ack_list(self, eio): def test_handle_event_with_ack_list(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(async_handlers=False) s = async_server.AsyncServer(async_handlers=False)
sid = _run(s.manager.connect('123', '/')) sid = _run(s.manager.connect('123', '/'))
handler = mock.MagicMock(return_value=[1, '2', True]) handler = mock.MagicMock(return_value=[1, '2', True])
s.on('my message', handler) s.on('my message', handler)
_run(s._handle_eio_message('123', '21000["my message","a","b","c"]')) _run(s._handle_eio_message('123', '21000["my message","a","b","c"]'))
handler.assert_called_once_with(sid, 'a', 'b', 'c') handler.assert_called_once_with(sid, 'a', 'b', 'c')
s.eio.send.mock.assert_called_once_with( s.eio.send.assert_awaited_once_with(
'123', '31000[[1,"2",true]]' '123', '31000[[1,"2",true]]'
) )
def test_handle_event_with_ack_binary(self, eio): def test_handle_event_with_ack_binary(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer(async_handlers=False) s = async_server.AsyncServer(async_handlers=False)
sid = _run(s.manager.connect('123', '/')) sid = _run(s.manager.connect('123', '/'))
handler = mock.MagicMock(return_value=b'foo') handler = mock.MagicMock(return_value=b'foo')
@ -765,7 +763,7 @@ class TestAsyncServer:
_run(s._handle_eio_message('123', '9')) _run(s._handle_eio_message('123', '9'))
def test_send_with_ack(self, eio): def test_send_with_ack(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
s.handlers['/'] = {} s.handlers['/'] = {}
_run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_connect('123', 'environ'))
@ -781,7 +779,7 @@ class TestAsyncServer:
cb.assert_called_once_with('foo', 2) cb.assert_called_once_with('foo', 2)
def test_send_with_ack_namespace(self, eio): def test_send_with_ack_namespace(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
s.handlers['/foo'] = {} s.handlers['/foo'] = {}
_run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_connect('123', 'environ'))
@ -809,7 +807,7 @@ class TestAsyncServer:
assert eio_sid == '123' assert eio_sid == '123'
fake_session = session fake_session = session
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
s.handlers['/'] = {} s.handlers['/'] = {}
s.handlers['/ns'] = {} s.handlers['/ns'] = {}
@ -841,63 +839,63 @@ class TestAsyncServer:
_run(_test()) _run(_test())
def test_disconnect(self, eio): def test_disconnect(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
eio.return_value.disconnect = AsyncMock() eio.return_value.disconnect = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
s.handlers['/'] = {} s.handlers['/'] = {}
_run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_connect('123', 'environ'))
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
_run(s.disconnect('1')) _run(s.disconnect('1'))
s.eio.send.mock.assert_any_call('123', '1') s.eio.send.assert_any_await('123', '1')
assert not s.manager.is_connected('1', '/') assert not s.manager.is_connected('1', '/')
def test_disconnect_ignore_queue(self, eio): def test_disconnect_ignore_queue(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
eio.return_value.disconnect = AsyncMock() eio.return_value.disconnect = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
s.handlers['/'] = {} s.handlers['/'] = {}
_run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_connect('123', 'environ'))
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
_run(s.disconnect('1', ignore_queue=True)) _run(s.disconnect('1', ignore_queue=True))
s.eio.send.mock.assert_any_call('123', '1') s.eio.send.assert_any_await('123', '1')
assert not s.manager.is_connected('1', '/') assert not s.manager.is_connected('1', '/')
def test_disconnect_namespace(self, eio): def test_disconnect_namespace(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
eio.return_value.disconnect = AsyncMock() eio.return_value.disconnect = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
s.handlers['/foo'] = {} s.handlers['/foo'] = {}
_run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_connect('123', 'environ'))
_run(s._handle_eio_message('123', '0/foo,')) _run(s._handle_eio_message('123', '0/foo,'))
_run(s.disconnect('1', namespace='/foo')) _run(s.disconnect('1', namespace='/foo'))
s.eio.send.mock.assert_any_call('123', '1/foo,') s.eio.send.assert_any_await('123', '1/foo,')
assert not s.manager.is_connected('1', '/foo') assert not s.manager.is_connected('1', '/foo')
def test_disconnect_twice(self, eio): def test_disconnect_twice(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
eio.return_value.disconnect = AsyncMock() eio.return_value.disconnect = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
_run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_connect('123', 'environ'))
_run(s._handle_eio_message('123', '0')) _run(s._handle_eio_message('123', '0'))
_run(s.disconnect('1')) _run(s.disconnect('1'))
calls = s.eio.send.mock.call_count calls = s.eio.send.await_count
assert not s.manager.is_connected('1', '/') assert not s.manager.is_connected('1', '/')
_run(s.disconnect('1')) _run(s.disconnect('1'))
assert calls == s.eio.send.mock.call_count assert calls == s.eio.send.await_count
def test_disconnect_twice_namespace(self, eio): def test_disconnect_twice_namespace(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
_run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_connect('123', 'environ'))
_run(s._handle_eio_message('123', '0/foo,')) _run(s._handle_eio_message('123', '0/foo,'))
_run(s.disconnect('1', namespace='/foo')) _run(s.disconnect('1', namespace='/foo'))
calls = s.eio.send.mock.call_count calls = s.eio.send.await_count
assert not s.manager.is_connected('1', '/foo') assert not s.manager.is_connected('1', '/foo')
_run(s.disconnect('1', namespace='/foo')) _run(s.disconnect('1', namespace='/foo'))
assert calls == s.eio.send.mock.call_count assert calls == s.eio.send.await_count
def test_namespace_handler(self, eio): def test_namespace_handler(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
result = {} result = {}
class MyNamespace(async_namespace.AsyncNamespace): class MyNamespace(async_namespace.AsyncNamespace):
@ -931,7 +929,7 @@ class TestAsyncServer:
assert result['result'] == ('disconnect', '1') assert result['result'] == ('disconnect', '1')
def test_catchall_namespace_handler(self, eio): def test_catchall_namespace_handler(self, eio):
eio.return_value.send = AsyncMock() eio.return_value.send = mock.AsyncMock()
result = {} result = {}
class MyNamespace(async_namespace.AsyncNamespace): class MyNamespace(async_namespace.AsyncNamespace):
@ -1047,9 +1045,9 @@ class TestAsyncServer:
def test_shutdown(self, eio): def test_shutdown(self, eio):
s = async_server.AsyncServer() s = async_server.AsyncServer()
s.eio.shutdown = AsyncMock() s.eio.shutdown = mock.AsyncMock()
_run(s.shutdown()) _run(s.shutdown())
s.eio.shutdown.mock.assert_called_once_with() s.eio.shutdown.assert_awaited_once_with()
def test_start_background_task(self, eio): def test_start_background_task(self, eio):
s = async_server.AsyncServer() s = async_server.AsyncServer()
@ -1059,7 +1057,7 @@ class TestAsyncServer:
) )
def test_sleep(self, eio): def test_sleep(self, eio):
eio.return_value.sleep = AsyncMock() eio.return_value.sleep = mock.AsyncMock()
s = async_server.AsyncServer() s = async_server.AsyncServer()
_run(s.sleep(1.23)) _run(s.sleep(1.23))
s.eio.sleep.mock.assert_called_once_with(1.23) s.eio.sleep.assert_awaited_once_with(1.23)

40
tests/async/test_simple_client.py

@ -4,7 +4,7 @@ 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 AsyncMock, _run from .helpers import _run
class TestAsyncAsyncSimpleClient: class TestAsyncAsyncSimpleClient:
@ -20,14 +20,14 @@ class TestAsyncAsyncSimpleClient:
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 = AsyncMock() mock_client.return_value.connect = mock.AsyncMock()
_run(client.connect('url', headers='h', auth='a', transports='t', _run(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.mock.assert_called_once_with( mock_client().connect.assert_awaited_once_with(
'url', headers='h', auth='a', transports='t', 'url', headers='h', auth='a', transports='t',
namespaces=['n'], socketio_path='s', wait_timeout='w') namespaces=['n'], socketio_path='s', wait_timeout='w')
mock_client().event.call_count == 3 mock_client().event.call_count == 3
@ -40,14 +40,14 @@ class TestAsyncAsyncSimpleClient:
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') \
as mock_client: as mock_client:
mock_client.return_value.connect = AsyncMock() mock_client.return_value.connect = mock.AsyncMock()
await client.connect('url', headers='h', auth='a', await client.connect('url', headers='h', auth='a',
transports='t', namespace='n', transports='t', namespace='n',
socketio_path='s', wait_timeout='w') socketio_path='s', 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.mock.assert_called_once_with( mock_client().connect.assert_awaited_once_with(
'url', headers='h', auth='a', transports='t', 'url', headers='h', auth='a', transports='t',
namespaces=['n'], socketio_path='s', wait_timeout='w') namespaces=['n'], socketio_path='s', wait_timeout='w')
mock_client().event.call_count == 3 mock_client().event.call_count == 3
@ -79,14 +79,14 @@ class TestAsyncAsyncSimpleClient:
def test_emit(self): def test_emit(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
client.client = mock.MagicMock() client.client = mock.MagicMock()
client.client.emit = AsyncMock() client.client.emit = mock.AsyncMock()
client.namespace = '/ns' client.namespace = '/ns'
client.connected_event.set() client.connected_event.set()
client.connected = True client.connected = True
_run(client.emit('foo', 'bar')) _run(client.emit('foo', 'bar'))
client.client.emit.mock.assert_called_once_with('foo', 'bar', client.client.emit.assert_awaited_once_with('foo', 'bar',
namespace='/ns') namespace='/ns')
def test_emit_disconnected(self): def test_emit_disconnected(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
@ -100,23 +100,23 @@ class TestAsyncAsyncSimpleClient:
client.connected_event.set() client.connected_event.set()
client.connected = True client.connected = True
client.client = mock.MagicMock() client.client = mock.MagicMock()
client.client.emit = AsyncMock() client.client.emit = mock.AsyncMock()
client.client.emit.mock.side_effect = [SocketIOError(), None] client.client.emit.side_effect = [SocketIOError(), None]
_run(client.emit('foo', 'bar')) _run(client.emit('foo', 'bar'))
client.client.emit.mock.assert_called_with('foo', 'bar', namespace='/') client.client.emit.assert_awaited_with('foo', 'bar', namespace='/')
def test_call(self): def test_call(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
client.client = mock.MagicMock() client.client = mock.MagicMock()
client.client.call = AsyncMock() client.client.call = mock.AsyncMock()
client.client.call.mock.return_value = 'result' client.client.call.return_value = 'result'
client.namespace = '/ns' client.namespace = '/ns'
client.connected_event.set() client.connected_event.set()
client.connected = True client.connected = True
assert _run(client.call('foo', 'bar')) == 'result' assert _run(client.call('foo', 'bar')) == 'result'
client.client.call.mock.assert_called_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): def test_call_disconnected(self):
@ -131,12 +131,12 @@ class TestAsyncAsyncSimpleClient:
client.connected_event.set() client.connected_event.set()
client.connected = True client.connected = True
client.client = mock.MagicMock() client.client = mock.MagicMock()
client.client.call = AsyncMock() client.client.call = mock.AsyncMock()
client.client.call.mock.side_effect = [SocketIOError(), 'result'] client.client.call.side_effect = [SocketIOError(), 'result']
assert _run(client.call('foo', 'bar')) == 'result' assert _run(client.call('foo', 'bar')) == 'result'
client.client.call.mock.assert_called_with('foo', 'bar', namespace='/', client.client.call.assert_awaited_with('foo', 'bar', namespace='/',
timeout=60) timeout=60)
def test_receive_with_input_buffer(self): def test_receive_with_input_buffer(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
@ -180,10 +180,10 @@ class TestAsyncAsyncSimpleClient:
def test_disconnect(self): def test_disconnect(self):
client = AsyncSimpleClient() client = AsyncSimpleClient()
mc = mock.MagicMock() mc = mock.MagicMock()
mc.disconnect = AsyncMock() mc.disconnect = mock.AsyncMock()
client.client = mc client.client = mc
client.connected = True client.connected = True
_run(client.disconnect()) _run(client.disconnect())
_run(client.disconnect()) _run(client.disconnect())
mc.disconnect.mock.assert_called_once_with() mc.disconnect.assert_awaited_once_with()
assert client.client is None assert client.client is None

Loading…
Cancel
Save