-
Notifications
You must be signed in to change notification settings - Fork 312
fix(web): surface heartbeat failures with backoff and warning state #1416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RuffR1d3r
wants to merge
4
commits into
meshtastic:main
Choose a base branch
from
RuffR1d3r:fix/heartbeat-surface-retry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4f61b56
fix(web): surface heartbeat failures with backoff and warning state
RuffR1d3r 6414868
test(web): cover heartbeat warning backoff and recovery
RuffR1d3r a54b74b
fix(web): ignore stale heartbeat callbacks after stop/restart
RuffR1d3r 3ab10de
fix(web): restore warning connections after restart
RuffR1d3r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { useDeviceStore } from "@core/stores/deviceStore"; | ||
| import type { MeshDevice } from "@meshtastic/sdk"; | ||
| import { startMaintenanceHeartbeat, stopHeartbeat } from "./heartbeat.ts"; | ||
|
|
||
| function mockMeshDevice( | ||
| fails: number, | ||
| ): MeshDevice & { callCount: () => number } { | ||
| let count = 0; | ||
| const md = { | ||
| heartbeat: vi.fn(() => { | ||
| count += 1; | ||
| if (count <= fails) return Promise.reject(new Error(`fail ${count}`)); | ||
| return Promise.resolve(0); | ||
| }), | ||
| } as unknown as MeshDevice & { callCount: () => number }; | ||
| (md as any).callCount = () => count; | ||
| return md as any; | ||
| } | ||
|
|
||
| describe("heartbeat", () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| // Reset store | ||
| const store = useDeviceStore.getState(); | ||
| // Ensure a connection exists | ||
| if (!store.savedConnections.find((c) => c.id === 999)) { | ||
| store.addSavedConnection({ | ||
| id: 999, | ||
| type: "http", | ||
| name: "test", | ||
| url: "http://192.168.1.3", | ||
| status: "configured", | ||
| createdAt: Date.now(), | ||
| } as any); | ||
| } else { | ||
| store.updateSavedConnection(999, { | ||
| status: "configured", | ||
| error: undefined, | ||
| }); | ||
| } | ||
| stopHeartbeat(999); | ||
| }); | ||
|
|
||
| it("flips to warning after 3 consecutive failures", async () => { | ||
| const md = mockMeshDevice(10); // always fail | ||
| startMaintenanceHeartbeat(999, md); | ||
|
|
||
| // One interval (5min) fails -> schedules retry (~1s), retry fails -> retry (~2s), retry fails -> warning | ||
| await vi.advanceTimersByTimeAsync(5 * 60 * 1000); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| await vi.advanceTimersByTimeAsync(5000); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| await vi.advanceTimersByTimeAsync(5000); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
|
|
||
| const conn = useDeviceStore | ||
| .getState() | ||
| .savedConnections.find((c) => c.id === 999); | ||
| expect(conn?.status).toBe("warning"); | ||
| expect(conn?.error).toMatch(/Heartbeat failed/); | ||
|
|
||
| stopHeartbeat(999); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it("recovers from warning on success", async () => { | ||
| const md = mockMeshDevice(3); // fail 3, then succeed | ||
| startMaintenanceHeartbeat(999, md); | ||
|
|
||
| await vi.advanceTimersByTimeAsync(5 * 60 * 1000); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| await vi.advanceTimersByTimeAsync(5000); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| await vi.advanceTimersByTimeAsync(5000); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
|
|
||
| let conn = useDeviceStore | ||
| .getState() | ||
| .savedConnections.find((c) => c.id === 999); | ||
| expect(conn?.status).toBe("warning"); | ||
|
|
||
| // Next interval should succeed and clear warning | ||
| await vi.advanceTimersByTimeAsync(5 * 60 * 1000); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
|
|
||
| conn = useDeviceStore.getState().savedConnections.find((c) => c.id === 999); | ||
| expect(conn?.status).toBe("configured"); | ||
| expect(conn?.error).toBeUndefined(); | ||
|
|
||
| stopHeartbeat(999); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it("clears warning when a restarted session succeeds", async () => { | ||
| const failing = mockMeshDevice(10); | ||
| startMaintenanceHeartbeat(999, failing); | ||
|
|
||
| await vi.advanceTimersByTimeAsync(5 * 60 * 1000); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| await vi.advanceTimersByTimeAsync(5000); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| await vi.advanceTimersByTimeAsync(5000); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
|
|
||
| expect( | ||
| useDeviceStore.getState().savedConnections.find((c) => c.id === 999) | ||
| ?.status, | ||
| ).toBe("warning"); | ||
|
|
||
| stopHeartbeat(999); | ||
| const recovered = mockMeshDevice(0); | ||
| startMaintenanceHeartbeat(999, recovered); | ||
|
|
||
| await vi.advanceTimersByTimeAsync(5 * 60 * 1000); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
|
|
||
| const conn = useDeviceStore | ||
| .getState() | ||
| .savedConnections.find((c) => c.id === 999); | ||
| expect(conn?.status).toBe("configured"); | ||
| expect(conn?.error).toBeUndefined(); | ||
|
|
||
| stopHeartbeat(999); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it("ignores late failure from stopped session after restart", async () => { | ||
| let rejectA!: (e: Error) => void; | ||
| const pendingA = new Promise<number>((_, rej) => { | ||
| rejectA = rej; | ||
| }); | ||
| const mdA = { heartbeat: vi.fn(() => pendingA) } as unknown as MeshDevice; | ||
|
|
||
| startMaintenanceHeartbeat(999, mdA); | ||
| await vi.advanceTimersByTimeAsync(5 * 60 * 1000); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| stopHeartbeat(999); | ||
| const mdB = mockMeshDevice(0); | ||
| useDeviceStore | ||
| .getState() | ||
| .updateSavedConnection(999, { status: "configured", error: undefined }); | ||
| startMaintenanceHeartbeat(999, mdB); | ||
|
|
||
| rejectA(new Error("stale fail")); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
|
|
||
| let conn = useDeviceStore | ||
| .getState() | ||
| .savedConnections.find((c) => c.id === 999); | ||
| expect(conn?.status).toBe("configured"); | ||
| expect(mdB.heartbeat).not.toHaveBeenCalled(); | ||
|
|
||
| await vi.advanceTimersByTimeAsync(5 * 60 * 1000); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| conn = useDeviceStore.getState().savedConnections.find((c) => c.id === 999); | ||
| expect(conn?.status).toBe("configured"); | ||
|
|
||
| stopHeartbeat(999); | ||
| vi.useRealTimers(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.