Fix private reflection (#60)
* Fix private reflection Two changes in this PR - Remove private reflection from one of the tests - Move to use `TaskCompletionSource` (feedback from previous PR) * fix * spelling
This commit is contained in:
parent
d18da9a08d
commit
e76dbb5372
2 changed files with 17 additions and 19 deletions
|
|
@ -89,10 +89,12 @@ public class McpServerTests
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var server = McpServerFactory.Create(_serverTransport.Object, _options, _loggerFactory.Object, _serviceProvider);
|
await using var server = McpServerFactory.Create(_serverTransport.Object, _options, _loggerFactory.Object, _serviceProvider);
|
||||||
server.GetType().GetField("_isInitializing", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.SetValue(server, true);
|
var task = server.StartAsync(TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
await Assert.ThrowsAsync<InvalidOperationException>(() => server.StartAsync(TestContext.Current.CancellationToken));
|
await Assert.ThrowsAsync<InvalidOperationException>(() => server.StartAsync(TestContext.Current.CancellationToken));
|
||||||
|
|
||||||
|
await task;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
|
|
@ -110,48 +110,44 @@ public class SseClientTransportTests
|
||||||
using var mockHttpHandler = new MockHttpHandler();
|
using var mockHttpHandler = new MockHttpHandler();
|
||||||
using var httpClient = new HttpClient(mockHttpHandler);
|
using var httpClient = new HttpClient(mockHttpHandler);
|
||||||
await using var transport = new SseClientTransport(_transportOptions, _serverConfig, httpClient, NullLoggerFactory.Instance);
|
await using var transport = new SseClientTransport(_transportOptions, _serverConfig, httpClient, NullLoggerFactory.Instance);
|
||||||
using var mreConnected = new ManualResetEventSlim(false);
|
var tcsConnected = new TaskCompletionSource();
|
||||||
using var mreDone = new ManualResetEventSlim(false);
|
var tcsDone = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||||
var callIndex = 0;
|
var callIndex = 0;
|
||||||
|
|
||||||
mockHttpHandler.RequestHandler = (request) =>
|
mockHttpHandler.RequestHandler = async (request) =>
|
||||||
{
|
{
|
||||||
switch (callIndex++)
|
switch (callIndex++)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
return Task.FromResult(new HttpResponseMessage
|
return new HttpResponseMessage
|
||||||
{
|
{
|
||||||
StatusCode = HttpStatusCode.OK,
|
StatusCode = HttpStatusCode.OK,
|
||||||
Content = new StringContent("event: endpoint\r\ndata: http://localhost\r\n\r\n")
|
Content = new StringContent("event: endpoint\r\ndata: http://localhost\r\n\r\n")
|
||||||
});
|
};
|
||||||
case 1:
|
case 1:
|
||||||
mreConnected.Set();
|
tcsConnected.SetResult();
|
||||||
mreDone.Wait();
|
await tcsDone.Task;
|
||||||
return Task.FromResult(new HttpResponseMessage
|
return new HttpResponseMessage
|
||||||
{
|
{
|
||||||
StatusCode = HttpStatusCode.OK,
|
StatusCode = HttpStatusCode.OK,
|
||||||
Content = new StringContent("")
|
Content = new StringContent("")
|
||||||
});
|
};
|
||||||
default:
|
default:
|
||||||
return Task.FromResult(new HttpResponseMessage
|
return new HttpResponseMessage
|
||||||
{
|
{
|
||||||
StatusCode = HttpStatusCode.OK,
|
StatusCode = HttpStatusCode.OK,
|
||||||
Content = new StringContent("")
|
Content = new StringContent("")
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var task = Task.Run(async () =>
|
var task = transport.ConnectAsync(TestContext.Current.CancellationToken);
|
||||||
{
|
await tcsConnected.Task;
|
||||||
await transport.ConnectAsync(TestContext.Current.CancellationToken);
|
|
||||||
}, TestContext.Current.CancellationToken);
|
|
||||||
|
|
||||||
mreConnected.Wait(TestContext.Current.CancellationToken);
|
|
||||||
Assert.True(transport.IsConnected);
|
Assert.True(transport.IsConnected);
|
||||||
var action = async () => await transport.ConnectAsync();
|
var action = async () => await transport.ConnectAsync();
|
||||||
var exception = await Assert.ThrowsAsync<McpTransportException>(action);
|
var exception = await Assert.ThrowsAsync<McpTransportException>(action);
|
||||||
Assert.Equal("Transport is already connected", exception.Message);
|
Assert.Equal("Transport is already connected", exception.Message);
|
||||||
mreDone.Set();
|
tcsDone.SetResult();
|
||||||
await transport.CloseAsync();
|
await transport.CloseAsync();
|
||||||
await task;
|
await task;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue