|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | + "sync/atomic" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "nvpair-shared/httpcon/testclient" |
| 16 | +) |
| 17 | + |
| 18 | +func TestProbeHTTPReusesConnections(t *testing.T) { |
| 19 | + test := func(name string, chunked bool) { |
| 20 | + t.Run(name, func(t *testing.T) { |
| 21 | + var requests atomic.Int32 |
| 22 | + client, connections := testclient.New(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 23 | + if r.Header.Get(engineIdentityProbeHeader) != "1" { |
| 24 | + t.Error("missing engine identity header") |
| 25 | + } |
| 26 | + status := http.StatusOK |
| 27 | + if requests.Add(1)%2 == 0 { |
| 28 | + status = http.StatusServiceUnavailable |
| 29 | + } |
| 30 | + w.WriteHeader(status) |
| 31 | + if chunked { |
| 32 | + _ = http.NewResponseController(w).Flush() |
| 33 | + } |
| 34 | + _, _ = io.WriteString(w, `{"models":[],"status":"responding"}`) |
| 35 | + })) |
| 36 | + ex := &Executor{client: client} |
| 37 | + probe := &Probe{HTTP: "http://127.0.0.1:{port}/api/version"} |
| 38 | + const rounds = 32 |
| 39 | + for i := 0; i < rounds; i++ { |
| 40 | + if got, want := ex.probe(context.Background(), probe, 1), i%2 == 0; got != want { |
| 41 | + t.Fatalf("poll %d: healthy = %v, want %v", i, got, want) |
| 42 | + } |
| 43 | + } |
| 44 | + if got := connections.Count(); got != 1 { |
| 45 | + t.Fatalf("accepted %d HTTP/1 connections for %d polls, want 1", got, rounds) |
| 46 | + } |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + test("content-length", false) |
| 51 | + test("chunked", true) |
| 52 | +} |
| 53 | + |
| 54 | +func TestProbeHTTPBoundsBodyDrain(t *testing.T) { |
| 55 | + body := &healthProbeBody{reader: strings.NewReader(strings.Repeat("x", 4<<20))} |
| 56 | + ex := &Executor{client: &http.Client{Transport: healthProbeTransport(func(*http.Request) (*http.Response, error) { |
| 57 | + return &http.Response{StatusCode: http.StatusAccepted, Body: body, Header: make(http.Header)}, nil |
| 58 | + })}} |
| 59 | + probe := &Probe{HTTP: "http://127.0.0.1:{port}/", Status: http.StatusAccepted} |
| 60 | + if !ex.probe(context.Background(), probe, 1) { |
| 61 | + t.Fatal("body drain changed the configured HTTP status health result") |
| 62 | + } |
| 63 | + if body.read == 0 || body.read > 1<<20 || !body.closed { |
| 64 | + t.Fatalf("body read = %d, closed = %v; want bounded drain and close", body.read, body.closed) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestProbeHTTPBodyDrainHonorsDeadline(t *testing.T) { |
| 69 | + var body *healthProbeBody |
| 70 | + ex := &Executor{client: &http.Client{Transport: healthProbeTransport(func(req *http.Request) (*http.Response, error) { |
| 71 | + body = &healthProbeBody{ctx: req.Context()} |
| 72 | + return &http.Response{StatusCode: http.StatusOK, Body: body, Header: make(http.Header)}, nil |
| 73 | + })}} |
| 74 | + probe := &Probe{HTTP: "http://127.0.0.1:{port}/"} |
| 75 | + ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) |
| 76 | + defer cancel() |
| 77 | + start := time.Now() |
| 78 | + if !ex.probe(ctx, probe, 1) { |
| 79 | + t.Fatal("body drain changed the HTTP status health result") |
| 80 | + } |
| 81 | + if !body.attempted || !body.closed || time.Since(start) > time.Second { |
| 82 | + t.Fatalf("stalled body did not drain and close within deadline: %+v", body) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +type healthProbeTransport func(*http.Request) (*http.Response, error) |
| 87 | + |
| 88 | +func (f healthProbeTransport) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) } |
| 89 | + |
| 90 | +type healthProbeBody struct { |
| 91 | + reader io.Reader |
| 92 | + ctx context.Context |
| 93 | + read int |
| 94 | + attempted bool |
| 95 | + closed bool |
| 96 | +} |
| 97 | + |
| 98 | +func (b *healthProbeBody) Read(p []byte) (int, error) { |
| 99 | + b.attempted = true |
| 100 | + if b.ctx != nil { |
| 101 | + <-b.ctx.Done() |
| 102 | + return 0, b.ctx.Err() |
| 103 | + } |
| 104 | + n, err := b.reader.Read(p) |
| 105 | + b.read += n |
| 106 | + return n, err |
| 107 | +} |
| 108 | + |
| 109 | +func (b *healthProbeBody) Close() error { b.closed = true; return nil } |
0 commit comments