Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'test/timers/test-timers-reliability.js')
-rw-r--r--test/timers/test-timers-reliability.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/test/timers/test-timers-reliability.js b/test/timers/test-timers-reliability.js
new file mode 100644
index 00000000000..c240a44685a
--- /dev/null
+++ b/test/timers/test-timers-reliability.js
@@ -0,0 +1,53 @@
+// FaketimeFlags: --exclude-monotonic -f '2014-07-21 09:00:00'
+
+var common = require('../common');
+
+var Timer = process.binding('timer_wrap').Timer;
+var assert = require('assert');
+
+var timerFired = false;
+var intervalFired = false;
+
+/*
+ * 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.
+ */
+
+var monoTimer = new Timer();
+monoTimer.ontimeout = function () {
+ /*
+ * 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.
+ */
+ assert(timerFired);
+ assert(intervalFired);
+};
+
+monoTimer.start(300, 0);
+
+var timer = setTimeout(function () {
+ timerFired = true;
+}, 200);
+
+var interval = setInterval(function () {
+ intervalFired = true;
+ clearInterval(interval);
+}, 200);