From 3f489ae1aeff6f27b2214dc8201ed068abd5f973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 3 Apr 2020 19:20:36 +0200 Subject: [PATCH] fix: async ops sanitizer false positives in timers (#4602) --- cli/js/testing.ts | 10 ++++++++++ cli/js/tests/signal_test.ts | 6 ------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cli/js/testing.ts b/cli/js/testing.ts index 5769495b27..542e1d065a 100644 --- a/cli/js/testing.ts +++ b/cli/js/testing.ts @@ -14,6 +14,12 @@ const GREEN_OK = green("ok"); const YELLOW_IGNORED = yellow("ignored"); const disabledConsole = new Console((): void => {}); +function delay(n: number): Promise { + return new Promise((resolve: () => void, _) => { + setTimeout(resolve, n); + }); +} + function formatDuration(time = 0): string { const timeStr = `(${time}ms)`; return gray(italic(timeStr)); @@ -28,6 +34,10 @@ function assertOps(fn: () => void | Promise): () => void | Promise { return async function asyncOpSanitizer(): Promise { const pre = metrics(); await fn(); + // Defer until next event loop turn - that way timeouts and intervals + // cleared can actually be removed from resource table, otherwise + // false positives may occur (https://github.com/denoland/deno/issues/4591) + await delay(0); const post = metrics(); // We're checking diff because one might spawn HTTP server in the background // that will be a pending async op before test starts. diff --git a/cli/js/tests/signal_test.ts b/cli/js/tests/signal_test.ts index a51df09d78..c2b4f878b7 100644 --- a/cli/js/tests/signal_test.ts +++ b/cli/js/tests/signal_test.ts @@ -130,9 +130,6 @@ unitTest( assertEquals(c, 3); clearInterval(t); - // Defer for a moment to allow async op from `setInterval` to resolve; - // for more explanation see `FIXME` in `cli/js/timers.ts::setGlobalTimeout` - await defer(20); await resolvable; } ); @@ -153,9 +150,6 @@ unitTest( sig.dispose(); clearInterval(t); - // Defer for a moment to allow async op from `setInterval` to resolve; - // for more explanation see `FIXME` in `cli/js/timers.ts::setGlobalTimeout` - await defer(20); await resolvable; } );