|
1 | | -using System.Diagnostics; |
| 1 | +using ModelContextProtocol.Client; |
| 2 | +using ModelContextProtocol.Protocol.Transport; |
| 3 | +using System.Diagnostics; |
2 | 4 | using System.Runtime.InteropServices; |
3 | 5 |
|
4 | 6 | namespace ModelContextProtocol.Tests; |
@@ -26,19 +28,44 @@ public async Task SigInt_DisposesTestServerWithHosting_Gracefully() |
26 | 28 |
|
27 | 29 | process.Start(); |
28 | 30 |
|
| 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 | + |
29 | 47 | // I considered writing a similar test for windows using Ctrl-C, then saw that dotnet watch doesn't even send a Ctrl-C |
30 | 48 | // signal because it's such a pain without support CREATE_NEW_PROCESS_GROUP in System.Diagnostics.Process. |
31 | 49 | // https://github.com/dotnet/sdk/blob/43b1c12e3362098a23ca1018503eb56516840b6a/src/BuiltInTools/dotnet-watch/Internal/ProcessRunner.cs#L277-L303 |
32 | 50 | // https://github.com/dotnet/runtime/issues/109432, https://github.com/dotnet/runtime/issues/44944 |
33 | 51 | Assert.Equal(0, kill(process.Id, SIGINT)); |
34 | 52 |
|
35 | | - using var shutdownCts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 53 | + using var shutdownCts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.Current.CancellationToken); |
| 54 | + shutdownCts.CancelAfter(TimeSpan.FromSeconds(10)); |
36 | 55 | await process.WaitForExitAsync(shutdownCts.Token); |
37 | 56 |
|
38 | 57 | Assert.True(process.HasExited); |
39 | | - Assert.Equal(130, process.ExitCode); |
| 58 | + Assert.Equal(0, process.ExitCode); |
40 | 59 | } |
41 | 60 |
|
42 | 61 | [DllImport("libc", SetLastError = true)] |
43 | 62 | 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 | + } |
44 | 71 | } |
0 commit comments