Пример #1
0
    def _connect_and_send(self, path, criteria, callback):

        client = Client(loop=self.loop,
                        host=self.host,
                        port=self.port,
                        path=path,
                        recv=callback)

        client.connect()

        self.connections.add(client)

        def on_client_closed(websocket, code, reason):
            """Remove the underlying client from the connections set once its connection is closed.
            """
            # client was closed
            if client in self.connections:
                self.connections.remove(client)

        client.on_close(on_client_closed)

        msg = json.dumps(criteria)

        client.send(msg)

        return ResultHandler(client)
Пример #2
0
def test_client_send(m_send, m_recv, m_connect):
    from time import sleep
    evloop = asyncio.new_event_loop()

    async def fake_connect(url, loop):
        assert url == 'ws://localhost:1122/path'
        assert loop == evloop

        return WebSocket()

    async def fake_recv():
        await asyncio.sleep(100)
        return None

    send_msgs = []

    async def fake_send(msg):
        print('[fake send]')
        send_msgs.append(msg)
        evloop.stop()

    m_connect.side_effect = fake_connect
    m_recv.side_effect = fake_recv
    m_send.side_effect = fake_send

    client = Client(loop=evloop, host='localhost', port=1122, path='/path')

    client.connect()

    assert client.loop == evloop
    assert client.host == 'localhost'
    assert client.port == 1122
    assert client.secure == False
    assert client.path == '/path'
    assert client.serializer is not None
    assert client.websocket is not None
    assert client._is_open == True

    client.send(message='TEST MESSAGE')

    evloop.run_forever()
    assert len(send_msgs) == 1

    for t in asyncio.Task.all_tasks(evloop):
        t.cancel()
        print('Task:', t, 'cancelled.')

    evloop.close()