Files
magpie/internal/provider/shared_all_test.go
T
yetone 773902a77b provider: Find copies the provider it gives; a list asks each group once
In the same live profile (v0.1.2906, two idle minutes, 269 MB):
- provider.Find copied every provider through All, each account's keys
  and headers, to give one (9.3 MB); All's copies were 4.6 MB of their
  own. sharedAll is the providers held, for reading; Find and resolveIn
  copy only the one they give, and the pricers read sharedAll.
- buildModels asked a group's members up to five times (its compaction,
  its price twice, its protocols, its tiers), each walking the members
  and resolving each (GroupFinder 7.9 MB, membersFound 6.3 MB).
  provider.FoundOnce answers each group once per build; every caller
  only reads the members.

Run (mote, at this tree):
- gofmt -l internal, go vet ./..., GOOS=windows and GOOS=linux go vet
  ./internal/agent/ ./internal/provider/: clean
- go test -count=1 -timeout 45m ./... (all three commits' tree): 85
  packages ok, internal/gateway 748s, internal/provider 193s. Two red:
  internal/gui's TestMainWindowCorners/macOS_26_SDK, mote's macOS 27
  (#3949, filed before, untouched here), and internal/agent's
  TestCodexEffortOptionsReadTheKeptList, whose bar counted the
  environment's copy (see the effort test's comment), fixed in its test.
- then go test -count=1 -timeout 45m ./internal/agent/ with that fix:
  ok (200.1s)

Broken by hand (mote, -run):
- Find through All again: 167 allocations against a bar of 60, red;
  green it is 6 (TestFindCopiesTheOneItGives, which also checks that a
  change to what Find gives reaches neither the next Find nor All).
- FoundOnce forgetting what it found: group:a walked 5 times, red
  (TestFoundOnceAsksEachGroupOnce).

Not tried: the numbers on the live app after this build.
2026-11-06 12:48:51 +08:00

62 lines
1.9 KiB
Go

package provider
import (
"fmt"
"testing"
)
// Find under a hold copies the provider it gives, and no other: it copied
// every provider, every account's keys and headers, to give one. What it
// gives is still the caller's to change.
func TestFindCopiesTheOneItGives(t *testing.T) {
isolate(t)
for i := range 40 {
p := Provider{ID: fmt.Sprintf("p%d", i), Name: fmt.Sprintf("P%d", i), Key: "k", Chat: "http://127.0.0.1:1/v1",
Models: []string{"m1", "m2", "m3"}, Keys: []KeyAccount{{Key: "k1"}, {Key: "k2"}}, Headers: map[string]string{"X-A": "1", "X-B": "2"}}
if err := Save(p); err != nil {
t.Fatal(err)
}
}
defer Hold()()
p, err := Find("p7")
if err != nil || p.ID != "p7" {
t.Fatalf("Find: %v %v", p, err)
}
p.Keys[0].Key, p.Models[0], p.Headers["X-A"] = "changed", "changed", "changed"
again, _ := Find("p7")
if again.Keys[0].Key != "k1" || again.Models[0] != "m1" || again.Headers["X-A"] != "1" {
t.Fatalf("a change to what Find gave reached the next: %v %v %v", again.Keys, again.Models, again.Headers)
}
for _, q := range All() {
if q.ID == "p7" && q.Keys[0].Key != "k1" {
t.Fatalf("a change to what Find gave reached All: %v", q.Keys)
}
}
n := leastAllocs(20, func() { Find("p7") })
if n > 60 {
t.Fatalf("Find of one of 40 providers made %v allocations, want at most 60", n)
}
}
// FoundOnce walks each group once, however often an agent's list asks.
func TestFoundOnceAsksEachGroupOnce(t *testing.T) {
asked := map[string]int{}
find := FoundOnce(func(id string) (Group, []Member, bool) {
asked[id]++
return Group{ID: id}, []Member{{ID: id + "-m"}}, id != "group:none"
})
for range 5 {
for _, id := range []string{"group:a", "group:b", "group:none"} {
g, ms, ok := find(id)
if g.ID != id || len(ms) != 1 || ms[0].ID != id+"-m" || ok != (id != "group:none") {
t.Fatalf("%s: %v %v %v", id, g, ms, ok)
}
}
}
for id, n := range asked {
if n != 1 {
t.Fatalf("%s walked %d times, want once", id, n)
}
}
}