@@ -21,7 +21,7 @@ public ObservabilityTests(ITestOutputHelper output)
2121 [ Fact ]
2222 public void It_creates_activity_spans_for_parsing ( )
2323 {
24- List < Activity > activities = SetupListener ( ) ;
24+ var ( listener , activities ) = SetupListener ( ) ;
2525
2626 var command = new Command ( "the-command" )
2727 {
@@ -31,7 +31,7 @@ public void It_creates_activity_spans_for_parsing()
3131 var args = new [ ] { "--option" , "the-argument" } ;
3232
3333 var result = command . Parse ( args ) ;
34-
34+ listener . Dispose ( ) ;
3535 activities
3636 . Should ( )
3737 . ContainSingle (
@@ -43,7 +43,7 @@ public void It_creates_activity_spans_for_parsing()
4343 [ Fact ]
4444 public void It_creates_activity_spans_for_parsing_errors ( )
4545 {
46- List < Activity > activities = SetupListener ( ) ;
46+ var ( listener , activities ) = SetupListener ( ) ;
4747
4848 var command = new Command ( "the-command" )
4949 {
@@ -52,7 +52,7 @@ public void It_creates_activity_spans_for_parsing_errors()
5252
5353 var args = new [ ] { "--opt" , "the-argument" } ;
5454 var result = command . Parse ( args ) ;
55-
55+ listener . Dispose ( ) ;
5656 activities
5757 . Should ( )
5858 . ContainSingle (
@@ -65,12 +65,14 @@ public void It_creates_activity_spans_for_parsing_errors()
6565 [ Fact ]
6666 public async Task It_creates_activity_spans_for_invocations ( )
6767 {
68- List < Activity > activities = SetupListener ( ) ;
68+ var ( listener , activities ) = SetupListener ( ) ;
6969
7070 var command = new Command ( "the-command" ) ;
7171 command . SetAction ( async ( pr , ctok ) => await Task . FromResult ( 0 ) ) ;
7272
7373 var result = await command . Parse ( Array . Empty < string > ( ) ) . InvokeAsync ( ) ;
74+ listener . Dispose ( ) ;
75+
7476 activities
7577 . Should ( )
7678 . ContainSingle (
@@ -85,7 +87,7 @@ public async Task It_creates_activity_spans_for_invocations()
8587 [ Fact ]
8688 public async Task It_creates_activity_spans_for_invocation_errors ( )
8789 {
88- List < Activity > activities = SetupListener ( ) ;
90+ var ( listener , activities ) = SetupListener ( ) ;
8991
9092 var command = new Command ( "the-command" ) ;
9193#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
@@ -96,10 +98,13 @@ public async Task It_creates_activity_spans_for_invocation_errors()
9698#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
9799
98100 var result = await command . Parse ( Array . Empty < string > ( ) ) . InvokeAsync ( ) ;
101+ listener . Dispose ( ) ;
102+
99103 foreach ( var x in activities )
100104 {
101105 log . WriteLine ( $ "{ x . DisplayName } ({ x . OperationName } )/{ x . Status } ({ x . Duration } ) - { x . TagObjects } - { string . Join ( "," , x . Events . Select ( ( k ) => $ "{ k . Name } ,{ k . Tags } ") ) } ") ;
102106 }
107+
103108 activities
104109 . Should ( )
105110 . ContainSingle (
@@ -112,15 +117,15 @@ public async Task It_creates_activity_spans_for_invocation_errors()
112117 && a . Events . Any ( t => t . Name == "exception" ) ) ;
113118 }
114119
115- private static List < Activity > SetupListener ( )
120+ private static ( ActivityListener , List < Activity > ) SetupListener ( )
116121 {
117122 List < Activity > activities = new ( ) ;
118123 var listener = new ActivityListener ( ) ;
119124 listener . ShouldListenTo = s => true ;
120125 listener . Sample = ( ref ActivityCreationOptions < ActivityContext > options ) => ActivitySamplingResult . AllData ;
121126 listener . ActivityStopped = a => activities . Add ( a ) ;
122127 ActivitySource . AddActivityListener ( listener ) ;
123- return activities ;
128+ return new ( listener , activities ) ;
124129 }
125130 }
126131}
0 commit comments