@@ -36,8 +36,6 @@ func SayHi(ctx context.Context, req *mcp.CallToolRequest, args SayHiParams) (*mc
3636}
3737
3838func 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
0 commit comments