From e718f0f326bf9752119277cbd0111f364805bc48 Mon Sep 17 00:00:00 2001 From: host Date: Sun, 1 Nov 2026 00:44:12 +0800 Subject: [PATCH] =?UTF-8?q?test(chv):=20=E4=BF=AE=E5=A4=8D=20isWorkerAlive?= =?UTF-8?q?=20=E8=BE=B9=E7=95=8C=E6=B5=8B=E8=AF=95=E6=8A=96=E5=8A=A8?= =?UTF-8?q?=EF=BC=88=E7=B2=BE=E7=A1=AE=E8=BE=B9=E7=95=8C=E6=94=B9=E7=A1=AE?= =?UTF-8?q?=E5=AE=9A=E6=80=A7=E5=AD=97=E6=AE=B5=E6=B5=8B=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloud/console-react/src/api/chv.closeup.test.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cloud/console-react/src/api/chv.closeup.test.ts b/cloud/console-react/src/api/chv.closeup.test.ts index 47adda4..ed7d758 100644 --- a/cloud/console-react/src/api/chv.closeup.test.ts +++ b/cloud/console-react/src/api/chv.closeup.test.ts @@ -69,8 +69,17 @@ describe("附件拦截与排队提示边角(closeup)", () => { }); describe("isWorkerAlive 边界(closeup)", () => { - it("last_seen 恰好 120 秒算在线(<=120)", () => { + // 精确边界不用活时钟测:原写法先取 Date.now() 再让被测函数 + // 内部重取一次时钟,age = 120 + 两次取时钟的差,两次取时钟 + // 跨过毫秒边界时 age = 120.001,<=120 判定为 false,约 7% + // 概率抖动(30 轮 2 败,均是本用例)。生产逻辑 <=120 本身 + // 没错,错的是用活时钟测精确边界。 + it("恰好 120 秒算在线(<=120,用不取时钟的字段确定性测)", () => { + expect(isWorkerAlive({ executor: "cloud-hypervisor", last_seen_seconds_ago: 120 })).toBe(true); + expect(isWorkerAlive({ executor: "cloud-hypervisor", last_seen_seconds_ago: 121 })).toBe(false); + }); + it("活时钟只测带 1 秒余量的内侧,不测精确边界", () => { const now = Date.now() / 1000; - expect(isWorkerAlive({ executor: "cloud-hypervisor", last_seen: now - 120 })).toBe(true); + expect(isWorkerAlive({ executor: "cloud-hypervisor", last_seen: now - 119 })).toBe(true); }); });