Skip to content

Commit dde8f40

Browse files
[Infra] Avoid test flakiness (open-telemetry#6329)
1 parent e21e9cc commit dde8f40

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorTests.cs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void CheckConstructorWithInvalidValues()
2727
}
2828

2929
[Fact]
30-
public void CheckIfBatchIsExportingOnQueueLimit()
30+
public async Task CheckIfBatchIsExportingOnQueueLimit()
3131
{
3232
var exportedItems = new List<Activity>();
3333
using var exporter = new InMemoryExporter<Activity>(exportedItems);
@@ -44,10 +44,7 @@ public void CheckIfBatchIsExportingOnQueueLimit()
4444

4545
processor.OnEnd(activity);
4646

47-
for (int i = 0; i < 10 && exportedItems.Count == 0; i++)
48-
{
49-
Thread.Sleep(500);
50-
}
47+
await WaitForMinimumCountAsync(exportedItems, 1);
5148

5249
Assert.Single(exportedItems);
5350

@@ -69,7 +66,7 @@ public void CheckForceFlushWithInvalidTimeout()
6966
[InlineData(Timeout.Infinite)]
7067
[InlineData(0)]
7168
[InlineData(1)]
72-
public void CheckForceFlushExport(int timeout)
69+
public async Task CheckForceFlushExport(int timeout)
7370
{
7471
var exportedItems = new List<Activity>();
7572
using var exporter = new InMemoryExporter<Activity>(exportedItems);
@@ -95,30 +92,31 @@ public void CheckForceFlushExport(int timeout)
9592
Assert.Equal(0, processor.ProcessedCount);
9693

9794
// waiting to see if time is triggering the exporter
98-
Thread.Sleep(1_000);
95+
await Task.Delay(TimeSpan.FromSeconds(1));
9996
Assert.Empty(exportedItems);
10097

10198
// forcing flush
102-
processor.ForceFlush(timeout);
99+
var result = processor.ForceFlush(timeout);
103100

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);
109102

110-
Assert.Equal(2, exportedItems.Count);
103+
// Wait for the expected number of items to be exported
104+
int expectedCount = 2;
111105

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);
114112
Assert.Equal(0, processor.DroppedCount);
115113
}
116114

117115
[Theory]
118116
[InlineData(Timeout.Infinite)]
119117
[InlineData(0)]
120118
[InlineData(1)]
121-
public void CheckShutdownExport(int timeoutMilliseconds)
119+
public async Task CheckShutdownExport(int timeoutMilliseconds)
122120
{
123121
var exportedItems = new List<Activity>();
124122
using var exporter = new InMemoryExporter<Activity>(exportedItems);
@@ -136,10 +134,7 @@ public void CheckShutdownExport(int timeoutMilliseconds)
136134
processor.OnEnd(activity);
137135
processor.Shutdown(timeoutMilliseconds);
138136

139-
if (timeoutMilliseconds < 1_000)
140-
{
141-
Thread.Sleep(1_000 - timeoutMilliseconds);
142-
}
137+
await WaitForMinimumCountAsync(exportedItems, 1);
143138

144139
Assert.Single(exportedItems);
145140

@@ -193,6 +188,21 @@ public void CheckExportDrainsBatchOnFailure()
193188
Assert.Equal(3, processor.ProcessedCount); // Verify batch was drained even though nothing was exported.
194189
}
195190

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+
196206
private sealed class FailureExporter<T> : BaseExporter<T>
197207
where T : class
198208
{

0 commit comments

Comments
 (0)