-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix disposable extensions bugs and add relevant tests. (#157)
* Fixed disposable extensions bugs and added relevant tests. * Added one more test for DisposeAsync method and made it awaitable.
- Loading branch information
1 parent
a349a54
commit f876cd5
Showing
3 changed files
with
100 additions
and
8 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,93 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using NUnit.Framework; | ||
|
||
namespace CodeJam; | ||
|
||
[TestFixture(Category = "Disposable")] | ||
[SuppressMessage("ReSharper", "HeapView.CanAvoidClosure")] | ||
public static class DisposableExtensionsTests | ||
{ | ||
[Test] | ||
public static void DisposeAllMustReleaseAllObjects() | ||
{ | ||
const int expectedDisposeCount = 10; | ||
|
||
int actualDisposeCount = 0; | ||
|
||
var objectsForDispose = Enumerable | ||
.Range(0, expectedDisposeCount) | ||
.Select(x => Disposable.Create(() => ++actualDisposeCount)); | ||
|
||
objectsForDispose.DisposeAll(); | ||
|
||
Assert.AreEqual(expectedDisposeCount, actualDisposeCount); | ||
} | ||
|
||
[Test] | ||
public static void DisposeAllMustCollectAllExceptions() | ||
{ | ||
const int expectedExceptionCount = 7; | ||
|
||
var objectsWithException = Enumerable | ||
.Range(0, expectedExceptionCount) | ||
.Select(x => Disposable.Create(() => throw new Exception())); | ||
|
||
const int expectedSuccessCount = 3; | ||
|
||
var objectsWithoutException = Enumerable | ||
.Range(0, expectedSuccessCount) | ||
.Select(x => Disposable.Create(() => { })); | ||
|
||
var objectsForDispose = objectsWithException.Concat(objectsWithoutException).ToArray(); | ||
|
||
int actualExceptionCount = -1; | ||
|
||
try | ||
{ | ||
objectsForDispose.DisposeAll(); | ||
} | ||
catch (AggregateException ex) | ||
{ | ||
actualExceptionCount = ex.InnerExceptions.Count; | ||
} | ||
|
||
Assert.AreEqual(expectedExceptionCount, actualExceptionCount); | ||
} | ||
|
||
#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() | ||
{ | ||
var disposeDuration = new TimeSpan(0, 0, 1); | ||
|
||
var longDisposableObject = Disposable.Create(() => Thread.Sleep(disposeDuration)); | ||
|
||
var startTime = DateTime.Now; | ||
|
||
var task = longDisposableObject.DisposeAsync(); | ||
|
||
var callDuration = DateTime.Now - startTime; | ||
|
||
Assert.Less(callDuration, disposeDuration); | ||
} | ||
#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