From 9a808ba2188c28721d84bad47510dd6f4ab72ecc Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Mon, 23 Nov 2026 22:12:28 -0500 Subject: [PATCH] test(grpc-data): H2 review 10 - real Kestrel HTTP/2 proves the connection-close hook clears the session In-process loopback h2c host: IConnectionLifetimeFeature.ConnectionClosed is exposed on the HTTP/2 stream context, two requests on one connection share one session, and disposing the client connection clears it. --- .../Grpc/DataConnectionGrpcService.cs | 6 ++- .../GrpcDataHistoryKestrelChannelTests.cs | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 tests/Server/ZB.MOM.WW.OtOpcUa.Host.Tests/DataConnections/GrpcDataHistoryKestrelChannelTests.cs diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Host/Grpc/DataConnectionGrpcService.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Host/Grpc/DataConnectionGrpcService.cs index 863d36da2..4122a3a71 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Host/Grpc/DataConnectionGrpcService.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Host/Grpc/DataConnectionGrpcService.cs @@ -211,9 +211,11 @@ public sealed class DataConnectionGrpcService(IDataConnectionFactory factory, IO /// The transport channel of this call: its HTTP/2 connection. Origin continuations are partitioned by it and cleared /// when Kestrel reports the connection closed (idle expiry is the backstop where the feature is absent). /// - private static DataHistoryChannel Channel(ServerCallContext context) + private static DataHistoryChannel Channel(ServerCallContext context) => ChannelOf(context.GetHttpContext()); + + /// The history channel of an HTTP request: its connection id, closed when Kestrel closes the connection. + internal static DataHistoryChannel ChannelOf(Microsoft.AspNetCore.Http.HttpContext http) { - var http = context.GetHttpContext(); var closed = http.Features.Get()?.ConnectionClosed ?? CancellationToken.None; return new DataHistoryChannel("http:" + (http.Connection.Id ?? "unknown"), closed); diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Host.Tests/DataConnections/GrpcDataHistoryKestrelChannelTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Host.Tests/DataConnections/GrpcDataHistoryKestrelChannelTests.cs new file mode 100644 index 000000000..792500412 --- /dev/null +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Host.Tests/DataConnections/GrpcDataHistoryKestrelChannelTests.cs @@ -0,0 +1,54 @@ +using System.Net; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Server.Kestrel.Core; +using Microsoft.Extensions.Logging; +using Shouldly; +using Xunit; +using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; +using ZB.MOM.WW.OtOpcUa.Host.Grpc; +using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections; + +namespace ZB.MOM.WW.OtOpcUa.Host.Tests.DataConnections; + +/// +/// H2 review 10: the origin's history session of an HTTP/2 connection must be cleared when Kestrel closes that +/// connection. A real in-process Kestrel HTTP/2 (cleartext, loopback) host proves the close hook fires on a stream's +/// HttpContext — the path uses. +/// +[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "H2")] +public sealed class GrpcDataHistoryKestrelChannelTests +{ + [Fact] + public async Task Closing_the_client_connection_clears_its_history_session() + { + var ct = TestContext.Current.CancellationToken; + var sessions = new OriginHistorySessions(TimeProvider.System); + var caller = new DataAuthenticatedContext("peer", "key", "v1", null, 1); + var cancellable = false; + var builder = WebApplication.CreateSlimBuilder(); + builder.Logging.ClearProviders(); + builder.WebHost.ConfigureKestrel(k => k.Listen(IPAddress.Loopback, 0, o => o.Protocols = HttpProtocols.Http2)); + await using var app = builder.Build(); + app.MapGet("/probe", (HttpContext http) => + { + var channel = DataConnectionGrpcService.ChannelOf(http); + cancellable = channel.Closed.CanBeCanceled; + sessions.Get(caller, channel, 4); + return Results.Ok(channel.ChannelId); + }); + await app.StartAsync(ct); + var address = app.Urls.Single(); + var client = new HttpClient(new SocketsHttpHandler()) { DefaultRequestVersion = HttpVersion.Version20, DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact }; + (await client.GetAsync(address + "/probe", ct)).StatusCode.ShouldBe(HttpStatusCode.OK); + (await client.GetAsync(address + "/probe", ct)).StatusCode.ShouldBe(HttpStatusCode.OK); + cancellable.ShouldBeTrue("Kestrel exposes IConnectionLifetimeFeature.ConnectionClosed on an HTTP/2 stream"); + sessions.Count.ShouldBe(1, "two requests on one HTTP/2 connection share one session"); + client.Dispose(); // closes the TCP/HTTP2 connection + var until = DateTime.UtcNow.AddSeconds(15); + while (sessions.Count != 0 && DateTime.UtcNow < until) await Task.Delay(20, ct); + sessions.Count.ShouldBe(0, "the connection close cleared the session"); + await app.StopAsync(ct); + } +}