Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
132 lines
3.3 KiB
JavaScript
132 lines
3.3 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("node:assert/strict");
|
|
|
|
const {clock, set_global, zrequire} = require("./lib/namespace.cjs");
|
|
const {run_test} = require("./lib/test.cjs");
|
|
const blueslip = require("./lib/zblueslip.cjs");
|
|
|
|
let checker;
|
|
|
|
function advance_secs(secs) {
|
|
clock.tick(secs * 1000);
|
|
}
|
|
|
|
set_global("setInterval", (f, interval) => {
|
|
checker = f;
|
|
assert.equal(interval, 5000);
|
|
});
|
|
|
|
let resume_handler;
|
|
let pageshow_handler;
|
|
|
|
set_global("document", {
|
|
addEventListener(event, handler) {
|
|
if (event === "resume") {
|
|
resume_handler = handler;
|
|
}
|
|
},
|
|
});
|
|
|
|
set_global("window", {
|
|
addEventListener(event, handler) {
|
|
if (event === "pageshow") {
|
|
pageshow_handler = handler;
|
|
}
|
|
},
|
|
});
|
|
|
|
run_test("basics", () => {
|
|
const watchdog = zrequire("watchdog");
|
|
watchdog._reset_for_testing();
|
|
watchdog.initialize();
|
|
// 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(81);
|
|
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(100);
|
|
watchdog.check_for_unsuspend();
|
|
assert.equal(num_times_called_back, 1);
|
|
|
|
// Error while executing callback
|
|
num_times_called_back = 0;
|
|
advance_secs(100);
|
|
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);
|
|
});
|
|
|
|
run_test("suspect_offline", () => {
|
|
const watchdog = zrequire("watchdog");
|
|
watchdog._reset_for_testing();
|
|
watchdog.initialize();
|
|
watchdog.set_suspect_offline(true);
|
|
assert.ok(watchdog.suspects_user_is_offline());
|
|
|
|
watchdog.set_suspect_offline(false);
|
|
assert.ok(!watchdog.suspects_user_is_offline());
|
|
});
|
|
|
|
run_test("browser_events", () => {
|
|
const watchdog = zrequire("watchdog");
|
|
watchdog._reset_for_testing();
|
|
watchdog.initialize();
|
|
// Verify handlers were registered
|
|
assert.ok(resume_handler);
|
|
assert.ok(pageshow_handler);
|
|
|
|
let num_times_called_back = 0;
|
|
watchdog.on_unsuspend(() => {
|
|
num_times_called_back += 1;
|
|
});
|
|
|
|
// Simulate resume event (after >75s delay)
|
|
advance_secs(80);
|
|
resume_handler();
|
|
assert.equal(num_times_called_back, 1);
|
|
|
|
// Simulate pageshow event (persisted) (after >75s delay)
|
|
advance_secs(80);
|
|
pageshow_handler({persisted: true});
|
|
assert.equal(num_times_called_back, 2);
|
|
|
|
// Simulate pageshow event (not persisted)
|
|
advance_secs(80);
|
|
pageshow_handler({persisted: false});
|
|
assert.equal(num_times_called_back, 2);
|
|
});
|