Skip to content

Commit 159933c

Browse files
committed
mcp: speed up TestCommandTransportDuration
At 7s, this test was 70% of our test execution time on my computer. Speed it up by mutating the defaultTerminateDuration for the purposes of the test. Also update our test workflow to use go1.25, now that it's out.
1 parent 9f1b864 commit 159933c

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
runs-on: ubuntu-latest
4040
strategy:
4141
matrix:
42-
go: ["1.23", "1.24", "1.25.0-rc.3"]
42+
go: ["1.23", "1.24", "1.25"]
4343
steps:
4444
- name: Check out code
4545
uses: actions/checkout@v4

mcp/cmd.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import (
1313
"time"
1414
)
1515

16-
const (
17-
defaultTerminateDuration = 5 * time.Second
18-
)
16+
var defaultTerminateDuration = 5 * time.Second // mutable for testing
1917

2018
// A CommandTransport is a [Transport] that runs a command and communicates
2119
// with it over stdin/stdout, using newline-delimited JSON.

mcp/cmd_export_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2+
// Use of this source code is governed by an MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package mcp
6+
7+
import "time"
8+
9+
// This file exports some helpers for mutating internals of the command
10+
// transport for testing.
11+
12+
// SetDefaultTerminateDuration sets the default command terminate duration,
13+
// and returns a function to reset it to the default.
14+
func SetDefaultTerminateDuration(d time.Duration) (reset func()) {
15+
initial := defaultTerminateDuration
16+
defaultTerminateDuration = d
17+
return func() {
18+
defaultTerminateDuration = initial
19+
}
20+
}

mcp/cmd_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,25 +257,35 @@ func TestCommandTransportTerminateDuration(t *testing.T) {
257257
}
258258
requireExec(t)
259259

260+
// Unfortunately, since it does I/O, this test needs to rely on timing (we
261+
// can't use synctest). However, we can still decreate the default
262+
// termination duration to speed up the test.
263+
const defaultDur = 50 * time.Millisecond
264+
defer mcp.SetDefaultTerminateDuration(defaultDur)()
265+
260266
tests := []struct {
261267
name string
262268
duration time.Duration
269+
wantMinDuration time.Duration
263270
wantMaxDuration time.Duration
264271
}{
265272
{
266273
name: "default duration (zero)",
267274
duration: 0,
268-
wantMaxDuration: 6 * time.Second, // default 5s + buffer
275+
wantMinDuration: defaultDur,
276+
wantMaxDuration: 1 * time.Second, // default + buffer
269277
},
270278
{
271279
name: "below minimum duration",
272-
duration: 500 * time.Millisecond,
273-
wantMaxDuration: 6 * time.Second, // should use default 5s + buffer
280+
duration: -500 * time.Millisecond,
281+
wantMinDuration: defaultDur,
282+
wantMaxDuration: 1 * time.Second, // should use default + buffer
274283
},
275284
{
276285
name: "custom valid duration",
277-
duration: 2 * time.Second,
278-
wantMaxDuration: 3 * time.Second, // custom 2s + buffer
286+
duration: 200 * time.Millisecond,
287+
wantMinDuration: 200 * time.Millisecond,
288+
wantMaxDuration: 1 * time.Second, // custom + buffer
279289
},
280290
}
281291

@@ -306,7 +316,9 @@ func TestCommandTransportTerminateDuration(t *testing.T) {
306316
t.Fatalf("Close() failed with unexpected error: %v", err)
307317
}
308318
}
309-
319+
if elapsed < tt.wantMinDuration {
320+
t.Errorf("Close() took %v, expected at least %v", elapsed, tt.wantMinDuration)
321+
}
310322
if elapsed > tt.wantMaxDuration {
311323
t.Errorf("Close() took %v, expected at most %v", elapsed, tt.wantMaxDuration)
312324
}

0 commit comments

Comments
 (0)