Artifact a3c86a52a80c9f82ef6dc84b89d289f0b2d65d0e5dddf71d766d6bc21ad722d6:
- File
internal/work/work_test.go
— part of check-in
[45bb75ade2]
at
2026-08-17 21:26:48
on branch trunk
— work: bound bulk operations; footer times keep their precision
Nothing in quarry limited concurrency — every zit subprocess ran in the request goroutine with the arrival rate as the only bound. work.Lane is a concurrency limit plus a queue cap that SHEDS past it: a request accepted into a queue it cannot be served from consumes the capacity that would have served someone else.
Wired to archive first, which buffers 256 MiB per request on an anonymous route. That is latent rather than live — archive 503s on zit#97 — which is why the bound belongs there now, so the endpoint is born bounded rather than acquiring a limit after someone finds it.
Footer precision: '<1ms' was true and useless once most pages went sub-millisecond, collapsing the interesting range into one label. timing.Format keeps ~3 significant figures across five orders of magnitude, so 900µs and 90µs no longer read the same. Fossil has the mirror-image bug: three decimals of seconds prints everything faster than a millisecond as 0.000s. (user: dmestas, size: 3076) [annotate] [blame] [check-ins using]
package work import ( "context" "errors" "sync" "sync/atomic" "testing" "time" ) // The limit is the whole point: without it the file page forked a // subprocess per request and throughput went backwards under load. func TestLaneNeverExceedsItsLimit(t *testing.T) { l := NewLane("test", 3, 100) var now, peak atomic.Int64 var wg sync.WaitGroup for i := 0; i < 40; i++ { wg.Add(1) go func() { defer wg.Done() l.Do(context.Background(), func() error { n := now.Add(1) for { p := peak.Load() if n <= p || peak.CompareAndSwap(p, n) { break } } time.Sleep(2 * time.Millisecond) now.Add(-1) return nil }) }() } wg.Wait() if got := peak.Load(); got > 3 { t.Errorf("ran %d concurrently, limit is 3", got) } if got := peak.Load(); got < 2 { t.Errorf("peak concurrency %d — the lane serialized instead of admitting its limit", got) } } // Past the cap a lane must REFUSE, not accept work it cannot serve. // An accepted request that times out consumed the capacity that would // have served someone else. func TestLaneShedsPastItsQueue(t *testing.T) { l := NewLane("test", 1, 0) // one at a time, no waiting room release := make(chan struct{}) started := make(chan struct{}) go l.Do(context.Background(), func() error { close(started) <-release return nil }) <-started err := l.Do(context.Background(), func() error { t.Error("ran a function the lane should have refused") return nil }) if !errors.Is(err, ErrBusy) { t.Fatalf("err = %v, want ErrBusy", err) } if s := l.Stats(); s.Shed != 1 { t.Errorf("Shed = %d, want 1 — the refusal must be counted, it is the scaling signal", s.Shed) } close(release) } // A caller who gives up while queued must not run later against a // context nobody is waiting on. func TestLaneHonoursCancellation(t *testing.T) { l := NewLane("test", 1, 10) release := make(chan struct{}) started := make(chan struct{}) go l.Do(context.Background(), func() error { close(started) <-release return nil }) <-started ctx, cancel := context.WithCancel(context.Background()) cancel() var ran bool err := l.Do(ctx, func() error { ran = true; return nil }) if !errors.Is(err, context.Canceled) { t.Errorf("err = %v, want context.Canceled", err) } if ran { t.Error("ran the function for a caller who had already given up") } close(release) } // The error from fn must reach the caller unchanged; a lane is a gate, // not a wrapper. func TestLanePassesErrorsThrough(t *testing.T) { l := NewLane("test", 2, 2) want := errors.New("boom") if got := l.Do(context.Background(), func() error { return want }); !errors.Is(got, want) { t.Errorf("err = %v, want %v", got, want) } } // A nil lane runs the work — so a caller with no lane configured is not // a special case at every call site. func TestNilLaneRuns(t *testing.T) { var l *Lane ran := false if err := l.Do(context.Background(), func() error { ran = true; return nil }); err != nil { t.Fatal(err) } if !ran { t.Error("nil lane did not run the work") } }