### Question:
MultiTimeout implements Multi<T> { @Override public void subscribe(Flow.Subscriber<? super T> subscriber) { TimeoutSubscriber<T> parent = new TimeoutSubscriber<>(subscriber, timeout, unit, executor, fallback); subscriber.onSubscribe(parent); parent.schedule(0L); source.subscribe(parent); } MultiTimeout(Multi<T> source, long timeout, TimeUnit unit,
                 ScheduledExecutorService executor, Flow.Publisher<T> fallback); @Override void subscribe(Flow.Subscriber<? super T> subscriber);  }### Answer:
@Test public void fallback() { TestSubscriber<Integer> ts = new TestSubscriber<>(Long.MAX_VALUE); Multi.<Integer>never() .timeout(1, TimeUnit.MILLISECONDS, executor, Multi.just(1)) .subscribe(ts); ts.awaitDone(5, TimeUnit.SECONDS) .assertResult(1); }
@Test public void fallback2() { TestSubscriber<Long> ts = new TestSubscriber<>(Long.MAX_VALUE); Multi.timer(1, TimeUnit.MINUTES, executor) .timeout(1, TimeUnit.MILLISECONDS, executor, Multi.just(1L)) .subscribe(ts); ts.awaitDone(5, TimeUnit.SECONDS) .assertResult(1L); }
@Test public void mainError() { TestSubscriber<Long> ts = new TestSubscriber<>(Long.MAX_VALUE); Multi.<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); Multi.<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); Multi.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!"); }
@Test public void fallbackBackpressure() throws Exception { TestSubscriber<Long> ts = new TestSubscriber<>(1L); SubmissionPublisher<Long> sp = new SubmissionPublisher<>(Runnable::run, 128); SubmissionPublisher<Long> sp2 = new SubmissionPublisher<>(Runnable::run, 128); Multi.create(sp) .timeout(100, TimeUnit.MILLISECONDS, executor, sp2) .subscribe(ts); sp.submit(1L); for (int i = 0; i < 5000; i++) { if (!sp.hasSubscribers()) { break; } Thread.sleep(10); } assertFalse(sp.hasSubscribers()); ts.assertValuesOnly(1L); assertTrue(sp2.hasSubscribers()); sp2.submit(2L); sp2.close(); ts.assertValuesOnly(1L); ts.request(1) .assertResult(1L, 2L); }