@Test public void test() { TestSubscriber<Object> tester = new TestSubscriber<>(); TestScheduler scheduler = Schedulers.test(); Observable<Long> left = Observable.interval(100, TimeUnit.MILLISECONDS, scheduler).take(6); Observable<Long> right = Observable.interval(200, TimeUnit.MILLISECONDS, scheduler).take(3); left.groupJoin( right, i -> Observable.never(), i -> Observable.timer(0, TimeUnit.MILLISECONDS, scheduler), (l, rs) -> rs.toList().map(rl -> Tuple.create(l, rl))) .flatMap(i -> i) .subscribe(tester); scheduler.advanceTimeTo(600, TimeUnit.MILLISECONDS); tester.assertReceivedOnNext( Arrays.asList( Tuple.create(0L, Arrays.asList(0L, 1L, 2L)), Tuple.create(1L, Arrays.asList(0L, 1L, 2L)), Tuple.create(2L, Arrays.asList(1L, 2L)), Tuple.create(3L, Arrays.asList(1L, 2L)), Tuple.create(4L, Arrays.asList(2L)), Tuple.create(5L, Arrays.asList(2L)))); }
@Test public void cancelLoadingEvents() { when(repo.getModificationId()).thenReturn("modif id"); when(repo.getEventsSubscribedOnProperScheduler()).thenReturn(Observable.never()); sut.loadEvents(false, callback); sut.cancelLoadingEvents(); verify(callback).onEventsLoadingStarted("modif id"); verify(callback).onEventsLoadingCancelled("modif id"); }
@Test public void testErrorPropagatesWhenNoOutstandingRequests() { Observable<Long> timer = Observable.timer(0, 1, TimeUnit.MICROSECONDS) .doOnEach( new Action1<Notification<? super Long>>() { @Override public void call(Notification<? super Long> n) { // System.out.println("BEFORE " + // n); } }) .observeOn(Schedulers.newThread()) .doOnEach( new Action1<Notification<? super Long>>() { @Override public void call(Notification<? super Long> n) { try { Thread.sleep(100); } catch (InterruptedException e) { } // System.out.println("AFTER " + // n); } }); TestSubscriber<Long> ts = new TestSubscriber<Long>(); Observable.combineLatest( timer, Observable.<Integer>never(), new Func2<Long, Integer, Long>() { @Override public Long call(Long t1, Integer t2) { return t1; } }) .take(RxRingBuffer.SIZE * 2) .subscribe(ts); ts.awaitTerminalEvent(); assertEquals(1, ts.getOnErrorEvents().size()); assertEquals(MissingBackpressureException.class, ts.getOnErrorEvents().get(0).getClass()); }
public void example() { Observable<String> left = Observable.interval(100, TimeUnit.MILLISECONDS).map(i -> "L" + i).take(6); Observable<String> right = Observable.interval(200, TimeUnit.MILLISECONDS).map(i -> "R" + i).take(3); left.groupJoin( right, i -> Observable.never(), i -> Observable.timer(0, TimeUnit.MILLISECONDS), (l, rs) -> rs.toList().subscribe(list -> System.out.println(l + ": " + list))) .subscribe(); // L0: [R0, R1, R2] // L1: [R0, R1, R2] // L2: [R1, R2] // L3: [R1, R2] // L4: [R2] // L5: [R2] }
@Test public void ongoingLoadEventsShouldBeCancelledOnNextLoadEvents() { when(repo.getModificationId()).thenReturn("first modif id"); when(repo.getEventsSubscribedOnProperScheduler()).thenReturn(Observable.never()); sut.loadEvents(false, callback); verify(callback).onEventsLoadingStarted("first modif id"); verify(callback, never()).onEventsLoadingFinished(anyList(), eq("first modif id")); reset(callback, repo); // now we do a second loadEvents (the first one should now be cancelled) List<IEvent> events = Arrays.asList(mock(IEvent.class)); when(repo.getModificationId()).thenReturn("second modif id"); when(repo.getEventsSubscribedOnProperScheduler()).thenReturn(Observable.from(events)); sut.loadEvents(true, callback); verify(callback).onEventsLoadingCancelled("first modif id"); verify(callback).onEventsLoadingStarted("second modif id"); verify(callback).onEventsLoadingFinished(events, "second modif id"); verify(repo).getEventsSubscribedOnProperScheduler(); }
@Override public Observable<Void> getLifecycle() { return Observable.never(); }