-
Notifications
You must be signed in to change notification settings - Fork 311
Add DisposeHelper unit tests #10649
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
Amaury Levé (Evangelink)
merged 2 commits into
main
from
dev/amauryleve/test-dispose-helper
Aug 24, 2026
Merged
Add DisposeHelper unit tests #10649
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
162 changes: 162 additions & 0 deletions
162
test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/DisposeHelperTests.cs
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,162 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Microsoft.Testing.Platform.Extensions; | ||
| using Microsoft.Testing.Platform.Helpers; | ||
|
|
||
| namespace Microsoft.Testing.Platform.UnitTests; | ||
|
|
||
| [TestClass] | ||
| public sealed class DisposeHelperTests | ||
| { | ||
| [TestMethod] | ||
| public async Task DisposeAsync_Null_DoesNothing() | ||
| => await DisposeHelper.DisposeAsync(null); | ||
|
|
||
| [TestMethod] | ||
| public async Task DisposeAsync_UnsupportedObject_DoesNothing() | ||
| => await DisposeHelper.DisposeAsync(new object()); | ||
|
|
||
| [TestMethod] | ||
| public async Task DisposeAsync_CleanableDisposable_CleansUpBeforeDisposing() | ||
| { | ||
| RecordingSyncResource resource = new(); | ||
|
|
||
| await DisposeHelper.DisposeAsync(resource); | ||
|
|
||
| Assert.AreSequenceEqual( | ||
| new[] { nameof(RecordingSyncResource.CleanupAsync), nameof(RecordingSyncResource.Dispose) }, | ||
| resource.Invocations); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task DisposeAsync_CleanupThrows_PropagatesExceptionWithoutDisposing() | ||
| { | ||
| InvalidOperationException expectedException = new("Cleanup failed."); | ||
| RecordingSyncResource resource = new(() => Task.FromException(expectedException)); | ||
|
|
||
| InvalidOperationException actualException = await Assert.ThrowsExactlyAsync<InvalidOperationException>( | ||
| () => DisposeHelper.DisposeAsync(resource)); | ||
|
|
||
| Assert.AreSame(expectedException, actualException); | ||
| Assert.AreSequenceEqual(new[] { nameof(RecordingSyncResource.CleanupAsync) }, resource.Invocations); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task DisposeAsync_SynchronousDisposeThrows_PropagatesException() | ||
| { | ||
| InvalidOperationException expectedException = new("Dispose failed."); | ||
| RecordingSyncResource resource = new(dispose: () => throw expectedException); | ||
|
|
||
| InvalidOperationException actualException = await Assert.ThrowsExactlyAsync<InvalidOperationException>( | ||
| () => DisposeHelper.DisposeAsync(resource)); | ||
|
|
||
| Assert.AreSame(expectedException, actualException); | ||
| Assert.AreSequenceEqual( | ||
| new[] { nameof(RecordingSyncResource.CleanupAsync), nameof(RecordingSyncResource.Dispose) }, | ||
| resource.Invocations); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task DisposeAsync_CalledTwice_InvokesCleanupAndDisposeTwice() | ||
| { | ||
| RecordingSyncResource resource = new(); | ||
|
|
||
| await DisposeHelper.DisposeAsync(resource); | ||
| await DisposeHelper.DisposeAsync(resource); | ||
|
|
||
| Assert.AreSequenceEqual( | ||
| new[] | ||
| { | ||
| nameof(RecordingSyncResource.CleanupAsync), | ||
| nameof(RecordingSyncResource.Dispose), | ||
| nameof(RecordingSyncResource.CleanupAsync), | ||
| nameof(RecordingSyncResource.Dispose), | ||
| }, | ||
| resource.Invocations); | ||
| } | ||
|
|
||
| #if NETCOREAPP | ||
| [TestMethod] | ||
| public async Task DisposeAsync_AsyncDisposable_CleansUpThenPrefersAsyncDispose() | ||
| { | ||
| RecordingAsyncResource resource = new(); | ||
|
|
||
| await DisposeHelper.DisposeAsync(resource); | ||
|
|
||
| Assert.AreSequenceEqual( | ||
| new[] { nameof(RecordingAsyncResource.CleanupAsync), nameof(RecordingAsyncResource.DisposeAsync) }, | ||
| resource.Invocations); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task DisposeAsync_AsyncDisposeIsCanceled_PropagatesCancellationWithoutSynchronousDispose() | ||
| { | ||
| CancellationToken cancellationToken = new(canceled: true); | ||
| OperationCanceledException expectedException = new(cancellationToken); | ||
| RecordingAsyncResource resource = new(() => ValueTask.FromException(expectedException)); | ||
|
|
||
| OperationCanceledException actualException = await Assert.ThrowsExactlyAsync<OperationCanceledException>( | ||
| () => DisposeHelper.DisposeAsync(resource)); | ||
|
|
||
| Assert.AreSame(expectedException, actualException); | ||
| Assert.AreEqual(cancellationToken, actualException.CancellationToken); | ||
| Assert.AreSequenceEqual( | ||
| new[] { nameof(RecordingAsyncResource.CleanupAsync), nameof(RecordingAsyncResource.DisposeAsync) }, | ||
| resource.Invocations); | ||
| } | ||
| #endif | ||
|
|
||
| private sealed class RecordingSyncResource : IAsyncCleanableExtension, IDisposable | ||
| { | ||
| private readonly Func<Task> _cleanupAsync; | ||
| private readonly Action _dispose; | ||
|
|
||
| public RecordingSyncResource(Func<Task>? cleanupAsync = null, Action? dispose = null) | ||
| { | ||
| _cleanupAsync = cleanupAsync ?? (() => Task.CompletedTask); | ||
| _dispose = dispose ?? (() => { }); | ||
| } | ||
|
|
||
| public List<string> Invocations { get; } = []; | ||
|
|
||
| public Task CleanupAsync() | ||
| { | ||
| Invocations.Add(nameof(CleanupAsync)); | ||
| return _cleanupAsync(); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| Invocations.Add(nameof(Dispose)); | ||
| _dispose(); | ||
| } | ||
| } | ||
|
|
||
| #if NETCOREAPP | ||
| private sealed class RecordingAsyncResource : IAsyncCleanableExtension, IAsyncDisposable, IDisposable | ||
| { | ||
| private readonly Func<ValueTask> _disposeAsync; | ||
|
|
||
| public RecordingAsyncResource(Func<ValueTask>? disposeAsync = null) | ||
| => _disposeAsync = disposeAsync ?? (() => ValueTask.CompletedTask); | ||
|
|
||
| public List<string> Invocations { get; } = []; | ||
|
|
||
| public Task CleanupAsync() | ||
| { | ||
| Invocations.Add(nameof(CleanupAsync)); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| public ValueTask DisposeAsync() | ||
| { | ||
| Invocations.Add(nameof(DisposeAsync)); | ||
| return _disposeAsync(); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| => Invocations.Add(nameof(Dispose)); | ||
| } | ||
| #endif | ||
| } | ||
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.