Files
StarPilot/openpilot/starpilot/galaxy/tests/test_polling.mjs
T
firestar5683 00364fd645 Galaxy Access
Separate Galaxy authentication and local access policy from web presentation.

Co-authored-by: Danny <dannyirwin31@yahoo.com>
Co-authored-by: Prabhaav Pillai <prabhaav@msu.edu>
Co-authored-by: dirwin31 <83434411+dirwin31@users.noreply.github.com>
Co-authored-by: firestarsdog <229254897+firestarsdog@users.noreply.github.com>
2026-10-14 19:56:17 -05:00

24 lines
972 B
JavaScript

import assert from "node:assert/strict"
import { PollTimer } from "../web/js/polling.js"
const timers = new Map()
let next = 0, reads = 0, finish
const poller = new PollTimer({ read: () => { reads++; return new Promise(resolve => { finish = resolve }) },
later: fn => { timers.set(++next, fn); return next }, cancel: id => timers.delete(id) })
poller.start()
const retired = [...timers.values()][0]
poller.stop(); poller.start()
await retired()
assert.equal(reads, 0, "a retired callback must not read after restart")
const [id, tick] = [...timers.entries()][0]
timers.delete(id)
const running = tick()
assert.equal(reads, 1)
assert.equal(timers.size, 0, "polls do not overlap an outstanding read")
poller.stop(); poller.start()
finish(); await running
assert.equal(timers.size, 1, "a retired read must not create another polling loop")
poller.stop()
assert.equal(timers.size, 0)
console.log("Polling lifecycle: stale callbacks, single flight and stop/restart passed")