2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2014-07-23 03:03:10 +02:00
|
|
|
// FaketimeFlags: --exclude-monotonic -f '2014-07-21 09:00:00'
|
|
|
|
|
2015-12-24 01:02:12 +01:00
|
|
|
require('../common');
|
2014-07-23 03:03:10 +02:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const Timer = process.binding('timer_wrap').Timer;
|
2016-12-31 00:38:06 +01:00
|
|
|
const assert = require('assert');
|
2014-07-23 03:03:10 +02:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
let timerFired = false;
|
|
|
|
let intervalFired = false;
|
2014-07-23 03:03:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This test case aims at making sure that timing utilities such
|
|
|
|
* as setTimeout and setInterval are not vulnerable to time
|
|
|
|
* drifting or inconsistent time changes (such as ntp time sync
|
|
|
|
* in the past, etc.).
|
|
|
|
*
|
|
|
|
* It is run using faketime so that we change how
|
|
|
|
* non-monotonic clocks perceive time movement. We freeze
|
|
|
|
* non-monotonic time, and check if setTimeout and setInterval
|
|
|
|
* work properly in that situation.
|
|
|
|
*
|
|
|
|
* We check this by setting a timer based on a monotonic clock
|
|
|
|
* to fire after setTimeout's callback is supposed to be called.
|
|
|
|
* This monotonic timer, by definition, is not subject to time drifting
|
|
|
|
* and inconsistent time changes, so it can be considered as a solid
|
|
|
|
* reference.
|
|
|
|
*
|
|
|
|
* When the monotonic timer fires, if the setTimeout's callback
|
|
|
|
* hasn't been called yet, it means that setTimeout's underlying timer
|
|
|
|
* is vulnerable to time drift or inconsistent time changes.
|
|
|
|
*/
|
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const monoTimer = new Timer();
|
2016-02-23 07:14:32 +01:00
|
|
|
monoTimer[Timer.kOnTimeout] = function() {
|
2017-07-25 19:37:08 +02:00
|
|
|
/*
|
2014-07-23 03:03:10 +02:00
|
|
|
* Make sure that setTimeout's and setInterval's callbacks have
|
|
|
|
* already fired, otherwise it means that they are vulnerable to
|
|
|
|
* time drifting or inconsistent time changes.
|
|
|
|
*/
|
2015-05-19 13:00:06 +02:00
|
|
|
assert(timerFired);
|
|
|
|
assert(intervalFired);
|
2014-07-23 03:03:10 +02:00
|
|
|
};
|
|
|
|
|
2016-08-06 14:29:50 +02:00
|
|
|
monoTimer.start(300);
|
2014-07-23 03:03:10 +02:00
|
|
|
|
2015-12-27 07:08:08 +01:00
|
|
|
setTimeout(function() {
|
2015-05-19 13:00:06 +02:00
|
|
|
timerFired = true;
|
2014-07-23 03:03:10 +02:00
|
|
|
}, 200);
|
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const interval = setInterval(function() {
|
2015-05-19 13:00:06 +02:00
|
|
|
intervalFired = true;
|
|
|
|
clearInterval(interval);
|
2014-07-23 03:03:10 +02:00
|
|
|
}, 200);
|