Fix tests in Dev Container (#965)

This commit is contained in:
Stephen Halter 2025-11-17 08:48:18 -08:00 • committed by GitHub
commit b9c9f44e06
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,13 +7,18 @@ namespace ModelContextProtocol.AspNetCore.Tests.Utils;
public sealed class KestrelInMemoryTransport : IConnectionListenerFactory
{
// socket accept queues keyed by listen port.
// Socket accept queues keyed by listen port.
private readonly ConcurrentDictionary<int, Channel<ConnectionContext>> _acceptQueues = [];
public KestrelInMemoryConnection CreateConnection(EndPoint endpoint)
{
if (!_acceptQueues.TryGetValue(GetEndpointPort(endpoint), out var acceptQueue))
{
throw new IOException($"No listener is bound to endpoint '{endpoint}'.");
}
var connection = new KestrelInMemoryConnection();
if (!GetAcceptQueue(endpoint).Writer.TryWrite(connection))
if (!acceptQueue.Writer.TryWrite(connection))
{
throw new IOException("The KestrelInMemoryTransport has been shut down.");
};
@ -21,11 +26,11 @@ public sealed class KestrelInMemoryTransport : IConnectionListenerFactory
return connection;
}
public ValueTask<IConnectionListener> BindAsync(EndPoint endpoint, CancellationToken cancellationToken = default) =>
new(new KestrelInMemoryListener(endpoint, GetAcceptQueue(endpoint)));
private Channel<ConnectionContext> GetAcceptQueue(EndPoint endpoint) =>
_acceptQueues.GetOrAdd(GetEndpointPort(endpoint), _ => Channel.CreateUnbounded<ConnectionContext>());
public ValueTask<IConnectionListener> BindAsync(EndPoint endpoint, CancellationToken cancellationToken = default)
{
var acceptQueue = _acceptQueues.GetOrAdd(GetEndpointPort(endpoint), _ => Channel.CreateUnbounded<ConnectionContext>());
return new(new KestrelInMemoryListener(endpoint, acceptQueue));
}
private static int GetEndpointPort(EndPoint endpoint) =>
endpoint switch