public void TestWaitAnyWithTwoParallelAsynchronousTaskWithResults() { this.TestWithError(() => { SharedEntry entry = new SharedEntry(); Task <int> task1 = Task.Run(async() => { return(await entry.GetWriteResultWithDelayAsync(5)); }); Task <int> task2 = Task.Run(async() => { return(await entry.GetWriteResultWithDelayAsync(3)); }); int index = Task.WaitAny(task1, task2); SystemTasks.Task <int> result = index is 0 ? task1.UncontrolledTask : task2.UncontrolledTask; Specification.Assert(index is 0 || index is 1, $"Index is {index}."); Specification.Assert(result.Result is 5 || result.Result is 3, "Found unexpected value."); AssertCompleted(task1, task2); }, configuration: this.GetConfiguration().WithTestingIterations(200), expectedError: "One task has not completed.", replay: true); }
/// <summary> /// Tries to safely retrieve the payload of a value task if that payload is an asynchronous task. /// If the payload is a <see cref="SystemTasks.Sources.IValueTaskSource"/>, then it returns null. /// </summary> internal static bool TryGetTask <TResult>(ref SystemTasks.ValueTask <TResult> task, out SystemTasks.Task <TResult> payload) { // Access the payload through reflection. var field = task.GetType().GetField("_obj", BindingFlags.NonPublic | BindingFlags.Instance); payload = field?.GetValueDirect(__makeref(task)) as SystemTasks.Task <TResult>; return(payload != null); }
/// <summary> /// Gets the result value of the specified generic task. /// </summary> #pragma warning disable CA1707 // Remove the underscores from member name #pragma warning disable SA1300 // Element should begin with an uppercase letter #pragma warning disable IDE1006 // Naming Styles public static TResult get_Result(SystemTasks.Task <TResult> task) { var runtime = CoyoteRuntime.Current; if (runtime.SchedulingPolicy != SchedulingPolicy.None) { runtime.WaitUntilTaskCompletes(task); } return(task.Result); }
public static SystemTask Unwrap(this SystemTasks.Task <SystemTask> task) { var runtime = CoyoteRuntime.Current; if (runtime.SchedulingPolicy is SchedulingPolicy.None) { return(SystemTasks.TaskExtensions.Unwrap(task)); } return(runtime.UnwrapTask(task)); }
protected static async Task <T> WaitAsync <T>(SystemTasks.Task <T> task, int millisecondsDelay = 5000) { if (Debugger.IsAttached) { millisecondsDelay = 500000; } await SystemTasks.Task.WhenAny(task, SystemTasks.Task.Delay(millisecondsDelay)); Assert.True(task.IsCompleted); return(task.Result); }
public void TestWaitAnyWithTwoAsynchronousTaskWithResults() { this.TestWithError(() => { SharedEntry entry = new SharedEntry(); Task <int> task1 = entry.GetWriteResultWithDelayAsync(5); Task <int> task2 = entry.GetWriteResultWithDelayAsync(3); int index = Task.WaitAny(task1, task2); SystemTasks.Task <int> result = index == 0 ? GetUncontrolledTask(task1) : GetUncontrolledTask(task2); Specification.Assert(index == 0 || index == 1, $"Index is {index}."); Specification.Assert(result.Result == 5 || result.Result == 3, "Found unexpected value."); Specification.Assert(task1.IsCompleted && task2.IsCompleted, "One task has not completed."); }, configuration: GetConfiguration().WithTestingIterations(200), expectedError: "One task has not completed.", replay: true); }
public void TestWaitAnyWithTwoSynchronousTaskWithResults() { this.TestWithError(() => { SharedEntry entry = new SharedEntry(); Task <int> task1 = entry.GetWriteResultAsync(5); Task <int> task2 = entry.GetWriteResultAsync(3); int index = Task.WaitAny(task1, task2); SystemTasks.Task <int> result = index is 0 ? task1.UncontrolledTask : task2.UncontrolledTask; Specification.Assert(index is 0 || index is 1, $"Index is {index}."); Specification.Assert(result.Result is 5 || result.Result is 3, "Found unexpected value."); Specification.Assert((task1.IsCompleted && !task2.IsCompleted) || (!task1.IsCompleted && task2.IsCompleted), "Both task have completed."); }, configuration: this.GetConfiguration().WithTestingIterations(200), expectedError: "Both task have completed.", replay: true); }
internal Task(TaskController taskController, SystemTasks.Task <TResult> task) : base(taskController, task) { }
/// <summary> /// Returns a dummy controlled <see cref="Task{TResult}"/> that wraps this uncontrolled /// <see cref="SystemTasks.Task{TResult}"/>. /// </summary> /// <remarks> /// The returned dummy controlled <see cref="Task{TResult}"/> does not actually take control of the /// uncontrolled <see cref="SystemTasks.Task{TResult}"/> during systematic testing, so this method /// should only be used to cross an interface boundary where a controlled <see cref="Task{TResult}"/> /// must be temporarily converted into an uncontrolled <see cref="SystemTasks.Task{TResult}"/> and /// then coverted back to a controlled <see cref="Task{TResult}"/>. /// </remarks> public static Task <TResult> WrapInControlledTask <TResult>(this SystemTasks.Task <TResult> @this) => new Task <TResult>(null, @this);
/// <summary> /// Configures an awaiter used to await this task. /// </summary> public static ConfiguredTaskAwaitable <TResult> ConfigureAwait( SystemTasks.Task <TResult> task, bool continueOnCapturedContext) => new ConfiguredTaskAwaitable <TResult>(task, continueOnCapturedContext);
#pragma warning restore CA1707 // Remove the underscores from member name #pragma warning restore SA1300 // Element should begin with an uppercase letter #pragma warning restore IDE1006 // Naming Styles /// <summary> /// Returns a generic task awaiter for the specified generic task. /// </summary> public static TaskAwaiter <TResult> GetAwaiter(SystemTasks.Task <TResult> task) => new TaskAwaiter <TResult>(task);
public static SystemTasks.Task <SystemTasks.Task <TResult> > WhenAny <TResult>( SystemTasks.Task <TResult> task1, SystemTasks.Task <TResult> task2) => SystemTask.WhenAny(task1, task2);
internal Task(CoyoteRuntime runtime, SystemTasks.Task <TResult> task) : base(runtime, task) { }
public static SystemTask Unwrap(this SystemTasks.Task <SystemTask> task) => CoyoteRuntime.IsExecutionControlled ? CoyoteRuntime.Current.UnwrapTask(task) : SystemTasks.TaskExtensions.Unwrap(task);