From 8e1347fb3b29a483c06a5060a5060b09a3304e95 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Fri, 14 Nov 2025 17:54:52 -0800 Subject: [PATCH] Fix tests in Dev Container --- .devcontainer/devcontainer.json | 2 +- .../Utils/KestrelInMemoryTest.cs | 10 +++++----- .../Utils/KestrelInMemoryTransport.cs | 19 ++++++++++++------- .../Program.cs | 3 --- .../Program.cs | 2 -- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 03b0a88d9..fcd032783 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -19,5 +19,5 @@ } } }, - "postCreateCommand": "dotnet --list-sdks && echo 'Available .NET SDKs installed successfully!'" + "postCreateCommand": "dotnet dev-certs https --trust && dotnet --list-sdks && echo 'Available .NET SDKs installed successfully!'" } \ No newline at end of file diff --git a/tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTest.cs b/tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTest.cs index 4ae743f72..0045e7bc2 100644 --- a/tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTest.cs +++ b/tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTest.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; using ModelContextProtocol.Tests.Utils; namespace ModelContextProtocol.AspNetCore.Tests.Utils; @@ -11,11 +11,11 @@ public class KestrelInMemoryTest : LoggedTest public KestrelInMemoryTest(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { - // Use SlimBuilder instead of EmptyBuilder to avoid having to call UseRouting() and UseEndpoints(_ => { }) - // or a helper that does the same every test. But clear out the existing socket transport to avoid potential port conflicts. - Builder = WebApplication.CreateSlimBuilder(); - Builder.Services.RemoveAll(); + Builder = WebApplication.CreateEmptyBuilder(new()); Builder.Services.AddSingleton(KestrelInMemoryTransport); + Builder.WebHost.UseKestrelCore(); + Builder.Services.AddRoutingCore(); + Builder.Services.AddLogging(); Builder.Services.AddSingleton(XunitLoggerProvider); SocketsHttpHandler.ConnectCallback = (context, token) => diff --git a/tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTransport.cs b/tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTransport.cs index e5686a16f..e2f77a98a 100644 --- a/tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTransport.cs +++ b/tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTransport.cs @@ -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> _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 KestrelInMemoryConnection CreateConnection(EndPoint endpoint) return connection; } - public ValueTask BindAsync(EndPoint endpoint, CancellationToken cancellationToken = default) => - new(new KestrelInMemoryListener(endpoint, GetAcceptQueue(endpoint))); - - private Channel GetAcceptQueue(EndPoint endpoint) => - _acceptQueues.GetOrAdd(GetEndpointPort(endpoint), _ => Channel.CreateUnbounded()); + public ValueTask BindAsync(EndPoint endpoint, CancellationToken cancellationToken = default) + { + var acceptQueue = _acceptQueues.GetOrAdd(GetEndpointPort(endpoint), _ => Channel.CreateUnbounded()); + return new(new KestrelInMemoryListener(endpoint, acceptQueue)); + } private static int GetEndpointPort(EndPoint endpoint) => endpoint switch diff --git a/tests/ModelContextProtocol.TestOAuthServer/Program.cs b/tests/ModelContextProtocol.TestOAuthServer/Program.cs index bb251035d..dea484bfe 100644 --- a/tests/ModelContextProtocol.TestOAuthServer/Program.cs +++ b/tests/ModelContextProtocol.TestOAuthServer/Program.cs @@ -96,9 +96,6 @@ public async Task RunServerAsync(string[]? args = null, CancellationToken cancel var app = builder.Build(); - app.UseRouting(); - app.UseEndpoints(_ => { }); - // Set up the demo client var clientId = "demo-client"; var clientSecret = "demo-secret"; diff --git a/tests/ModelContextProtocol.TestSseServer/Program.cs b/tests/ModelContextProtocol.TestSseServer/Program.cs index 9eef66400..183a64e7e 100644 --- a/tests/ModelContextProtocol.TestSseServer/Program.cs +++ b/tests/ModelContextProtocol.TestSseServer/Program.cs @@ -424,8 +424,6 @@ public static async Task MainAsync(string[] args, ILoggerProvider? loggerProvide .WithHttpTransport(); var app = builder.Build(); - app.UseRouting(); - app.UseEndpoints(_ => { }); // Handle the /stateless endpoint if no other endpoints have been matched by the call to UseRouting above. HandleStatelessMcp(app);