Skip to content

Commit

Permalink
Added one more test for DisposeAsync method and made it awaitable.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSquidCombatant committed Mar 3, 2024
1 parent c1a0001 commit ca46ce8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
14 changes: 14 additions & 0 deletions CodeJam.Main.Tests/DisposableExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ public static void DisposeAllMustCollectAllExceptions()
}

#if NETSTANDARD21_OR_GREATER || NETCOREAPP30_OR_GREATER
[Test]
public static async Task DisposeAsyncMustCallDiposeOnce()
{
const int expectedDisposeCount = 1;

int actualDisposeCount = 0;

var objectForDispose = Disposable.Create(() => ++actualDisposeCount);

await objectForDispose.DisposeAsync();

Assert.AreEqual(expectedDisposeCount, actualDisposeCount);
}

[Test]
public static void DisposeAsyncMustNotBlockThread()
{
Expand Down
9 changes: 4 additions & 5 deletions CodeJam.Main/DisposableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Collections.Generic;
#if NETSTANDARD21_OR_GREATER || NETCOREAPP30_OR_GREATER
using System.Threading.Tasks;
#endif

using CodeJam.Internal;

using JetBrains.Annotations;
Expand Down Expand Up @@ -67,12 +66,12 @@ public static void DisposeAll(
/// Calls DisposeAsync if <paramref name="disposable"/> implements <see cref="IAsyncDisposable"/>, otherwise
/// calls <see cref="IDisposable.Dispose"/>
/// </summary>
public static ValueTask DisposeAsync(this IDisposable disposable)
public static async ValueTask DisposeAsync(this IDisposable disposable)
{
Code.NotNull(disposable, nameof(disposable));
if (disposable is IAsyncDisposable asyncDisposable)
return asyncDisposable.DisposeAsync();
return new ValueTask(Task.Run(() => disposable.Dispose()));
await asyncDisposable.DisposeAsync();
await new ValueTask(Task.Run(() => disposable.Dispose()));
}
#endif
}
Expand Down

0 comments on commit ca46ce8

Please sign in to comment.