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)
Example #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()
Example #3
0
def test_websocket_double_add(websocket_service: WebsocketService):
    websocket_service.add("test", "test")
    with raises(RuntimeError):
        websocket_service.add("test", "test")
Example #4
0
def test_websocket_add(websocket_service: WebsocketService,
                       expected_response, side_effect, path, regex):
    websocket_service.add(side_effect, path, regex=regex)
    conn = _connect(websocket_service)
    assert conn.recv() == expected_response
Example #5
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")
Example #6
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")