From 43ed7dd7e8db00a7988818d2348110f25be88993 Mon Sep 17 00:00:00 2001 From: randomizedcoder Date: Fri, 4 Dec 2026 22:54:20 -0800 Subject: [PATCH] xtcp: poll the drop in TestTCPDest_writeTimeout, not the first write error The test polled destTCP/Write/error >= 1 and then read the dropped counter once. With attempts = 2 the first write error is counted when the 100 ms deadline fires (when the test's 20 ms poll also wakes), the second attempt fails at once on the closed pipe, and only then is the drop counted, so a poll landing between them failed the test: 3 of 20 runs on main, 17 of 300 with -race -cpu 1,2,8. It now polls the drop, the worker's last step, and then checks one write error per attempt. 300 of 300 with -race -cpu 1,2,8. Co-Authored-By: Claude Opus 5.5 --- pkg/xtcp/destinations_tcp_test.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/xtcp/destinations_tcp_test.go b/pkg/xtcp/destinations_tcp_test.go index 78df5ef54..898b75019 100644 --- a/pkg/xtcp/destinations_tcp_test.go +++ b/pkg/xtcp/destinations_tcp_test.go @@ -185,22 +185,27 @@ func TestTCPDest_writeTimeout(t *testing.T) { t.Fatalf("Send: %v", err) } - // The write must fail within ~attempts*timeout, not park for the - // retransmit window. Poll the write-error counter. + // The envelope must be dropped within ~attempts*timeout, not park for + // the retransmit window. Poll the drop, the worker's last step: the + // first write error is counted when the deadline fires, the second + // attempt fails at once on the closed pipe, and only then is the drop + // counted, so polling the write-error counter and then reading the + // drop raced the second attempt. deadline := time.After(5 * time.Second) for { - if got := testutil.ToFloat64(x.pC.WithLabelValues("destTCP", "Write", "error")); got >= 1 { + if got := testutil.ToFloat64(x.pC.WithLabelValues("destTCP", "dropped", "error")); got >= 1 { break } select { case <-deadline: - t.Fatalf("the blocked write never errored (elapsed %v)", time.Since(started)) + t.Fatalf("the blocked write was never dropped (elapsed %v, write errors %v)", + time.Since(started), testutil.ToFloat64(x.pC.WithLabelValues("destTCP", "Write", "error"))) default: time.Sleep(20 * time.Millisecond) } } - if got := testutil.ToFloat64(x.pC.WithLabelValues("destTCP", "dropped", "error")); got < 1 { - t.Errorf("destTCP dropped counter = %v, want >= 1 (attempts exhausted)", got) + if got := testutil.ToFloat64(x.pC.WithLabelValues("destTCP", "Write", "error")); got != float64(td.attempts) { + t.Errorf("destTCP write errors = %v, want %d (one per attempt)", got, td.attempts) } if err := dest.Close(); err != nil { t.Errorf("Close: %v", err)