public void Completed_ReturnsCompletedTask() { Task result = TaskHelpers.Completed(); Assert.NotNull(result); Assert.Equal(TaskStatus.RanToCompletion, result.Status); }
public static Task IterateImpl(IEnumerator <Task> enumerator, CancellationToken cancellationToken) { try { while (true) { if (cancellationToken.IsCancellationRequested) { return(TaskHelpers.Canceled()); } if (!enumerator.MoveNext()) { return(TaskHelpers.Completed()); } var currentTask = enumerator.Current; if (currentTask.Status == TaskStatus.RanToCompletion) { continue; } if (currentTask.IsCanceled || currentTask.IsFaulted) { return(currentTask); } return(IterateImplIncompleteTask(enumerator, currentTask, cancellationToken)); } } catch (Exception ex) { return(TaskHelpers.FromError(ex)); } }
private static Task CopyResultToCompletionSourceImpl <TTask, TResult>(this TTask task, TaskCompletionSource <TResult> tcs, Func <TTask, TResult> resultThunk) where TTask : Task { // Stay on the same thread if we can if (task.IsCompleted) { switch (task.Status) { case TaskStatus.Canceled: case TaskStatus.Faulted: tcs.TrySetFromTask(task); break; case TaskStatus.RanToCompletion: tcs.TrySetResult(resultThunk(task)); break; } return(TaskHelpers.Completed()); } // Split into a continuation method so that we don't create a closure unnecessarily return(CopyResultToCompletionSourceImplContinuation(task, tcs, resultThunk)); }
private static IEnumerable <Task> SyncContextVerifyingEnumerable(SynchronizationContext sc) { for (int i = 0; i < 10; i++) { Assert.Same(sc, SynchronizationContext.Current); yield return(TaskHelpers.Completed()); } }
public async Task ConvertFromTaskShouldSucceed() { // Arrange var task = TaskHelpers.Completed().CastToObject(); // Act var result = await task; // Assert Assert.Equal(TaskStatus.RanToCompletion, task.Status); Assert.Null(result); }
public Task ConvertFromTaskShouldSucceed() { // Arrange return(TaskHelpers.Completed() // Act .CastToObject() // Assert .ContinueWith((task) => { Assert.Equal(TaskStatus.RanToCompletion, task.Status); Assert.Equal(null, task.Result); })); }
private static Task IterateImpl(IEnumerator <Task> enumerator, CancellationToken cancellationToken, Func <Task, bool> breakCondition) { try { while (true) { // short-circuit: iteration canceled if (cancellationToken.IsCancellationRequested) { return(TaskHelpers.Canceled()); } // short-circuit: iteration complete if (!enumerator.MoveNext()) { return(TaskHelpers.Completed()); } // fast case: Task completed synchronously & successfully Task currentTask = enumerator.Current; if (currentTask.Status == TaskStatus.RanToCompletion) { if (breakCondition != null && breakCondition(currentTask)) { return(currentTask); } continue; } // fast case: Task completed synchronously & unsuccessfully if (currentTask.IsCanceled || currentTask.IsFaulted) { return(currentTask); } // slow case: Task isn't yet complete return(IterateImplIncompleteTask(enumerator, currentTask, cancellationToken, breakCondition)); } } catch (Exception ex) { return(TaskHelpers.FromError(ex)); } }
private static Task CopyResultToCompletionSourceImpl <TTask, TResult>(this TTask task, TaskCompletionSource <TResult> tcs, Func <TTask, TResult> resultThunk) where TTask : Task { if (task.IsCompleted) { switch (task.Status) { case TaskStatus.Canceled: case TaskStatus.Faulted: tcs.TrySetFromTask(task); break; case TaskStatus.RanToCompletion: tcs.TrySetResult(resultThunk(task)); break; } return(TaskHelpers.Completed()); } return(CopyResultToCompletionSourceImplContinuation(task, tcs, resultThunk)); }