TestFXStateDoesNotWait held the rate's endpoint and required /api/state
to come back within one second. Under load (~100-200 here) the state
took longer than that on its own and the test went red with nothing
waiting on the endpoint (seen in the full run of e085d6e92; it passed
5 of 5 alone on that tree and on main 252bc61ce).
The endpoint now answers only once the first look is back, so that
look coming back at all is the proof that the state doesn't wait on
it; the guard is a minute, and the new-rate loop's deadline is a
minute too. A sync.Once releases the endpoint from the test and its
cleanup alike.
Run at this head (252bc61ce + this change):
- broken by hand, red both ways: currentFX on fx.Get with a 4s timeout
-> "the new rate never came in"; fx.Get(context.Background()) -> "the
state waited on the rate's endpoint"
- go test -race -count=10 -cpu 1,2 -run TestFXStateDoesNotWait
./internal/gui: ok
- go test -tags nogui -timeout 45m ./...: 64 packages ok, internal/gui
ok; internal/agent (#2544) and internal/gateway (#2108, #2191) timed
out at 45m at load ~190. Only internal/gui's test file changed, and
neither package imports it.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet ./internal/gui ok
The rate is internal/fx: a free, no-key endpoint (open.er-api.com/v6/latest/USD), cached in memory and in a small file beside magpie's other caches (XDG_CACHE_HOME/magpie/fxrate.json) for 12h, refetched only once that's stale, and falling back to the last rate it ever had — or a fixed 7.2 — while offline; the currency row's tooltip names the rate and when it was learned. usage.FormatCost carries the $/¥ formatting (2 decimals above $1, 3 below, whole numbers above 100) that usage_cli.go and internal/tui/pages.go used to each have their own copy of; both now read Settings' currency once per command/redraw rather than per row. internal/gui/api.go exposes the rate on /api/settings always and on /api/state only when cny is actually chosen, so usd users' hot path never touches the network for a rate nothing shows with.
Tested with new internal/fx/fx_test.go (a fake server: fetches and caches, answers from memory then from the cache file with no further calls, refetches once stale, falls back to a stale cache or Fallback when offline, ignores a reply with no CNY rate), TestFXInState in internal/gui/fx_test.go, TestFmtCostShowsChosenCurrency in internal/tui/usage_test.go, and a new internal/gui/tests/currency.test.cjs in Chromium and WebKit, English and Chinese (the Usage page's $ by default and ¥12.34×7.2 once cny is picked, the currency row's labels/state/tooltip, a real wheel scroll deep into Settings left alone by picking a currency, the choice surviving a reload). go build -tags nogui, GOOS=linux and GOOS=windows builds, and go test -tags nogui -count=1 ./... all pass clean.
A fetch that failed is not tried again for 10 minutes (the stale or fixed rate shown at once meanwhile), so offline each state() does not wait up to 4s on the network: TestGetDoesNotRetryAFailedFetchAtOnce.