Exemplo n.º 1
0
def test_websocket_stop():
    # prevent stoppping session websocket.
    websocket_service = WebsocketService()
    websocket_service.start()
    done = False

    def stuck(ws):
        nonlocal done
        try:
            while True:
                yield  # Should be able to close while a generator is stuck.
        finally:
            done = True

    websocket_service.add(stuck, TEST_PATH)
    conn = _connect(websocket_service)
    conn.send("test")
    websocket_service.stop()
    assert done

    # Make sure we're actually closed.
    with raises((ConnectionRefusedError, socket.timeout)):
        websocket.create_connection(urljoin(websocket_service.local_url,
                                            TEST_PATH),
                                    timeout=0.2)
Exemplo n.º 2
0
def test_websocket_generator_recv(websocket_service: WebsocketService):
    event = threading.Event()
    data = []

    def gen(ws):
        data.append((yield))
        data.append((yield))
        event.set()
    websocket_service.add(gen, TEST_PATH)
    conn = _connect(websocket_service)
    conn.send("testy")
    conn.send("bin")
    event.wait()
    assert data == ["testy", "bin"]
    conn.close()
Exemplo n.º 3
0
def test_websocket_patch(websocket_service: WebsocketService,
                         expected_response, side_effect, path, regex):
    patch = websocket_service.patch(side_effect, path, regex=regex)

    with patch:
        conn = _connect(websocket_service)
        assert conn.recv() == expected_response
Exemplo n.º 4
0
def test_websocket_double_add(websocket_service: WebsocketService):
    websocket_service.add("test", "test")
    with raises(RuntimeError):
        websocket_service.add("test", "test")
Exemplo n.º 5
0
def test_websocket_set(websocket_service: WebsocketService,
                       expected_response, side_effect, path, regex):
    websocket_service.set(side_effect, path, regex=regex)
    conn = _connect(websocket_service)
    assert conn.recv() == expected_response
Exemplo n.º 6
0
def _global_websocket_service():
    with WebsocketService().start() as ws:
        yield ws
Exemplo n.º 7
0
def test_websocket_clear(websocket_service: WebsocketService):
    websocket_service.add("test", "test")
    websocket_service.clear()
    # Make sure it was cleared, no error.
    websocket_service.add("test", "test")
Exemplo n.º 8
0
def test_websocket_remove_regex(websocket_service: WebsocketService):
    websocket_service.add("test", regex="test")
    websocket_service.remove(regex="test")
    with raises(KeyError):
        websocket_service.remove(regex="test")
Exemplo n.º 9
0
def test_websocket_double_patch(websocket_service: WebsocketService):
    with websocket_service.patch("test", "test"), raises(RuntimeError), \
            websocket_service.patch("test", "test"):
        pass
Exemplo n.º 10
0
def test_websocket_double_set(websocket_service: WebsocketService):
    websocket_service.set("test", "test")
    websocket_service.set("test", "test")  # No error
Exemplo n.º 11
0
def test_websocket_double_route(websocket_service: WebsocketService):
    websocket_service.route("test")("test")
    with raises(RuntimeError):
        websocket_service.route("test")("test")