Exemplo n.º 1
0
async def test_listen_reconnect(ds: IdexDatastream):
    class BreakExc(Exception):
        pass

    exc = websockets.ConnectionClosed(code=999, reason='some reason')
    exit_exc = BreakExc('to break the infinite loop')

    ds._check_connection = CoroutineMock()
    ds._ws = MagicMock()
    ds._ws.closed = False
    ds._ws.__aiter__.side_effect = exc

    ds.init = CoroutineMock()

    ds.sub_manager.resubscribe = CoroutineMock()
    ds.sub_manager.resubscribe.side_effect = exit_exc

    ds._logger.error = Mock()

    with pytest.raises(BreakExc):
        async for m in ds.listen():
            break

    ds._check_connection.assert_awaited_once()
    ds._logger.error.assert_called_once_with(exc)
    ds.init.assert_awaited_once()
    ds.sub_manager.resubscribe.assert_awaited_once()
Exemplo n.º 2
0
async def test_check_connection(ds: IdexDatastream):
    ds.init = CoroutineMock()

    ds._ws = object
    await ds._check_connection()
    ds.init.assert_not_awaited()

    ds._ws = None
    await ds._check_connection()
    ds.init.assert_awaited_once()