测试用例在 deno 上泄漏异步操作

Ram*_*ros 3 deno

我从 Drash ( https://github.com/drashland/deno-drash )下载了示例应用程序

$ deno run --allow-run --allow-read --allow-write --allow-net https://deno.land/x/drash/create_app.ts --api

并尝试添加一个新测试,其中:

  1. 将获取 GET /
  2. 断言状态代码和响应 json
Deno.test("HomeResource - GET /", async () => {
  const response = await fetch("http://localhost:1557", {
    method: "GET",
  });
  assertEquals(response.status, 200);
  assertEquals(
    await response.json(),
    JSON.stringify({
      success: true,
      message: "GET request received.",
    }),
  );
});
Run Code Online (Sandbox Code Playgroud)

这是错误信息

Server listening: http://localhost:1557
running 5 tests
test HomeResource - GET / ... FAILED (9ms)
test HomeResource - POST / ... ok (2ms)
test HomeResource - PUT / ... ok (2ms)
test HomeResource - DELETE / ... ok (2ms)

Stop the server ... ok (0ms)

failures:

HomeResource - GET /
AssertionError: Test case is leaking async ops.
Before:
  - dispatched: 1
  - completed: 0
After:
  - dispatched: 9
  - completed: 7

Make sure to await all promises returned from Deno APIs before
finishing test case.
    at assert (rt/06_util.js:33:13)
    at asyncOpSanitizer (rt/40_testing.js:44:7)
    at async Object.resourceSanitizer [as fn] (rt/40_testing.js:68:7)
    at async TestRunner.[Symbol.asyncIterator] (rt/40_testing.js:240:13)
    at async Object.runTests (rt/40_testing.js:317:22)

failures:

    HomeResource - GET /

test result: FAILED. 4 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out (15ms)

Run Code Online (Sandbox Code Playgroud)

我试图用 取消主体 response.body?.cancel(),但说流已锁定。

测试:https : //github.com/ramonmedeiros/learning_deno/blob/master/tests/resources/home_resource_test.ts

Shi*_*gla 5

测试用例正在泄漏操作和资源。也许 Drash 没有正确处理这个问题。

这些是获取请求之前和之后的资源 -

?????????????????????????
? (idx) ?    Values     ?
?????????????????????????
?   0   ?    "stdin"    ?
?   1   ?   "stdout"    ?
?   2   ?   "stderr"    ?
?   3   ? "tcpListener" ?
?????????????????????????
?????????????????????????
? (idx) ?    Values     ?
?????????????????????????
?   0   ?    "stdin"    ?
?   1   ?   "stdout"    ?
?   2   ?   "stderr"    ?
?   3   ? "tcpListener" ?
?   4   ?  "tcpStream"  ?
?????????????????????????
Run Code Online (Sandbox Code Playgroud)

这些是获取请求之前和之后的操作 -

????????????????????????????????????
?          (idx)          ? Values ?
????????????????????????????????????
?      opsDispatched      ?   5    ?
?    opsDispatchedSync    ?   4    ?
?   opsDispatchedAsync    ?   1    ?
? opsDispatchedAsyncUnref ?   0    ?
?      opsCompleted       ?   4    ?
?    opsCompletedSync     ?   4    ?
?    opsCompletedAsync    ?   0    ?
? opsCompletedAsyncUnref  ?   0    ?
?    bytesSentControl     ?  121   ?
?      bytesSentData      ?   46   ?
?      bytesReceived      ?  418   ?
????????????????????????????????????
????????????????????????????????????
?          (idx)          ? Values ?
????????????????????????????????????
?      opsDispatched      ?   14   ?
?    opsDispatchedSync    ?   6    ?
?   opsDispatchedAsync    ?   8    ?
? opsDispatchedAsyncUnref ?   0    ?
?      opsCompleted       ?   12   ?
?    opsCompletedSync     ?   6    ?
?    opsCompletedAsync    ?   6    ?
? opsCompletedAsyncUnref  ?   0    ?
?    bytesSentControl     ?  323   ?
?      bytesSentData      ? 73903  ?
?      bytesReceived      ?  1060  ?
????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

tcpStream资源在测试后未关闭。请参阅opsDispatchedAsyncopsCompletedAsync。所有异步操作都没有完成。这是这里提到的异步操作和资源清理。这些默认情况下是启用的,但可以通过设置sanitizeResourcessanitizeOpsto禁用,false就像在最后一个测试用例中所做的那样 -

Deno.test({
  name: "\b\b\b\b\b     \nStop the server",
  fn() {
    server.close();
  },
  sanitizeResources: false,
  sanitizeOps: false,
});
Run Code Online (Sandbox Code Playgroud)