ObservableCache extends AbstractObservableWithUpstream<T, T> { public static <T> Observable<T> from(Observable<T> source) { return from(source, 16); } private  ObservableCache(Observable<T> source, CacheState<T> state); static Observable<T> from(Observable<T> source); static Observable<T> from(Observable<T> source, int capacityHint);  }
@Test public void testAsync() { Observable<Integer> source = Observable.range(1, 10000); for (int i = 0; i < 100; i++) { TestObserver<Integer> to1 = new TestObserver<Integer>(); ObservableCache<Integer> cached = (ObservableCache<Integer>)ObservableCache.from(source); cached.observeOn(Schedulers.computation()).subscribe(to1); to1.awaitTerminalEvent(2, TimeUnit.SECONDS); to1.assertNoErrors(); to1.assertComplete(); assertEquals(10000, to1.values().size()); TestObserver<Integer> to2 = new TestObserver<Integer>(); cached.observeOn(Schedulers.computation()).subscribe(to2); to2.awaitTerminalEvent(2, TimeUnit.SECONDS); to2.assertNoErrors(); to2.assertComplete(); assertEquals(10000, to2.values().size()); } }
@Test public void testAsyncComeAndGo() { Observable<Long> source = Observable.interval(1, 1, TimeUnit.MILLISECONDS) .take(1000) .subscribeOn(Schedulers.io()); ObservableCache<Long> cached = (ObservableCache<Long>)ObservableCache.from(source); Observable<Long> output = cached.observeOn(Schedulers.computation()); List<TestObserver<Long>> list = new ArrayList<TestObserver<Long>>(100); for (int i = 0; i < 100; i++) { TestObserver<Long> to = new TestObserver<Long>(); list.add(to); output.skip(i * 10).take(10).subscribe(to); } List<Long> expected = new ArrayList<Long>(); for (int i = 0; i < 10; i++) { expected.add((long)(i - 10)); } int j = 0; for (TestObserver<Long> to : list) { to.awaitTerminalEvent(3, TimeUnit.SECONDS); to.assertNoErrors(); to.assertComplete(); for (int i = j * 10; i < j * 10 + 10; i++) { expected.set(i - j * 10, (long)i); } to.assertValueSequence(expected); j++; } }