Skip to content

Commit 7343527

Browse files
authored
Improve MTP unit tests (#6861)
1 parent ea7bfe8 commit 7343527

File tree

3 files changed

+55
-61
lines changed

3 files changed

+55
-61
lines changed

test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/SystemAsyncMonitorTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ public async Task AsyncMonitor_ShouldCorrectlyLock()
2424

2525
await Task.WhenAll([.. tasks]);
2626

27-
// Give more time to be above 3s
28-
Thread.Sleep(500);
29-
30-
Assert.IsGreaterThan(3000, stopwatch.ElapsedMilliseconds);
27+
Assert.IsGreaterThanOrEqualTo(3000, stopwatch.ElapsedMilliseconds);
3128

3229
async Task TestLock()
3330
{

test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/TaskExtensionsTests.cs

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -72,49 +72,47 @@ public async Task CancellationAsync_NonCanceledWithArgument_Succeeds()
7272

7373
[TestMethod]
7474
public async Task CancellationAsync_ObserveException_Succeeds()
75-
=> await RetryHelper.RetryAsync(
76-
async () =>
77-
{
78-
ManualResetEvent waitException = new(false);
79-
await Assert.ThrowsAsync<OperationCanceledException>(async ()
80-
=> await Task.Run(
81-
async () =>
82-
{
83-
await Task.Delay(TimeSpan.FromSeconds(10), TestContext.CancellationToken);
84-
waitException.Set();
85-
throw new InvalidOperationException();
86-
}, TestContext.CancellationToken).WithCancellationAsync(new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token));
87-
88-
waitException.WaitOne();
89-
await Task.Delay(TimeSpan.FromSeconds(4), TestContext.CancellationToken);
90-
}, 3, TimeSpan.FromSeconds(3));
75+
{
76+
ManualResetEvent waitException = new(false);
77+
CancellationToken token = new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token;
78+
OperationCanceledException ex = await Assert.ThrowsAsync<OperationCanceledException>(async ()
79+
=> await Task.Run(
80+
async () =>
81+
{
82+
await Task.Delay(TimeSpan.FromSeconds(5), TestContext.CancellationToken);
83+
waitException.Set();
84+
throw new InvalidOperationException();
85+
}, TestContext.CancellationToken).WithCancellationAsync(token));
86+
#if !NETFRAMEWORK // Polyfill bug in Task.WaitAsync implementation :/
87+
Assert.AreEqual(token, ex.CancellationToken);
88+
#endif
89+
waitException.WaitOne();
90+
}
9191

9292
[TestMethod]
9393
public async Task CancellationAsyncWithReturnValue_ObserveException_Succeeds()
94-
=> await RetryHelper.RetryAsync(
95-
async () =>
94+
{
95+
ManualResetEvent waitException = new(false);
96+
CancellationToken token = new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token;
97+
OperationCanceledException ex = await Assert.ThrowsAsync<OperationCanceledException>(async ()
98+
=> await Task.Run(async () =>
9699
{
97-
ManualResetEvent waitException = new(false);
98-
await Assert.ThrowsAsync<OperationCanceledException>(async ()
99-
=> await Task.Run(async () =>
100-
{
101-
await Task.Delay(TimeSpan.FromSeconds(10), TestContext.CancellationToken);
102-
try
103-
{
104-
return 2;
105-
}
106-
finally
107-
{
108-
waitException.Set();
100+
await Task.Delay(TimeSpan.FromSeconds(5), TestContext.CancellationToken);
101+
try
102+
{
103+
return 2;
104+
}
105+
finally
106+
{
107+
waitException.Set();
109108
#pragma warning disable CA2219 // Do not raise exceptions in finally clauses
110-
throw new InvalidOperationException();
109+
throw new InvalidOperationException();
111110
#pragma warning restore CA2219 // Do not raise exceptions in finally clauses
112-
}
113-
}).WithCancellationAsync(new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token));
114-
115-
waitException.WaitOne();
116-
await Task.Delay(TimeSpan.FromSeconds(4), TestContext.CancellationToken);
117-
}, 3, TimeSpan.FromSeconds(3));
111+
}
112+
}).WithCancellationAsync(token));
113+
Assert.AreEqual(token, ex.CancellationToken);
114+
waitException.WaitOne();
115+
}
118116

119117
private static async Task<string> DoSomething()
120118
{

test/UnitTests/Microsoft.Testing.Platform.UnitTests/ServerMode/ServerTests.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,25 @@ private static bool IsHotReloadEnabled(SystemEnvironment environment)
3030
|| environment.GetEnvironmentVariable(EnvironmentVariableConstants.TESTINGPLATFORM_HOTRELOAD_ENABLED) == "1";
3131

3232
[TestMethod]
33-
public async Task ServerCanBeStartedAndAborted_TcpIp() => await RetryHelper.RetryAsync(
34-
async () =>
35-
{
36-
using var server = TcpServer.Create();
37-
38-
TestApplicationHooks testApplicationHooks = new();
39-
string[] args = ["--no-banner", "--server", "--client-host", "localhost", "--client-port", $"{server.Port}", "--internal-testingplatform-skipbuildercheck"];
40-
ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args);
41-
builder.TestHost.AddTestHostApplicationLifetime(_ => testApplicationHooks);
42-
builder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, __) => new MockTestAdapter());
43-
var testApplication = (TestApplication)await builder.BuildAsync();
44-
testApplication.ServiceProvider.GetRequiredService<SystemConsole>().SuppressOutput();
45-
Task<int> serverTask = testApplication.RunAsync();
46-
47-
await testApplicationHooks.WaitForBeforeRunAsync();
48-
ITestApplicationCancellationTokenSource stopService = testApplication.ServiceProvider.GetTestApplicationCancellationTokenSource();
49-
50-
stopService.Cancel();
51-
Assert.AreEqual(ExitCodes.TestSessionAborted, await serverTask);
52-
}, 3, TimeSpan.FromSeconds(10));
33+
public async Task ServerCanBeStartedAndAborted_TcpIp()
34+
{
35+
using var server = TcpServer.Create();
36+
37+
TestApplicationHooks testApplicationHooks = new();
38+
string[] args = ["--no-banner", "--server", "--client-host", "localhost", "--client-port", $"{server.Port}", "--internal-testingplatform-skipbuildercheck"];
39+
ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args);
40+
builder.TestHost.AddTestHostApplicationLifetime(_ => testApplicationHooks);
41+
builder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, __) => new MockTestAdapter());
42+
var testApplication = (TestApplication)await builder.BuildAsync();
43+
testApplication.ServiceProvider.GetRequiredService<SystemConsole>().SuppressOutput();
44+
Task<int> serverTask = testApplication.RunAsync();
45+
46+
await testApplicationHooks.WaitForBeforeRunAsync();
47+
ITestApplicationCancellationTokenSource stopService = testApplication.ServiceProvider.GetTestApplicationCancellationTokenSource();
48+
49+
stopService.Cancel();
50+
Assert.AreEqual(ExitCodes.TestSessionAborted, await serverTask);
51+
}
5352

5453
[TestMethod]
5554
public async Task ServerCanInitialize()

0 commit comments

Comments
 (0)