-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tests for out of order dispose scenario
- Loading branch information
1 parent
b3161c6
commit 18510df
Showing
7 changed files
with
73 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,47 @@ | ||
namespace Pool.Tests.Fakes; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Pool.Tests.Fakes; | ||
|
||
internal sealed class Echo | ||
: IEcho | ||
, IDisposable | ||
{ | ||
private bool disposed; | ||
|
||
public bool IsReady { get; private set; } | ||
|
||
public Task MakeReadyAsync(CancellationToken cancellationToken) | ||
{ | ||
ThrowIfDisposed(); | ||
|
||
IsReady = true; | ||
return Task.CompletedTask; | ||
} | ||
|
||
public string Shout(string message) | ||
public string Shout(string message) => message; | ||
|
||
public void Dispose() | ||
{ | ||
if (disposed) | ||
{ | ||
return; | ||
} | ||
|
||
disposed = true; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private void ThrowIfDisposed() | ||
{ | ||
return message; | ||
#if NET7_0_OR_GREATER | ||
#pragma warning disable IDE0022 // Use expression body for method | ||
ObjectDisposedException.ThrowIf(disposed, nameof(Echo)); | ||
#pragma warning restore IDE0022 // Use expression body for method | ||
#elif NET6_0_OR_GREATER | ||
if (disposed) | ||
{ | ||
throw new ObjectDisposedException(nameof(Echo)); | ||
} | ||
#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,29 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Pool.Tests.Fakes; | ||
|
||
namespace Pool.Tests; | ||
|
||
public sealed class PoolItemFactoryTests | ||
{ | ||
[Fact] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("IDisposableAnalyzers.Correctness", "IDISP017:Prefer using", Justification = "required for test")] | ||
public async Task PoolItemFactory_Doesnt_Crash_On_Dispose() | ||
{ | ||
using var services = new ServiceCollection() | ||
.AddScoped<IEcho, Echo>() | ||
.BuildServiceProvider(); | ||
|
||
var factory = new DefaultPoolItemFactory<IEcho>(services); | ||
var pool = new Pool<IEcho>(factory, new DefaultReadyCheck<IEcho>(), new PoolOptions { MinSize = 5 }); | ||
var item = await pool.LeaseAsync(CancellationToken.None); | ||
Assert.NotNull(item); | ||
|
||
await pool.ReleaseAsync(item); | ||
|
||
// pool disposes all the items on the queue | ||
pool.Dispose(); | ||
|
||
// factory disposes the scope, which also disposes the items, so the items need to protect themselves with dispose pattern | ||
factory.Dispose(); | ||
} | ||
} |
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