11using System . Diagnostics ;
2+ using System . Collections . Concurrent ;
3+ using System . Diagnostics ;
24
35using Arbiter . Tests . Domain ;
46
@@ -24,14 +26,14 @@ private static ServiceProvider BuildProvider(Action<IServiceCollection> configur
2426 }
2527
2628 // Creates an ActivityListener subscribed to MediatorTelemetry.SourceName and collects stopped activities.
27- private static ( List < Activity > activities , ActivityListener listener ) CreateListener ( )
29+ private static ( ConcurrentQueue < Activity > activities , ActivityListener listener ) CreateListener ( )
2830 {
29- var activities = new List < Activity > ( ) ;
31+ var activities = new ConcurrentQueue < Activity > ( ) ;
3032 var listener = new ActivityListener
3133 {
3234 ShouldListenTo = src => src . Name == MediatorTelemetry . SourceName ,
3335 Sample = ( ref ActivityCreationOptions < ActivityContext > _ ) => ActivitySamplingResult . AllDataAndRecorded ,
34- ActivityStopped = activities . Add ,
36+ ActivityStopped = activities . Enqueue ,
3537 } ;
3638 ActivitySource . AddActivityListener ( listener ) ;
3739 return ( activities , listener ) ;
@@ -46,18 +48,18 @@ public async Task Send_CreatesTopLevelSpanWithCorrectAttributes()
4648 await using var provider = BuildProvider ( services =>
4749 {
4850 services . TryAddSingleton ( new Logger ( ) ) ;
49- services . TryAddTransient < IRequestHandler < Ping , Pong > , PingHandler > ( ) ;
51+ services . TryAddTransient < IRequestHandler < TelemetryPing , Pong > , TelemetryPingHandler > ( ) ;
5052 } ) ;
5153
5254 var mediator = provider . GetRequiredService < IMediator > ( ) ;
53- await mediator . Send < Ping , Pong > ( new Ping { Message = "Hello" } ) ;
55+ await mediator . Send < TelemetryPing , Pong > ( new TelemetryPing { Message = "Hello" } ) ;
5456
55- var span = activities . FirstOrDefault ( a => a . OperationName . StartsWith ( MediatorTelemetry . SendOperation ) ) ;
57+ var span = activities . FirstOrDefault ( a => a . OperationName == $ " { MediatorTelemetry . SendOperation } TelemetryPing" ) ;
5658 await Assert . That ( span ) . IsNotNull ( ) ;
5759 await Assert . That ( span ! . Status ) . IsEqualTo ( ActivityStatusCode . Ok ) ;
5860 await Assert . That ( span . Kind ) . IsEqualTo ( ActivityKind . Internal ) ;
59- await Assert . That ( span . OperationName ) . IsEqualTo ( $ "{ MediatorTelemetry . SendOperation } Ping ") ;
60- await Assert . That ( span . GetTagItem ( MediatorTelemetry . RequestTypeTag ) ? . ToString ( ) ) . IsEqualTo ( typeof ( Ping ) . FullName ) ;
61+ await Assert . That ( span . OperationName ) . IsEqualTo ( $ "{ MediatorTelemetry . SendOperation } TelemetryPing ") ;
62+ await Assert . That ( span . GetTagItem ( MediatorTelemetry . RequestTypeTag ) ? . ToString ( ) ) . IsEqualTo ( typeof ( TelemetryPing ) . FullName ) ;
6163 await Assert . That ( span . GetTagItem ( MediatorTelemetry . ResponseTypeTag ) ? . ToString ( ) ) . IsEqualTo ( typeof ( Pong ) . FullName ) ;
6264 }
6365
@@ -133,4 +135,17 @@ public async Task Publish_CreatesTopLevelSpanWithNotificationTag()
133135 await Assert . That ( span ! . Status ) . IsEqualTo ( ActivityStatusCode . Ok ) ;
134136 await Assert . That ( span . GetTagItem ( MediatorTelemetry . NotificationTypeTag ) ? . ToString ( ) ) . IsEqualTo ( typeof ( Pinged ) . FullName ) ;
135137 }
138+
139+ private sealed class TelemetryPing : IRequest < Pong >
140+ {
141+ public string ? Message { get ; init ; }
142+ }
143+
144+ private sealed class TelemetryPingHandler : IRequestHandler < TelemetryPing , Pong >
145+ {
146+ public ValueTask < Pong ? > Handle ( TelemetryPing request , CancellationToken cancellationToken = default )
147+ {
148+ return ValueTask . FromResult < Pong ? > ( new Pong { Message = $ "{ request . Message } Pong" } ) ;
149+ }
150+ }
136151}
0 commit comments