diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/mkdir_test.ts | 3 | ||||
-rw-r--r-- | js/timers_test.ts | 97 | ||||
-rw-r--r-- | js/unit_tests.ts | 1 |
3 files changed, 100 insertions, 1 deletions
diff --git a/js/mkdir_test.ts b/js/mkdir_test.ts index 4f82401dc..27da93c16 100644 --- a/js/mkdir_test.ts +++ b/js/mkdir_test.ts @@ -13,7 +13,8 @@ testPerm({ write: true }, function mkdirSyncMode() { const path = deno.makeTempDirSync() + "/dir/subdir"; deno.mkdirSync(path, 0o755); // no perm for x const pathInfo = deno.statSync(path); - if (pathInfo.mode !== null) { // Skip windows + if (pathInfo.mode !== null) { + // Skip windows assertEqual(pathInfo.mode & 0o777, 0o755); } }); diff --git a/js/timers_test.ts b/js/timers_test.ts new file mode 100644 index 000000000..af172e976 --- /dev/null +++ b/js/timers_test.ts @@ -0,0 +1,97 @@ +import { test, assertEqual } from "./test_util.ts"; + +function deferred() { + let resolve; + let reject; + const promise = new Promise((res, rej) => { + resolve = res; + reject = rej; + }); + return { + promise, + resolve, + reject + }; +} + +function waitForMs(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +test(async function timeoutSuccess() { + const { promise, resolve } = deferred(); + let count = 0; + setTimeout(() => { + count++; + resolve(); + }, 500); + await promise; + // count should increment + assertEqual(count, 1); +}); + +test(async function timeoutCancelSuccess() { + let count = 0; + const id = setTimeout(() => { + count++; + }, 500); + // Cancelled, count should not increment + clearTimeout(id); + // Wait a bit longer than 500ms + await waitForMs(600); + assertEqual(count, 0); +}); + +test(async function timeoutCancelInvalidSilentFail() { + // Expect no panic + const { promise, resolve } = deferred(); + let count = 0; + const id = setTimeout(() => { + count++; + // Should have no effect + clearTimeout(id); + resolve(); + }, 500); + await promise; + assertEqual(count, 1); + + // Should silently fail (no panic) + clearTimeout(2147483647); +}); + +test(async function intervalSuccess() { + const { promise, resolve } = deferred(); + let count = 0; + const id = setInterval(() => { + count++; + if (count === 2) { + // TODO: clearInterval(id) here alone seems not working + // causing unit_tests.ts to block forever + // Requires further investigation... + clearInterval(id); + resolve(); + } + }, 200); + await promise; + // Clear interval + clearInterval(id); + // count should increment twice + assertEqual(count, 2); +}); + +test(async function intervalCancelSuccess() { + let count = 0; + const id = setInterval(() => { + count++; + }, 500); + // Cancelled, count should not increment + clearInterval(id); + // Wait a bit longer than 500ms + await waitForMs(600); + assertEqual(count, 0); +}); + +test(async function intervalCancelInvalidSilentFail() { + // Should silently fail (no panic) + clearInterval(2147483647); +}); diff --git a/js/unit_tests.ts b/js/unit_tests.ts index e1385e011..4f7b9681d 100644 --- a/js/unit_tests.ts +++ b/js/unit_tests.ts @@ -12,3 +12,4 @@ import "./make_temp_dir_test.ts"; import "./stat_test.ts"; import "./rename_test.ts"; import "./blob_test.ts"; +import "./timers_test.ts"; |