简体   繁体   English

如何测试 deno WebSocket 服务器?

[英]How to test deno WebSocket Server?

I have written a small WebSocket Server class in Typescript using deno.我用deno在Typescript写了一个小的WebSocket Server class。

Now, I want to test it.现在,我想测试一下。 Everything works fine when I just run the server and after that close it.当我运行服务器然后关闭它时,一切正常。

But when I try to connect to the server in the testcase via new WebSocket("ws://{myserver}") I get the following error:但是当我尝试通过new WebSocket("ws://{myserver}")连接到测试用例中的服务器时,出现以下错误:

Test case is leaking async ops.测试用例正在泄漏异步操作。

My Code:我的代码:

Deno.test("run server", (): void => {
    const serverConfig = {
        autoServe: true,
        host: "localhost",
        port: 3045,
    };

    const server = new WSServer(serverConfig);

    console.log(server);

    const conn = new WebSocket("ws://localhost:3045");

    conn.send("hello");

    conn.close();

    assertEquals<number>(server.config.port, serverConfig.port);

    assertEquals<boolean>(server.listening, true);

    server.close();
});

Is there a solution to that?有解决办法吗?

I found a solution to this problem.我找到了解决这个问题的方法。 I have to wait for the WebSocket connection to open.我必须等待 WebSocket 连接打开。 But when I send something on the connection and the close the connection it won't send the data and I have to close the connection, because else Deno gives me the error that但是当我在连接上发送一些东西并关闭连接时,它不会发送数据,我必须关闭连接,否则 Deno 会给我一个错误

Test case is leaking async ops测试用例正在泄漏异步操作

So I implemented in my server code, that sends an answer to each sent data.所以我在我的服务器代码中实现了对每个发送的数据发送一个答案。

In the working server I will send a confirm message with a unique code.在工作服务器中,我将发送一条带有唯一代码的确认消息。

So for the test case (currently, it doesn't do something with a confirmation message, and listens only for any message from the server) I have now:所以对于测试用例(目前,它不对确认消息做任何事情,并且只监听来自服务器的任何消息)我现在有:

Deno.test("run server", async () => {
    const serverConfig = {
        autoServe: true,
        host: "localhost",
        port: 3045,
    };

    const server = new WSServer(serverConfig);

    const conn = new WebSocket("ws://localhost:3045");

    const awaitOpen = (connection: WebSocket): Promise<boolean> => {
        return new Promise(resolve => {
            conn.addEventListener("open", () => {
                resolve(true);
            });
        });
    };

    const awaitResponse = (connection: WebSocket): Promise<string> => {
        return new Promise(resolve => {
            conn.addEventListener("message", (event) => {
                resolve(String(event.data));
            });
        });
    }

    await awaitOpen(conn);

    conn.send("hello");

    const response = await awaitResponse(conn);

    if (response) {
        conn.close();

        server.close();
    }

    assertEquals<number>(server.config.port, serverConfig.port);

    assertEquals<boolean>(server.listening, true);
});

I hope, this is helpful for somebody.我希望,这对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM