Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions PowerKit.Tests/AsyncDisposableExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Threading.Tasks;
using FluentAssertions;
using PowerKit.Extensions;
using Xunit;

namespace PowerKit.Tests;

file class AsyncDisposableStub(Action onDispose) : IDisposable, IAsyncDisposable
{
public void Dispose() => throw new Exception("DisposeAsync() should've been called instead");

public ValueTask DisposeAsync()
{
onDispose();
return default;
}
}

file class DisposableStub(Action onDispose) : IDisposable
{
public void Dispose() => onDispose();
}

Comment thread
Tyrrrz marked this conversation as resolved.
public class AsyncDisposableExtensionsTests
{
[Fact]
public async Task ToAsyncDisposable_IAsyncDisposable_Test()
{
// Arrange
var asyncDisposeCalled = false;

var disposable = new AsyncDisposableStub(() => asyncDisposeCalled = true);

// Act
await disposable.ToAsyncDisposable().DisposeAsync();

// Assert
asyncDisposeCalled.Should().BeTrue();
}

[Fact]
public async Task ToAsyncDisposable_IDisposable_Test()
{
// Arrange
var disposeCalled = false;

var disposable = new DisposableStub(() => disposeCalled = true);

// Act
await disposable.ToAsyncDisposable().DisposeAsync();

// Assert
disposeCalled.Should().BeTrue();
}
}
47 changes: 47 additions & 0 deletions PowerKit/Extensions/AsyncDisposableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#if NET40_OR_GREATER || NETSTANDARD || NET
#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;

namespace PowerKit.Extensions;

// Provides a dynamic and uniform way to deal with async disposable.
// Used as an abstraction to dynamically polyfill IAsyncDisposable implementations in BCL types. For example:
// - Stream class on .NET Framework 4.6.1 -> calls Dispose()
// - Stream class on .NET Core 3.0 -> calls DisposeAsync()
// - Stream class on .NET Standard 2.0 -> calls DisposeAsync() or Dispose(), depending on the runtime
file class AsyncDisposableAdapter(IDisposable target) : IAsyncDisposable
{
public async ValueTask DisposeAsync()
{
if (target is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync().ConfigureAwait(false);
}
else
{
target.Dispose();
}
}
}

#if !POWERKIT_INCLUDE_COVERAGE
[ExcludeFromCodeCoverage]
#endif
internal static class AsyncDisposableExtensions
{
extension(IDisposable disposable)
{
/// <summary>
/// Wraps the disposable in an <see cref="IAsyncDisposable" /> adapter that calls
/// <see cref="IAsyncDisposable.DisposeAsync" /> if supported, or falls back to
/// <see cref="IDisposable.Dispose" />.
/// </summary>
public IAsyncDisposable ToAsyncDisposable() =>
disposable is IAsyncDisposable asyncDisposable
? asyncDisposable
: new AsyncDisposableAdapter(disposable);
}
}
#endif