Files
archie/internal/tools/mcp/parallel_test.go
T
sam 8ea45e836b refactor(tools/mcp): delete unreachable MCP adaptation path
DELETE: mcp.Client.RegisterTools and its handlerFor/writeMultimodalResult
chain, plus the WithMediaDir and WithParallelToolCalls options, had no
production caller -- the live adaptation path is provider/mcp.Provider,
which now owns binary media persistence itself. A repo-wide search finds
no remaining reference outside the composition contract test that asserts
RegisterTools must not appear, so the two paths can no longer diverge.
2026-09-15 12:58:10 +10:00

132 lines
3.2 KiB
Go

package mcp
import (
"context"
"encoding/json"
"sync"
"sync/atomic"
"testing"
"time"
)
// blockingSender is a Transport double whose Send blocks until release is
// closed, so tests can observe whether multiple calls are in flight at
// once (parallel) or one completes before the next starts (serialized).
type blockingSender struct {
release chan struct{}
mu sync.Mutex
inFlight int
maxInFlight int
}
func (b *blockingSender) Send(_ context.Context, body []byte) ([]byte, error) {
b.mu.Lock()
b.inFlight++
if b.inFlight > b.maxInFlight {
b.maxInFlight = b.inFlight
}
b.mu.Unlock()
<-b.release
b.mu.Lock()
b.inFlight--
b.mu.Unlock()
var msg Message
_ = json.Unmarshal(body, &msg)
return []byte(`{"jsonrpc":"2.0","id":` + string(msg.ID) + `,"result":{"content":[{"type":"text","text":"ok"}]}}`), nil
}
func (b *blockingSender) Notify(context.Context, []byte) error { return nil }
func (b *blockingSender) MaxInFlight() int {
b.mu.Lock()
defer b.mu.Unlock()
return b.maxInFlight
}
func TestCallToolSerializesByDefault(t *testing.T) {
b := &blockingSender{release: make(chan struct{})}
c := NewClient(b, "test-server")
const n = 5
var wg sync.WaitGroup
for range n {
wg.Go(func() {
_, _ = c.CallTool(context.Background(), "slow", nil)
})
}
// Give goroutines a moment to reach Send; with serialization, only one
// should ever be blocked in Send at a time.
time.Sleep(50 * time.Millisecond)
if got := b.MaxInFlight(); got > 1 {
t.Errorf("MaxInFlight = %d, want 1 (default is serialized)", got)
}
close(b.release)
wg.Wait()
}
func TestCallToolSerializedStillCompletesAllCalls(t *testing.T) {
b := &blockingSender{release: make(chan struct{})}
close(b.release) // never actually blocks -- just verifying correctness, not timing
c := NewClient(b, "test-server")
const n = 10
var completed atomic.Int32
var wg sync.WaitGroup
for range n {
wg.Go(func() {
if _, err := c.CallTool(context.Background(), "fast", nil); err == nil {
completed.Add(1)
}
})
}
wg.Wait()
if got := completed.Load(); got != n {
t.Errorf("completed = %d, want %d", got, n)
}
}
func TestCallToolSerializationDoesNotBlockDifferentClients(t *testing.T) {
// Serialization is per-Client (per-server), not global -- two
// independent Clients (different servers) must not block each other.
b1 := &blockingSender{release: make(chan struct{})}
b2 := &blockingSender{release: make(chan struct{})}
close(b2.release) // c2's own call should complete immediately, unblocked
c1 := NewClient(b1, "server-1")
c2 := NewClient(b2, "server-2")
done := make(chan struct{})
go func() {
_, _ = c1.CallTool(context.Background(), "slow", nil)
close(done)
}()
// c1's call is now blocked in Send. c2 must still be able to proceed
// immediately -- it has its own serialization lock.
select {
case <-done:
t.Fatal("c1's call should still be blocked")
case <-time.After(20 * time.Millisecond):
}
c2Done := make(chan struct{})
go func() {
_, _ = c2.CallTool(context.Background(), "fast", nil)
close(c2Done)
}()
select {
case <-c2Done:
case <-time.After(time.Second):
t.Fatal("c2's call should not be blocked by c1's in-flight call")
}
close(b1.release)
<-done
}