Files
magpie/internal/provider/stepfun_held_test.go
T
yetone 6874b892bb providers: a StepFun sign-in is read once a request, not once a row
/api/providers asks StepFunSignedIn for each StepFun provider on every
poll, and each ask opened stepfun-auth.json under stepfunMu, most often
a file that isn't there. In a profile of 200 lists against a copy of
the owner's real config (2 StepFun providers) it was 0.28s of 4.07s.

StepFunSignedIn now reads the file through filememo, which parses it
once until it changes and remembers it missing while a request holds
it. A file with a site's session from before #2384 ("ai", "com") still
goes through loadStepFun under the lock, which moves it. saveStepFun
calls filememo.Forget, so a sign-in saved during a request is seen by
the next ask in it.

Run (on mote unless named):
- go test -count=1 -timeout 45m ./internal/provider ok (115s);
  -run TestStepFun -count=3 ok; GOOS=linux and windows go vet
  ./internal/provider ok; gofmt clean (here).
- New TestStepFunSignedInReadOnce: 40 asks in one held request parse
  once, and a save inside it is seen. Broken by hand: each ask reading
  the file -> "parsed 0 times, want 1"; no Forget in saveStepFun -> "a
  sign-in saved in the request wasn't seen".
- Here, 200 GET /api/providers on the sandbox with the real config
  copy (offline, proxy :9): StepFunSignedIn 0.28s -> gone from the
  profile, syscalls 1.54s -> 1.08s; total CPU 4.49s -> 4.53s, within
  this machine's noise.

Not run: the full ./... suite; GUI tests (no page change); a real
StepFun sign-in (the save path is unchanged apart from Forget).
2026-11-01 13:40:29 +08:00

48 lines
1.6 KiB
Go

package provider
import (
"os"
"testing"
"time"
"github.com/yetone/magpie/internal/filememo"
)
// TestStepFunSignedInReadOnce: a provider list asked each StepFun
// provider's sign-in, and each ask read stepfun-auth.json; a request
// reads it once, and a sign-in saved in it is seen at once.
func TestStepFunSignedInReadOnce(t *testing.T) {
stepfunHome(t, stepfunOn("stepfun", "StepFun", "ai"), stepfunOn("stepfun-cn", "StepFun (China)", "com"))
defer func(d time.Duration) { heldFor = d }(heldFor)
heldFor = time.Hour // a loaded machine's pause isn't the bound
if StepFunSignedIn("stepfun") {
t.Fatal("signed in with no file")
}
acc, ref := stepfunTokens("4161", time.Now().Add(time.Hour), true)
if err := saveStepFun(map[string]stepfunSession{"stepfun": {Access: acc, Refresh: ref, Account: "4161"}}); err != nil {
t.Fatal(err)
}
// written a while ago, so a request keeps what it read without a look
ago := time.Now().Add(-time.Hour)
if err := os.Chtimes(stepfunPath(), ago, ago); err != nil {
t.Fatal(err)
}
defer Hold()()
defer filememo.Hold()() // as /api/ holds both
parses := stepfunParses.Load()
for range 20 {
if !StepFunSignedIn("stepfun") || StepFunSignedIn("stepfun-cn") {
t.Fatal("signed in: want stepfun only")
}
}
if n := stepfunParses.Load() - parses; n != 1 {
t.Fatalf("40 asks in one request parsed %d times, want 1", n)
}
if err := saveStepFun(map[string]stepfunSession{"stepfun-cn": {Access: acc, Refresh: ref, Account: "4161"}}); err != nil {
t.Fatal(err)
}
if StepFunSignedIn("stepfun") || !StepFunSignedIn("stepfun-cn") {
t.Fatal("a sign-in saved in the request wasn't seen")
}
}