Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

watchdog: Refactor initialization for testability.

Guru Vyas6032ddbaaa77
diff --git a/web/src/server_events.js b/web/src/server_events.js
index 65f2b4ca55e..6b0d17bc738 100644
--- a/web/src/server_events.js
+++ b/web/src/server_events.js
@@ -285,6 +285,7 @@ export function initialize(params) {
});
get_events();
+ watchdog.initialize();
}
function cleanup_event_queue() {
diff --git a/web/src/watchdog.ts b/web/src/watchdog.ts
index 5eda0665105..eee36e232c8 100644
--- a/web/src/watchdog.ts
+++ b/web/src/watchdog.ts
@@ -17,6 +17,18 @@ export function suspects_user_is_offline(): boolean {
return suspect_offline;
}
+/**
+ * Clears the internal state of the watchdog module.
+ *
+ * This is used by tests to ensure a clean slate between test cases,
+ * since the module is not reloaded.
+ */
+export function _reset_for_testing(): void {
+ unsuspend_callbacks.length = 0;
+ watchdog_time = Date.now();
+ suspect_offline = false;
+}
+
/*
There are two ways for us to detect that the web app had been on a
suspended device. The first is the `resume` event on Document, which
@@ -71,4 +83,6 @@ export function on_unsuspend(f: () => void): void {
unsuspend_callbacks.push(f);
}
-setInterval(check_for_unsuspend, CHECK_FREQUENCY_MILLISECONDS);
+export function initialize(): void {
+ setInterval(check_for_unsuspend, CHECK_FREQUENCY_MILLISECONDS);
+}
diff --git a/web/tests/watchdog.test.cjs b/web/tests/watchdog.test.cjs
index f761dac8d1b..61dac5944be 100644
--- a/web/tests/watchdog.test.cjs
+++ b/web/tests/watchdog.test.cjs
@@ -22,9 +22,10 @@ set_global("setInterval", (f, interval) => {
assert.equal(interval, 5000);
});
-const watchdog = zrequire("watchdog");
-
run_test("basics", () => {
+ const watchdog = zrequire("watchdog");
+ watchdog._reset_for_testing();
+ watchdog.initialize();
// Test without callbacks first.
checker();
advance_secs(5);
@@ -76,6 +77,9 @@ run_test("basics", () => {
});
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());