Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -53,7 +53,11 @@ internal TestMethodInfo(MethodInfo testMethod, TestClassInfo parent)

internal TestMethodAttribute Executor { get; /*For testing only*/set; }

internal static ITestContext TestContext => (ITestContext?)TestTools.UnitTesting.TestContext.Current ?? throw ApplicationStateGuard.Unreachable();
internal ITestContext TestContext
{
Comment thread
Evangelink marked this conversation as resolved.
get => field ?? (ITestContext?)TestTools.UnitTesting.TestContext.Current ?? throw ApplicationStateGuard.Unreachable();
set;
}

/// <summary>
/// Gets a value indicating whether timeout is set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ private async Task<TestResult[]> ExecuteTestAsync(TestMethodInfo testMethodInfo)
{
using (TestContextImplementation.SetCurrentTestContext(_testContext as TestContext))
{
testMethodInfo.TestContext = _testContext;
Comment thread
Evangelink marked this conversation as resolved.
tcs.SetResult(await _testMethodInfo.Executor.ExecuteAsync(testMethodInfo).ConfigureAwait(false));
Comment thread
Youssef1313 marked this conversation as resolved.
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,56 @@ private async Task RunTestMethodWithEmptyDataSourceShouldFailIfConsiderEmptyData
}
}

public async Task RunTestMethodShouldPassWhenAttributeInvokesTestMethodOnExecutionContextUnsafeThread()
{
_testablePlatformServiceProvider.MockThreadOperations
.Setup(tho => tho.Execute(It.IsAny<Action>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns(true)
.Callback((Action a, int timeout, CancellationToken token) => a.Invoke());

var localTestMethodOptions = new TestMethodOptions(TimeoutInfo.FromTimeout(200), new ExecutionContextUnsafeThreadTestMethodAttribute());
var testMethodInfo = new TestMethodInfo(_methodInfo, _testClassInfo)
{
TimeoutInfo = localTestMethodOptions.TimeoutInfo,
Executor = localTestMethodOptions.TestMethodAttribute,
};
var testMethodRunner = new TestMethodRunner(testMethodInfo, _testMethod, _testContextImplementation);

TestResult[] results = await testMethodRunner.ExecuteAsync(string.Empty, string.Empty, string.Empty, string.Empty);
results.Should().HaveCount(1);
results[0].Outcome.Should().Be(UnitTestOutcome.Passed);
}

#region Test data

private sealed class ExecutionContextUnsafeThreadTestMethodAttribute : TestMethodAttribute
{
private static readonly TimeSpan WaitTimeout = TimeSpan.FromSeconds(10);

public override async Task<TestResult[]> ExecuteAsync(ITestMethod testMethod)
{
var taskCompletionSource = new TaskCompletionSource<TestResult>(TaskCreationOptions.RunContinuationsAsynchronously);
ThreadPool.UnsafeQueueUserWorkItem(
_ =>
{
try
{
taskCompletionSource.SetResult(testMethod.InvokeAsync(null).ConfigureAwait(false).GetAwaiter().GetResult());
}
catch (Exception exception)
{
taskCompletionSource.SetException(exception);
}
},
null);

Task completedTask = await Task.WhenAny(taskCompletionSource.Task, Task.Delay(WaitTimeout)).ConfigureAwait(false);
return completedTask == taskCompletionSource.Task
? [await taskCompletionSource.Task.ConfigureAwait(false)]
: throw new TimeoutException($"The execution did not complete within {WaitTimeout}.");
}
}

[SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "Use through reflection")]
private static void InitMethodThrowingException(TestContext tc)
// TODO: Fix exception type
Expand Down