Skip to content

Commit 98b3879

Browse files
committed
Connect client before sending SIGINT in test
1 parent e6a591b commit 98b3879

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

tests/ModelContextProtocol.Tests/StdioServerIntegrationTests.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Diagnostics;
1+
using ModelContextProtocol.Client;
2+
using ModelContextProtocol.Protocol.Transport;
3+
using System.Diagnostics;
24
using System.Runtime.InteropServices;
35

46
namespace ModelContextProtocol.Tests;
@@ -26,19 +28,44 @@ public async Task SigInt_DisposesTestServerWithHosting_Gracefully()
2628

2729
process.Start();
2830

31+
await using var streamServerTransport = new StreamServerTransport(
32+
process.StandardOutput.BaseStream,
33+
process.StandardInput.BaseStream,
34+
serverName: "TestServerWithHosting");
35+
36+
var serverConfig = new McpServerConfig()
37+
{
38+
Id = "test-server-with-hosting",
39+
Name = "TestServerWithHosting",
40+
TransportType = TransportTypes.StdIo,
41+
};
42+
43+
await using var client = await McpClientFactory.CreateAsync(serverConfig,
44+
createTransportFunc: (_, _) => new TestClientTransport(streamServerTransport),
45+
cancellationToken: TestContext.Current.CancellationToken);
46+
2947
// I considered writing a similar test for windows using Ctrl-C, then saw that dotnet watch doesn't even send a Ctrl-C
3048
// signal because it's such a pain without support CREATE_NEW_PROCESS_GROUP in System.Diagnostics.Process.
3149
// https://github.com/dotnet/sdk/blob/43b1c12e3362098a23ca1018503eb56516840b6a/src/BuiltInTools/dotnet-watch/Internal/ProcessRunner.cs#L277-L303
3250
// https://github.com/dotnet/runtime/issues/109432, https://github.com/dotnet/runtime/issues/44944
3351
Assert.Equal(0, kill(process.Id, SIGINT));
3452

35-
using var shutdownCts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
53+
using var shutdownCts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.Current.CancellationToken);
54+
shutdownCts.CancelAfter(TimeSpan.FromSeconds(10));
3655
await process.WaitForExitAsync(shutdownCts.Token);
3756

3857
Assert.True(process.HasExited);
39-
Assert.Equal(130, process.ExitCode);
58+
Assert.Equal(0, process.ExitCode);
4059
}
4160

4261
[DllImport("libc", SetLastError = true)]
4362
internal static extern int kill(int pid, int sig);
63+
64+
private sealed class TestClientTransport(ITransport sessionTransport) : IClientTransport
65+
{
66+
public Task<ITransport> ConnectAsync(CancellationToken cancellationToken = default)
67+
{
68+
return Task.FromResult(sessionTransport);
69+
}
70+
}
4471
}

0 commit comments

Comments
 (0)