1+ using System . Diagnostics ;
12using System . Net ;
23using System . Text . Json . Nodes ;
34using System . Threading . RateLimiting ;
1314using Microsoft . Extensions . DependencyInjection ;
1415using Microsoft . Extensions . DependencyInjection . Extensions ;
1516using OpenTelemetry . Metrics ;
17+ using OpenTelemetry . Trace ;
1618using Serilog . Core ;
1719using Xunit ;
1820
@@ -21,6 +23,7 @@ namespace LearnStack.Tests.Integration;
2123public sealed partial class AuthenticatedRateLimitTests
2224{
2325 private static readonly TimeSpan AdmissionDeadline = TimeSpan . FromSeconds ( 15 ) ;
26+ private const string AdmissionTraceSource = "test.admission.host-lifetime" ;
2427 private const string UnknownAdmissionPath = "/api/v1/admission-unknown" ;
2528
2629 [ Theory ]
@@ -134,10 +137,10 @@ public async Task Signed_admission_exports_quota_capacity_and_sticky_fault_witho
134137 TenantAuthenticationIssuer . Actor . ToString ( "D" ) , SeedData . English . TenantId . Value . ToString ( "D" ) } )
135138 collector . Metrics . Should ( ) . NotContain ( canary ) ;
136139 await AssertFaultedShutdown ( scenario ) ;
137- DisposeFaultedClient ( client , scenario ) ;
140+ await DisposeAdmissionClientAsync ( client ) ;
138141 client = null ;
139142 }
140- finally { DisposeFaultedClient ( client , scenario ) ; }
143+ finally { await DisposeAdmissionClientAsync ( client ) ; }
141144 }
142145
143146 [ Fact ]
@@ -149,12 +152,17 @@ public async Task Blocked_acquisition_cleanup_holds_its_signed_request_until_fau
149152 AssertNoStoreHeaderControls ( ) ;
150153 var observed = new SubjectQuotaObservation ( ) ;
151154 var memberships = new AdmissionMembershipObservation ( ) ;
155+ using var traceProbe = new ActivitySource ( AdmissionTraceSource ) ;
156+ traceProbe . HasListeners ( ) . Should ( ) . BeFalse (
157+ "no previous admission-test host may retain this explicitly observed source" ) ;
152158 HttpClient ? client = AdmissionClient ( issuer , scenario , observed , memberships ) ;
153159 using var release = new ManualResetEventSlim ( ) ;
154160 var entered = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
155161 Task < HttpResponseMessage > ? pending = null ;
156162 try
157163 {
164+ traceProbe . HasListeners ( ) . Should ( ) . BeTrue (
165+ "the actual API host must make the cleanup control observable" ) ;
158166 using ( var first = await AdmissionSend ( client , issuer , "blocked-retiring-subject" ) )
159167 first . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
160168 using ( var existing = await AdmissionSend ( client , issuer , "blocked-retained-subject" ) )
@@ -190,8 +198,10 @@ public async Task Blocked_acquisition_cleanup_holds_its_signed_request_until_fau
190198 ( await fixture . AuditCountAsync ( SeedData . English ) ) . Should ( ) . Be ( audits ) ;
191199 scenario . Budgets . Should ( ) . HaveCount ( 3 , "the failed owner cannot allocate after its disposal returns" ) ;
192200 await AssertFaultedShutdown ( scenario ) ;
193- DisposeFaultedClient ( client , scenario ) ;
201+ await DisposeAdmissionClientAsync ( client ) ;
194202 client = null ;
203+ traceProbe . HasListeners ( ) . Should ( ) . BeFalse (
204+ "host disposal must reach telemetry after the scenario joins its deliberately faulted owner" ) ;
195205 }
196206 finally
197207 {
@@ -200,7 +210,7 @@ public async Task Blocked_acquisition_cleanup_holds_its_signed_request_until_fau
200210 {
201211 if ( pending is not null ) ( await pending . WaitAsync ( AdmissionDeadline ) ) . Dispose ( ) ;
202212 }
203- finally { DisposeFaultedClient ( client , scenario ) ; }
213+ finally { await DisposeAdmissionClientAsync ( client ) ; }
204214 }
205215 }
206216
@@ -210,7 +220,10 @@ private HttpClient AdmissionClient(TenantAuthenticationIssuer issuer, AdmissionS
210220 {
211221 AddQuotaBindings ( services , issuer ) ;
212222 services . RemoveAll < TenantSubjectRateLimiter > ( ) ;
213- services . AddSingleton ( _ => scenario . Owner ) ;
223+ // The scenario creates and joins this deliberately faulting owner.
224+ // Giving DI a second disposal claim would interrupt host cleanup.
225+ services . AddSingleton ( scenario . Owner ) ;
226+ services . AddOpenTelemetry ( ) . WithTracing ( tracing => tracing . AddSource ( AdmissionTraceSource ) ) ;
214227 services . AddScoped < AuditingTenantAssertionRecorder > ( ) ;
215228 services . AddScoped < ITenantAssertionRecorder > ( provider => new ObservedQuotaRecorder (
216229 provider . GetRequiredService < AuditingTenantAssertionRecorder > ( ) , observed ) ) ;
@@ -281,15 +294,16 @@ private static async Task AssertFaultedShutdown(AdmissionScenario scenario)
281294 "shutdown drains the retained partitions without retrying the failed retirement" ) ;
282295 }
283296
284- private static void DisposeFaultedClient ( HttpClient ? client , AdmissionScenario scenario )
297+ private static async Task DisposeAdmissionClientAsync ( HttpClient ? client )
285298 {
286299 if ( client is null ) return ;
287- try { client . Dispose ( ) ; }
288- catch ( Exception error ) when ( IsKnownFailureTree ( error , scenario . Failure ) )
289- {
290- // Preserve a primary assertion failure while joining the known
291- // faulted owner. Successful paths assert the failure above.
292- }
300+ client . Dispose ( ) ;
301+ // The entry point can still be finishing asynchronous host disposal.
302+ // Observe this host's real subscription retirement; time alone is no credit.
303+ using var source = new ActivitySource ( AdmissionTraceSource ) ;
304+ using var deadline = new CancellationTokenSource ( AdmissionDeadline ) ;
305+ while ( source . HasListeners ( ) )
306+ await Task . Delay ( TimeSpan . FromMilliseconds ( 10 ) , deadline . Token ) ;
293307 }
294308
295309 private static bool IsKnownFailureTree ( Exception error , Exception expected ) => ReferenceEquals ( error , expected )
0 commit comments