### Question:
SingleTimeout extends CompletionSingle<T> { @Override public void subscribe(Flow.Subscriber<? super T> subscriber) { TimeoutSubscriber<T> parent = new TimeoutSubscriber<>(subscriber, fallback); subscriber.onSubscribe(parent); parent.setFuture(executor.schedule(parent, timeout, unit)); source.subscribe(parent); } SingleTimeout(Single<T> source, long timeout, TimeUnit unit,
                  ScheduledExecutorService executor, Single<T> fallback); @Override void subscribe(Flow.Subscriber<? super T> subscriber);  }### Answer:
@Test public void fallback() { TestSubscriber<Integer> ts = new TestSubscriber<>(Long.MAX_VALUE); Single.<Integer>never() .timeout(1, TimeUnit.MILLISECONDS, executor, Single.just(1)) .subscribe(ts); ts.awaitDone(5, TimeUnit.SECONDS) .assertResult(1); }
@Test public void fallback2() { TestSubscriber<Long> ts = new TestSubscriber<>(Long.MAX_VALUE); Single.timer(1, TimeUnit.MINUTES, executor) .timeout(1, TimeUnit.MILLISECONDS, executor, Single.just(1L)) .subscribe(ts); ts.awaitDone(5, TimeUnit.SECONDS) .assertResult(1L); }
@Test public void mainError() { TestSubscriber<Long> ts = new TestSubscriber<>(Long.MAX_VALUE); Single.<Long>error(new IOException()) .timeout(1, TimeUnit.MINUTES, executor) .subscribe(ts); ts.assertFailure(IOException.class); }
@Test public void mainEmpty() { TestSubscriber<Long> ts = new TestSubscriber<>(Long.MAX_VALUE); Single.<Long>empty() .timeout(1, TimeUnit.MINUTES, executor) .subscribe(ts); ts.assertResult(); }
@Test public void mainCanceled() throws Exception { TestSubscriber<Long> ts = new TestSubscriber<>(Long.MAX_VALUE); SubmissionPublisher<Long> sp = new SubmissionPublisher<>(Runnable::run, 128); Single.create(sp) .timeout(1, TimeUnit.MILLISECONDS, executor) .subscribe(ts); for (int i = 0; i < 5000; i++) { if (!sp.hasSubscribers()) { return; } Thread.sleep(10); } throw new AssertionError("Did not cancel the main source!"); }