-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from mycroes/hotfix/dispose
Dispose requests on JobPool Dispose
- Loading branch information
Showing
8 changed files
with
144 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using FakeItEasy; | ||
using Sally7.RequestExecutor; | ||
|
||
namespace Sally7.Tests.RequestExecutor; | ||
|
||
public class RequestTests | ||
{ | ||
[Fact] | ||
public void Completes_On_Dispose() | ||
{ | ||
// Arrange | ||
var sut = new Request(); | ||
var callback = A.Fake<Action>(); | ||
sut.OnCompleted(callback); | ||
|
||
// Act | ||
sut.Dispose(); | ||
|
||
// Assert | ||
A.CallTo(() => callback.Invoke()).MustHaveHappenedOnceExactly(); | ||
} | ||
|
||
[Fact] | ||
public async Task Throws_When_Awaited_After_Dispose() | ||
{ | ||
// Arrange | ||
var sut = new Request(); | ||
|
||
// Act | ||
sut.Dispose(); | ||
|
||
// Assert | ||
await Should.ThrowAsync<ObjectDisposedException>(async () => await sut); | ||
} | ||
} |
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,19 @@ | ||
using System.Threading.Channels; | ||
using Sally7.RequestExecutor; | ||
|
||
namespace Sally7.Tests.RequestExecutor; | ||
|
||
public class SignalTests | ||
{ | ||
[Fact] | ||
public async Task WaitAsync_Throws_If_Disposed() | ||
{ | ||
// Arrange | ||
var sut = new Signal(); | ||
sut.Dispose(); | ||
|
||
// Act | ||
// Assert | ||
await Should.ThrowAsync<ChannelClosedException>(() => sut.WaitAsync(CancellationToken.None).AsTask()); | ||
} | ||
} |
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,17 @@ | ||
using System; | ||
|
||
namespace Sally7.Internal; | ||
|
||
internal static class DisposableHelper | ||
{ | ||
public static void ThrowIf(bool condition, object instance) | ||
{ | ||
#if NET7_OR_GREATER | ||
ObjectDisposedException.ThrowIf(condition, instance); | ||
#else | ||
void Throw() => throw new ObjectDisposedException(instance.GetType().FullName); | ||
|
||
if (condition) Throw(); | ||
#endif | ||
} | ||
} |
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,26 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Channels; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sally7.RequestExecutor | ||
{ | ||
[DebuggerNonUserCode] | ||
[DebuggerDisplay(nameof(NeedToWait) + ": {" + nameof(NeedToWait) + ",nq}")] | ||
internal class Signal : IDisposable | ||
{ | ||
private readonly Channel<int> channel = Channel.CreateBounded<int>(1); | ||
|
||
public void Dispose() => channel.Writer.Complete(); | ||
|
||
public bool TryInit() => channel.Writer.TryWrite(0); | ||
|
||
public ValueTask<int> WaitAsync(CancellationToken cancellationToken) => | ||
channel.Reader.ReadAsync(cancellationToken); | ||
|
||
public bool TryRelease() => channel.Writer.TryWrite(0); | ||
|
||
private bool NeedToWait => channel.Reader.Count == 0; | ||
} | ||
} |