### Question:
SingleFlatMapSingle extends CompletionSingle<R> { @Override public void subscribe(Flow.Subscriber<? super R> subscriber) { Objects.requireNonNull(subscriber, "subscriber is null"); source.subscribe(new FlatMapSubscriber<>(subscriber, mapper)); } SingleFlatMapSingle(Single<T> source, Function<? super T, ? extends Single<? extends R>> mapper); @Override void subscribe(Flow.Subscriber<? super R> subscriber);  }### Answer:
@Test public void emptySource() { TestSubscriber<Integer> ts = new TestSubscriber<>(); Single.empty() .flatMapSingle(v -> Single.just(2)) .subscribe(ts); assertThat(ts.getItems().isEmpty(), is(true)); assertThat(ts.getLastError(), is(nullValue())); assertThat(ts.isComplete(), is(true)); }
@Test public void emptyInner() { TestSubscriber<Integer> ts = new TestSubscriber<>(); Single.just(1) .flatMapSingle(v -> Single.<Integer>empty()) .subscribe(ts); ts.request1(); assertThat(ts.getItems().isEmpty(), is(true)); assertThat(ts.getLastError(), is(nullValue())); assertThat(ts.isComplete(), is(true)); }
@Test public void errorSource() { TestSubscriber<Integer> ts = new TestSubscriber<>(); Single.error(new IOException()) .flatMapSingle(v -> Single.just(2)) .subscribe(ts); assertThat(ts.getItems().isEmpty(), is(true)); assertThat(ts.getLastError(), instanceOf(IOException.class)); assertThat(ts.isComplete(), is(false)); }
@Test public void errorInner() { TestSubscriber<Integer> ts = new TestSubscriber<>(); Single.just(1) .flatMapSingle(v -> Single.<Integer>error(new IOException())) .subscribe(ts); ts.request1(); assertThat(ts.getItems().isEmpty(), is(true)); assertThat(ts.getLastError(), instanceOf(IOException.class)); assertThat(ts.isComplete(), is(false)); }
@Test public void cancelMain() { SubmissionPublisher<Integer> sp = new SubmissionPublisher<>(Runnable::run, 32); TestSubscriber<Integer> ts = new TestSubscriber<>(); Single.create(sp) .flatMapSingle(v -> Single.just(2)) .subscribe(ts); assertThat(sp.hasSubscribers(), is(true)); ts.getSubcription().cancel(); assertThat(sp.hasSubscribers(), is(false)); }
@Test public void cancelInner() { SubmissionPublisher<Integer> sp = new SubmissionPublisher<>(Runnable::run, 32); TestSubscriber<Integer> ts = new TestSubscriber<>(); Single.just(1) .flatMapSingle(v -> Single.create(sp)) .subscribe(ts); ts.request1(); assertThat(sp.hasSubscribers(), is(true)); ts.getSubcription().cancel(); assertThat(sp.hasSubscribers(), is(false)); }
@Test public void mapperCrash() { TestSubscriber<Integer> ts = new TestSubscriber<>(); Single.just(1) .<Integer>flatMapSingle(v -> { throw new IllegalArgumentException(); }) .subscribe(ts); ts.request1(); assertThat(ts.getItems().isEmpty(), is(true)); assertThat(ts.getLastError(), instanceOf(IllegalArgumentException.class)); assertThat(ts.isComplete(), is(false)); }
@Test public void mapperNull() { TestSubscriber<Integer> ts = new TestSubscriber<>(); Single.just(1) .<Integer>flatMapSingle(v -> null) .subscribe(ts); ts.request1(); assertThat(ts.getItems().isEmpty(), is(true)); assertThat(ts.getLastError(), instanceOf(NullPointerException.class)); assertThat(ts.isComplete(), is(false)); }