### Question:
MultiFlatMapIterable implements Multi<R> { @Override public void subscribe(Flow.Subscriber<? super R> subscriber) { source.subscribe(new FlatMapIterableSubscriber<>(subscriber, mapper, prefetch)); } MultiFlatMapIterable(Multi<T> source,
                         Function<? super T, ? extends Iterable<? extends R>> mapper,
                         int prefetch); @Override void subscribe(Flow.Subscriber<? super R> subscriber);  }### Answer:
@Test public void cancelAfterIterator() { TestSubscriber<Integer> ts = new TestSubscriber<>(Long.MAX_VALUE); Multi.just(1) .<Integer>flatMapIterable(v -> () -> { ts.cancel(); return IntStream.range(0, 2).boxed().iterator(); }) .subscribe(ts); ts.assertEmpty(); }
@Test public void crashInIterator() { TestSubscriber<Integer> ts = new TestSubscriber<>(Long.MAX_VALUE); Multi.just(1) .<Integer>flatMapIterable(v -> () -> { throw new IllegalArgumentException(); }) .subscribe(ts); ts.assertFailure(IllegalArgumentException.class); }
@Test public void cancelAfterFirstHasNext() { TestSubscriber<Integer> ts = new TestSubscriber<>(Long.MAX_VALUE); Multi.just(1) .<Integer>flatMapIterable(v -> () -> { return new Iterator<Integer>() { @Override public boolean hasNext() { ts.cancel(); return true; } @Override public Integer next() { return 1; } }; }) .subscribe(ts); ts.assertEmpty(); }
@Test public void cancelAfterFirstNext() { TestSubscriber<Integer> ts = new TestSubscriber<>(Long.MAX_VALUE); Multi.just(1) .<Integer>flatMapIterable(v -> () -> { return new Iterator<Integer>() { @Override public boolean hasNext() { return true; } @Override public Integer next() { ts.cancel(); return 1; } }; }) .subscribe(ts); ts.assertEmpty(); }
@Test public void crashAfterFirstNext() { TestSubscriber<Integer> ts = new TestSubscriber<>(Long.MAX_VALUE); Multi.just(1) .<Integer>flatMapIterable(v -> () -> { return new Iterator<Integer>() { @Override public boolean hasNext() { return true; } @Override public Integer next() { throw new IllegalArgumentException(); } }; }) .subscribe(ts); ts.assertFailure(IllegalArgumentException.class); }
@Test public void limit() { TestSubscriber<Integer> ts = new TestSubscriber<>(Long.MAX_VALUE); Multi.just(1) .flatMapIterable(v -> range(5)) .limit(3) .subscribe(ts); ts.assertResult(0, 1, 2); }
@Test public void crashAfterSecondHasNext() { TestSubscriber<Integer> ts = new TestSubscriber<>(Long.MAX_VALUE); Multi.just(1) .<Integer>flatMapIterable(v -> () -> { return new Iterator<Integer>() { int count; @Override public boolean hasNext() { if (++count == 2) { throw new IllegalArgumentException(); } return true; } @Override public Integer next() { return 1; } }; }) .subscribe(ts); ts.assertFailure(IllegalArgumentException.class, 1); }
@Test public void cancelAfterSecondHasNext() { TestSubscriber<Integer> ts = new TestSubscriber<>(Long.MAX_VALUE); Multi.just(1) .flatMapIterable(v -> () -> { return new Iterator<Integer>() { int count; @Override public boolean hasNext() { if (++count == 2) { ts.cancel(); } return true; } @Override public Integer next() { return 1; } }; }) .subscribe(ts); ts.assertValuesOnly(1); }