Files
pangolin/server/lib/statusHistory.test.ts
T
RishavTiwari25 05d2874844 fix(status-history): order same-second events by id
Status rows written in the same second have equal timestamps, and the
batched query returns them in an order that depends on which other
resources are in the request. When a recovery event sorted before the
failure it followed, the resource stayed unhealthy until its next event.

Break timestamp ties by id in the queries and in computeBuckets.

Fixes #3994
2026-10-26 13:56:19 +05:30

59 lines
1.6 KiB
TypeScript

import { computeBuckets } from "./statusHistory";
import { assertEquals } from "@test/assert";
function event(id: number, timestamp: number, status: string) {
return {
entityType: "resource",
entityId: 1,
orgId: "org",
status,
timestamp,
id
};
}
// Regression test for https://github.com/fosrl/pangolin/issues/3994
function testSameSecondEventsUseInsertionOrder() {
console.log("Running computeBuckets same-second ordering tests...");
// Two days ago the resource went unhealthy and recovered within the same
// second. The database may return the two rows in either order.
const nowSec = Math.floor(Date.now() / 1000);
const flap = nowSec - 2 * 86400;
const unhealthy = event(1, flap, "unhealthy");
const healthy = event(2, flap, "healthy");
for (const events of [
[unhealthy, healthy],
[healthy, unhealthy]
]) {
const { buckets, totalDowntime } = computeBuckets(
events,
5,
"healthy",
0
);
const order = events.map((e) => e.id).join(",");
assertEquals(
totalDowntime,
0,
`No downtime when the later event is healthy (rows ${order})`
);
assertEquals(
buckets.at(-1)!.status,
"good",
`Today should be good (rows ${order})`
);
}
console.log("All computeBuckets same-second ordering tests passed!");
}
try {
testSameSecondEventsUseInsertionOrder();
console.log("All tests passed successfully!");
} catch (error) {
console.error("Test failed:", error);
process.exit(1);
}