mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
A provider whose discovery fails often enough now reads as unhealthy on every pod when redis.sharedState is on. The shared breaker uses the same threshold and open times as the local one. The discovered model sets stay local. With the switch off, the provider state runs its local code only and makes no store call.
82 lines
2.8 KiB
Go
82 lines
2.8 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"errors"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSharedDiscoveryBreaker(t *testing.T) {
|
|
a, b, _, clk, _ := sharedGatewayPair(t, nil)
|
|
boom := errors.New("discovery down")
|
|
now := time.Now()
|
|
for i := 0; i < defaultBreakerFailureThreshold; i++ {
|
|
a.registry.finishRefresh(a.registry.states["alpha"], now, nil, boom)
|
|
}
|
|
if a.registry.discoveryHealthy("alpha") {
|
|
t.Fatal("A healthy after threshold failures, local breaker must open")
|
|
}
|
|
if !b.registry.discoveryHealthy("alpha") {
|
|
t.Fatal("B unhealthy before any refresh, discoveryHealthy must not read the store")
|
|
}
|
|
known := b.registry.states["alpha"].knownIDs()
|
|
a.sharedState.refresh()
|
|
b.sharedState.refresh()
|
|
if b.registry.discoveryHealthy("alpha") {
|
|
t.Fatal("B healthy after A tripped the discovery breaker and one refresh")
|
|
}
|
|
if !b.registry.discoveryHealthy("beta") {
|
|
t.Fatal("B beta unhealthy, a trip on alpha must not leak")
|
|
}
|
|
if got := b.registry.states["alpha"].knownIDs(); !reflect.DeepEqual(got, known) {
|
|
t.Fatalf("B model set changed by a shared trip: %v, want %v", got, known)
|
|
}
|
|
|
|
// A discovery success on B closes the fleet breaker.
|
|
b.registry.finishRefresh(b.registry.states["alpha"], now, []string{"a1"}, nil)
|
|
b.sharedState.refresh()
|
|
if !b.registry.discoveryHealthy("alpha") {
|
|
t.Fatal("B unhealthy after its own successful discovery")
|
|
}
|
|
|
|
// The open window ends on its own.
|
|
for i := 0; i < defaultBreakerFailureThreshold; i++ {
|
|
a.registry.finishRefresh(a.registry.states["beta"], now, nil, boom)
|
|
}
|
|
a.sharedState.refresh()
|
|
b.sharedState.refresh()
|
|
if b.registry.discoveryHealthy("beta") {
|
|
t.Fatal("B beta healthy right after the trip")
|
|
}
|
|
clk.advance(defaultBreakerOpenDuration + time.Second)
|
|
if !b.registry.discoveryHealthy("beta") {
|
|
t.Fatal("B beta unhealthy after the open window elapsed")
|
|
}
|
|
}
|
|
|
|
func TestSharedStateOffIdenticalDiscoveryBreaker(t *testing.T) {
|
|
gw, _ := sharedOffGateway(t)
|
|
rec, _ := sharedTestEnv()
|
|
gw.limiter.store = rec
|
|
st := gw.registry.states["alpha"]
|
|
ref := newHealthTestState(st.interval, defaultBreakerFailureThreshold, defaultBreakerOpenDuration, defaultBreakerMaxOpenDuration)
|
|
boom := errors.New("boom")
|
|
now := time.Now()
|
|
steps := []error{boom, boom, boom, boom, nil, boom}
|
|
for i, err := range steps {
|
|
ids := []string(nil)
|
|
if err == nil {
|
|
ids = []string{"a1"}
|
|
}
|
|
st.finishRefresh(now, ids, err)
|
|
ref.finishRefresh(now, ids, err)
|
|
if st.discoveryHealthy() != ref.discoveryHealthy() || st.health != ref.health || st.consecutiveFailures != ref.consecutiveFailures || st.backoff != ref.backoff {
|
|
t.Fatalf("step %d: diverged from the local-only state machine", i)
|
|
}
|
|
}
|
|
if len(rec.reads) != 0 || len(rec.writes) != 0 {
|
|
t.Fatalf("store touched with sharedState off: %d reads, %d writes", len(rec.reads), len(rec.writes))
|
|
}
|
|
}
|