Ejemplo n.º 1
0
def test_ws_loop_messages(client):
    """Test websocket loop messages received."""
    mock_ws = AsyncMock()
    receive_queue = asyncio.Queue(loop=client.loop)
    mock_ws.receive = receive_queue.get

    client._ws_connection = mock_ws
    client.ws_close = AsyncMock()
    client._handle_packet = MagicMock()

    msg = MagicMock()
    msg.type = aiohttp.WSMsgType.TEXT
    msg.data = 'mock message 1'
    client.loop.run_until_complete(receive_queue.put(msg))
    msg = MagicMock()
    msg.type = aiohttp.WSMsgType.TEXT
    msg.data = 'mock message 2'
    client.loop.run_until_complete(receive_queue.put(msg))
    msg = MagicMock()
    msg.type = aiohttp.WSMsgType.BINARY
    msg.data = b'binary message to be ignored'
    client.loop.run_until_complete(receive_queue.put(msg))
    msg = MagicMock()
    msg.type = aiohttp.WSMsgType.CLOSED
    client.loop.run_until_complete(receive_queue.put(msg))

    client.loop.run_until_complete(client._ws_loop())

    assert client._handle_packet.called
    assert len(client._handle_packet.mock_calls) == 2
    assert client._handle_packet.mock_calls[0][1][0] == 'mock message 1'
    assert client._handle_packet.mock_calls[1][1][0] == 'mock message 2'
Ejemplo n.º 2
0
def test_ws_connect(client):
    """Test websocket connect and ping loop."""
    mock_ws = AsyncMock()
    send_queue = queue.Queue()
    receive_queue = asyncio.Queue(loop=client.loop)
    mock_ws.receive = receive_queue.get

    @asyncio.coroutine
    def mock_send_str(data):
        send_queue.put(data)

    mock_ws.send_str = mock_send_str

    session_data = {
        "sid": "mock_session_id",
        "pingTimeout": 12.345,
        "pingInterval": 23.456,
    }
    client._get_engineio_session = AsyncMock()
    client._get_engineio_session.return_value = session_data
    client._get_ws_connection = AsyncMock()
    client._get_ws_connection.return_value = mock_ws

    ws_loop = client.loop.run_until_complete(client.ws_connect())
    assert not ws_loop.done()

    packet = send_queue.get(False)
    assert send_queue.empty()
    assert packet == "2"

    msg = MagicMock()
    msg.type = aiohttp.WSMsgType.CLOSED
    client.loop.run_until_complete(receive_queue.put(msg))
    assert ws_loop.done()

    packet = send_queue.get(False)
    assert packet == "41"
    packet = send_queue.get(False)
    assert packet == "1"
    assert send_queue.empty()
    assert mock_ws.close.called
    assert len(mock_ws.close.mock_calls) == 1
Ejemplo n.º 3
0
def test_ws_ping(client):
    """Test websocket ping."""
    mock_ws = AsyncMock()
    send_queue = queue.Queue()
    receive_queue = asyncio.Queue(loop=client.loop)
    mock_ws.receive = receive_queue.get

    @asyncio.coroutine
    def mock_send_str(data):
        send_queue.put(data)

    mock_ws.send_str = mock_send_str

    old_handle = MagicMock()
    client.ws_close = AsyncMock()
    client.loop.call_later = MagicMock()
    client._ws_connection = mock_ws
    client._ws_session_data = {
        "sid": "mock_session_id",
        "pingTimeout": 12.345,
        "pingInterval": 23.456,
        "pingTimeoutHandle": old_handle,
    }
    client.loop.run_until_complete(client._ping())

    packet = send_queue.get(False)
    assert send_queue.empty()
    assert packet == "2"

    assert old_handle.cancel.called
    assert len(old_handle.cancel.mock_calls) == 1

    assert client.loop.call_later.called
    assert len(client.loop.call_later.mock_calls) == 1
    assert client.loop.call_later.mock_calls[0][1][0] == 12.345
    timeout = client.loop.call_later.mock_calls[0][1][1]
    assert not client.ws_close.called
    future = timeout()
    client.loop.run_until_complete(future)
    assert client.ws_close.called
    assert len(client.ws_close.mock_calls) == 1
Ejemplo n.º 4
0
def test_ws_loop_error(client):
    """Test websocket loop error message."""
    mock_ws = AsyncMock()
    receive_queue = asyncio.Queue(loop=client.loop)
    mock_ws.receive = receive_queue.get

    client._ws_connection = mock_ws
    client.ws_close = AsyncMock()
    client._handle_event = MagicMock()

    msg = MagicMock()
    msg.type = aiohttp.WSMsgType.ERROR
    client.loop.run_until_complete(receive_queue.put(msg))

    with pytest.raises(exceptions.TransportError) as exc:
        client.loop.run_until_complete(client._ws_loop())

    assert client.ws_close.called
    assert len(client.ws_close.mock_calls) == 1
    assert client._handle_event.called
    assert len(client._handle_event.mock_calls) == 1
    assert client._handle_event.mock_calls[0][1][0] == 'closed'
    assert client._handle_event.mock_calls[0][1][1] is None
    assert str(exc.value) == "Websocket error detected. Connection closed."