Add resumable checked migrations and explicit schema administration commands. Port ML budgets, namespaces, identity and group persistence with three-backend contracts. Keep MySQL unavailable as the full application backend.
101 lines
3.0 KiB
Go
101 lines
3.0 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"pageswiki/internal/machinelearning"
|
|
)
|
|
|
|
func TestMLBudget(t *testing.T) {
|
|
for _, backend := range []storeSQLBackend{storeSQLite, storePostgreSQL, storeMySQL} {
|
|
t.Run(fmt.Sprint(backend), func(t *testing.T) {
|
|
s, d := mlTestStore(t, backend)
|
|
ctx := context.Background()
|
|
if backend != storeMySQL {
|
|
if e := s.EnsureMLBudgetSchema(ctx); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := s.EnsureMLBudgetSchema(ctx); e != nil {
|
|
t.Fatal("migration rerun", e)
|
|
}
|
|
}
|
|
p, e := s.SaveMachineLearningPolicy(ctx, d.UUID, "", machinelearning.Policy{Enabled: true, ScopeUUID: uuid.NewString(), Provider: "doc2vec", Model: strings.Repeat("a", 64), Dimensions: 8, Prefixes: []string{"/lab"}, MaxBatchBytes: 6000, DailyInputBytes: 10})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
day := time.Date(2026, 11, 28, 23, 59, 0, 0, time.UTC)
|
|
inputs := []machinelearning.Input{{ID: "chunk", Text: "abcdef"}}
|
|
reserve := func(id string, at time.Time) error {
|
|
_, err := s.reserveMLInputAt(ctx, d.UUID, id, "/lab/doc", p.Generation, inputs, at)
|
|
return err
|
|
}
|
|
id := uuid.NewString()
|
|
if e = reserve(id, day); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = reserve(id, day); !errors.Is(e, ErrMLAlreadyReserved) {
|
|
t.Fatal("duplicate", e)
|
|
}
|
|
if e = s.FinishMLInput(ctx, d.UUID, id, false, 0); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.FinishMLInput(ctx, d.UUID, id, true, 1); !errors.Is(e, ErrMLAlreadyReserved) {
|
|
t.Fatal("settled receipt changed", e)
|
|
}
|
|
if e = reserve(uuid.NewString(), day); !errors.Is(e, ErrMLBudget) {
|
|
t.Fatal("unknown refunded", e)
|
|
}
|
|
// New UTC day resets allowance, not idempotency receipts.
|
|
if e = reserve(id, day.Add(time.Minute)); !errors.Is(e, ErrMLAlreadyReserved) {
|
|
t.Fatal("old ID reused", e)
|
|
}
|
|
start := make(chan struct{})
|
|
results := make(chan error, 2)
|
|
for i := 0; i < 2; i++ {
|
|
go func() { <-start; results <- reserve(uuid.NewString(), day.Add(time.Minute)) }()
|
|
}
|
|
close(start)
|
|
wins := 0
|
|
for i := 0; i < 2; i++ {
|
|
e = <-results
|
|
if e == nil {
|
|
wins++
|
|
} else if !errors.Is(e, ErrMLBudget) {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
if wins != 1 {
|
|
t.Fatal("concurrent overspend", wins)
|
|
}
|
|
// Policy generations never reset usage. Reducing limits fails closed.
|
|
old := p.Generation
|
|
p.DailyInputBytes = 5
|
|
p, e = s.SaveMachineLearningPolicy(ctx, d.UUID, old, p)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = reserve(uuid.NewString(), day); !errors.Is(e, ErrMLBudget) {
|
|
t.Fatal("policy reset budget", e)
|
|
}
|
|
_, e = s.reserveMLInputAt(ctx, d.UUID, uuid.NewString(), "/lab/doc", old, inputs, day)
|
|
if !errors.Is(e, machinelearning.ErrNotAllowed) {
|
|
t.Fatal("old consent", e)
|
|
}
|
|
p.DailyInputBytes = 0
|
|
p, e = s.SaveMachineLearningPolicy(ctx, d.UUID, p.Generation, p)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = reserve(uuid.NewString(), day.Add(48*time.Hour)); !errors.Is(e, ErrMLBudget) {
|
|
t.Fatal("zero allows execution", e)
|
|
}
|
|
})
|
|
}
|
|
}
|