| index 293c1ce96..c35cf00a7 100644 |
| --- a/internal/edge/localhold_probed_test.go |
| +++ b/internal/edge/localhold_probed_test.go |
| @@ -2,6 +2,7 @@ package edge |
| |
| import ( |
| "context" |
| + "fmt" |
| "io" |
| "net" |
| "net/http" |
| @@ -66,9 +67,11 @@ func TestHoldMatchesFormula(t *testing.T) { |
| } |
| |
| // probedHold is the shortened hold the D10b behaviour tests run on unless |
| -// they say otherwise: one upstream gets min(300ms + 250ms, 5s) = 550ms. |
| +// they say otherwise: one upstream gets min(800ms + 250ms, 5s) = 1050ms. The |
| +// generous dial leaves the tests that restart an upstream at 100-200ms |
| +// 600ms of slack on a loaded CI box. |
| func probedHold() localHold { |
| - return localHold{dial: 300 * time.Millisecond, cap: 5 * time.Second, probe: 50 * time.Millisecond} |
| + return localHold{dial: 800 * time.Millisecond, cap: 5 * time.Second, probe: 50 * time.Millisecond} |
| } |
| |
| // fastHealth probes every 100ms, fails on one miss and rejoins on two passes. |
| @@ -165,7 +168,7 @@ func mustHold(t *testing.T, e *Edge) time.Duration { |
| } |
| |
| // want503At checks a held request's 503: Retry-After 2, cause |
| -// CauseNoUpstream, and elapsed in [at, at+150ms). |
| +// CauseNoUpstream, and elapsed in [at, at+300ms). |
| func want503At(t *testing.T, resp *http.Response, el, at time.Duration, sink *memErrSink) { |
| t.Helper() |
| if resp.StatusCode != http.StatusServiceUnavailable { |
| @@ -174,8 +177,8 @@ func want503At(t *testing.T, resp *http.Response, el, at time.Duration, sink *me |
| if got := resp.Header.Get("Retry-After"); got != "2" { |
| t.Fatalf("Retry-After = %q, want 2", got) |
| } |
| - if el < at || el >= at+150*time.Millisecond { |
| - t.Fatalf("503 after %v, want within 150ms of %v", el, at) |
| + if el < at || el >= at+300*time.Millisecond { |
| + t.Fatalf("503 after %v, want within 300ms of %v", el, at) |
| } |
| waitFor(t, time.Second, "the edge error record", func() bool { return len(sink.snap()) > 0 }) |
| recs := sink.snap() |
| @@ -189,7 +192,7 @@ func TestProbedLocalStillDownPastHold(t *testing.T) { |
| sink := &memErrSink{} |
| e.EnableErrors(sink) |
| hold := mustHold(t, e) |
| - if want := 300*time.Millisecond + readmitBound(1, 100*time.Millisecond, 2, 50*time.Millisecond); hold != want { |
| + if want := probedHold().dial + readmitBound(1, 100*time.Millisecond, 2, 50*time.Millisecond); hold != want { |
| t.Fatalf("Hold = %v, want %v", hold, want) |
| } |
| resp, el := probedGet(t, srv.URL) |
| @@ -220,8 +223,8 @@ func TestProbedRemoteOnlyAnswersAtOnce(t *testing.T) { |
| if resp.StatusCode != http.StatusServiceUnavailable || resp.Header.Get("Retry-After") != "2" { |
| t.Fatalf("status = %d Retry-After %q, want 503 and 2", resp.StatusCode, resp.Header.Get("Retry-After")) |
| } |
| - if el > 100*time.Millisecond { |
| - t.Fatalf("503 after %v, want within 100ms", el) |
| + if el > 300*time.Millisecond { |
| + t.Fatalf("503 after %v, want within 300ms (a hold would take a second)", el) |
| } |
| } |
| |
| @@ -232,8 +235,8 @@ func TestProbedMixedSetWithLiveUpstreamServesAtOnce(t *testing.T) { |
| if resp.StatusCode != http.StatusOK || hits.Load() != 1 { |
| t.Fatalf("status = %d, live upstream hits = %d, want 200 from it", resp.StatusCode, hits.Load()) |
| } |
| - if el > 100*time.Millisecond { |
| - t.Fatalf("200 after %v, want within 100ms", el) |
| + if el > 300*time.Millisecond { |
| + t.Fatalf("200 after %v, want within 300ms (a hold would take a second)", el) |
| } |
| } |
| |
| @@ -275,14 +278,30 @@ func TestProbedDelayedProbeServedWithinR(t *testing.T) { |
| |
| // The upstream comes back 100ms into the request's hold; a watcher |
| // stamps the moment the prober readmits it. |
| + // The goroutine never touches t: it reports a listen failure on listenErr |
| + // and owns the restarted server, which it closes when the test's cleanup |
| + // (registered before it starts, so an early Fatalf cannot skip it) stops |
| + // it and waits for it to finish. |
| var hits atomic.Int64 |
| back := make(chan time.Time, 1) |
| readmitted := make(chan time.Time, 1) |
| + listenErr := make(chan error, 1) |
| + stop := make(chan struct{}) |
| + done := make(chan struct{}) |
| + t.Cleanup(func() { |
| + close(stop) |
| + <-done |
| + }) |
| go func() { |
| - time.Sleep(100 * time.Millisecond) |
| + defer close(done) |
| + select { |
| + case <-time.After(100 * time.Millisecond): |
| + case <-stop: |
| + return |
| + } |
| ln, err := net.Listen("tcp", a) |
| if err != nil { |
| - t.Errorf("listen %s: %v", a, err) |
| + listenErr <- fmt.Errorf("listen %s: %w", a, err) |
| close(back) |
| close(readmitted) |
| return |
| @@ -291,12 +310,19 @@ func TestProbedDelayedProbeServedWithinR(t *testing.T) { |
| s.Listener.Close() |
| s.Listener = ln |
| s.Start() |
| - t.Cleanup(s.Close) |
| + defer s.Close() |
| back <- time.Now() |
| deadline := time.Now().Add(5 * time.Second) |
| for time.Now().Before(deadline) { |
| + select { |
| + case <-stop: |
| + close(readmitted) |
| + return |
| + default: |
| + } |
| if slices.Contains(e.probers.Live("shop.example.com"), a) { |
| readmitted <- time.Now() |
| + <-stop // keep serving until the test is over |
| return |
| } |
| time.Sleep(2 * time.Millisecond) |
| @@ -313,6 +339,11 @@ func TestProbedDelayedProbeServedWithinR(t *testing.T) { |
| upAt, ok1 := <-back |
| inAt, ok2 := <-readmitted |
| if !ok1 || !ok2 { |
| + select { |
| + case err := <-listenErr: |
| + t.Fatal(err) |
| + default: |
| + } |
| t.Fatalf("upstream never came back or was never readmitted") |
| } |
| if took := inAt.Sub(upAt); took > r { |
| @@ -365,3 +396,64 @@ func TestProbedHoldSurvivesReload(t *testing.T) { |
| t.Fatalf("status = %d after %v, hits = %d, want 200 across the reload", resp.StatusCode, el, hits.Load()) |
| } |
| } |
| + |
| +// 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. |
| +func TestProbedClientCancelDuringHoldDoesNotEject(t *testing.T) { |
| + const remote = "10.0.0.9:80" |
| + hold := probedHold() |
| + var plain net.Dialer |
| + hold.dialer = func(ctx context.Context, network, addr string) (net.Conn, error) { |
| + if addr == remote { |
| + <-ctx.Done() |
| + return nil, ctx.Err() |
| + } |
| + return plain.DialContext(ctx, network, addr) |
| + } |
| + e, srv := 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) |
| + } |
| + |
| + 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 |
| + err error |
| + at time.Time |
| + } |
| + res := make(chan result, 1) |
| + go func() { |
| + resp, err := http.DefaultClient.Do(req) |
| + res <- result{resp, err, time.Now()} |
| + }() |
| + |
| + // 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() |
| + cancel() |
| + |
| + r := <-res |
| + if r.err == nil { |
| + r.resp.Body.Close() |
| + t.Fatalf("Do succeeded with status %d, want the cancelled request to fail", r.resp.StatusCode) |
| + } |
| + if el := r.at.Sub(cancelled); el > 500*time.Millisecond { |
| + t.Fatalf("Do returned %v after the cancel, want within 500ms", el) |
| + } |
| + 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) |
| + } |
| +} |