-
Notifications
You must be signed in to change notification settings - Fork 288
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
f1fc896
ec2e2d4
01e63de
2253eaf
d69a4d8
9432cb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using AwesomeAssertions; | ||
|
Check failure on line 4 in test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/DisposeHelperTests.cs
|
||
|
|
||
| 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 | ||
| extension.CleanupCallCount.Should().Be(1, "CleanupAsync should be called exactly once"); | ||
Evangelink marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| [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 | ||
| extension.CleanupCallCount.Should().Be(1, "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 | ||
| extension.CleanupCallCount.Should().Be(2, "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 | ||
| extension.CleanupCallCount.Should().Be(1, "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; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.