Skip to content

Commit e8b7d0c

Browse files
committed
fix: race condition fixed for BatchProcessorTest
1 parent 2691b8b commit e8b7d0c

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
A .NET Standard 2.0 library providing helpful utility classes for threading, networking, collections, and other common patterns frequently used across projects.
44

55
[![CI](https://github.com/dsmithson/KnightwareCore/actions/workflows/ci.yml/badge.svg)](https://github.com/dsmithson/KnightwareCore/actions/workflows/ci.yml)
6-
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=dsmithson_KnightwareCore&metric=alert_status)](https://sonarcloud.io/dashboard?id=dsmithson_KnightwareCore)
76
[![codecov](https://codecov.io/gh/dsmithson/KnightwareCore/branch/main/graph/badge.svg)](https://codecov.io/gh/dsmithson/KnightwareCore)
87
[![NuGet](https://img.shields.io/nuget/v/KnightwareCore.svg)](https://www.nuget.org/packages/KnightwareCore/)
98

src/KnightwareCoreTests/Threading/Tasks/BatchProcessorTests.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ public async Task MinimumElapsedMultipleTest()
210210

211211
var batchesSizesProcessed = new List<int>();
212212
int itemsProcessedInCurrentBatch = 0;
213+
var batchCompleteSignal = new TaskCompletionSource<bool>();
213214

214215
//Setup a handler that will increment our batch process count per item and add the batch count and a timestamp when a batch completes
215216
await TestSimpleSetup(
@@ -222,25 +223,30 @@ await TestSimpleSetup(
222223
{
223224
batchesSizesProcessed.Add(itemsProcessedInCurrentBatch);
224225
itemsProcessedInCurrentBatch = 0;
226+
batchCompleteSignal.TrySetResult(true);
225227
},
226228
minimumTimeIntervalMs: minMs,
227229
maximumTimeIntervalMs: 10000,
228230
maximumCount: int.MaxValue);
229231

230232
//Run our test duration, adding items as needed
231-
List<Task> tasks = new();
232233
for(int i=0; i<expectedBatches; i++)
233234
{
234-
//We'll add a couple items, then wait for our min refresh time to elapse
235+
//Reset signal for this batch
236+
batchCompleteSignal = new TaskCompletionSource<bool>();
237+
238+
//Add item(s) for this batch
239+
List<Task> batchTasks = new();
235240
for (int j = 0; j < expectedItemsPerBatch; j++)
236241
{
237-
tasks.Add(processor.EnqueueAsync(0));
242+
batchTasks.Add(processor.EnqueueAsync(0));
238243
}
239-
await Task.Delay(minMs * 2);
240-
}
241244

242-
//Wait for last batch to finish...
243-
await Task.WhenAll(tasks);
245+
//Wait for the batch to fully complete (including onBatchProcessed callback)
246+
//Task.WhenAll only waits for SetResponse, but we need to wait for the callback too
247+
await Task.WhenAll(batchTasks);
248+
await batchCompleteSignal.Task;
249+
}
244250

245251
//Verify we processed the correct number of batches
246252
Assert.HasCount(expectedBatches, batchesSizesProcessed, "Incorrect number of batches processed");

0 commit comments

Comments
 (0)