diff --git a/internal/provider/stepfun_held_test.go b/internal/provider/stepfun_held_test.go new file mode 100644 index 000000000..ef75fe5a8 --- /dev/null +++ b/internal/provider/stepfun_held_test.go @@ -0,0 +1,47 @@ +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") + } +} diff --git a/internal/provider/stepfun_plan.go b/internal/provider/stepfun_plan.go index 531d1dfaa..84af89f28 100644 --- a/internal/provider/stepfun_plan.go +++ b/internal/provider/stepfun_plan.go @@ -37,8 +37,10 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "time" + "github.com/yetone/magpie/internal/filememo" "github.com/yetone/magpie/internal/lastgood" ) @@ -230,6 +232,7 @@ func renameStepFun(from, to string) error { } func saveStepFun(all map[string]stepfunSession) error { + defer filememo.Forget() // read again, where a request holds it b, err := json.MarshalIndent(all, "", " ") if err != nil { return err @@ -276,12 +279,30 @@ func (c stepfunClaims) account() string { } // StepFunSignedIn reports whether magpie has a sign-in for the provider. +// StepFunSignedIn reads the file once until it changes: a provider list +// asks it for each StepFun provider on every poll, and each read was a +// file open, most often of a file that isn't there. One with a site's +// session from before #2384 is moved first, under the lock. func StepFunSignedIn(id string) bool { - stepfunMu.Lock() - defer stepfunMu.Unlock() - return loadStepFun()[id].Access != "" + all, err := filememo.Read("stepfun-auth", stepfunPath(), func(b []byte) (map[string]stepfunSession, error) { + stepfunParses.Add(1) + m := map[string]stepfunSession{} + return m, json.Unmarshal(b, &m) + }) + if err != nil { + return false // no file, or one that doesn't parse: no session, as loadStepFun + } + if slices.ContainsFunc(stepfunOldSites, func(s string) bool { _, ok := all[s]; return ok }) { + stepfunMu.Lock() + defer stepfunMu.Unlock() + return loadStepFun()[id].Access != "" + } + return all[id].Access != "" } +// stepfunParses counts StepFunSignedIn's parses, for a test. +var stepfunParses atomic.Int64 + // SaveStepFunSession keeps the session the sign-in page ended in, once // the platform has taken it: a page that isn't signed in yet has an // anonymous one, which is refused here. It is the provider id's alone; site