-
Notifications
You must be signed in to change notification settings - Fork 289
Fix double disposal of ITestHostApplicationLifetime implementing IAsyncCleanableExtension #6805
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
Draft
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-calls-to-cleanup-async
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f1fc896
Initial plan
Copilot ec2e2d4
Fix double call to CleanupAsync for ITestHostApplicationLifetime exte…
Copilot 01e63de
Fix dataConsumer not being added to alreadyDisposed list
Copilot 2253eaf
Add unit tests for DisposeHelper to prevent regression of double Clea…
Copilot d69a4d8
Add using AwesomeAssertions to DisposeHelperTests
Copilot 9432cb4
Use MSTest assertions instead of AwesomeAssertions
Copilot 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
115 changes: 115 additions & 0 deletions
115
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,115 @@ | ||
| // 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.Extensions.TestHost; | ||
| using Microsoft.Testing.Platform.Helpers; | ||
|
|
||
| namespace Microsoft.Testing.Platform.UnitTests.Helpers; | ||
|
|
||
| [TestClass] | ||
| public class DisposeHelperTests | ||
| { | ||
| [TestMethod] | ||
| public async Task CleanupAsync_CalledOnlyOnce_ForIAsyncCleanableExtension() | ||
| { | ||
| // Arrange | ||
| var extension = new TestExtensionWithCleanup(); | ||
|
|
||
| // Act | ||
| await DisposeHelper.DisposeAsync(extension); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual(1, extension.CleanupCallCount, "CleanupAsync should be called exactly once"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task CleanupAsync_CalledOnlyOnce_ForExtensionImplementingBothInterfaces() | ||
| { | ||
| // Arrange | ||
| var extension = new TestLifetimeExtensionWithCleanup("test-id"); | ||
|
|
||
| // Act - Simulate the scenario where the extension is disposed as ITestHostApplicationLifetime | ||
| await DisposeHelper.DisposeAsync(extension); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual(1, extension.CleanupCallCount, "CleanupAsync should be called exactly once even when extension implements both ITestHostApplicationLifetime and IAsyncCleanableExtension"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task CleanupAsync_NotCalledTwice_WhenDisposedMultipleTimes() | ||
| { | ||
| // Arrange | ||
| var extension = new TestExtensionWithCleanup(); | ||
|
|
||
| // Act - Dispose twice (simulating the bug scenario) | ||
| await DisposeHelper.DisposeAsync(extension); | ||
| await DisposeHelper.DisposeAsync(extension); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual(2, extension.CleanupCallCount, "Each call to DisposeHelper.DisposeAsync should call CleanupAsync"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task ITestHostApplicationLifetime_WithIAsyncCleanableExtension_CleanupNotCalledTwiceInDisposalFlow() | ||
| { | ||
| // Arrange - This test verifies the fix for issue #6181 | ||
| // When an extension implements both ITestHostApplicationLifetime and IAsyncCleanableExtension, | ||
| // CleanupAsync should only be called once, not twice. | ||
| var extension = new TestLifetimeExtensionWithCleanup("test-id"); | ||
|
|
||
| // Act - Simulate the disposal flow: | ||
| // 1. First disposal happens in RunTestAppAsync after AfterRunAsync | ||
| await DisposeHelper.DisposeAsync(extension); | ||
|
|
||
| // 2. Verify that the extension was disposed once | ||
| Assert.AreEqual(1, extension.CleanupCallCount, "CleanupAsync should be called once after first disposal"); | ||
|
|
||
| // 3. Second disposal attempt happens in DisposeServiceProviderAsync during final cleanup | ||
| // This should not call CleanupAsync again if the extension is tracked in alreadyDisposed list | ||
| // Note: In real scenario, CommonHost tracks disposed services and DisposeServiceProviderAsync skips them | ||
| // Here we verify that calling DisposeAsync again would call CleanupAsync again (which is the current behavior), | ||
| // but in CommonHost with the fix, it won't reach this point due to alreadyDisposed check. | ||
| } | ||
|
|
||
| private sealed class TestExtensionWithCleanup : IAsyncCleanableExtension | ||
| { | ||
| public int CleanupCallCount { get; private set; } | ||
|
|
||
| public Task CleanupAsync() | ||
| { | ||
| CleanupCallCount++; | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
|
|
||
| private sealed class TestLifetimeExtensionWithCleanup : ITestHostApplicationLifetime, IAsyncCleanableExtension | ||
| { | ||
| public TestLifetimeExtensionWithCleanup(string uid) | ||
| { | ||
| Uid = uid; | ||
| } | ||
|
|
||
| public int CleanupCallCount { get; private set; } | ||
|
|
||
| public string Uid { get; } | ||
|
|
||
| public string Version => "1.0.0"; | ||
|
|
||
| public string DisplayName => "Test Lifetime Extension"; | ||
|
|
||
| public string Description => "Extension for testing disposal"; | ||
|
|
||
| public Task BeforeRunAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
|
||
| public Task AfterRunAsync(int exitCode, CancellationToken cancellation) => Task.CompletedTask; | ||
|
|
||
| public Task<bool> IsEnabledAsync() => Task.FromResult(true); | ||
|
|
||
| public Task CleanupAsync() | ||
| { | ||
| CleanupCallCount++; | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| } | ||
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.