Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ public async Task AsyncMonitor_ShouldCorrectlyLock()
bool lockState = false;
List<Task> tasks = [];
var stopwatch = Stopwatch.StartNew();
for (int i = 0; i < 3; i++)
try
{
tasks.Add(Task.Run(TestLock, TestContext.CancellationToken));
}

await Task.WhenAll([.. tasks]);
for (int i = 0; i < 3; i++)
{
tasks.Add(Task.Run(TestLock, TestContext.CancellationToken));
}

// Give more time to be above 3s
Thread.Sleep(500);
await Task.WhenAll([.. tasks]);
}
finally
{
stopwatch.Stop();
}

Assert.IsGreaterThan(3000, stopwatch.ElapsedMilliseconds);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,49 +72,47 @@ public async Task CancellationAsync_NonCanceledWithArgument_Succeeds()

[TestMethod]
public async Task CancellationAsync_ObserveException_Succeeds()
=> await RetryHelper.RetryAsync(
async () =>
{
ManualResetEvent waitException = new(false);
await Assert.ThrowsAsync<OperationCanceledException>(async ()
=> await Task.Run(
async () =>
{
await Task.Delay(TimeSpan.FromSeconds(10), TestContext.CancellationToken);
waitException.Set();
throw new InvalidOperationException();
}, TestContext.CancellationToken).WithCancellationAsync(new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token));

waitException.WaitOne();
await Task.Delay(TimeSpan.FromSeconds(4), TestContext.CancellationToken);
}, 3, TimeSpan.FromSeconds(3));
{
ManualResetEvent waitException = new(false);
CancellationToken token = new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token;
OperationCanceledException ex = await Assert.ThrowsAsync<OperationCanceledException>(async ()
=> await Task.Run(
async () =>
{
await Task.Delay(TimeSpan.FromSeconds(5), TestContext.CancellationToken);
waitException.Set();
throw new InvalidOperationException();
}, TestContext.CancellationToken).WithCancellationAsync(token));
#if !NETFRAMEWORK // Polyfill bug in Task.WhenAsync implementation :/
Assert.AreEqual(token, ex.CancellationToken);
#endif
waitException.WaitOne();
}

[TestMethod]
public async Task CancellationAsyncWithReturnValue_ObserveException_Succeeds()
=> await RetryHelper.RetryAsync(
async () =>
{
ManualResetEvent waitException = new(false);
CancellationToken token = new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token;
OperationCanceledException ex = await Assert.ThrowsAsync<OperationCanceledException>(async ()
=> await Task.Run(async () =>
{
ManualResetEvent waitException = new(false);
await Assert.ThrowsAsync<OperationCanceledException>(async ()
=> await Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(10), TestContext.CancellationToken);
try
{
return 2;
}
finally
{
waitException.Set();
await Task.Delay(TimeSpan.FromSeconds(5), TestContext.CancellationToken);
try
{
return 2;
}
finally
{
waitException.Set();
#pragma warning disable CA2219 // Do not raise exceptions in finally clauses
throw new InvalidOperationException();
throw new InvalidOperationException();
#pragma warning restore CA2219 // Do not raise exceptions in finally clauses
}
}).WithCancellationAsync(new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token));

waitException.WaitOne();
await Task.Delay(TimeSpan.FromSeconds(4), TestContext.CancellationToken);
}, 3, TimeSpan.FromSeconds(3));
}
}).WithCancellationAsync(token));
Assert.AreEqual(token, ex.CancellationToken);
waitException.WaitOne();
}

private static async Task<string> DoSomething()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,25 @@ private static bool IsHotReloadEnabled(SystemEnvironment environment)
|| environment.GetEnvironmentVariable(EnvironmentVariableConstants.TESTINGPLATFORM_HOTRELOAD_ENABLED) == "1";

[TestMethod]
public async Task ServerCanBeStartedAndAborted_TcpIp() => await RetryHelper.RetryAsync(
async () =>
{
using var server = TcpServer.Create();

TestApplicationHooks testApplicationHooks = new();
string[] args = ["--no-banner", "--server", "--client-host", "localhost", "--client-port", $"{server.Port}", "--internal-testingplatform-skipbuildercheck"];
ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args);
builder.TestHost.AddTestHostApplicationLifetime(_ => testApplicationHooks);
builder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, __) => new MockTestAdapter());
var testApplication = (TestApplication)await builder.BuildAsync();
testApplication.ServiceProvider.GetRequiredService<SystemConsole>().SuppressOutput();
Task<int> serverTask = testApplication.RunAsync();

await testApplicationHooks.WaitForBeforeRunAsync();
ITestApplicationCancellationTokenSource stopService = testApplication.ServiceProvider.GetTestApplicationCancellationTokenSource();

stopService.Cancel();
Assert.AreEqual(ExitCodes.TestSessionAborted, await serverTask);
}, 3, TimeSpan.FromSeconds(10));
public async Task ServerCanBeStartedAndAborted_TcpIp()
{
using var server = TcpServer.Create();

TestApplicationHooks testApplicationHooks = new();
string[] args = ["--no-banner", "--server", "--client-host", "localhost", "--client-port", $"{server.Port}", "--internal-testingplatform-skipbuildercheck"];
ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args);
builder.TestHost.AddTestHostApplicationLifetime(_ => testApplicationHooks);
builder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, __) => new MockTestAdapter());
var testApplication = (TestApplication)await builder.BuildAsync();
testApplication.ServiceProvider.GetRequiredService<SystemConsole>().SuppressOutput();
Task<int> serverTask = testApplication.RunAsync();

await testApplicationHooks.WaitForBeforeRunAsync();
ITestApplicationCancellationTokenSource stopService = testApplication.ServiceProvider.GetTestApplicationCancellationTokenSource();

stopService.Cancel();
Assert.AreEqual(ExitCodes.TestSessionAborted, await serverTask);
}

[TestMethod]
public async Task ServerCanInitialize()
Expand Down
Loading