Skip to content

Commit

Permalink
Improve some flaky tests (#4381)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink authored Dec 18, 2024
1 parent 68fd5bd commit 2dd2f37
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Globalization;
using System.Runtime.InteropServices;

using Microsoft.Testing.Platform.Acceptance.IntegrationTests;
using Microsoft.Testing.Platform.Acceptance.IntegrationTests.Helpers;
Expand Down Expand Up @@ -38,11 +37,6 @@ public sealed class RunSettingsTests : AcceptanceTestBase<RunSettingsTests.TestA
[DynamicData(nameof(TargetFrameworks.AllForDynamicData), typeof(TargetFrameworks))]
public async Task UnsupportedRunSettingsEntriesAreFlagged(string tfm)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && tfm == TargetFrameworks.NetFramework.First())
{
return;
}

var testHost = TestHost.LocateFrom(AssetFixture.ProjectPath, TestAssetFixture.ProjectName, tfm);
TestHostResult testHostResult = await testHost.ExecuteAsync("--settings my.runsettings");

Expand Down Expand Up @@ -101,7 +95,7 @@ public sealed class TestAssetFixture() : TestAssetFixtureBase(AcceptanceFixture.
{
yield return (ProjectName, ProjectName,
SourceCode
.PatchTargetFrameworks(TargetFrameworks.NetCurrent, TargetFrameworks.NetFramework.First())
.PatchTargetFrameworks(TargetFrameworks.All)
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Licensed under dual-license. See LICENSE.PLATFORMTOOLS.txt file in the project root for full license information.
#pragma warning restore IDE0073 // The file header does not match the required text

using System.Runtime.InteropServices;

namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests;

[TestClass]
Expand Down Expand Up @@ -131,11 +133,18 @@ public async Task RetryFailedTests_MaxTestsCount_Succeeds(string tfm, bool fail)
}
}

// We use crash dump, not supported in NetFramework at the moment
[TestMethod]
// We use crash dump, not supported in NetFramework at the moment
[DynamicData(nameof(TargetFrameworks.NetForDynamicData), typeof(TargetFrameworks))]
public async Task RetryFailedTests_MoveFiles_Succeeds(string tfm)
=> await RetryHelper.RetryAsync(
{
// TODO: Crash dump is not working properly on macos, so we skip the test for now
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return;
}

await RetryHelper.RetryAsync(
async () =>
{
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, tfm);
Expand Down Expand Up @@ -171,6 +180,7 @@ public async Task RetryFailedTests_MoveFiles_Succeeds(string tfm)
Assert.Fail($"Expected 1 or 2 dump files, but found {dumpFilesCount}");
}
}, 3, TimeSpan.FromSeconds(5));
}

public sealed class TestAssetFixture() : TestAssetFixtureBase(AcceptanceFixture.NuGetGlobalPackagesFolder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,17 @@ public async Task Log_WhenSyncFlush_StreamWriterIsCalledOnlyWhenLogLevelAllowsIt

if (LogTestHelpers.IsLogEnabled(defaultLogLevel, currentLogLevel))
{
if (_memoryStream.Length == 0)
await _memoryStream.FlushAsync();
int iteration = 0;
while (_memoryStream.Length == 0 && iteration < 10)
{
await Task.Delay(1000);
iteration++;
await Task.Delay(200);
}

await _memoryStream.FlushAsync();

_mockConsole.Verify(x => x.WriteLine(It.IsAny<string>()), Times.Never);
Assert.AreEqual($"[00:00:00.000 Test - {currentLogLevel}] Message{Environment.NewLine}", Encoding.Default.GetString(_memoryStream.ToArray()));
}
else
Expand Down

0 comments on commit 2dd2f37

Please sign in to comment.