1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 17:19:06 -05:00

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
This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2018-09-16 13:35:16 -07:00 committed by Ryan Dahl
parent 0040486539
commit b0958073ba
5 changed files with 104 additions and 11 deletions

View file

@ -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);
}
});

97
js/timers_test.ts Normal file
View file

@ -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);
});

View file

@ -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";

View file

@ -154,8 +154,7 @@ fn parse_core_args(args: Vec<String>) -> (Vec<String>, Vec<String>) {
}
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<String>) -> Vec<String> {
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()
}

View file

@ -288,8 +288,7 @@ fn handle_env(d: *const DenoC, base: &msg::Base) -> Box<Op> {
..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<Op> {
// 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