magpie icon indicating copy to clipboard operation
magpie copied to clipboard

middleware: TestDocsExample red under load (strategies.fewest-tokens: took too long)

Open yetone opened this issue 1 month ago • 2 comments

internal/middleware's TestDocsExample went red in a full go test -tags nogui -count=1 -timeout 45m ./... run on macOS (load average ~140) at bf931f82c plus the uncommitted #2671 change (provider found groups; nothing in middleware or routes):

2026/10/17 18:44:51 middleware .../TestDocsExample2088259992/001/spread.gateway.js [error]: strategies.fewest-tokens: took too long
--- FAIL: TestDocsExample (0.04s)
    points_test.go:348: [0 1 2 3] {[] []  0 0 1 strategies.fewest-tokens: took too long [spread/fewest-tokens] 1 true 0 0}
FAIL	github.com/yetone/magpie/internal/middleware	24.227s

Run alone right after on the same tree, it passes (3.2s).

The strategy call runs under the middleware's wall-clock budget (requestLimit 250ms / eventLimit 50ms in middleware.go, in.rt.Interrupt(errTimeout)). On a busy machine the docs example's fewest-tokens order went past it, so routes.Apply fell back to the input order [0 1 2 3].

What the test is about is the example's order. The budget is a separate guarantee, and it is also what a user's strategy meets on a busy machine. The test should either hold the budget out of its way or measure CPU, not wall time. Filed so it isn't left as "passes alone"; not worked on here.

yetone avatar Oct 17 '26 11:10 yetone

Fixed on fix/2728-docs-strategy-test-budget, commit c01aa5a1, based on upstream ed0ab567.

TestDocsExample now temporarily holds its strategy budget out of the ordering assertion and restores it afterwards. The current production strategy budget is 20 ms; production behavior is unchanged, and the separate fail-open test still verifies that a spinning strategy times out.

The original load-dependent failure was not naturally reproduced. With a deliberately exhausted budget and temporary synchronization on the timer interrupt, the old test fails with identity ordering and the adjusted test passes. Reversing the real JS ordering or making it throw still makes the ordering test fail. Related middleware tests pass under -race, including the independent timeout test; scoped middleware/routes vet and build pass. I independently reran the related race tests.

The full suite, full application build and cross-platform runtime were not run. AI-assisted implementation and review.

skygunner avatar Nov 08 '26 20:11 skygunner

For whoever picks this up: the branch on #5965 gives every middleware test room past the limits from the package's TestMain (the limit tests set their own), which covers TestDocsExample too; with #3673's 25ms after each arm it goes red without that line and passes with it. skygunner's c01aa5a1 here does the same for TestDocsExample alone, so only one of the two is needed.

yinwu01 avatar Nov 10 '26 19:11 yinwu01