Repository navigation
fix(settings): share solar-position reads between pollers #914
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| currentSolarPosition: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/ipc", () => ({ | ||
| currentSolarPosition: mocks.currentSolarPosition, | ||
| })); | ||
| vi.mock("@/store/settings.store", () => ({ | ||
| settingsStore: { config: { position_source: { type: "AUTOMATIC" } } }, | ||
| })); | ||
|
|
||
| const source = { type: "AUTOMATIC" as const, cache_minutes: 15 }; | ||
|
|
||
| async function freshLoader() { | ||
| vi.resetModules(); | ||
|
|
||
| return await import("./useSolarPosition"); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(new Date(0)); | ||
| mocks.currentSolarPosition.mockImplementation(async () => ({ | ||
| altitude: 12, | ||
| azimuth: 34, | ||
| })); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| describe("solar position polling", () => { | ||
| test("simultaneous callers share one backend call", async () => { | ||
| const { loadSolarPosition } = await freshLoader(); | ||
|
|
||
| const [a, b] = await Promise.all([ | ||
| loadSolarPosition(source), | ||
| loadSolarPosition(source), | ||
| ]); | ||
|
|
||
| expect(a).toEqual(b); | ||
| expect(mocks.currentSolarPosition).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test("a reading younger than the TTL is reused", async () => { | ||
| const { loadSolarPosition } = await freshLoader(); | ||
|
|
||
| await loadSolarPosition(source); | ||
| await loadSolarPosition(source); | ||
|
|
||
| expect(mocks.currentSolarPosition).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test("the next poll after the TTL asks the backend again", async () => { | ||
| const { loadSolarPosition } = await freshLoader(); | ||
|
|
||
| await loadSolarPosition(source); | ||
| vi.advanceTimersByTime(30_001); | ||
| await loadSolarPosition(source); | ||
|
|
||
| expect(mocks.currentSolarPosition).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| test("different position sources are not shared", async () => { | ||
| const { loadSolarPosition } = await freshLoader(); | ||
|
|
||
| await loadSolarPosition(source); | ||
| await loadSolarPosition({ | ||
| type: "MANUAL", | ||
| latitude: 1, | ||
| longitude: 2, | ||
| altitude: 0, | ||
| }); | ||
|
|
||
| expect(mocks.currentSolarPosition).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| test("a failed read is retried instead of cached", async () => { | ||
| const { loadSolarPosition } = await freshLoader(); | ||
| mocks.currentSolarPosition.mockRejectedValueOnce(new Error("denied")); | ||
|
|
||
| await expect(loadSolarPosition(source)).rejects.toThrow("denied"); | ||
| await loadSolarPosition(source); | ||
|
|
||
| expect(mocks.currentSolarPosition).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,13 +12,61 @@ interface Options { | |
| pollMs?: number; | ||
| } | ||
|
|
||
| type PositionSource = Parameters<typeof currentSolarPosition>[0]; | ||
| type SolarPosition = Awaited<ReturnType<typeof currentSolarPosition>>; | ||
|
|
||
| /** | ||
| * How long a reading is shared between callers. | ||
| * | ||
| * The stage and the engine panel each poll once a minute, so three windows of the same | ||
| * reading overlapped and each invoke landed on a different blocking thread. The Rust | ||
| * position cache is thread-local, so those calls did not share a sensor read either. | ||
| * Sharing the result here makes the window cost one call per interval, while staying | ||
| * well below the backend's 60 s maximum reading age. | ||
| */ | ||
| const POSITION_CACHE_TTL_MS = 30_000; | ||
|
|
||
| let cached: { key: string; at: number; value: SolarPosition } | undefined; | ||
| let inflight: { key: string; promise: Promise<SolarPosition> } | undefined; | ||
|
|
||
| /** | ||
| * Loads a position, sharing a fresh reading and an in-flight request between callers. | ||
| * | ||
| * Errors are not cached: a denied location or a starting sensor must be retried by the | ||
| * next poll rather than replayed for the TTL. | ||
| */ | ||
| export const loadSolarPosition = ( | ||
| source: PositionSource, | ||
| ): Promise<SolarPosition> => { | ||
| const key = JSON.stringify(source); | ||
| const now = Date.now(); | ||
|
|
||
| if (cached?.key === key && now - cached.at < POSITION_CACHE_TTL_MS) { | ||
| return Promise.resolve(cached.value); | ||
| } | ||
|
|
||
| if (inflight?.key === key) return inflight.promise; | ||
|
|
||
| const promise = currentSolarPosition(source) | ||
| .then((value) => { | ||
| cached = { key, at: Date.now(), value }; | ||
| return value; | ||
| }) | ||
| .finally(() => { | ||
| if (inflight?.promise === promise) inflight = undefined; | ||
| }); | ||
|
|
||
| inflight = { key, promise }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Retain pending requests for each source. If A is pending, a request for B replaces the only Use a source-keyed map of pending promises. Remove only the matching promise when it completes. This preserves separate reads for different sources and lets A/B/A share the first A read.
|
||
| return promise; | ||
| }; | ||
|
|
||
| export function useSolarPosition(options: Options = {}) { | ||
| const [position, { refetch }] = createResource( | ||
| () => | ||
| (options.enabled?.() ?? true) | ||
| ? settingsStore.config?.position_source | ||
| : undefined, | ||
| (ps) => currentSolarPosition(ps), | ||
| (ps) => loadSolarPosition(ps), | ||
| ); | ||
|
|
||
| const pollMs = options.pollMs; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clear the mock call history in
beforeEach.currentSolarPositionkeeps its call history across tests in this file. Vitest does not clear it by default, and the config does not enable clearing. The first test leaves one call. The cache-reuse test then adds one more backend call, so itstoHaveBeenCalledTimes(1)check at line 54 sees a total of 2 and fails. The later call-count assertions fail for the same reason.Clear the mock before each test, as the other store test files do:
Suggested fix
Committable suggestion