-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tomasz Maruszak <[email protected]>
- Loading branch information
Showing
8 changed files
with
115 additions
and
54 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
namespace SlimMessageBus.Host.Collections; | ||
|
||
public interface IAsyncTaskList | ||
{ | ||
void Add(Task task, CancellationToken cancellationToken); | ||
Task EnsureAllFinished(); | ||
} | ||
|
||
/// <summary> | ||
/// Tracks a list of Tasks that have to be awaited before we can proceed. | ||
/// </summary> | ||
public class AsyncTaskList : IAsyncTaskList | ||
{ | ||
private readonly object _initTaskLock = new(); | ||
private Task _initTask = null; | ||
|
||
public void Add(Task task, CancellationToken cancellationToken) | ||
{ | ||
lock (_initTaskLock) | ||
{ | ||
var prevInitTask = _initTask; | ||
_initTask = prevInitTask?.ContinueWith(_ => task, cancellationToken) ?? task; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Awaits (if any) bus intialization (e.g. topology provisining) before we can produce message into the bus. | ||
/// </summary> | ||
/// <returns></returns> | ||
public async Task EnsureAllFinished() | ||
{ | ||
var initTask = _initTask; | ||
if (initTask != null) | ||
{ | ||
await initTask.ConfigureAwait(false); | ||
|
||
lock (_initTaskLock) | ||
{ | ||
if (ReferenceEquals(_initTask, initTask)) | ||
{ | ||
_initTask = null; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
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
37 changes: 37 additions & 0 deletions
37
src/Tests/SlimMessageBus.Host.Test/Collections/AsyncTaskListTests.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,37 @@ | ||
namespace SlimMessageBus.Host.Test.Collections; | ||
|
||
using System.Collections.Concurrent; | ||
|
||
using SlimMessageBus.Host.Collections; | ||
|
||
public class AsyncTaskListTests | ||
{ | ||
private readonly AsyncTaskList _sut = new(); | ||
|
||
[Fact] | ||
public async Task Given_TaskAdded_When_EnsureAllFinished_Then_TaskIsCompleted() | ||
{ | ||
// arrange | ||
var numberList = new ConcurrentQueue<int>(); | ||
|
||
async Task RunTask(int n) | ||
{ | ||
numberList.Enqueue(n); | ||
await Task.Delay(100); | ||
} | ||
|
||
_sut.Add(RunTask(1), CancellationToken.None); | ||
_sut.Add(RunTask(2), CancellationToken.None); | ||
|
||
// act | ||
await _sut.EnsureAllFinished(); | ||
|
||
// assert | ||
numberList.Should().HaveCount(2); | ||
numberList.TryDequeue(out var n1).Should().BeTrue(); | ||
n1.Should().Be(1); | ||
numberList.TryDequeue(out var n2).Should().BeTrue(); | ||
n2.Should().Be(2); | ||
} | ||
|
||
} |