Search in sources :

Example 1 with ObservableEmitter

use of io.reactivex.rxjava3.core.ObservableEmitter in project RxJava by ReactiveX.

the class ObservableCreateTest method basicSerialized.

@Test
@SuppressUndeliverable
public void basicSerialized() {
    final Disposable d = Disposable.empty();
    Observable.<Integer>create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> e) throws Exception {
            e = e.serialize();
            e.setDisposable(d);
            e.onNext(1);
            e.onNext(2);
            e.onNext(3);
            e.onComplete();
            e.onError(new TestException());
            e.onNext(4);
            e.onError(new TestException());
            e.onComplete();
        }
    }).test().assertResult(1, 2, 3);
    assertTrue(d.isDisposed());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with ObservableEmitter

use of io.reactivex.rxjava3.core.ObservableEmitter in project RxJava by ReactiveX.

the class ObservableCreateTest method basicWithCancellable.

@Test
@SuppressUndeliverable
public void basicWithCancellable() {
    final Disposable d1 = Disposable.empty();
    final Disposable d2 = Disposable.empty();
    Observable.<Integer>create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> e) throws Exception {
            e.setDisposable(d1);
            e.setCancellable(new Cancellable() {

                @Override
                public void cancel() throws Exception {
                    d2.dispose();
                }
            });
            e.onNext(1);
            e.onNext(2);
            e.onNext(3);
            e.onComplete();
            e.onError(new TestException());
            e.onNext(4);
            e.onError(new TestException());
            e.onComplete();
        }
    }).test().assertResult(1, 2, 3);
    assertTrue(d1.isDisposed());
    assertTrue(d2.isDisposed());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) Cancellable(io.reactivex.rxjava3.functions.Cancellable) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with ObservableEmitter

use of io.reactivex.rxjava3.core.ObservableEmitter in project RxJava by ReactiveX.

the class ObservableCreateTest method onErrorRace.

@Test
public void onErrorRace() {
    Observable<Object> source = Observable.create(new ObservableOnSubscribe<Object>() {

        @Override
        public void subscribe(ObservableEmitter<Object> e) throws Exception {
            final ObservableEmitter<Object> f = e.serialize();
            final TestException ex = new TestException();
            Runnable r1 = new Runnable() {

                @Override
                public void run() {
                    f.onError(null);
                }
            };
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    f.onError(ex);
                }
            };
            TestHelper.race(r1, r2);
        }
    });
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
            source.test().assertFailure(Throwable.class);
        }
    } finally {
        RxJavaPlugins.reset();
    }
    assertFalse(errors.isEmpty());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with ObservableEmitter

use of io.reactivex.rxjava3.core.ObservableEmitter in project RxJava by ReactiveX.

the class ObservableCreateTest method serializedDrainDoneButNotEmpty.

@Test
public void serializedDrainDoneButNotEmpty() throws Throwable {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        TestObserver<Integer> to = new TestObserver<>();
        AtomicReference<ObservableEmitter<Integer>> ref = new AtomicReference<>();
        CountDownLatch cdl = new CountDownLatch(1);
        Observable.<Integer>create(emitter -> {
            emitter = emitter.serialize();
            ref.set(emitter);
            emitter.onNext(1);
        }).doOnNext(v -> {
            if (v == 1) {
                TestHelper.raceOther(() -> {
                    ref.get().onNext(2);
                    ref.get().onComplete();
                }, cdl);
                ref.get().onNext(3);
            }
        }).subscribe(to);
        cdl.await();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Cancellable(io.reactivex.rxjava3.functions.Cancellable) IOException(java.io.IOException) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Disposable(io.reactivex.rxjava3.disposables.Disposable) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Assert(org.junit.Assert) io.reactivex.rxjava3.testsupport(io.reactivex.rxjava3.testsupport) RxJavaPlugins(io.reactivex.rxjava3.plugins.RxJavaPlugins) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.Test)

Example 5 with ObservableEmitter

use of io.reactivex.rxjava3.core.ObservableEmitter in project RxJava by ReactiveX.

the class ObservableCreateTest method onErrorCrash.

@Test
public void onErrorCrash() {
    Observable.create(new ObservableOnSubscribe<Object>() {

        @Override
        public void subscribe(ObservableEmitter<Object> e) throws Exception {
            Disposable d = Disposable.empty();
            e.setDisposable(d);
            try {
                e.onError(new IOException());
                fail("Should have thrown");
            } catch (TestException ex) {
            // expected
            }
            assertTrue(d.isDisposed());
        }
    }).subscribe(new Observer<Object>() {

        @Override
        public void onSubscribe(Disposable d) {
        }

        @Override
        public void onNext(Object value) {
        }

        @Override
        public void onError(Throwable e) {
            throw new TestException();
        }

        @Override
        public void onComplete() {
        }
    });
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

TestException (io.reactivex.rxjava3.exceptions.TestException)11 Disposable (io.reactivex.rxjava3.disposables.Disposable)10 Test (org.junit.Test)10 IOException (java.io.IOException)9 ObservableEmitter (io.reactivex.rxjava3.core.ObservableEmitter)3 Cancellable (io.reactivex.rxjava3.functions.Cancellable)3 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)2 TestObserver (io.reactivex.rxjava3.observers.TestObserver)2 RxJavaPlugins (io.reactivex.rxjava3.plugins.RxJavaPlugins)2 io.reactivex.rxjava3.testsupport (io.reactivex.rxjava3.testsupport)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Assert (org.junit.Assert)2 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 View (android.view.View)1 ArrayAdapter (android.widget.ArrayAdapter)1 EditText (android.widget.EditText)1 ListView (android.widget.ListView)1