Skip to content

Commit 3b7bbdd

Browse files
committed
Add cancellation test
1 parent 997ebbc commit 3b7bbdd

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

tests/ModelContextProtocol.Tests/Client/McpClientFactoryTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using ModelContextProtocol.Protocol.Types;
66
using ModelContextProtocol.Utils.Json;
77
using Moq;
8+
using System.IO.Pipelines;
89
using System.Text.Json;
910
using System.Threading.Channels;
1011

@@ -29,6 +30,31 @@ public async Task CreateAsync_NopTransport_ReturnsClient()
2930
Assert.NotNull(client);
3031
}
3132

33+
[Theory]
34+
[InlineData(false)]
35+
[InlineData(true)]
36+
public async Task Cancellation_ThrowsCancellationException(bool preCanceled)
37+
{
38+
var cts = new CancellationTokenSource();
39+
40+
if (preCanceled)
41+
{
42+
cts.Cancel();
43+
}
44+
45+
Task t = McpClientFactory.CreateAsync(
46+
new StreamClientTransport(new Pipe().Writer.AsStream(), new Pipe().Reader.AsStream()),
47+
cancellationToken: cts.Token);
48+
Assert.False(t.IsCompleted);
49+
50+
if (!preCanceled)
51+
{
52+
cts.Cancel();
53+
}
54+
55+
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => t);
56+
}
57+
3258
[Theory]
3359
[InlineData(typeof(NopTransport))]
3460
[InlineData(typeof(FailureTransport))]

0 commit comments

Comments
 (0)