-
Notifications
You must be signed in to change notification settings - Fork 5
V9.0.0/support for async disposable #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd4d321
:art: cleaning
gimlichael b1adef8
:sparkles: support for AsyncDisposable
gimlichael a63ff92
:white_check_mark: additional unit test
gimlichael b3736bc
:speech_balloon: updated community health pages
gimlichael c809efb
:package: updated NuGet package definition
gimlichael fbaaf75
:arrow_up: bump dependencies
gimlichael 36ee471
:rotating_light: applied suggestions from coderabbit
gimlichael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,38 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Cuemon.Extensions | ||
| { | ||
| /// <summary> | ||
| /// Provides a mechanism for asynchronously releasing both managed and unmanaged resources with focus on the former. | ||
| /// </summary> | ||
| /// <seealso cref="Disposable" /> | ||
| /// <seealso cref="IAsyncDisposable" /> | ||
| public abstract class AsyncDisposable : Disposable, IAsyncDisposable | ||
| { | ||
| /// <summary> | ||
| /// Called when this object is being disposed by either <see cref="Disposable.Dispose()"/> or <see cref="Disposable.Dispose(bool)"/> having <c>disposing</c> set to <c>true</c> and <see cref="Disposable.Disposed"/> is <c>false</c>. | ||
| /// </summary> | ||
| /// <remarks>You should almost never override this - unless you want to call it from <see cref="OnDisposeManagedResourcesAsync"/>.</remarks> | ||
| protected override void OnDisposeManagedResources() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Called when this object is being disposed by <see cref="DisposeAsync()"/>. | ||
| /// </summary> | ||
| protected abstract ValueTask OnDisposeManagedResourcesAsync(); | ||
|
|
||
| /// <summary> | ||
| /// Asynchronously releases the resources used by the <see cref="AsyncDisposable"/>. | ||
| /// </summary> | ||
| /// <returns>A <see cref="ValueTask"/> that represents the asynchronous dispose operation.</returns> | ||
| /// <remarks>https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync#the-disposeasync-method</remarks> | ||
| public async ValueTask DisposeAsync() | ||
| { | ||
| await OnDisposeManagedResourcesAsync().ConfigureAwait(false); | ||
| Dispose(false); | ||
| GC.SuppressFinalize(this); | ||
| } | ||
gimlichael marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,88 @@ | ||
| using System.Diagnostics; | ||
| using System.Threading.Tasks; | ||
| using Codebelt.Extensions.Xunit; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Cuemon.Extensions | ||
| { | ||
| public class AsyncDisposableTest : Test | ||
| { | ||
| public AsyncDisposableTest(ITestOutputHelper output) : base(output) | ||
| { | ||
| } | ||
|
|
||
| private class TestAsyncDisposable : AsyncDisposable | ||
| { | ||
| public bool ManagedResourcesDisposed { get; private set; } | ||
|
|
||
| public bool IsAsyncOperationCompleted { get; private set; } | ||
|
|
||
| public bool UnmanagedResourcesDisposed { get; private set; } | ||
|
|
||
| protected override ValueTask OnDisposeManagedResourcesAsync() | ||
| { | ||
| ManagedResourcesDisposed = true; | ||
| return new ValueTask(Task.Run(async () => | ||
| { | ||
| await Task.Delay(100); // Simulate async work | ||
| IsAsyncOperationCompleted = true; | ||
| })); | ||
| } | ||
|
|
||
| protected override void OnDisposeUnmanagedResources() | ||
| { | ||
| UnmanagedResourcesDisposed = true; | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DisposeAsync_ShouldCallOnDisposeManagedResourcesAsync() | ||
| { | ||
| // Arrange | ||
| var disposable = new TestAsyncDisposable(); | ||
|
|
||
| // Act | ||
| await disposable.DisposeAsync(); | ||
|
|
||
| // Assert | ||
| Assert.True(disposable.ManagedResourcesDisposed); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DisposeAsync_ShouldSuppressFinalize() | ||
| { | ||
| // Arrange | ||
| var disposable = new TestAsyncDisposable(); | ||
|
|
||
| // Act | ||
| await disposable.DisposeAsync(); | ||
|
|
||
| // Assert | ||
| Assert.True(disposable.Disposed); | ||
| } | ||
|
Comment on lines
+39
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add more comprehensive async disposal tests. While the current tests cover basic scenarios, consider adding:
Here's a suggested additional test: [Theory]
[InlineData(100)] // Fast disposal
[InlineData(1000)] // Slower disposal
public async Task DisposeAsync_ShouldHandleVariousAsyncScenarios(int delayMs)
{
// Arrange
var disposable = new TestAsyncDisposable();
var sw = Stopwatch.StartNew();
// Act
await using (disposable)
{
await Task.Delay(delayMs);
}
// Assert
Assert.True(disposable.ManagedResourcesDisposed);
Assert.True(disposable.IsAsyncOperationCompleted);
Assert.True(sw.ElapsedMilliseconds >= delayMs);
} |
||
|
|
||
| [Theory] | ||
| [InlineData(100)] // Fast disposal | ||
| [InlineData(1000)] // Slower disposal | ||
| public async Task DisposeAsync_ShouldHandleVariousAsyncScenarios(int delayMs) | ||
| { | ||
| // Arrange | ||
| var disposable = new TestAsyncDisposable(); | ||
| var sw = Stopwatch.StartNew(); | ||
|
|
||
| // Act | ||
| await using (disposable) | ||
| { | ||
| await Task.Delay(delayMs); | ||
| } | ||
|
|
||
| sw.Stop(); | ||
|
|
||
| // Assert | ||
| Assert.True(disposable.ManagedResourcesDisposed); | ||
| Assert.True(disposable.IsAsyncOperationCompleted); | ||
| Assert.True(sw.ElapsedMilliseconds >= delayMs); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.