Permalink
Browse files

TaskSample - FiveItemsAllHangExceptLastWhichMatchesAsyncReturns

  • Loading branch information...
bobbymcr committed May 23, 2018
1 parent 5cf6cb4 commit 647a970c6fd26ba275009eff44f4d48c6e4fc379
Showing with 33 additions and 0 deletions.
  1. +33 −0 projects/TaskSample/TaskSample.Test/FirstAsyncTest.cs
@@ -177,6 +177,39 @@ public void ThreeItemsNoMatchExceptLastAsyncReturns()
task.Result.Should().Be("good 3");
}
[Fact]
public void FiveItemsAllHangExceptLastWhichMatchesAsyncReturns()
{
List<Task> canceledTasks = new List<Task>();
Func<CancellationToken, Task<string>> hang = t =>
{
var current = new TaskCompletionSource<string>();
t.Register(() => current.SetCanceled());
Task<string> ct = current.Task;
canceledTasks.Add(ct);
return ct;
};
TaskCompletionSource<string> delayed = new TaskCompletionSource<string>();
IEnumerable<Func<CancellationToken, Task<string>>> funcs = new Func<CancellationToken, Task<string>>[]
{
hang,
hang,
hang,
hang,
t => delayed.Task
};
Task<string> task = funcs.FirstAsync(r => r.StartsWith("good", StringComparison.Ordinal));
task.IsCompleted.Should().BeFalse();
delayed.SetResult("good 1");
task.IsCompletedSuccessfully.Should().BeTrue();
task.Result.Should().Be("good 1");
canceledTasks.Should().OnlyContain(t => t.IsCanceled);
}
private static void ShouldBeFaulted(Task task)
{
task.IsCompleted.Should().BeTrue();

0 comments on commit 647a970

Please sign in to comment.