all: modernize code (#868)

Use `go fix ./...` to modernize the code.
This commit is contained in:
Oleksandr Redko 2026-03-31 11:58:02 +03:00 • committed by GitHub
commit a1f808c86e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 44 additions and 69 deletions

View file

@ -200,8 +200,7 @@ func TestStdioContextCancellation(t *testing.T) {
func TestCmdTransport(t *testing.T) {
requireExec(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
cmd := createServerCommand(t, "default")
@ -287,8 +286,7 @@ func TestCommandTransportTerminateDuration(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
// Use a command that won't exit when stdin is closed
cmd := exec.Command("sleep", "20")

View file

@ -22,8 +22,8 @@ func TestContentUnmarshalNil(t *testing.T) {
tests := []struct {
name string
json string
content interface{}
want interface{}
content any
want any
}{
{
name: "CallToolResult nil Content",
@ -83,7 +83,7 @@ func TestContentUnmarshalNilWithDifferentTypes(t *testing.T) {
tests := []struct {
name string
json string
content interface{}
content any
expectError bool
}{
{
@ -145,7 +145,7 @@ func TestContentUnmarshalNilWithEmptyContent(t *testing.T) {
tests := []struct {
name string
json string
content interface{}
content any
expectError bool
}{
{
@ -186,7 +186,7 @@ func TestContentUnmarshalNilWithInvalidContent(t *testing.T) {
tests := []struct {
name string
json string
content interface{}
content any
expectError bool
}{
{

View file

@ -676,13 +676,11 @@ func TestServerClosing(t *testing.T) {
ctx := context.Background()
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Go(func() {
if err := cs.Wait(); err != nil {
t.Errorf("server connection failed: %v", err)
}
wg.Done()
}()
})
if _, err := cs.CallTool(ctx, &CallToolParams{
Name: "greet",
Arguments: map[string]any{"Name": "user"},

View file

@ -194,7 +194,7 @@ func TestAddToolWithSharedCache(t *testing.T) {
}
// Simulate stateless server pattern: new server per request, shared cache.
for i := 0; i < 3; i++ {
for range 3 {
s := NewServer(&Implementation{Name: "test", Version: "1.0"}, &ServerOptions{
SchemaCache: cache,
})

View file

@ -5,7 +5,6 @@
package mcp_test
import (
"context"
"flag"
"log"
"net/http"
@ -51,8 +50,7 @@ func BenchmarkStreamableServing(b *testing.B) {
httpServer := httptest.NewServer(handler)
defer httpServer.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := b.Context()
session, err := mcp.NewClient(testImpl, nil).Connect(ctx, &mcp.StreamableClientTransport{Endpoint: httpServer.URL}, nil)
if err != nil {
b.Fatal(err)
@ -84,8 +82,7 @@ func BenchmarkStreamableServing_BadSessions(b *testing.B) {
httpServer := httptest.NewServer(handler)
defer httpServer.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := b.Context()
if *streamableHeap != "" {
writeHeap := func(file string) {

View file

@ -108,9 +108,7 @@ func TestStreamableTransports(t *testing.T) {
}
// Now, spin off a goroutine that runs after the sampling request, to
// check behavior when the client request has completed.
sampleWG.Add(1)
go func() {
defer sampleWG.Done()
sampleWG.Go(func() {
<-sampleDone
// Test that sampling requests in the tool context fail outside of
// tool handling, but succeed on the background context.
@ -120,7 +118,7 @@ func TestStreamableTransports(t *testing.T) {
} {
testSample(test)
}
}()
})
return &CallToolResult{}, nil, nil
})
@ -268,9 +266,7 @@ func TestStreamableConcurrentHandling(t *testing.T) {
client := NewClient(testImpl, nil)
var wg sync.WaitGroup
for range 100 {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
clientSession, err := client.Connect(ctx, &StreamableClientTransport{Endpoint: httpServer.URL}, nil)
if err != nil {
t.Errorf("Connect failed: %v", err)
@ -287,7 +283,7 @@ func TestStreamableConcurrentHandling(t *testing.T) {
t.Errorf("got count %d, want %d", got, i)
}
}
}()
})
}
wg.Wait()
}
@ -1293,9 +1289,7 @@ func testStreamableHandler(t *testing.T, handler http.Handler, requests []stream
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
for m := range out {
if req, ok := m.(*jsonrpc.Request); ok && req.IsCall() {
@ -1315,7 +1309,7 @@ func testStreamableHandler(t *testing.T, handler http.Handler, requests []stream
cancel()
}
}
}()
})
gotSessionID, gotStatusCode, gotBody, err := request.do(ctx, httpServer.URL, sessionID.Load().(string), out)
@ -1353,11 +1347,9 @@ func testStreamableHandler(t *testing.T, handler http.Handler, requests []stream
var wg sync.WaitGroup
for i, request := range requests {
if request.async || request.onRequest > 0 {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
doStep(t, i, request)
}()
})
} else {
doStep(t, i, request)
}
@ -2071,18 +2063,15 @@ func TestStreamableSessionTimeout(t *testing.T) {
// Spin up two goroutines, each making a request every 10ms. These requests
// should keep the server from timing out.
var wg sync.WaitGroup
wg.Add(2)
for range 2 {
go func() {
defer wg.Done()
wg.Go(func() {
for range 20 {
if _, err := session.ListTools(ctx, nil); err != nil {
t.Errorf("ListTools failed: %v", err)
}
time.Sleep(10 * time.Millisecond)
}
}()
})
}
wg.Wait()
@ -2154,8 +2143,7 @@ data: keepalive
}
// Create a minimal streamableClientConn for testing
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
incoming := make(chan jsonrpc.Message, 10)
done := make(chan struct{})