@Test public void bufferWithBOEmptyLastViaBoundary() { PublishSubject<Integer> source = PublishSubject.create(); PublishSubject<Integer> boundary = PublishSubject.create(); Observer<Object> o = TestHelper.mockObserver(); InOrder inOrder = Mockito.inOrder(o); source.buffer(boundary).subscribe(o); boundary.onComplete(); inOrder.verify(o, times(1)).onNext(Arrays.asList()); inOrder.verify(o).onComplete(); verify(o, never()).onError(any(Throwable.class)); }
@Test public void bufferWithBOEmptyLastViaSource() { PublishSubject<Integer> source = PublishSubject.create(); PublishSubject<Integer> boundary = PublishSubject.create(); Observer<Object> o = TestHelper.mockObserver(); InOrder inOrder = Mockito.inOrder(o); source.buffer(boundary).subscribe(o); source.onComplete(); inOrder.verify(o, times(1)).onNext(Arrays.asList()); inOrder.verify(o).onComplete(); verify(o, never()).onError(any(Throwable.class)); }
@Test public void bufferWithBOEmptyLastViaBoth() { PublishSubject<Integer> source = PublishSubject.create(); PublishSubject<Integer> boundary = PublishSubject.create(); Observer<Object> o = TestHelper.mockObserver(); InOrder inOrder = Mockito.inOrder(o); source.buffer(boundary).subscribe(o); source.onComplete(); boundary.onComplete(); inOrder.verify(o, times(1)).onNext(Arrays.asList()); inOrder.verify(o).onComplete(); verify(o, never()).onError(any(Throwable.class)); }
@Test public void bufferWithBOSourceThrows() { PublishSubject<Integer> source = PublishSubject.create(); PublishSubject<Integer> boundary = PublishSubject.create(); Observer<Object> o = TestHelper.mockObserver(); source.buffer(boundary).subscribe(o); source.onNext(1); source.onError(new TestException()); verify(o).onError(any(TestException.class)); verify(o, never()).onComplete(); verify(o, never()).onNext(any()); }
@Test public void bufferWithBOBoundaryThrows() { PublishSubject<Integer> source = PublishSubject.create(); PublishSubject<Integer> boundary = PublishSubject.create(); Observer<Object> o = TestHelper.mockObserver(); source.buffer(boundary).subscribe(o); source.onNext(1); boundary.onError(new TestException()); verify(o).onError(any(TestException.class)); verify(o, never()).onComplete(); verify(o, never()).onNext(any()); }
@Test public void bufferWithSizeThrows() { PublishSubject<Integer> source = PublishSubject.create(); Observable<List<Integer>> result = source.buffer(2); Observer<Object> o = TestHelper.mockObserver(); InOrder inOrder = inOrder(o); result.subscribe(o); source.onNext(1); source.onNext(2); source.onNext(3); source.onError(new TestException()); inOrder.verify(o).onNext(Arrays.asList(1, 2)); inOrder.verify(o).onError(any(TestException.class)); inOrder.verifyNoMoreInteractions(); verify(o, never()).onNext(Arrays.asList(3)); verify(o, never()).onComplete(); }
@Test public void bufferWithStartEndEndFunctionThrows() { PublishSubject<Integer> start = PublishSubject.create(); Function<Integer, Observable<Integer>> end = new Function<Integer, Observable<Integer>>() { @Override public Observable<Integer> apply(Integer t1) { throw new TestException(); } }; PublishSubject<Integer> source = PublishSubject.create(); Observable<List<Integer>> result = source.buffer(start, end); Observer<Object> o = TestHelper.mockObserver(); result.subscribe(o); start.onNext(1); source.onNext(1); source.onNext(2); verify(o, never()).onNext(any()); verify(o, never()).onComplete(); verify(o).onError(any(TestException.class)); }
@Test public void bufferWithStartEndEndThrows() { PublishSubject<Integer> start = PublishSubject.create(); Function<Integer, Observable<Integer>> end = new Function<Integer, Observable<Integer>>() { @Override public Observable<Integer> apply(Integer t1) { return Observable.error(new TestException()); } }; PublishSubject<Integer> source = PublishSubject.create(); Observable<List<Integer>> result = source.buffer(start, end); Observer<Object> o = TestHelper.mockObserver(); result.subscribe(o); start.onNext(1); source.onNext(1); source.onNext(2); verify(o, never()).onNext(any()); verify(o, never()).onComplete(); verify(o).onError(any(TestException.class)); }
@Test public void bufferWithTimeThrows() { PublishSubject<Integer> source = PublishSubject.create(); Observable<List<Integer>> result = source.buffer(100, TimeUnit.MILLISECONDS, scheduler); Observer<Object> o = TestHelper.mockObserver(); InOrder inOrder = inOrder(o); result.subscribe(o); source.onNext(1); source.onNext(2); scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); source.onNext(3); source.onError(new TestException()); scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); inOrder.verify(o).onNext(Arrays.asList(1, 2)); inOrder.verify(o).onError(any(TestException.class)); inOrder.verifyNoMoreInteractions(); verify(o, never()).onNext(Arrays.asList(3)); verify(o, never()).onComplete(); }
@Test public void bufferWithStartEndStartThrows() { PublishSubject<Integer> start = PublishSubject.create(); Function<Integer, Observable<Integer>> end = new Function<Integer, Observable<Integer>>() { @Override public Observable<Integer> apply(Integer t1) { return Observable.never(); } }; PublishSubject<Integer> source = PublishSubject.create(); Observable<List<Integer>> result = source.buffer(start, end); Observer<Object> o = TestHelper.mockObserver(); result.subscribe(o); start.onNext(1); source.onNext(1); source.onNext(2); start.onError(new TestException()); verify(o, never()).onNext(any()); verify(o, never()).onComplete(); verify(o).onError(any(TestException.class)); }
@SuppressWarnings("unchecked") @Test public void bufferExactBoundarySecondBufferCrash() { PublishSubject<Integer> ps = PublishSubject.create(); PublishSubject<Integer> b = PublishSubject.create(); TestObserver<List<Integer>> to = ps.buffer(b, new Callable<List<Integer>>() { int calls; @Override public List<Integer> call() throws Exception { if (++calls == 2) { throw new TestException(); } return new ArrayList<Integer>(); } }).test(); b.onNext(1); to.assertFailure(TestException.class); }
@SuppressWarnings("unchecked") @Test public void bufferTimedExactBoundedError() { TestScheduler scheduler = new TestScheduler(); PublishSubject<Integer> ps = PublishSubject.create(); TestObserver<List<Integer>> to = ps .buffer(1, TimeUnit.MILLISECONDS, scheduler, 1, Functions.<Integer>createArrayList(16), true) .test(); ps.onError(new TestException()); to .assertFailure(TestException.class); }
@SuppressWarnings("unchecked") @Test public void boundaryMainError() { PublishSubject<Object> ps = PublishSubject.create(); TestObserver<Collection<Object>> to = ps .buffer(Functions.justCallable(Observable.never()), new Callable<Collection<Object>>() { @Override public Collection<Object> call() throws Exception { return new ArrayList<Object>(); } }) .test(); ps.onError(new TestException()); to.assertFailure(TestException.class); }
@SuppressWarnings("unchecked") @Test public void boundaryBoundaryError() { PublishSubject<Object> ps = PublishSubject.create(); TestObserver<Collection<Object>> to = ps .buffer(Functions.justCallable(Observable.error(new TestException())), new Callable<Collection<Object>>() { @Override public Collection<Object> call() throws Exception { return new ArrayList<Object>(); } }) .test(); ps.onError(new TestException()); to.assertFailure(TestException.class); }
@Test public void boundaryCancel() { PublishSubject<Object> ps = PublishSubject.create(); TestObserver<Collection<Object>> to = ps .buffer(Functions.justCallable(Observable.never()), new Callable<Collection<Object>>() { @Override public Collection<Object> call() throws Exception { return new ArrayList<Object>(); } }) .test(); assertTrue(ps.hasObservers()); to.dispose(); assertFalse(ps.hasObservers()); }
@Test @SuppressWarnings("unchecked") public void openCloseTake() { PublishSubject<Integer> source = PublishSubject.create(); PublishSubject<Integer> openIndicator = PublishSubject.create(); PublishSubject<Integer> closeIndicator = PublishSubject.create(); TestObserver<List<Integer>> to = source .buffer(openIndicator, Functions.justFunction(closeIndicator)) .take(1) .test(); openIndicator.onNext(1); closeIndicator.onComplete(); assertFalse(source.hasObservers()); assertFalse(openIndicator.hasObservers()); assertFalse(closeIndicator.hasObservers()); to.assertResult(Collections.<Integer>emptyList()); }
@Test public void dispose() { TestHelper.checkDisposed(Observable.range(1, 5).buffer(1, TimeUnit.DAYS, Schedulers.single())); TestHelper.checkDisposed(Observable.range(1, 5).buffer(2, 1, TimeUnit.DAYS, Schedulers.single())); TestHelper.checkDisposed(Observable.range(1, 5).buffer(1, 2, TimeUnit.DAYS, Schedulers.single())); TestHelper.checkDisposed(Observable.range(1, 5) .buffer(1, TimeUnit.DAYS, Schedulers.single(), 2, Functions.<Integer>createArrayList(16), true)); TestHelper.checkDisposed(Observable.range(1, 5).buffer(1)); TestHelper.checkDisposed(Observable.range(1, 5).buffer(2, 1)); TestHelper.checkDisposed(Observable.range(1, 5).buffer(1, 2)); TestHelper.checkDisposed(PublishSubject.create().buffer(Observable.never())); TestHelper.checkDisposed(PublishSubject.create().buffer(Functions.justCallable(Observable.never()))); TestHelper.checkDisposed(PublishSubject.create().buffer(Observable.never(), Functions.justFunction(Observable.never()))); }
@Test @SuppressWarnings("unchecked") public void openCloseOpenCompletes() { PublishSubject<Integer> source = PublishSubject.create(); PublishSubject<Integer> openIndicator = PublishSubject.create(); PublishSubject<Integer> closeIndicator = PublishSubject.create(); TestObserver<List<Integer>> to = source .buffer(openIndicator, Functions.justFunction(closeIndicator)) .test(); openIndicator.onNext(1); assertTrue(closeIndicator.hasObservers()); openIndicator.onComplete(); assertTrue(source.hasObservers()); assertTrue(closeIndicator.hasObservers()); closeIndicator.onComplete(); assertFalse(source.hasObservers()); to.assertResult(Collections.<Integer>emptyList()); }
@Test @SuppressWarnings("unchecked") public void openCloseOpenCompletesNoBuffers() { PublishSubject<Integer> source = PublishSubject.create(); PublishSubject<Integer> openIndicator = PublishSubject.create(); PublishSubject<Integer> closeIndicator = PublishSubject.create(); TestObserver<List<Integer>> to = source .buffer(openIndicator, Functions.justFunction(closeIndicator)) .test(); openIndicator.onNext(1); assertTrue(closeIndicator.hasObservers()); closeIndicator.onComplete(); assertTrue(source.hasObservers()); assertTrue(openIndicator.hasObservers()); openIndicator.onComplete(); assertFalse(source.hasObservers()); to.assertResult(Collections.<Integer>emptyList()); }
@Test @SuppressWarnings("unchecked") public void boundaryOpenCloseDisposedOnComplete() { PublishSubject<Integer> source = PublishSubject.create(); PublishSubject<Integer> openIndicator = PublishSubject.create(); PublishSubject<Integer> closeIndicator = PublishSubject.create(); TestObserver<List<Integer>> to = source .buffer(openIndicator, Functions.justFunction(closeIndicator)) .test(); assertTrue(source.hasObservers()); assertTrue(openIndicator.hasObservers()); assertFalse(closeIndicator.hasObservers()); openIndicator.onNext(1); assertTrue(openIndicator.hasObservers()); assertTrue(closeIndicator.hasObservers()); source.onComplete(); to.assertResult(Collections.<Integer>emptyList()); assertFalse(openIndicator.hasObservers()); assertFalse(closeIndicator.hasObservers()); }