Files
geo/server/cmd/scheduler/main_test.go
T
root 618399f86d feat(server): hot-reload config store and reloadable infra clients
- Replace single Load() with a watching Store that re-reads config files
  (and config.local.yaml) and fans out a ReloadEvent with a per-field diff
  so consumers can decide whether the change is hot-applicable or requires
  a process restart.
- Wrap llm, retrieval, vector store, and object storage clients in
  Reloadable* shells so the bootstrap can swap their underlying impls when
  config changes without re-instantiating handlers.
- Make jwt.Manager and ops TokenIssuer mutable under a lock so secrets and
  TTLs can be rotated live; thread default plan code through a setter on
  the ops AdminUserService.
- Wire ConfigStore through bootstrap and every cmd/main.go, scheduler /
  worker / tenant-api / ops-api start the watcher; services and handlers
  take a config.Provider so they always read current values for things
  like generation.stream_enabled, scheduler dispatch, retrieval, etc.
- Switch shared/config decoding off viper to a Kratos-derived runtime
  package so env placeholders (\${VAR:default}) resolve consistently and
  the same source machinery powers both the loader and the watcher.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 16:01:23 +08:00

96 lines
2.6 KiB
Go

package main
import (
"context"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/gin-gonic/gin"
)
func TestSchedulerHTTPAddrDefaultsToLoopback(t *testing.T) {
if got := schedulerHTTPAddr("", 8081); got != "127.0.0.1:8081" {
t.Fatalf("schedulerHTTPAddr() = %q, want 127.0.0.1:8081", got)
}
if got := schedulerHTTPAddr("0.0.0.0", 8081); got != "0.0.0.0:8081" {
t.Fatalf("schedulerHTTPAddr() = %q, want 0.0.0.0:8081", got)
}
}
func TestSchedulerMetricsAuthMiddleware(t *testing.T) {
gin.SetMode(gin.TestMode)
tests := []struct {
name string
headerName string
header string
wantStatus int
}{
{name: "missing token", wantStatus: http.StatusUnauthorized},
{name: "bad bearer token", headerName: "Authorization", header: "Bearer bad", wantStatus: http.StatusUnauthorized},
{name: "valid bearer token", headerName: "Authorization", header: "Bearer secret-token", wantStatus: http.StatusNoContent},
{name: "valid internal token header", headerName: "X-Internal-Metrics-Token", header: "secret-token", wantStatus: http.StatusNoContent},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
router := gin.New()
router.GET("/metrics", schedulerMetricsAuthMiddlewareWithToken(func() string { return "secret-token" }), func(c *gin.Context) {
c.Status(http.StatusNoContent)
})
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
if tt.headerName != "" {
req.Header.Set(tt.headerName, tt.header)
}
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
if rec.Code != tt.wantStatus {
t.Fatalf("status = %d, want %d", rec.Code, tt.wantStatus)
}
})
}
}
func TestStartSchedulerWorkerWaitsForRunToReturn(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var wg sync.WaitGroup
started := make(chan struct{})
startSchedulerWorker(ctx, &wg, "test_worker", testSchedulerLogger{}, func(ctx context.Context) {
close(started)
<-ctx.Done()
})
select {
case <-started:
case <-time.After(time.Second):
t.Fatal("worker did not start")
}
cancel()
if ok := waitSchedulerWorkers(&wg, time.Second, testSchedulerLogger{}); !ok {
t.Fatal("expected worker shutdown wait to complete")
}
}
func TestWaitSchedulerWorkersTimesOut(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
defer wg.Done()
if ok := waitSchedulerWorkers(&wg, time.Nanosecond, testSchedulerLogger{}); ok {
t.Fatal("expected worker shutdown wait to time out")
}
}
type testSchedulerLogger struct{}
func (testSchedulerLogger) Infof(string, ...interface{}) {}
func (testSchedulerLogger) Errorf(string, ...interface{}) {}