Skip to content

Commit 99ab514

Browse files
committed
mcp: fix more issues found in unit tests
1 parent df334d6 commit 99ab514

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

mcp/cmd_test.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ func SayHi(ctx context.Context, req *mcp.CallToolRequest, args SayHiParams) (*mc
3636
}
3737

3838
func TestMain(m *testing.M) {
39-
goleak.VerifyTestMain(m)
40-
4139
// If the runAsServer variable is set, execute the relevant serverFunc
4240
// instead of running tests (aka the fork and exec trick).
4341
if name := os.Getenv(runAsServer); name != "" {
@@ -49,6 +47,8 @@ func TestMain(m *testing.M) {
4947
run()
5048
return
5149
}
50+
51+
goleak.VerifyTestMain(m)
5252
os.Exit(m.Run())
5353
}
5454

@@ -127,36 +127,35 @@ func TestServerInterrupt(t *testing.T) {
127127
}
128128
requireExec(t)
129129

130-
ctx, cancel := context.WithCancel(context.Background())
131-
defer cancel()
132-
130+
t.Log("Starting server command")
133131
cmd := createServerCommand(t, "default")
134132

135133
client := mcp.NewClient(testImpl, nil)
134+
t.Log("Connecting to server")
135+
136+
ctx := context.Background()
136137
session, err := client.Connect(ctx, &mcp.CommandTransport{Command: cmd}, nil)
137138
if err != nil {
138139
t.Fatal(err)
139140
}
140-
t.Cleanup(func() { session.Close() })
141141

142-
// get a signal when the server process exits
143-
onExit := make(chan struct{})
144-
go func() {
145-
cmd.Process.Wait()
146-
close(onExit)
147-
}()
142+
_, err = session.ListTools(ctx, nil)
143+
if err != nil {
144+
t.Fatal(err)
145+
}
148146

149-
// send a signal to the server process to terminate it
147+
t.Log("Send a signal to the server process to terminate it")
150148
if err := cmd.Process.Signal(os.Interrupt); err != nil {
151149
t.Fatal(err)
152150
}
153151

154-
// wait for the server to exit
155-
// TODO: use synctest when available
156-
select {
157-
case <-time.After(5 * time.Second):
158-
t.Fatal("server did not exit after SIGINT")
159-
case <-onExit:
152+
t.Log("Closing client session so server can exit immediately")
153+
session.Close()
154+
155+
t.Log("Wait for process to terminate after interrupt signal")
156+
_, err = cmd.Process.Wait()
157+
if err == nil {
158+
t.Errorf("unexpected error: %v", err)
160159
}
161160
}
162161

mcp/streamable_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func testClientReplay(t *testing.T, test clientReplayTest) {
316316
})
317317

318318
realServer := httptest.NewServer(NewStreamableHTTPHandler(func(*http.Request) *Server { return server }, nil))
319-
defer realServer.Close()
319+
t.Cleanup(func() { realServer.Close() })
320320
realServerURL, err := url.Parse(realServer.URL)
321321
if err != nil {
322322
t.Fatalf("Failed to parse real server URL: %v", err)

0 commit comments

Comments
 (0)