Exemplo n.º 1
0
def test_secure_echo(ws):
    custom_port_range = range(1024, 65535)
    config = EvaluatorServerConfig(custom_port_range=custom_port_range)

    async def handler(websocket, path):
        msg = await websocket.recv()
        await websocket.send(msg)

    ws(
        config.host,
        config.port,
        handler,
        ssl=config.get_server_ssl_context(),
        sock=config.get_socket(),
    )
    with ExitStack() as stack:
        duplexer = SyncWebsocketDuplexer(
            f"wss://{config.host}:{config.port}",
            f"wss://{config.host}:{config.port}",
            cert=config.cert,
            token=None,
        )
        stack.callback(duplexer.stop)
        duplexer.send("Hello Secure World")
        assert next(duplexer.receive()) == "Hello Secure World"
Exemplo n.º 2
0
def test_failed_connection():
    with patch(
            "ert_shared.ensemble_evaluator.sync_ws_duplexer.wait_for_evaluator"
    ) as w:
        w.side_effect = OSError("expected oserror")
        duplexer = SyncWebsocketDuplexer("ws://localhost:0",
                                         "http://localhost:0", None, None)
        with pytest.raises(OSError):
            duplexer.send("hello")
Exemplo n.º 3
0
 def _send_event(self, cloud_event: CloudEvent) -> None:
     with ExitStack() as stack:
         duplexer = self._ws_duplexer
         if not duplexer:
             duplexer = SyncWebsocketDuplexer(self._client_uri,
                                              self._base_uri, self._cert,
                                              self._token)
             stack.callback(duplexer.stop)
         duplexer.send(
             to_json(cloud_event,
                     data_marshaller=serialization.evaluator_marshaller))
Exemplo n.º 4
0
def test_failed_send(unused_tcp_port, ws):
    async def handler(websocket, path):
        await websocket.recv()

    ws("localhost", unused_tcp_port, handler)
    duplexer = SyncWebsocketDuplexer(
        f"ws://localhost:{unused_tcp_port}",
        f"ws://localhost:{unused_tcp_port}",
        None,
        None,
    )
    with patch("websockets.WebSocketCommonProtocol.send") as send:
        send.side_effect = OSError("expected OSError")
        with pytest.raises(OSError, match="expected OSError"):
            duplexer.send("hello")
Exemplo n.º 5
0
def test_echo(unused_tcp_port, ws):
    async def handler(websocket, path):
        msg = await websocket.recv()
        await websocket.send(msg)

    ws("localhost", unused_tcp_port, handler)
    with ExitStack() as stack:
        duplexer = SyncWebsocketDuplexer(
            f"ws://localhost:{unused_tcp_port}",
            f"ws://localhost:{unused_tcp_port}",
            None,
            None,
        )
        stack.callback(duplexer.stop)

        duplexer.send("Hello World")
        assert next(duplexer.receive()) == "Hello World"