Files
magpie/internal/gateway/pi_concurrent_codex_test.go
T
yetone 6e0b8113dd gateway tests: a rest one test records is let go when it ends (#6717)
restingUntil is package-global. Six tests leave a Codex usage-limit rest
behind (TestAccountProxy, TestCodexOwnModelMovesToNextAccount,
TestCodexSwitchedAccountRestsAsItself, TestCodexPausedOwnAccountPassedOver,
TestCodexPoolForwardsCodexHeaders, TestCodexSocketErrorIsItsStatus), and
TestEffortChangeKeepsTheCache, whose effortCodexAccount sets its home
without fresh(t), started held by it whenever the chunking put them in one
process: its first turn was refused in 15ms with "The usage limit has been
reached". Red in Race at 6e6a18eda and in Test at 0dbdc7481.

The class, both sides:
- fresh(t) clears the rests through noRests() and again when the test
  ends (t.Cleanup), so a test that comes through fresh leaves none.
- noRests() clears restingUntil.lifted too, not only m and note.
- The helpers that set a home without fresh (effortCodexAccount,
  leadSignIn, claudeMadeFirst, piTwoTurns) call noRests first and when
  they end, as b64Home already did.

Run on mote, at 6af327e2b plus this change:
- Broken by hand: each test of the eleven files whose fakes answer a
  usage limit, run with TestEffortChangeKeepsTheCache in one process
  (-test.run '^(T|TestEffortChangeKeepsTheCache)$'). Before: red with the
  six tests above. After: none red.
- internal/gateway plain, sharded as CI does (build/race-shards.sh, 12
  workers): 4300 tests, shards_rc=0.
- internal/gateway -race, sharded: one red, TestBurstTellsNoSpeed/
  responses_from_chat_burst (839ms burst read as 5959 tok/s), a timing
  red with its issue, #2719 (output recorded there); every other test ok.
- gofmt -l internal/gateway: clean.

Not run: the other packages (test files of internal/gateway only).

Fixes #6717
2026-11-15 08:45:42 +08:00

112 lines
3.1 KiB
Go

package gateway
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// Two Pi turns at once on one Codex (ChatGPT) sign-in are both out at the
// vendor at once: the second is not held in "routing" until the first has
// been answered (tmr11235 on X, v0.1.3852).
func TestPiTwoTurnsOnCodexAreOutTogether(t *testing.T) {
for _, socket := range []bool{false, true} {
t.Run(fmt.Sprint("socket=", socket), func(t *testing.T) { piTwoTurns(t, socket) })
}
}
func piTwoTurns(t *testing.T, socket bool) {
if socket {
socketOn(t)
}
noRests() // a rest an earlier test left must not hold this account (#6717)
t.Cleanup(noRests)
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
claims := func(m map[string]any) string {
b, _ := json.Marshal(m)
return "h." + base64.RawURLEncoding.EncodeToString(b) + ".s"
}
os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
auth := fmt.Sprintf(`{"auth_mode":"chatgpt","tokens":{"id_token":%q,"access_token":%q,"refresh_token":"r","account_id":"acct-1"}}`,
claims(map[string]any{"email": "me@example.com"}), claims(map[string]any{"exp": time.Now().Add(time.Hour).Unix()}))
os.WriteFile(filepath.Join(home, ".codex", "auth.json"), []byte(auth), 0o600)
var out, peak atomic.Int32
release := make(chan struct{})
var once sync.Once
hold := func() {
n := out.Add(1)
for {
p := peak.Load()
if n <= p || peak.CompareAndSwap(p, n) {
break
}
}
if n >= 2 {
once.Do(func() { close(release) })
}
select { // a slow answer: held until the other one is out too
case <-release:
case <-time.After(5 * time.Second):
}
out.Add(-1)
}
var h0 http.Handler
if socket {
f := &fakeSocketBackend{keep: false, frames: func(socketTurn) []string {
hold()
return wholeReply(socketTurn{})
}}
h0 = f
} else {
h0 = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
w.Header()["Content-Type"] = nil
io.WriteString(w, strings.SplitAfter(piReply, "\n\n")[0])
w.(http.Flusher).Flush()
hold() // mid-reply: the first has begun answering
io.WriteString(w, strings.TrimPrefix(piReply, strings.SplitAfter(piReply, "\n\n")[0]))
})
}
up := httptest.NewServer(h0)
defer up.Close()
old := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
defer func() { provider.CodexBase = old }()
h := New().Handler()
var wg sync.WaitGroup
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(fmt.Sprintf(piResponses, "codex/gpt-6.1-sol")))
h.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Errorf("reply: %d %s", rec.Code, rec.Body.String())
}
}()
}
wg.Wait()
if peak.Load() < 2 {
t.Fatalf("at most %d turn out at the vendor at once: the second waited for the first", peak.Load())
}
}