forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStdioServerIntegrationTests.cs
More file actions
67 lines (55 loc) · 2.84 KB
/
StdioServerIntegrationTests.cs
File metadata and controls
67 lines (55 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using ModelContextProtocol.Tests.Utils;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ModelContextProtocol.Tests;
public class StdioServerIntegrationTests(ITestOutputHelper testOutputHelper) : LoggedTest(testOutputHelper)
{
public static bool CanSendSigInt { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
private const int SIGINT = 2;
[Fact(Skip = "Platform not supported by this test.", SkipUnless = nameof(CanSendSigInt))]
public async Task SigInt_DisposesTestServerWithHosting_Gracefully()
{
using var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = "TestServerWithHosting.dll",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
await using var streamServerTransport = new StreamServerTransport(
process.StandardOutput.BaseStream,
process.StandardInput.BaseStream,
serverName: "TestServerWithHosting");
await using var client = await McpClientFactory.CreateAsync(
new TestClientTransport(streamServerTransport),
loggerFactory: LoggerFactory,
cancellationToken: TestContext.Current.CancellationToken);
// I considered writing a similar test for Windows using Ctrl-C, then saw that dotnet watch doesn't even send a Ctrl-C
// signal because it's such a pain without support for CREATE_NEW_PROCESS_GROUP in System.Diagnostics.Process.
// https://github.com/dotnet/sdk/blob/43b1c12e3362098a23ca1018503eb56516840b6a/src/BuiltInTools/dotnet-watch/Internal/ProcessRunner.cs#L277-L303
// https://github.com/dotnet/runtime/issues/109432, https://github.com/dotnet/runtime/issues/44944
Assert.Equal(0, kill(process.Id, SIGINT));
using var shutdownCts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.Current.CancellationToken);
shutdownCts.CancelAfter(TimeSpan.FromSeconds(10));
await process.WaitForExitAsync(shutdownCts.Token);
Assert.True(process.HasExited);
Assert.Equal(0, process.ExitCode);
}
[DllImport("libc", SetLastError = true)]
private static extern int kill(int pid, int sig);
private sealed class TestClientTransport(ITransport sessionTransport) : IClientTransport
{
public string Name => nameof(TestClientTransport);
public Task<ITransport> ConnectAsync(CancellationToken cancellationToken = default)
=> Task.FromResult(sessionTransport);
}
}