



























@@ -78,6 +78,16 @@ function createMonitorHarness(params?: { cpuMsPerWallMs?: number; utilization?:
7878};
7979}
808081+function expectSnapshotFields(snapshot: unknown, expected: Record<string, unknown>) {
82+expect(typeof snapshot).toBe("object");
83+expect(snapshot).not.toBeNull();
84+const actual = snapshot as Record<string, unknown>;
85+for (const [key, value] of Object.entries(expected)) {
86+expect(actual[key]).toEqual(value);
87+}
88+return actual;
89+}
90+8191describe("classifyGatewayEventLoopHealthReasons", () => {
8292it("does not degrade on utilization or CPU from a sub-second sample", () => {
8393expect(
@@ -172,7 +182,7 @@ describe("createGatewayEventLoopHealthMonitor", () => {
172182expect(harness.eventLoopUtilization).toHaveBeenCalledTimes(1);
173183174184harness.setNow(1_000);
175-expect(harness.monitor.snapshot()).toMatchObject({
185+expectSnapshotFields(harness.monitor.snapshot(), {
176186degraded: false,
177187reasons: [],
178188intervalMs: 1_000,
@@ -188,7 +198,7 @@ describe("createGatewayEventLoopHealthMonitor", () => {
188198harness.setDelay({ p99Ms: 30 });
189199harness.setNow(1_000);
190200191-expect(harness.monitor.snapshot()).toMatchObject({
201+expectSnapshotFields(harness.monitor.snapshot(), {
192202degraded: true,
193203reasons: ["event_loop_utilization", "cpu"],
194204intervalMs: 1_000,
@@ -204,7 +214,7 @@ describe("createGatewayEventLoopHealthMonitor", () => {
204214harness.setDelay({ maxMs: 1_500 });
205215harness.setNow(42);
206216207-expect(harness.monitor.snapshot()).toMatchObject({
217+expectSnapshotFields(harness.monitor.snapshot(), {
208218degraded: true,
209219reasons: ["event_loop_delay"],
210220intervalMs: 42,
@@ -217,7 +227,7 @@ describe("createGatewayEventLoopHealthMonitor", () => {
217227const harness = createMonitorHarness({ cpuMsPerWallMs: 0.1, utilization: 0.2 });
218228harness.setNow(1_000);
219229220-expect(harness.monitor.snapshot()).toMatchObject({
230+expectSnapshotFields(harness.monitor.snapshot(), {
221231degraded: false,
222232reasons: [],
223233intervalMs: 1_000,
@@ -231,7 +241,7 @@ describe("createGatewayEventLoopHealthMonitor", () => {
231241harness.setNow(1_000);
232242const first = harness.monitor.snapshot();
233243234-expect(first).toEqual(expect.objectContaining({ intervalMs: 1_000 }));
244+expectSnapshotFields(first, { intervalMs: 1_000 });
235245expect(harness.cpuUsage).toHaveBeenCalledTimes(3);
236246expect(harness.eventLoopUtilization).toHaveBeenCalledTimes(3);
237247@@ -243,7 +253,7 @@ describe("createGatewayEventLoopHealthMonitor", () => {
243253harness.setNow(2_000);
244254const second = harness.monitor.snapshot();
245255246-expect(second).toEqual(expect.objectContaining({ intervalMs: 1_000 }));
256+expectSnapshotFields(second, { intervalMs: 1_000 });
247257expect(second).not.toBe(first);
248258});
249259@@ -257,7 +267,7 @@ describe("createGatewayEventLoopHealthMonitor", () => {
257267expect(harness.monitor.snapshot()).toBe(first);
258268259269harness.setNow(2_000);
260-expect(harness.monitor.snapshot()).toMatchObject({
270+expectSnapshotFields(harness.monitor.snapshot(), {
261271degraded: true,
262272reasons: ["event_loop_utilization", "cpu"],
263273intervalMs: 1_000,
@@ -272,9 +282,7 @@ describe("createGatewayEventLoopHealthMonitor", () => {
272282const harness = createMonitorHarness({ cpuMsPerWallMs: 0.1, utilization: 0.2 });
273283harness.setNow(1_000);
274284275-expect(harness.monitor.snapshot()).toEqual(
276-expect.objectContaining({ degraded: false, intervalMs: 1_000 }),
277-);
285+expectSnapshotFields(harness.monitor.snapshot(), { degraded: false, intervalMs: 1_000 });
278286279287harness.setNow(1_250);
280288harness.monitor.stop();
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。