public void AsyncMerge() { var maxConcurrency = 2; var prefetch = Flowable.BufferSize(); var backpressureStrategy = BackpressureStrategy.BUFFER; var flowable = Flowable.Range(0, 5).FlatMap(x => { return(Flowable.Create <int>(e => { // Thread to emulate some asynchronous action new Thread(() => { for (var i = 0; i < 10; ++i) { Thread.Sleep(100); e.OnNext(x * i); } e.OnComplete(); }).Start(); }, backpressureStrategy)); }, maxConcurrency, prefetch); var v = flowable .Test() .AwaitDone(TimeSpan.FromSeconds(6)) .AssertNoError() .AssertComplete() .Values ; Assert.AreEqual(50, v.Count); }
public static IPublisher <T> ToPublisher <T>(this ISingle <T> source) { if (source is ScalarSource <T> ) { return((ScalarSource <T>)source); } return(Flowable.Create <T>(s => { source.Subscribe(new SubscriberToSingleSubscriber <T>(s)); })); }
public override IPublisher <int> CreatePublisher(long elements) { return(Flowable.Create <int>(e => { for (int i = 0; i < elements; i++) { if (e.IsCancelled) { return; } e.OnNext(i); } if (!e.IsCancelled) { e.OnComplete(); } }, BackpressureStrategy.BUFFER)); }
public override IPublisher <int> CreatePublisher(long elements) { return(Flowable.Create <int>(e => { var cts = new CancellationTokenSource(); e.OnCancel(cts.Dispose); Task.Factory.StartNew(() => { long now = SchedulerHelper.NowUTC(); long f = 0; for (int i = 0; i < elements; i++) { while (e.Requested == f) { if (e.IsCancelled) { return; } Thread.Sleep(1); if (SchedulerHelper.NowUTC() - now > 1000) { return; } } if (e.IsCancelled) { return; } e.OnNext(i); f++; } if (!e.IsCancelled) { e.OnComplete(); } }, cts.Token); }, BackpressureStrategy.ERROR)); }