public static async Task <List <T> > ToList <T>(this IAsyncEnumerator <T> asyncEnumerator) { List <T> itemList = new List <T>(); await asyncEnumerator.ForEach(itemList, (item, list) => list.Add(item)).ConfigureAwait(false); return(itemList); }
public void ItemExceptionIsPropagated() { IAsyncEnumerator <int> enumerator = TaskAsyncEnumerator.Create ( Task.Delay(100).ContinueWith(_ => 64), Task.FromException <int>(new InvalidOperationException()) ); Func <Task> act = () => enumerator.ForEach(_ => { }); act.ShouldThrow <InvalidOperationException>(); }
public async Task LambdaIsCalledForEachItem() { List <int> callArguments = new List <int>(); Task <int>[] itemTasks = new Task <int>[] { Task.FromResult(42), Task.Delay(100).ContinueWith(_ => 64), Task.FromResult(128) }; IAsyncEnumerator <int> enumerator = TaskAsyncEnumerator.Create(itemTasks); await enumerator.ForEach(item => callArguments.Add(item)).ConfigureAwait(true); int[] expectedItems = await Task.WhenAll(itemTasks).ConfigureAwait(true); callArguments.Should().HaveEquivalentItems(expectedItems); }
public void LambdaExceptionIsPropagated() { const int dangerousItem = 42; IAsyncEnumerator <int> enumerator = TaskAsyncEnumerator.Create ( Task.Delay(100).ContinueWith(_ => 64), Task.FromResult(dangerousItem) ); Func <Task> act = () => enumerator.ForEach ( value => { if (value == dangerousItem) { throw new InvalidOperationException(); } } ); act.ShouldThrow <InvalidOperationException>(); }
public static Task ForEach <T>(this IAsyncEnumerator <T> asyncEnumerator, Action <T> action) => asyncEnumerator.ForEach(action, (item, actionArg) => actionArg(item));