From b0958073ba426db9636a98900a153af857e129c1 Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Sun, 16 Sep 2018 13:35:16 -0700 Subject: [PATCH] Remove remove_timer asserts (#760) * Remove remove_timer asserts * Add clearTimeout invalid id no-panic test * Move timer test to its file AND some lint side-effects --- js/mkdir_test.ts | 3 +- js/timers_test.ts | 97 +++++++++++++++++++++++++++++++++++++++++++++++ js/unit_tests.ts | 1 + src/flags.rs | 6 +-- src/handlers.rs | 8 +--- 5 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 js/timers_test.ts diff --git a/js/mkdir_test.ts b/js/mkdir_test.ts index 4f82401dcc..27da93c160 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 0000000000..af172e9765 --- /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 e1385e0116..4f7b9681d8 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"; diff --git a/src/flags.rs b/src/flags.rs index 13f0657350..2e48622186 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -154,8 +154,7 @@ fn parse_core_args(args: Vec) -> (Vec, Vec) { } true - }) - .collect(); + }).collect(); // Replace args being sent to V8 for idx in 0..args.len() { @@ -222,7 +221,6 @@ pub fn v8_set_flags(args: Vec) -> Vec { let cstr = CStr::from_ptr(*ptr as *const i8); let slice = cstr.to_str().unwrap(); slice.to_string() - }) - .chain(rest.into_iter()) + }).chain(rest.into_iter()) .collect() } diff --git a/src/handlers.rs b/src/handlers.rs index 0917fd22ab..39f2d51aaa 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -288,8 +288,7 @@ fn handle_env(d: *const DenoC, base: &msg::Base) -> Box { ..Default::default() }, ) - }) - .collect(); + }).collect(); let tables = builder.create_vector(&vars); let msg = msg::EnvironRes::create( builder, @@ -402,8 +401,7 @@ where .and_then(|_| { cb(); Ok(()) - }) - .select(cancel_rx) + }).select(cancel_rx) .map(|_| ()) .map_err(|_| ()); @@ -604,9 +602,7 @@ fn handle_write_file(d: *const DenoC, base: &msg::Base) -> Box { // TODO(ry) Use Deno instead of DenoC as first arg. fn remove_timer(d: *const DenoC, timer_id: u32) { let deno = from_c(d); - assert!(deno.timers.contains_key(&timer_id)); deno.timers.remove(&timer_id); - assert!(!deno.timers.contains_key(&timer_id)); } // Prototype: https://github.com/ry/deno/blob/golang/timers.go#L25-L39