dbreads: the draining test waits for the last worker unit; deadline test margins
- TestADrainingBoxDoesNotPick ("no result for dbr_...", main 81db4870):
the in-process worker's unit count drops in its own goroutine after the
connection closes, as systemd's deactivation does. A pass before then
counts it as an orphan (reconcileLocked: units > conns) and admits
nothing, and this test gives each read exactly one pass. It now waits
for the healthy box's unit to go before that pass. Production passes
again on its ticker. A probe with the unit lingering 50 ms fails the old
loop 3 of 3 with the CI error.
- TestServeKeepsThePublishedDeadline (main f7644a9c, region-ha 23d219c6):
"a late answer inside the deadline" had 300 ms between the election's
answer and a 600 ms deadline to dial, and "an answer after the deadline"
allowed 150 ms over a deadline it is 100 ms past by construction. The
first case is now 1 s answer, 2.5 s deadline; the allowance is 500 ms
past the later of the deadline and the answer. A deadline taken afresh
at the answer (the bug the test guards) still ends the first case 1 s
late, twice the allowance.
Neither reproduced here under load (2 cores, 8 busy loops, the whole
package -count=10 -cpu 4: 0 of 740); the CI logs give the numbers. After:
0 failures in 300 runs of both tests (-cpu 1,2,4, 8 busy loops).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+33
−3
internal/dbreads/fixture_test.go+12 −0internal/dbreads/loop_test.go+7 −1internal/dbreads/read_test.go+14 −2
Intent
diff --git a/internal/dbreads/fixture_test.go b/internal/dbreads/fixture_test.go| index b6388798e..9f3bc3b6e 100644 |
| --- a/internal/dbreads/fixture_test.go |
| +++ b/internal/dbreads/fixture_test.go |
| @@ -112,6 +112,18 @@ func (w *inProcWorkers) units(context.Context) (int, error) { return int(w.live. |
| func plentyFree() (uint64, uint64, error) { return 100 << 30, 200 << 30, nil } |
| +// waitFor polls cond until it holds, failing the test after ten seconds. |
| +func waitFor(t *testing.T, what string, cond func() bool) { |
| + t.Helper() |
| + deadline := time.Now().Add(10 * time.Second) |
| + for !cond() { |
| + if time.Now().After(deadline) { |
| + t.Fatalf("timed out waiting for %s", what) |
| + } |
| + time.Sleep(time.Millisecond) |
| + } |
| +} |
| + |
| type fixture struct { |
| t *testing.T |
| store release.Store |
diff --git a/internal/dbreads/loop_test.go b/internal/dbreads/loop_test.go| index ea8e7441c..ac825ef47 100644 |
| --- a/internal/dbreads/loop_test.go |
| +++ b/internal/dbreads/loop_test.go |
| @@ -159,12 +159,18 @@ func TestADrainingBoxDoesNotPick(t *testing.T) { |
| c.Draining = flip.Load |
| c.Jitter = func() time.Duration { flip.Store(true); return time.Millisecond } |
| }) |
| - healthy, _ := fx.agent("uk-1") |
| + healthy, hw := fx.agent("uk-1") |
| for i := 0; i < 5; i++ { |
| req := fx.request("SELECT 1", "") |
| fx.submit(req) |
| drain.Pass(context.Background()) // first: it passes the first check, then drains in its jitter |
| drain.Wait() |
| + // The last read's worker unit goes away after its connection does, |
| + // as systemd's deactivation does: a pass before then counts it as an |
| + // orphan and admits nothing, and this test makes one pass a read |
| + // (CI 2026-10-26, "no result for dbr_..."). Production passes again |
| + // on its ticker; here, wait for the unit to go. |
| + waitFor(t, "the last worker unit to go", func() bool { return hw.live.Load() == 0 }) |
| healthy.Pass(context.Background()) |
| healthy.Wait() |
| if _, _, r := fx.answer(req.ID); r.Box != "uk-1" { |
diff --git a/internal/dbreads/read_test.go b/internal/dbreads/read_test.go| index ad0bd5d50..e6c7b8b60 100644 |
| --- a/internal/dbreads/read_test.go |
| +++ b/internal/dbreads/read_test.go |
| @@ -528,13 +528,23 @@ func (s slowPicked) Put(ctx context.Context, key string, body []byte, ifMatch st |
| // one from whenever the election's answer arrived (Astra round 1, finding |
| // 3): a hung read ends by picked.Deadline, and a read whose election |
| // answered after its deadline starts no work at all. |
| +// |
| +// The margins are wide enough for a loaded runner (CI 2026-10-25 and |
| +// 2026-10-27: a 600 ms deadline passed before the dial, and an answer 100 ms |
| +// late plus scheduling ran past a 150 ms allowance) and still tell the bug |
| +// apart: a deadline taken afresh when the election answered would end the |
| +// first case a whole delay (1 s) late, over twice the allowance. |
| func TestServeKeepsThePublishedDeadline(t *testing.T) { |
| t.Parallel() |
| + // slack is how long after the later of the published deadline and the |
| + // election's answer the read may end: the result's own write and the |
| + // scheduler. |
| + const slack = 500 * time.Millisecond |
| for name, c := range map[string]struct { |
| deadline, delay time.Duration |
| dials int64 |
| }{ |
| - "a late answer inside the deadline": {deadline: 600 * time.Millisecond, delay: 300 * time.Millisecond, dials: 1}, |
| + "a late answer inside the deadline": {deadline: 2500 * time.Millisecond, delay: time.Second, dials: 1}, |
| "an answer after the deadline": {deadline: 200 * time.Millisecond, delay: 300 * time.Millisecond, dials: 0}, |
| } { |
| t.Run(name, func(t *testing.T) { |
| @@ -561,7 +571,9 @@ func TestServeKeepsThePublishedDeadline(t *testing.T) { |
| if r.State != StateFailed || r.Code != CodeInterrupted || w.dials.Load() != c.dials { |
| t.Fatalf("result %+v, %d worker connections", r, w.dials.Load()) |
| } |
| - if over := r.Ended.Sub(p.Deadline); over > 150*time.Millisecond { |
| + // An answer after the deadline cannot end the read before it |
| + // arrives: that much past the deadline is allowed too. |
| + if over := r.Ended.Sub(p.Deadline); over > slack+max(0, c.delay-c.deadline) { |
| t.Fatalf("the read ran %s past the deadline it published", over) |
| } |
| }) |