Files
node/test/parallel/test-timers-refresh.js
T
Jeremiah Senkpiel bb5575aa75 timers: add internal [@@ refresh()] function
Hidden via a symbol because I'm unsure exactly what the API should look
like in the end.

Removes the need to use _unrefActive for efficiently refreshing
timeouts.
It still uses it under the hood but that could be replaced with
insert() directly if it were in the same file.

PR-URL: https://github.com/nodejs/node/pull/18065
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
2018-01-26 16:47:16 -05:00

60 lines
1.3 KiB
JavaScript

// Flags: --expose-internals
'use strict';
const common = require('../common');
const { strictEqual } = require('assert');
const { setUnrefTimeout, refreshFnSymbol } = require('internal/timers');
// Schedule the unrefed cases first so that the later case keeps the event loop
// active.
// Every case in this test relies on implicit sorting within either Node's or
// libuv's timers storage data structures.
// unref()'d timer
{
let called = false;
const timer = setTimeout(common.mustCall(() => {
called = true;
}), 1);
timer.unref();
// This relies on implicit timers handle sorting withing libuv.
setTimeout(common.mustCall(() => {
strictEqual(called, false, 'unref()\'d timer returned before check');
}), 1);
timer[refreshFnSymbol]();
}
// unref pooled timer
{
let called = false;
const timer = setUnrefTimeout(common.mustCall(() => {
called = true;
}), 1);
setUnrefTimeout(common.mustCall(() => {
strictEqual(called, false, 'unref pooled timer returned before check');
}), 1);
timer[refreshFnSymbol]();
}
// regular timer
{
let called = false;
const timer = setTimeout(common.mustCall(() => {
called = true;
}), 1);
setTimeout(common.mustCall(() => {
strictEqual(called, false, 'pooled timer returned before check');
}), 1);
timer[refreshFnSymbol]();
}