Search in sources :

Example 96 with Observable

use of io.reactivex.Observable in project RxJava by ReactiveX.

the class ObservableUsingTest method performTestUsing.

private void performTestUsing(boolean disposeEagerly) {
    final Resource resource = mock(Resource.class);
    when(resource.getTextFromWeb()).thenReturn("Hello world!");
    Callable<Resource> resourceFactory = new Callable<Resource>() {

        @Override
        public Resource call() {
            return resource;
        }
    };
    Function<Resource, Observable<String>> observableFactory = new Function<Resource, Observable<String>>() {

        @Override
        public Observable<String> apply(Resource res) {
            return Observable.fromArray(res.getTextFromWeb().split(" "));
        }
    };
    Observer<String> observer = TestHelper.mockObserver();
    Observable<String> o = Observable.using(resourceFactory, observableFactory, new DisposeAction(), disposeEagerly);
    o.subscribe(observer);
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("Hello");
    inOrder.verify(observer, times(1)).onNext("world!");
    inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
    // The resouce should be closed
    verify(resource, times(1)).dispose();
}
Also used : InOrder(org.mockito.InOrder) Callable(java.util.concurrent.Callable) Observable(io.reactivex.Observable)

Example 97 with Observable

use of io.reactivex.Observable in project RxJava by ReactiveX.

the class ObservableWindowWithObservableTest method newBoundaryCalledAfterWindowClosed.

@Test
public void newBoundaryCalledAfterWindowClosed() {
    final AtomicInteger calls = new AtomicInteger();
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> boundary = PublishSubject.create();
    Callable<Observable<Integer>> boundaryFunc = new Callable<Observable<Integer>>() {

        @Override
        public Observable<Integer> call() {
            calls.getAndIncrement();
            return boundary;
        }
    };
    TestObserver<Observable<Integer>> ts = new TestObserver<Observable<Integer>>();
    source.window(boundaryFunc).subscribe(ts);
    source.onNext(1);
    boundary.onNext(1);
    assertTrue(boundary.hasObservers());
    source.onNext(2);
    boundary.onNext(2);
    assertTrue(boundary.hasObservers());
    source.onNext(3);
    boundary.onNext(3);
    assertTrue(boundary.hasObservers());
    source.onNext(4);
    source.onComplete();
    ts.assertNoErrors();
    ts.assertValueCount(4);
    ts.assertComplete();
    assertFalse(source.hasObservers());
    assertFalse(boundary.hasObservers());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observable(io.reactivex.Observable) Test(org.junit.Test)

Example 98 with Observable

use of io.reactivex.Observable in project RxJava by ReactiveX.

the class ObservableTimeoutWithSelectorTest method testTimeoutSelectorSubsequentObservableThrows.

@Test
public void testTimeoutSelectorSubsequentObservableThrows() {
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> timeout = PublishSubject.create();
    Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return Observable.<Integer>error(new TestException());
        }
    };
    Observable<Integer> other = Observable.fromIterable(Arrays.asList(100));
    Observer<Object> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    source.timeout(timeout, timeoutFunc, other).subscribe(o);
    source.onNext(1);
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onError(any(TestException.class));
    verify(o, never()).onComplete();
}
Also used : Function(io.reactivex.functions.Function) InOrder(org.mockito.InOrder) TestException(io.reactivex.exceptions.TestException) Observable(io.reactivex.Observable) Test(org.junit.Test)

Example 99 with Observable

use of io.reactivex.Observable in project RxJava by ReactiveX.

the class ObservableTimeoutWithSelectorTest method testTimeoutSelectorWithTimeoutFirstAndNoOtherObservable.

@Test
public void testTimeoutSelectorWithTimeoutFirstAndNoOtherObservable() {
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> timeout = PublishSubject.create();
    Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return timeout;
        }
    };
    Observer<Object> o = TestHelper.mockObserver();
    source.timeout(PublishSubject.create(), timeoutFunc).subscribe(o);
    source.onNext(1);
    timeout.onNext(1);
    InOrder inOrder = inOrder(o);
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onError(isA(TimeoutException.class));
    inOrder.verifyNoMoreInteractions();
}
Also used : Function(io.reactivex.functions.Function) InOrder(org.mockito.InOrder) Observable(io.reactivex.Observable) Test(org.junit.Test)

Example 100 with Observable

use of io.reactivex.Observable in project RxJava by ReactiveX.

the class ObservableTimeoutWithSelectorTest method testTimeoutSelectorSubsequentThrows.

@Test
public void testTimeoutSelectorSubsequentThrows() {
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> timeout = PublishSubject.create();
    Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            throw new TestException();
        }
    };
    Observable<Integer> other = Observable.fromIterable(Arrays.asList(100));
    Observer<Object> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    source.timeout(timeout, timeoutFunc, other).subscribe(o);
    source.onNext(1);
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onError(any(TestException.class));
    verify(o, never()).onComplete();
}
Also used : Function(io.reactivex.functions.Function) InOrder(org.mockito.InOrder) TestException(io.reactivex.exceptions.TestException) Observable(io.reactivex.Observable) Test(org.junit.Test)

Aggregations

Observable (io.reactivex.Observable)170 List (java.util.List)54 ArrayList (java.util.ArrayList)39 Test (org.junit.Test)36 Collectors (java.util.stream.Collectors)29 InOrder (org.mockito.InOrder)27 TestException (io.reactivex.exceptions.TestException)24 Page (io.nem.symbol.sdk.api.Page)19 Address (io.nem.symbol.sdk.model.account.Address)18 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)18 Disposable (io.reactivex.disposables.Disposable)18 Bundle (android.os.Bundle)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Function (io.reactivex.functions.Function)15 TestObserver (io.reactivex.observers.TestObserver)15 Schedulers (io.reactivex.schedulers.Schedulers)15 Nullable (android.support.annotation.Nullable)14 MerkleStateInfo (io.nem.symbol.sdk.model.blockchain.MerkleStateInfo)14 ApiClient (io.nem.symbol.sdk.openapi.vertx.invoker.ApiClient)14 AsyncResult (io.vertx.core.AsyncResult)14