-
-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed issue that deadlocked DataLoader key batches (#7437)
- Loading branch information
1 parent
8929302
commit 0031a06
Showing
3 changed files
with
98 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/GreenDonut/test/Core.Tests/DataLoaderListBatchTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace GreenDonut; | ||
|
||
public static class DataLoaderListBatchTests | ||
{ | ||
[Fact] | ||
public static async Task Overflow_InternalBatch_Async() | ||
{ | ||
// arrange | ||
using var cts = new CancellationTokenSource(5000); | ||
var services = new ServiceCollection() | ||
.AddDataLoader<TestDataLoader>() | ||
.BuildServiceProvider(); | ||
var dataLoader = services.GetRequiredService<TestDataLoader>(); | ||
|
||
// act | ||
var result = await dataLoader.LoadAsync( | ||
Enumerable.Range(0, 5000).ToArray(), | ||
CancellationToken.None); | ||
|
||
// assert | ||
Assert.Equal(5000, result.Count); | ||
} | ||
|
||
[Fact] | ||
public static async Task Ensure_Multiple_Large_Batches_Can_Be_Enqueued_Concurrently_Async() | ||
{ | ||
// arrange | ||
using var cts = new CancellationTokenSource(5000); | ||
var ct = cts.Token; | ||
var services = new ServiceCollection() | ||
.AddDataLoader<TestDataLoader>() | ||
.BuildServiceProvider(); | ||
var dataLoader = services.GetRequiredService<TestDataLoader>(); | ||
|
||
// act | ||
List<Task> tasks = new(); | ||
foreach (var _ in Enumerable.Range(0, 10)) | ||
{ | ||
tasks.Add( | ||
Task.Run( | ||
async () => | ||
{ | ||
var result = await dataLoader.LoadAsync(Enumerable.Range(0, 5000).ToArray(), ct); | ||
// assert | ||
Assert.Equal(5000, result.Count); | ||
}, | ||
ct)); | ||
} | ||
|
||
await Task.WhenAll(tasks); | ||
} | ||
|
||
|
||
public sealed class TestDataLoader( | ||
IBatchScheduler batchScheduler, | ||
DataLoaderOptions options) | ||
: BatchDataLoader<int, int[]>(batchScheduler, options) | ||
{ | ||
protected override async Task<IReadOnlyDictionary<int, int[]>> LoadBatchAsync( | ||
IReadOnlyList<int> runNumbers, | ||
CancellationToken cancellationToken) | ||
{ | ||
await Task.Delay(300, cancellationToken).ConfigureAwait(false); | ||
|
||
return runNumbers | ||
.Select(t => (t, Enumerable.Range(0, 500))) | ||
.ToDictionary(t => t.Item1, t => t.Item2.ToArray()); | ||
} | ||
} | ||
} |