mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
A provider keeps at most 10000 discovered ids of at most 256 bytes. The registry drops empty ids, ids with control characters, longer ids and the overflow, and logs the count once per 10 minutes.
95 lines
2.6 KiB
Go
95 lines
2.6 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func newBoundsRegistry(t *testing.T, logf func(string, ...any)) (*modelRegistry, *providerState) {
|
|
t.Helper()
|
|
adapters := map[string]providerAdapter{"openai": newFakeAdapter("openai")}
|
|
cfg := &Config{Providers: map[string]*ProviderConfig{"openai": {Models: []string{"gpt-test"}}}}
|
|
reg, err := newModelRegistry(adapters, cfg, logf)
|
|
if err != nil {
|
|
t.Fatalf("newModelRegistry: %v", err)
|
|
}
|
|
return reg, reg.states["openai"]
|
|
}
|
|
|
|
func manyIDs(n int) []string {
|
|
ids := make([]string, n)
|
|
for i := range ids {
|
|
ids[i] = fmt.Sprintf("m-%d", i)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func discoveredCount(reg *modelRegistry, st *providerState) int {
|
|
st.mu.Lock()
|
|
defer st.mu.Unlock()
|
|
return len(st.discovered)
|
|
}
|
|
|
|
func TestDiscovery_DropsOverLengthAndInvalidIDs(t *testing.T) {
|
|
reg, st := newBoundsRegistry(t, func(string, ...any) {})
|
|
reg.finishRefresh(st, reg.now(), []string{strings.Repeat("a", 300), "", "bad\nid", "good-1", "good-2"}, nil)
|
|
if got := discoveredCount(reg, st); got != 2 {
|
|
t.Fatalf("discovered = %d, want 2", got)
|
|
}
|
|
if !st.discovered["good-1"] || !st.discovered["good-2"] {
|
|
t.Errorf("discovered = %v, want good-1 and good-2", st.discovered)
|
|
}
|
|
}
|
|
|
|
func TestDiscovery_AtCapAccepted(t *testing.T) {
|
|
reg, st := newBoundsRegistry(t, func(string, ...any) {})
|
|
reg.finishRefresh(st, reg.now(), manyIDs(maxDiscoveredModels), nil)
|
|
if got := discoveredCount(reg, st); got != maxDiscoveredModels {
|
|
t.Errorf("discovered = %d, want %d", got, maxDiscoveredModels)
|
|
}
|
|
}
|
|
|
|
func TestDiscovery_OverCountKeepsCapAndLogsDebounced(t *testing.T) {
|
|
var mu sync.Mutex
|
|
var lines []string
|
|
reg, st := newBoundsRegistry(t, func(f string, a ...any) {
|
|
mu.Lock()
|
|
lines = append(lines, fmt.Sprintf(f, a...))
|
|
mu.Unlock()
|
|
})
|
|
now := time.Now()
|
|
reg.finishRefresh(st, now, manyIDs(maxDiscoveredModels+5), nil)
|
|
reg.finishRefresh(st, now.Add(time.Second), manyIDs(maxDiscoveredModels+5), nil)
|
|
if got := discoveredCount(reg, st); got != maxDiscoveredModels {
|
|
t.Errorf("discovered = %d, want %d", got, maxDiscoveredModels)
|
|
}
|
|
st.mu.Lock()
|
|
over := st.discovered["m-"+fmt.Sprint(maxDiscoveredModels)]
|
|
st.mu.Unlock()
|
|
if over {
|
|
t.Error("an id past the cap was kept")
|
|
}
|
|
var n int
|
|
for _, l := range lines {
|
|
if strings.Contains(l, "dropped 5 model ids") {
|
|
n++
|
|
}
|
|
}
|
|
if n != 1 {
|
|
t.Errorf("drop log lines = %d, want 1 (debounced): %v", n, lines)
|
|
}
|
|
reg.finishRefresh(st, now.Add(upstreamErrLogEvery+time.Second), manyIDs(maxDiscoveredModels+5), nil)
|
|
n = 0
|
|
for _, l := range lines {
|
|
if strings.Contains(l, "dropped 5 model ids") {
|
|
n++
|
|
}
|
|
}
|
|
if n != 2 {
|
|
t.Errorf("drop log lines after the window = %d, want 2", n)
|
|
}
|
|
}
|