Files
magpie/internal/plugin/head_completion_test.go
yetoneandrecih 171475905f plugin: a reply's head that arrives with its completion is read, not dropped (#7017)
headRead checked the queue, found it empty, and then found the call's
result: a head the plugin queued between those two checks was lost, and
the reply was read as headless ("the vendor ended early"). The result
branch now takes a head queued meanwhile, as the c.done branch already
did. body.Read is not affected: it loops back to the queue after taking
the result.

From recih's branch (68bdf17e6), ported as its own commit.

Run: TestHostHeadArrivesWithCompletion passes on origin/main without
help (the window is narrow); with a 2ms sleep injected between the pop
and the result check it is red on origin/main ("completed reply's head
= {...}, the vendor ended early") and green with this change.
The full stack's run, on mote at e4506887c (this stack on origin/main 7300be4d0):
go test -timeout 45m ./...: 101 packages ok, 2 FAIL, both open issues
that fail the same at 7300be4d0 on mote, where an ssh session forwards
127.0.0.1:3425 to a real magpie: internal/cli's
TestHealthcheckFindsTheRunningGatewayWithoutMagpieAddr,
TestUpdateRestartsTheGatewayItFindsWithoutMagpieAddr and
TestUpdateFindsAMagpieThatWroteNoRecord (#6223), internal/gui's
TestLaneLimitRoutes (#5150). On mica: gofmt -l clean; go vet ./...,
GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go
build, GOOS=linux go build -tags nogui, GOOS=windows go build all ok.
go test -race -count=2 of the new and touched tests in internal/gateway,
gui, plugin, tui ok on mote.

Rebased since. At dc507aefd on mote: go vet (darwin, GOOS=linux
-tags nogui, windows) ok; go test of internal/cli, tui, plugin,
gateway, gui, claudebridge ok but for the same #6223 and #5150 reds.
At 939fe31b6 (on origin/main 78b6d47cc) on mote: gofmt -l clean, the
three vets ok, the stack's tests with TestClaude* and
TestCoreScriptBudget ok in all six packages. Pushed on b400566f9 after
a clean rebase; the commits between touch no Go file this stack does.

Co-authored-by: recih <240406+recih@users.noreply.github.com>
2026-11-18 23:06:38 +08:00

58 lines
1.6 KiB
Go

package plugin
import (
"context"
"encoding/base64"
"io"
"runtime"
"strings"
"testing"
"time"
)
// A short reply can finish between the head reader's empty-queue check
// and its check for a completed call. The queued head still belongs to
// that reply, and its body and terminal error must reach the consumer.
func TestHostHeadArrivesWithCompletion(t *testing.T) {
for _, why := range []string{"", "the vendor ended early"} {
t.Run(why, func(t *testing.T) {
f := newFakeHost(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
for range 128 {
id, c := f.begin(t, true)
type result struct {
head message
answer *message
err error
}
read := make(chan result, 1)
go func() {
h, a, err := c.headRead(ctx)
read <- result{h, a, err}
}()
runtime.Gosched()
c.push(message{Event: "head", Status: 200})
c.push(message{Event: "chunk", Data: base64.StdEncoding.EncodeToString([]byte("reply"))})
answer := message{}
if why != "" {
answer = errAnswer(why)
}
c.transition(callAnswered, nil, &answer)
r := <-read
if r.err != nil || r.head.Status != 200 {
c.release()
t.Fatalf("completed reply's head = %+v, %v", r.head, r.err)
}
b := readBody(f, id, c, ctx)
b.answer = r.answer
got, err := io.ReadAll(b)
b.Close()
if string(got) != "reply" || why == "" && err != nil || why != "" && (err == nil || !strings.Contains(err.Error(), why)) {
t.Fatalf("body = %q, %v; want reply, error %q", got, err, why)
}
}
})
}
}