diff --git a/src/main/java/rx/internal/operators/OnSubscribeFromIterable.java b/src/main/java/rx/internal/operators/OnSubscribeFromIterable.java index b94e35c35c..9389c4480c 100644 --- a/src/main/java/rx/internal/operators/OnSubscribeFromIterable.java +++ b/src/main/java/rx/internal/operators/OnSubscribeFromIterable.java @@ -20,6 +20,7 @@ import rx.*; import rx.Observable.OnSubscribe; +import rx.exceptions.Exceptions; /** * Converts an {@code Iterable} sequence into an {@code Observable}. @@ -42,11 +43,25 @@ public OnSubscribeFromIterable(Iterable iterable) { @Override public void call(final Subscriber o) { - final Iterator it = is.iterator(); - if (!it.hasNext() && !o.isUnsubscribed()) - o.onCompleted(); - else - o.setProducer(new IterableProducer(o, it)); + final Iterator it; + boolean b; + + try { + it = is.iterator(); + + b = it.hasNext(); + } catch (Throwable ex) { + Exceptions.throwOrReport(ex, o); + return; + } + + if (!o.isUnsubscribed()) { + if (!b) { + o.onCompleted(); + } else { + o.setProducer(new IterableProducer(o, it)); + } + } } private static final class IterableProducer extends AtomicLong implements Producer { @@ -81,38 +96,58 @@ void slowpath(long n) { final Iterator it = this.it; long r = n; - while (true) { - /* - * This complicated logic is done to avoid touching the - * volatile `requested` value during the loop itself. If - * it is touched during the loop the performance is - * impacted significantly. - */ - long numToEmit = r; - while (true) { + long e = 0; + + for (;;) { + while (e != r) { if (o.isUnsubscribed()) { return; - } else if (it.hasNext()) { - if (--numToEmit >= 0) { - o.onNext(it.next()); - } else - break; - } else if (!o.isUnsubscribed()) { - o.onCompleted(); + } + + T value; + + try { + value = it.next(); + } catch (Throwable ex) { + Exceptions.throwOrReport(ex, o); return; - } else { - // is unsubscribed + } + + o.onNext(value); + + if (o.isUnsubscribed()) { return; } + + boolean b; + + try { + b = it.hasNext(); + } catch (Throwable ex) { + Exceptions.throwOrReport(ex, o); + return; + } + + if (!b) { + if (!o.isUnsubscribed()) { + o.onCompleted(); + } + return; + } + + e++; } - r = addAndGet(-r); - if (r == 0L) { - // we're done emitting the number requested so - // return - return; + + r = get(); + if (e == r) { + r = BackpressureUtils.produced(this, e); + if (r == 0L) { + break; + } + e = 0L; } - } + } void fastpath() { @@ -120,16 +155,39 @@ void fastpath() { final Subscriber o = this.o; final Iterator it = this.it; - while (true) { + for (;;) { if (o.isUnsubscribed()) { return; - } else if (it.hasNext()) { - o.onNext(it.next()); - } else if (!o.isUnsubscribed()) { - o.onCompleted(); + } + + T value; + + try { + value = it.next(); + } catch (Throwable ex) { + Exceptions.throwOrReport(ex, o); + return; + } + + o.onNext(value); + + if (o.isUnsubscribed()) { return; - } else { - // is unsubscribed + } + + boolean b; + + try { + b = it.hasNext(); + } catch (Throwable ex) { + Exceptions.throwOrReport(ex, o); + return; + } + + if (!b) { + if (!o.isUnsubscribed()) { + o.onCompleted(); + } return; } } diff --git a/src/test/java/rx/internal/operators/OnSubscribeFromIterableTest.java b/src/test/java/rx/internal/operators/OnSubscribeFromIterableTest.java index a75e733951..00956b9cae 100644 --- a/src/test/java/rx/internal/operators/OnSubscribeFromIterableTest.java +++ b/src/test/java/rx/internal/operators/OnSubscribeFromIterableTest.java @@ -15,28 +15,21 @@ */ package rx.internal.operators; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; +import static org.mockito.Mockito.*; + +import java.util.*; +import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicBoolean; -import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; import rx.Observable; import rx.Observer; import rx.Subscriber; +import rx.exceptions.TestException; import rx.internal.util.RxRingBuffer; import rx.observers.TestSubscriber; import rx.schedulers.Schedulers; @@ -313,5 +306,230 @@ public void onNext(Integer t) { }); assertFalse(called.get()); } + + @Test + public void getIteratorThrows() { + Iterable it = new Iterable() { + @Override + public Iterator iterator() { + throw new TestException("Forced failure"); + } + }; + + TestSubscriber ts = new TestSubscriber(); + + Observable.from(it).unsafeSubscribe(ts); + + ts.assertNoValues(); + ts.assertError(TestException.class); + ts.assertNotCompleted(); + } + + @Test + public void hasNextThrowsImmediately() { + Iterable it = new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + @Override + public boolean hasNext() { + throw new TestException("Forced failure"); + } + + @Override + public Integer next() { + return null; + } + + @Override + public void remove() { + // ignored + } + }; + } + }; + + TestSubscriber ts = new TestSubscriber(); + + Observable.from(it).unsafeSubscribe(ts); + + ts.assertNoValues(); + ts.assertError(TestException.class); + ts.assertNotCompleted(); + } + + @Test + public void hasNextThrowsSecondTimeFastpath() { + Iterable it = new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + int count; + @Override + public boolean hasNext() { + if (++count >= 2) { + throw new TestException("Forced failure"); + } + return true; + } + + @Override + public Integer next() { + return 1; + } + + @Override + public void remove() { + // ignored + } + }; + } + }; + + TestSubscriber ts = new TestSubscriber(); + + Observable.from(it).unsafeSubscribe(ts); + + ts.assertValues(1); + ts.assertError(TestException.class); + ts.assertNotCompleted(); + } + + @Test + public void hasNextThrowsSecondTimeSlowpath() { + Iterable it = new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + int count; + @Override + public boolean hasNext() { + if (++count >= 2) { + throw new TestException("Forced failure"); + } + return true; + } + + @Override + public Integer next() { + return 1; + } + + @Override + public void remove() { + // ignored + } + }; + } + }; + + TestSubscriber ts = new TestSubscriber(5); + + Observable.from(it).unsafeSubscribe(ts); + + ts.assertValues(1); + ts.assertError(TestException.class); + ts.assertNotCompleted(); + } + @Test + public void nextThrowsFastpath() { + Iterable it = new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + @Override + public boolean hasNext() { + return true; + } + + @Override + public Integer next() { + throw new TestException("Forced failure"); + } + + @Override + public void remove() { + // ignored + } + }; + } + }; + + TestSubscriber ts = new TestSubscriber(); + + Observable.from(it).unsafeSubscribe(ts); + + ts.assertNoValues(); + ts.assertError(TestException.class); + ts.assertNotCompleted(); + } + + @Test + public void nextThrowsSlowpath() { + Iterable it = new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + @Override + public boolean hasNext() { + return true; + } + + @Override + public Integer next() { + throw new TestException("Forced failure"); + } + + @Override + public void remove() { + // ignored + } + }; + } + }; + + TestSubscriber ts = new TestSubscriber(5); + + Observable.from(it).unsafeSubscribe(ts); + + ts.assertNoValues(); + ts.assertError(TestException.class); + ts.assertNotCompleted(); + } + + @Test + public void deadOnArrival() { + Iterable it = new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + @Override + public boolean hasNext() { + return false; + } + + @Override + public Integer next() { + throw new NoSuchElementException(); + } + + @Override + public void remove() { + // ignored + } + }; + } + }; + + TestSubscriber ts = new TestSubscriber(5); + ts.unsubscribe(); + + Observable.from(it).unsafeSubscribe(ts); + + ts.assertNoValues(); + ts.assertNoErrors(); + ts.assertNotCompleted(); + + } }