edge-roll-blip: cancel-during-hold test proves no Eject
Drive the multiProxy DialContext directly: the fake dialer signals entered and dialAborted, so the test proves the cancel reaches the attempt and that the post-dial branch does not Eject. An http.Client version cannot show this because http.Transport detaches the dial context from request cancellation.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+63
−29
internal/edge/localhold_probed_test.go+63 −29
diff --git a/internal/edge/localhold_probed_test.go b/internal/edge/localhold_probed_test.go| index c35cf00a7..1b2ad1154 100644 |
| --- a/internal/edge/localhold_probed_test.go |
| +++ b/internal/edge/localhold_probed_test.go |
| @@ -2,12 +2,14 @@ package edge |
| import ( |
| "context" |
| + "errors" |
| "fmt" |
| "io" |
| "net" |
| "net/http" |
| "net/http/httptest" |
| "slices" |
| + "sync" |
| "sync/atomic" |
| "testing" |
| "time" |
| @@ -397,63 +399,95 @@ func TestProbedHoldSurvivesReload(t *testing.T) { |
| } |
| } |
| -// TestProbedClientCancelDuringHoldDoesNotEject: a client that gives up while |
| -// its request is held returns at once, and the abandoned dial is not taken |
| -// for the upstream failing. The local address refuses and is ejected by the |
| -// request itself; the remote one hangs in dial until the request is |
| -// cancelled, and must still be live afterwards. |
| +// TestProbedClientCancelDuringHoldDoesNotEject: when the request context ends |
| +// while the multi-upstream dial is parked on an upstream, the dial stops at |
| +// once and the upstream is not taken for failing. The local address refuses |
| +// and is ejected by the dial itself; the remote one hangs until its context |
| +// ends, and must still be live afterwards. |
| +// |
| +// This drives the Transport's DialContext directly, with a context the test |
| +// cancels. An end-to-end version (cancel an http.Client request, watch the |
| +// edge) cannot prove this: since Go 1.23 http.Transport hands DialContext a |
| +// context detached from the request's cancellation (context.WithoutCancel), |
| +// so a client that walks away never reaches the dial at all, and http.Client.Do |
| +// returns on its own context whatever the edge does. |
| func TestProbedClientCancelDuringHoldDoesNotEject(t *testing.T) { |
| - const remote = "10.0.0.9:80" |
| + const ( |
| + host = "shop.example.com" |
| + remote = "10.0.0.9:80" |
| + ) |
| hold := probedHold() |
| var plain net.Dialer |
| + // entered: the dial is parked on the remote. dialAborted: that attempt saw |
| + // its context end, i.e. the cancel reached the attempt's context. |
| + entered := make(chan struct{}) |
| + dialAborted := make(chan struct{}) |
| + var enteredOnce, abortedOnce sync.Once |
| hold.dialer = func(ctx context.Context, network, addr string) (net.Conn, error) { |
| if addr == remote { |
| + enteredOnce.Do(func() { close(entered) }) |
| <-ctx.Done() |
| + abortedOnce.Do(func() { close(dialAborted) }) |
| return nil, ctx.Err() |
| } |
| return plain.DialContext(ctx, network, addr) |
| } |
| - e, srv := probedEdge(t, []string{deadAddr(t), remote}, frozenHealth(), hold) |
| + e, _ := probedEdge(t, []string{deadAddr(t), remote}, frozenHealth(), hold) |
| if got := mustHold(t, e); got < 2*time.Second { |
| t.Fatalf("Hold = %v, want a hold far longer than the cancel below", got) |
| } |
| - if live := e.probers.Live("shop.example.com"); len(live) != 2 { |
| - t.Fatalf("live set = %v, want both upstreams live before the request", live) |
| + if live := e.probers.Live(host); len(live) != 2 || !slices.Contains(live, remote) { |
| + t.Fatalf("live set = %v, want both upstreams live before the dial", live) |
| + } |
| + tr, ok := multiProxy(host, e.probers, hold).Transport.(*http.Transport) |
| + if !ok { |
| + t.Fatal("multiProxy transport is not an *http.Transport") |
| } |
| ctx, cancel := context.WithCancel(context.Background()) |
| defer cancel() |
| - req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL, nil) |
| - if err != nil { |
| - t.Fatalf("new request: %v", err) |
| - } |
| - req.Host = "shop.example.com" |
| type result struct { |
| - resp *http.Response |
| + conn net.Conn |
| err error |
| - at time.Time |
| } |
| res := make(chan result, 1) |
| go func() { |
| - resp, err := http.DefaultClient.Do(req) |
| - res <- result{resp, err, time.Now()} |
| + conn, err := tr.DialContext(ctx, "tcp", multiDialHost) |
| + res <- result{conn, err} |
| }() |
| - // Let the request reach the hung remote dial (the refused local one is |
| - // ejected on the way, in whichever order they come), then walk away. |
| - time.Sleep(150 * time.Millisecond) |
| - cancelled := time.Now() |
| + // Wait for the dial to be parked on the remote (the refused local one is |
| + // ejected on the way, in whichever order they come), then cancel. |
| + select { |
| + case <-entered: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("the dial never reached the remote upstream") |
| + } |
| cancel() |
| - r := <-res |
| + // The cancel must reach the attempt: a dial that ignored its context |
| + // would sit here until multiDialTimeout. |
| + select { |
| + case <-dialAborted: |
| + case <-time.After(500 * time.Millisecond): |
| + t.Fatal("the remote dial was not aborted within 500ms of the cancel") |
| + } |
| + var r result |
| + select { |
| + case r = <-res: |
| + case <-time.After(500 * time.Millisecond): |
| + t.Fatal("DialContext did not return within 500ms of the cancel") |
| + } |
| if r.err == nil { |
| - r.resp.Body.Close() |
| - t.Fatalf("Do succeeded with status %d, want the cancelled request to fail", r.resp.StatusCode) |
| + r.conn.Close() |
| + t.Fatal("DialContext succeeded, want the cancelled dial to fail") |
| } |
| - if el := r.at.Sub(cancelled); el > 500*time.Millisecond { |
| - t.Fatalf("Do returned %v after the cancel, want within 500ms", el) |
| + if !errors.Is(r.err, context.Canceled) { |
| + t.Fatalf("DialContext error = %v, want the cancellation", r.err) |
| } |
| - if live := e.probers.Live("shop.example.com"); !slices.Contains(live, remote) { |
| - t.Fatalf("live set = %v, want %s still live: the client left, the upstream did not fail", live, remote) |
| + // DialContext has returned, so its post-dial branch (where an Eject would |
| + // happen) has run: the remote must have stayed live. |
| + if live := e.probers.Live(host); !slices.Contains(live, remote) { |
| + t.Fatalf("live set = %v, want %s still live: the caller left, the upstream did not fail", live, remote) |
| } |
| } |