2021-03-22 13:59:50 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
|
|
|
const MockDate = require("mockdate");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {set_global, zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2023-11-19 07:32:48 +01:00
|
|
|
const blueslip = require("./lib/zblueslip");
|
2021-03-22 13:59:50 +01:00
|
|
|
|
|
|
|
let time = 0;
|
|
|
|
let checker;
|
|
|
|
MockDate.set(time);
|
|
|
|
|
|
|
|
function advance_secs(secs) {
|
|
|
|
time += secs * 1000;
|
|
|
|
MockDate.set(time);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_global("setInterval", (f, interval) => {
|
|
|
|
checker = f;
|
|
|
|
assert.equal(interval, 5000);
|
|
|
|
});
|
|
|
|
|
|
|
|
const watchdog = zrequire("watchdog");
|
|
|
|
|
|
|
|
run_test("basics", () => {
|
|
|
|
// Test without callbacks first.
|
|
|
|
checker();
|
|
|
|
advance_secs(5);
|
|
|
|
checker();
|
|
|
|
|
|
|
|
let num_times_called_back = 0;
|
|
|
|
|
|
|
|
function callback() {
|
|
|
|
num_times_called_back += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
watchdog.on_unsuspend(callback);
|
|
|
|
|
|
|
|
// Simulate healthy operation.
|
|
|
|
advance_secs(5);
|
|
|
|
checker();
|
|
|
|
assert.equal(num_times_called_back, 0);
|
|
|
|
|
|
|
|
// Simulate machine going to sleep.
|
|
|
|
advance_secs(21);
|
|
|
|
checker();
|
|
|
|
assert.equal(num_times_called_back, 1);
|
|
|
|
|
|
|
|
// Simulate healthy operations resume, and
|
|
|
|
// explicitly call check_for_unsuspend.
|
|
|
|
num_times_called_back = 0;
|
|
|
|
advance_secs(5);
|
|
|
|
watchdog.check_for_unsuspend();
|
|
|
|
assert.equal(num_times_called_back, 0);
|
|
|
|
|
|
|
|
// Simulate another suspension.
|
|
|
|
advance_secs(21);
|
|
|
|
watchdog.check_for_unsuspend();
|
|
|
|
assert.equal(num_times_called_back, 1);
|
2023-11-19 07:32:48 +01:00
|
|
|
|
|
|
|
// Error while executing callback
|
|
|
|
num_times_called_back = 0;
|
|
|
|
advance_secs(21);
|
|
|
|
watchdog.on_unsuspend(() => {
|
|
|
|
num_times_called_back += 1;
|
|
|
|
throw new Error("Some error while executing");
|
|
|
|
});
|
|
|
|
blueslip.expect(
|
|
|
|
"error",
|
|
|
|
`Error while executing callback 'Anonymous function' from unsuspend_callbacks.`,
|
|
|
|
);
|
|
|
|
watchdog.check_for_unsuspend();
|
|
|
|
assert.equal(num_times_called_back, 2);
|
2021-03-22 13:59:50 +01:00
|
|
|
});
|
|
|
|
|
2021-03-23 14:24:49 +01:00
|
|
|
run_test("suspect_offline", () => {
|
|
|
|
watchdog.set_suspect_offline(true);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(watchdog.suspects_user_is_offline());
|
2021-03-23 14:24:49 +01:00
|
|
|
|
|
|
|
watchdog.set_suspect_offline(false);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!watchdog.suspects_user_is_offline());
|
2021-03-23 14:24:49 +01:00
|
|
|
});
|
|
|
|
|
2022-05-07 02:53:53 +02:00
|
|
|
run_test("reset MockDate", () => {
|
|
|
|
MockDate.reset();
|
|
|
|
});
|