@@ -27,7 +27,7 @@ public void CheckConstructorWithInvalidValues()
27
27
}
28
28
29
29
[ Fact ]
30
- public void CheckIfBatchIsExportingOnQueueLimit ( )
30
+ public async Task CheckIfBatchIsExportingOnQueueLimit ( )
31
31
{
32
32
var exportedItems = new List < Activity > ( ) ;
33
33
using var exporter = new InMemoryExporter < Activity > ( exportedItems ) ;
@@ -44,10 +44,7 @@ public void CheckIfBatchIsExportingOnQueueLimit()
44
44
45
45
processor . OnEnd ( activity ) ;
46
46
47
- for ( int i = 0 ; i < 10 && exportedItems . Count == 0 ; i ++ )
48
- {
49
- Thread . Sleep ( 500 ) ;
50
- }
47
+ await WaitForMinimumCountAsync ( exportedItems , 1 ) ;
51
48
52
49
Assert . Single ( exportedItems ) ;
53
50
@@ -69,7 +66,7 @@ public void CheckForceFlushWithInvalidTimeout()
69
66
[ InlineData ( Timeout . Infinite ) ]
70
67
[ InlineData ( 0 ) ]
71
68
[ InlineData ( 1 ) ]
72
- public void CheckForceFlushExport ( int timeout )
69
+ public async Task CheckForceFlushExport ( int timeout )
73
70
{
74
71
var exportedItems = new List < Activity > ( ) ;
75
72
using var exporter = new InMemoryExporter < Activity > ( exportedItems ) ;
@@ -95,30 +92,31 @@ public void CheckForceFlushExport(int timeout)
95
92
Assert . Equal ( 0 , processor . ProcessedCount ) ;
96
93
97
94
// waiting to see if time is triggering the exporter
98
- Thread . Sleep ( 1_000 ) ;
95
+ await Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) ;
99
96
Assert . Empty ( exportedItems ) ;
100
97
101
98
// forcing flush
102
- processor . ForceFlush ( timeout ) ;
99
+ var result = processor . ForceFlush ( timeout ) ;
103
100
104
- if ( timeout == 0 )
105
- {
106
- // ForceFlush(0) will trigger flush and return immediately, so let's sleep for a while
107
- Thread . Sleep ( 1_000 ) ;
108
- }
101
+ Assert . Equal ( timeout != 0 , result ) ;
109
102
110
- Assert . Equal ( 2 , exportedItems . Count ) ;
103
+ // Wait for the expected number of items to be exported
104
+ int expectedCount = 2 ;
111
105
112
- Assert . Equal ( 2 , processor . ProcessedCount ) ;
113
- Assert . Equal ( 2 , processor . ReceivedCount ) ;
106
+ await WaitForMinimumCountAsync ( exportedItems , expectedCount ) ;
107
+
108
+ Assert . Equal ( expectedCount , exportedItems . Count ) ;
109
+
110
+ Assert . Equal ( expectedCount , processor . ProcessedCount ) ;
111
+ Assert . Equal ( expectedCount , processor . ReceivedCount ) ;
114
112
Assert . Equal ( 0 , processor . DroppedCount ) ;
115
113
}
116
114
117
115
[ Theory ]
118
116
[ InlineData ( Timeout . Infinite ) ]
119
117
[ InlineData ( 0 ) ]
120
118
[ InlineData ( 1 ) ]
121
- public void CheckShutdownExport ( int timeoutMilliseconds )
119
+ public async Task CheckShutdownExport ( int timeoutMilliseconds )
122
120
{
123
121
var exportedItems = new List < Activity > ( ) ;
124
122
using var exporter = new InMemoryExporter < Activity > ( exportedItems ) ;
@@ -136,10 +134,7 @@ public void CheckShutdownExport(int timeoutMilliseconds)
136
134
processor . OnEnd ( activity ) ;
137
135
processor . Shutdown ( timeoutMilliseconds ) ;
138
136
139
- if ( timeoutMilliseconds < 1_000 )
140
- {
141
- Thread . Sleep ( 1_000 - timeoutMilliseconds ) ;
142
- }
137
+ await WaitForMinimumCountAsync ( exportedItems , 1 ) ;
143
138
144
139
Assert . Single ( exportedItems ) ;
145
140
@@ -193,6 +188,21 @@ public void CheckExportDrainsBatchOnFailure()
193
188
Assert . Equal ( 3 , processor . ProcessedCount ) ; // Verify batch was drained even though nothing was exported.
194
189
}
195
190
191
+ private static async Task WaitForMinimumCountAsync ( List < Activity > collection , int minimum )
192
+ {
193
+ var maximumWait = TimeSpan . FromSeconds ( 5 ) ;
194
+ var waitInterval = TimeSpan . FromSeconds ( 0.25 ) ;
195
+
196
+ using var cts = new CancellationTokenSource ( maximumWait ) ;
197
+
198
+ // We check for a minimum because if there are too many it's better to
199
+ // terminate the loop and let the assert in the caller fail immediately
200
+ while ( ! cts . IsCancellationRequested && collection . Count < minimum )
201
+ {
202
+ await Task . Delay ( waitInterval ) ;
203
+ }
204
+ }
205
+
196
206
private sealed class FailureExporter < T > : BaseExporter < T >
197
207
where T : class
198
208
{
0 commit comments