feat(companion): add heartbeat failure-threshold controls
This commit is contained in:
@@ -100,4 +100,34 @@ describe('CompanionHeartbeatLoop', () => {
|
||||
expect(publishHeartbeat).toHaveBeenCalledTimes(1);
|
||||
expect(loop.running).toBe(false);
|
||||
});
|
||||
|
||||
it('stops when maxConsecutiveFailures threshold is reached', async () => {
|
||||
const publishHeartbeat = vi
|
||||
.fn<() => Promise<NodeStatusSetResult>>()
|
||||
.mockRejectedValue(new Error('persistent-failure'));
|
||||
const onError = vi.fn();
|
||||
const onFailureLimitReached = vi.fn();
|
||||
const loop = new CompanionHeartbeatLoop(
|
||||
{ publishHeartbeat },
|
||||
{
|
||||
intervalMs: 300,
|
||||
onError,
|
||||
maxConsecutiveFailures: 2,
|
||||
onFailureLimitReached,
|
||||
},
|
||||
);
|
||||
|
||||
loop.start();
|
||||
await Promise.resolve();
|
||||
expect(loop.running).toBe(true);
|
||||
|
||||
await vi.advanceTimersByTimeAsync(300);
|
||||
expect(loop.running).toBe(false);
|
||||
expect(onError).toHaveBeenCalledTimes(2);
|
||||
expect(onFailureLimitReached).toHaveBeenCalledTimes(1);
|
||||
expect(onFailureLimitReached).toHaveBeenCalledWith(expect.any(Error), 2);
|
||||
|
||||
await vi.advanceTimersByTimeAsync(1000);
|
||||
expect(publishHeartbeat).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user