-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix hangdump not showing tests in progress (#3992)
- Loading branch information
Showing
2 changed files
with
162 additions
and
5 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
157 changes: 157 additions & 0 deletions
157
...rationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/HangDumpOutputTests.cs
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,157 @@ | ||
// 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.Acceptance.IntegrationTests.Helpers; | ||
using Microsoft.Testing.Platform.Helpers; | ||
|
||
namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests; | ||
|
||
[TestGroup] | ||
public sealed class HangDumpOutputTests : AcceptanceTestBase | ||
{ | ||
private readonly TestAssetFixture _testAssetFixture; | ||
|
||
public HangDumpOutputTests(ITestExecutionContext testExecutionContext, TestAssetFixture testAssetFixture) | ||
: base(testExecutionContext) => _testAssetFixture = testAssetFixture; | ||
|
||
[Arguments("Mini")] | ||
public async Task HangDump_Outputs_HangingTests_EvenWhenHangingTestsHaveTheSameDisplayName(string format) | ||
{ | ||
// This test makes sure that when tests have the same display name (e.g. like Test1 from both Class1 and Class2) | ||
// they will still show up in the hanging tests. This was not the case before when we were just putting them into | ||
// a dictionary based on DisplayName. In that case both tests were started at the same time, and only 1 entry was added | ||
// to currently executing tests. When first test with name Test1 completed we removed that entry, but Class2.Test1 was still | ||
// running. Solution is to use a more unique identifier. | ||
string resultDirectory = Path.Combine(_testAssetFixture.TargetAssetPath, Guid.NewGuid().ToString("N"), format); | ||
var testHost = TestInfrastructure.TestHost.LocateFrom(_testAssetFixture.TargetAssetPath, "HangDump", TargetFrameworks.NetCurrent.Arguments); | ||
TestHostResult testHostResult = await testHost.ExecuteAsync( | ||
$"--hangdump --hangdump-timeout 8s --hangdump-type {format} --results-directory {resultDirectory} --no-progress", | ||
new Dictionary<string, string> | ||
{ | ||
{ "SLEEPTIMEMS1", "100" }, | ||
{ "SLEEPTIMEMS2", "600000" }, | ||
}); | ||
testHostResult.AssertExitCodeIs(ExitCodes.TestHostProcessExitedNonGracefully); | ||
testHostResult.AssertOutputContains("Test1"); | ||
} | ||
|
||
[TestFixture(TestFixtureSharingStrategy.PerTestGroup)] | ||
public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture) : TestAssetFixtureBase(acceptanceFixture.NuGetGlobalPackagesFolder) | ||
{ | ||
private const string AssetName = "TestAssetFixture"; | ||
|
||
public string TargetAssetPath => GetAssetPath(AssetName); | ||
|
||
public override IEnumerable<(string ID, string Name, string Code)> GetAssetsToGenerate() | ||
{ | ||
yield return (AssetName, AssetName, | ||
Sources | ||
.PatchTargetFrameworks(TargetFrameworks.All) | ||
.PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion)); | ||
} | ||
|
||
private const string Sources = """ | ||
#file HangDump.csproj | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks> | ||
<OutputType>Exe</OutputType> | ||
<UseAppHost>true</UseAppHost> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>preview</LangVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Testing.Extensions.HangDump" Version="$MicrosoftTestingPlatformVersion$" /> | ||
</ItemGroup> | ||
</Project> | ||
#file Program.cs | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Globalization; | ||
using Microsoft.Testing.Platform; | ||
using Microsoft.Testing.Platform.Extensions.TestFramework; | ||
using Microsoft.Testing.Platform.Builder; | ||
using Microsoft.Testing.Platform.Capabilities.TestFramework; | ||
using Microsoft.Testing.Extensions; | ||
using Microsoft.Testing.Platform.Extensions.Messages; | ||
using Microsoft.Testing.Platform.Requests; | ||
using Microsoft.Testing.Platform.Services; | ||
public class Startup | ||
{ | ||
public static async Task<int> Main(string[] args) | ||
{ | ||
ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args); | ||
builder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_,__) => new DummyTestAdapter()); | ||
builder.AddHangDumpProvider(); | ||
using ITestApplication app = await builder.BuildAsync(); | ||
return await app.RunAsync(); | ||
} | ||
} | ||
public class DummyTestAdapter : ITestFramework, IDataProducer | ||
{ | ||
public string Uid => nameof(DummyTestAdapter); | ||
public string Version => "2.0.0"; | ||
public string DisplayName => nameof(DummyTestAdapter); | ||
public string Description => nameof(DummyTestAdapter); | ||
public Task<bool> IsEnabledAsync() => Task.FromResult(true); | ||
public Type[] DataTypesProduced => new[] { typeof(TestNodeUpdateMessage) }; | ||
public Task<CreateTestSessionResult> CreateTestSessionAsync(CreateTestSessionContext context) | ||
=> Task.FromResult(new CreateTestSessionResult() { IsSuccess = true }); | ||
public Task<CloseTestSessionResult> CloseTestSessionAsync(CloseTestSessionContext context) | ||
=> Task.FromResult(new CloseTestSessionResult() { IsSuccess = true }); | ||
public async Task ExecuteRequestAsync(ExecuteRequestContext context) | ||
{ | ||
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid, new TestNode() | ||
{ | ||
Uid = "Class1.Test1", | ||
DisplayName = "Test1", | ||
Properties = new PropertyBag(new InProgressTestNodeStateProperty()), | ||
})); | ||
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid, new TestNode() | ||
{ | ||
Uid = "Class2.Test1", | ||
DisplayName = "Test1", | ||
Properties = new PropertyBag(new InProgressTestNodeStateProperty()), | ||
})); | ||
Thread.Sleep(int.Parse(Environment.GetEnvironmentVariable("SLEEPTIMEMS1")!, CultureInfo.InvariantCulture)); | ||
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid, new TestNode() | ||
{ | ||
Uid = "Class1.Test1", | ||
DisplayName = "Test1", | ||
Properties = new PropertyBag(new PassedTestNodeStateProperty()), | ||
})); | ||
Thread.Sleep(int.Parse(Environment.GetEnvironmentVariable("SLEEPTIMEMS2")!, CultureInfo.InvariantCulture)); | ||
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid, new TestNode() | ||
{ | ||
Uid = "Class2.Test1", | ||
DisplayName = "Test1", | ||
Properties = new PropertyBag(new PassedTestNodeStateProperty()), | ||
})); | ||
context.Complete(); | ||
} | ||
} | ||
"""; | ||
} | ||
} |