public async Task ExceptionBefore() { string result = ""; var scheduler = new TestAsyncScheduler(); var catched = false; try { await AsyncObservable.Range(0, 5) .Do(i => result += i) .Do(i => { if (i == 2) { throw new TestException(); } }) .Prefetch() .Do(i => result += i) .SubscribeAsync() .ConfigureAwait(false); } catch (TestException) { catched = true; } catched .Should().BeTrue(); result .Should().Be("00112"); }
public async Task ConsumeSlowlySingle() { var scheduler = new TestAsyncScheduler(); var result = await scheduler.RunAsync(async() => { var inner = AsyncObservable.Range(0, 5); return(await new [] { inner } .ToAsyncObservable() .Merge() .ConsumeSlowly(scheduler, 10.Seconds(), 10.Seconds(), 10.Seconds(), 10.Seconds(), 10.Seconds())); }); result .Should().Equal( (0, 10.Seconds()), (1, 20.Seconds()), (2, 30.Seconds()), (3, 40.Seconds()), (4, 50.Seconds())); }
public async Task RangeFalse() { var value = await AsyncObservable.Range(0, 10).AllAsync(i => i != 0); value .Should().Be(false); }
public async Task Range() { var value = await AsyncObservable.Range(0, 10).ToListAsync(); value .Should().Equal(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); }
public async Task Count10() { var value = await AsyncObservable.Range(0, 10).AnyAsync(); value .Should().Be(true); }
public async Task Count10WithPredicate2() { var value = await AsyncObservable.Range(0, 10).AnyAsync(i => i > 20); value .Should().Be(false); }
public async Task Count10() { var value = await AsyncObservable.Range(1, 10).FirstAsync(); value .Should().Be(1); }
public async Task IntermediateCancel() { var result = ""; var scheduler = new TestAsyncScheduler(); await scheduler.RunAsync(async() => { using (var cts = new CancellationTokenSource()) { var t1 = AsyncObservable.Range(0, 10) .Select(async(i, ca) => { await scheduler.Delay(20.Seconds(), ca); return(i); }) .SubscribeAsync(i => result += i, onCompleted: () => result += "C", ca: cts.Token); await scheduler.Delay(50.Seconds(), default); cts.Cancel(); await t1; } return(Unit.Default); }); result .Should().Be("01"); }
public async Task RangeTrue() { var value = await AsyncObservable.Range(0, 10).AllAsync(i => i < 20); value .Should().Be(true); }
static async Task SelectManyAsync() { var res = from i in AsyncObservable.Range(0, 10) from j in AsyncObservable.Range(i * 10, 10) select i + " -> " + j; await res.SubscribeAsync(Print <string>()); }
public async Task Cancellation() { var result = ""; var value = await AsyncObservable.Range(1, 10).Do(i => result += i).FirstAsync(); result .Should().Be("1"); }
public static IAsyncObservable <int> ProduceSlowly(this IAsyncScheduler scheduler, params TimeSpan[] delays) { return(AsyncObservable.Range(0, delays.Length) .Select(async(i, ca) => { await scheduler.Delay(delays[i], ca).ConfigureAwait(false); return i; })); }
public async Task Already_Ordered() { var l = await AsyncObservable.Range(1, 5) .OrderBy(v => v) .ToListAsync(); l .Should().Equal(1, 2, 3, 4, 5); }
public async Task Inverse() { var l = await AsyncObservable.Range(1, 5) .OrderBy(v => - v) .ToListAsync(); l .Should().Equal(5, 4, 3, 2, 1); }
public async Task Int_Intermediate_Range() { var result = await AsyncObservable.Range(0, 3) .Max(intermediateResults: true) .ToListAsync(); result .Should() .BeEquivalentTo(0, 1, 2); }
public async Task NegativStartPoint() { string result = ""; await AsyncObservable.Range(-3, 5) .SubscribeAsync(i => result += i, onCompleted: () => result += "C"); result .Should().Be("-3-2-101C"); }
public async Task Count0() { string result = ""; await AsyncObservable.Range(0, 0) .SubscribeAsync(i => result += i, onCompleted: () => result += "C"); result .Should().Be("C"); }
public async Task Select1() { string result = ""; await AsyncObservable.Range(0, 10) .Select(i => 9 - i) .SubscribeAsync(i => result += i, onCompleted: () => result += "C"); result .Should().Be("9876543210C"); }
public async Task WhereAll() { string result = ""; await AsyncObservable.Range(0, 10) .Where(_ => true) .SubscribeAsync(i => result += i, onCompleted: () => result += "C"); result .Should().Be("0123456789C"); }
public async Task WhereNone() { string result = ""; await AsyncObservable.Range(0, 10) .Where(_ => false) .SubscribeAsync(i => result += i, onCompleted: () => result += "C"); result .Should().Be("C"); }
public async Task WhereOdd() { string result = ""; await AsyncObservable.Range(0, 10) .Where(i => i % 2 != 0) .SubscribeAsync(i => result += i, onCompleted: () => result += "C"); result .Should().Be("13579C"); }
static async Task ConcatAsync() { await AsyncObservable.Concat( AsyncObservable.Range(0, 5), AsyncObservable.Range(5, 5), AsyncObservable.Range(10, 5), AsyncObservable.Range(15, 5) ) .SubscribeAsync(Print <int>()); // TODO: Use ForEachAsync. }
public async Task Select2() { string result = ""; await AsyncObservable.Range(0, 3) .SelectMany(i => Enumerable.Range(i * 2, 2)) .SubscribeAsync(i => result += i, onCompleted: () => result += "C"); result .Should().Be("012345C"); }
public async Task All() { string result = ""; await AsyncObservable.Range(0, 10) .DistinctUntilChanged() .SubscribeAsync(i => result += i, onCompleted: () => result += "C"); result .Should().Be("0123456789C"); }
public void ThrowingComparer() { Func <Task> func = async() => await AsyncObservable.Range(0, 4) .OrderBy(x => x, new IntThrowingComparer()) .ToListAsync(); func .Should().Throw <Exception>() .WithMessage("test"); }
public async Task Take1() { string result = ""; await AsyncObservable.Range(0, 10) .Take(2) .SubscribeAsync(i => result += i, onCompleted: () => result += "C"); result .Should().Be("01C"); }
public async Task DistinctByMod() { string result = ""; await AsyncObservable.Range(0, 10) .DistinctBy(v => v % 3) .SubscribeAsync(i => result += i, onCompleted: () => result += "C"); result .Should().Be("012C"); }
public async Task Skip5() { string result = ""; await AsyncObservable.Range(0, 10) .Skip(5) .SubscribeAsync(i => result += i, onCompleted: () => result += "C"); result .Should().Be("56789C"); }
public async Task Long_Intermediate_Range() { var result = await AsyncObservable.Range(0, 3) .Select(i => (long)i) .Max(intermediateResults: true) .ToListAsync(); result .Should() .BeEquivalentTo(0L, 1L, 2L); }
public void ArgumentExceptions() { var obs1 = AsyncObservable.Range(0, 10); var obs2 = (IAsyncObservable <int>)null; obs1.Invoking(o => o.Skip(-1)) .Should().Throw <ArgumentOutOfRangeException>(); obs2.Invoking(o => o.Skip(2)) .Should().Throw <ArgumentNullException>(); obs2.Invoking(o => o.Skip(-1)) .Should().Throw <ArgumentNullException>(); }