Search in sources :

Example 11 with Observer

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

the class ObservableRepeatTest method testRepeatTakeWithSubscribeOn.

@Test
public void testRepeatTakeWithSubscribeOn() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    Observable<Integer> oi = Observable.unsafeCreate(new ObservableSource<Integer>() {

        @Override
        public void subscribe(Observer<? super Integer> sub) {
            sub.onSubscribe(Disposables.empty());
            counter.incrementAndGet();
            sub.onNext(1);
            sub.onNext(2);
            sub.onComplete();
        }
    }).subscribeOn(Schedulers.newThread());
    Object[] ys = oi.repeat().subscribeOn(Schedulers.newThread()).map(new Function<Integer, Integer>() {

        @Override
        public Integer apply(Integer t1) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return t1;
        }
    }).take(4).toList().blockingGet().toArray();
    assertEquals(2, counter.get());
    assertArrayEquals(new Object[] { 1, 2, 1, 2 }, ys);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver) Observer(io.reactivex.Observer) Test(org.junit.Test)

Example 12 with Observer

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

the class ObservableObserveOnTest method nonFusedPollThrows.

@Test
public void nonFusedPollThrows() {
    new Observable<Integer>() {

        @Override
        protected void subscribeActual(Observer<? super Integer> observer) {
            observer.onSubscribe(Disposables.empty());
            @SuppressWarnings("unchecked") ObserveOnObserver<Integer> oo = (ObserveOnObserver<Integer>) observer;
            oo.queue = new SimpleQueue<Integer>() {

                @Override
                public boolean offer(Integer value) {
                    return false;
                }

                @Override
                public boolean offer(Integer v1, Integer v2) {
                    return false;
                }

                @Nullable
                @Override
                public Integer poll() throws Exception {
                    throw new TestException();
                }

                @Override
                public boolean isEmpty() {
                    return false;
                }

                @Override
                public void clear() {
                }
            };
            oo.clear();
            oo.schedule();
        }
    }.observeOn(Schedulers.single()).test().awaitDone(5, TimeUnit.SECONDS).assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.exceptions.TestException) ObserveOnObserver(io.reactivex.internal.operators.observable.ObservableObserveOn.ObserveOnObserver) Observer(io.reactivex.Observer) ObserveOnObserver(io.reactivex.internal.operators.observable.ObservableObserveOn.ObserveOnObserver) Observable(io.reactivex.Observable) Nullable(io.reactivex.annotations.Nullable) TestException(io.reactivex.exceptions.TestException) Test(org.junit.Test)

Example 13 with Observer

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

the class ObservableObserveOnTest method badSource.

@Test
public void badSource() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        TestScheduler scheduler = new TestScheduler();
        TestObserver<Integer> to = new Observable<Integer>() {

            @Override
            protected void subscribeActual(Observer<? super Integer> observer) {
                observer.onSubscribe(Disposables.empty());
                observer.onComplete();
                observer.onNext(1);
                observer.onError(new TestException());
                observer.onComplete();
            }
        }.observeOn(scheduler).test();
        scheduler.triggerActions();
        to.assertResult();
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.exceptions.TestException) ObserveOnObserver(io.reactivex.internal.operators.observable.ObservableObserveOn.ObserveOnObserver) Observer(io.reactivex.Observer) Observable(io.reactivex.Observable) Test(org.junit.Test)

Example 14 with Observer

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

the class ObservableElementAtTest method badSourceObservable.

@Test
public void badSourceObservable() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        new Observable<Integer>() {

            @Override
            protected void subscribeActual(Observer<? super Integer> observer) {
                observer.onSubscribe(Disposables.empty());
                observer.onNext(1);
                observer.onNext(2);
                observer.onError(new TestException());
                observer.onComplete();
            }
        }.elementAt(0).toObservable().test().assertResult(1);
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.exceptions.TestException) Observer(io.reactivex.Observer) Observable(io.reactivex.Observable) Test(org.junit.Test)

Example 15 with Observer

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

the class RxJavaPluginsTest method overrideConnectableObservable.

@SuppressWarnings("rawtypes")
@Test
public void overrideConnectableObservable() {
    try {
        RxJavaPlugins.setOnConnectableObservableAssembly(new Function<ConnectableObservable, ConnectableObservable>() {

            @Override
            public ConnectableObservable apply(ConnectableObservable co) throws Exception {
                return new ConnectableObservable() {

                    @Override
                    public void connect(Consumer connection) {
                    }

                    @SuppressWarnings("unchecked")
                    @Override
                    protected void subscribeActual(Observer observer) {
                        observer.onSubscribe(Disposables.empty());
                        observer.onNext(10);
                        observer.onComplete();
                    }
                };
            }
        });
        Observable.just(1).publish().autoConnect().test().assertResult(10);
    } finally {
        RxJavaPlugins.reset();
    }
    Observable.just(1).publish().autoConnect().test().assertResult(1);
}
Also used : Observer(io.reactivex.Observer) ConnectableObservable(io.reactivex.observables.ConnectableObservable)

Aggregations

Observer (io.reactivex.Observer)18 Observable (io.reactivex.Observable)12 Test (org.junit.Test)9 TestException (io.reactivex.exceptions.TestException)8 TestObserver (io.reactivex.observers.TestObserver)6 ConnectableObservable (io.reactivex.observables.ConnectableObservable)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Bundle (android.os.Bundle)2 Log (android.util.Log)2 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)2 Disposable (io.reactivex.disposables.Disposable)2 ObserveOnObserver (io.reactivex.internal.operators.observable.ObservableObserveOn.ObserveOnObserver)2 Schedulers (io.reactivex.schedulers.Schedulers)2 TimeUnit (java.util.concurrent.TimeUnit)2 Inject (javax.inject.Inject)2 UserPrivate (kaaes.spotify.webapi.android.models.UserPrivate)2 Callback (retrofit.Callback)2 RetrofitError (retrofit.RetrofitError)2 Response (retrofit.client.Response)2 ApplicationConstants (se.zinokader.spotiq.constant.ApplicationConstants)2