mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
Corrects passthrough metering, the cost gate, credential redaction, reload drain, response headers and the discovery cap.
141 lines
3.9 KiB
Go
141 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// fakeRedis stands in for the shared Redis client: closing it makes later use fail, as in a real closed pool.
|
|
type fakeRedis struct {
|
|
closed atomic.Bool
|
|
writes atomic.Int32
|
|
failed atomic.Int32
|
|
}
|
|
|
|
func (f *fakeRedis) incr() {
|
|
if f.closed.Load() {
|
|
f.failed.Add(1)
|
|
return
|
|
}
|
|
f.writes.Add(1)
|
|
}
|
|
|
|
func waitFor(t *testing.T, what string, cond func() bool) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(3 * time.Second)
|
|
for !cond() {
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("timed out waiting for %s", what)
|
|
}
|
|
time.Sleep(2 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
// TestGenerationOutlivingDrainTimeoutStaysOpen: a request that runs past the drain timeout still reaches its
|
|
// Redis-backed state, and the generation closes only after that request leaves.
|
|
func TestGenerationOutlivingDrainTimeoutStaysOpen(t *testing.T) {
|
|
redis := &fakeRedis{}
|
|
var closes atomic.Int32
|
|
release := make(chan struct{})
|
|
started := make(chan struct{})
|
|
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
close(started)
|
|
<-release
|
|
redis.incr()
|
|
w.WriteHeader(http.StatusNoContent)
|
|
})
|
|
oldGen := newGeneration(h, func() { closes.Add(1); redis.closed.Store(true) })
|
|
var warned atomic.Int32
|
|
oldGen.overdue = func(int64) { warned.Add(1) }
|
|
sw := &swapHandler{}
|
|
sw.cur.Store(oldGen)
|
|
|
|
done := make(chan struct{})
|
|
rec := httptest.NewRecorder()
|
|
go func() {
|
|
sw.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
close(done)
|
|
}()
|
|
<-started
|
|
sw.swap(newGeneration(okHandler(), func() {}), 15*time.Millisecond)
|
|
|
|
waitFor(t, "the overdue warning", func() bool { return warned.Load() >= 1 })
|
|
if closes.Load() != 0 {
|
|
t.Fatalf("generation closed %d times under a running request", closes.Load())
|
|
}
|
|
close(release)
|
|
<-done
|
|
if redis.writes.Load() != 1 || redis.failed.Load() != 0 {
|
|
t.Errorf("request after the timeout: writes=%d failed=%d, want 1 and 0", redis.writes.Load(), redis.failed.Load())
|
|
}
|
|
if closes.Load() != 1 {
|
|
t.Errorf("closes = %d after the last request left, want 1", closes.Load())
|
|
}
|
|
}
|
|
|
|
func TestGenerationWarnsRepeatedlyWithRunningCount(t *testing.T) {
|
|
var closes atomic.Int32
|
|
g := countingGen(okHandler(), &closes)
|
|
var mu sync.Mutex
|
|
var counts []int64
|
|
g.overdue = func(n int64) {
|
|
mu.Lock()
|
|
counts = append(counts, n)
|
|
mu.Unlock()
|
|
}
|
|
g.enter()
|
|
g.enter()
|
|
g.retire(10 * time.Millisecond)
|
|
waitFor(t, "two warnings", func() bool { mu.Lock(); defer mu.Unlock(); return len(counts) >= 2 })
|
|
g.leave()
|
|
g.leave()
|
|
if closes.Load() != 1 {
|
|
t.Fatalf("closes = %d, want 1", closes.Load())
|
|
}
|
|
mu.Lock()
|
|
n := len(counts)
|
|
first := counts[0]
|
|
mu.Unlock()
|
|
if first != 2 {
|
|
t.Errorf("first warning counted %d running requests, want 2", first)
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if len(counts) != n {
|
|
t.Errorf("warnings continued after the generation closed: %d then %d", n, len(counts))
|
|
}
|
|
}
|
|
|
|
func TestGenerationNoWarningWhenDrainedInTime(t *testing.T) {
|
|
var closes atomic.Int32
|
|
g := countingGen(okHandler(), &closes)
|
|
var warned atomic.Int32
|
|
g.overdue = func(int64) { warned.Add(1) }
|
|
g.enter()
|
|
g.retire(20 * time.Millisecond)
|
|
g.leave()
|
|
time.Sleep(60 * time.Millisecond)
|
|
if warned.Load() != 0 || closes.Load() != 1 {
|
|
t.Errorf("warned=%d closes=%d, want 0 and 1", warned.Load(), closes.Load())
|
|
}
|
|
}
|
|
|
|
// TestLoggedGenerationWarnsWhenOverdue covers the generation that main installs first: its first replacement must warn too.
|
|
func TestLoggedGenerationWarnsWhenOverdue(t *testing.T) {
|
|
g := loggedGeneration(okHandler(), newLogger("info"), 10*time.Millisecond)
|
|
g.enter()
|
|
out := captureTelemetryStderr(t, func() {
|
|
// Called directly: a timer callback could still log while captureTelemetryStderr restores os.Stderr.
|
|
g.overdue(1)
|
|
})
|
|
if !strings.Contains(out, "still has 1 running requests") {
|
|
t.Errorf("no overdue warning from the initial generation: %q", out)
|
|
}
|
|
}
|