Search in sources :

Example 61 with Observable

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

the class ConverterTest method observableConverterThrows.

@Test
public void observableConverterThrows() {
    try {
        Observable.just(1).to(new ObservableConverter<Integer, Integer>() {

            @Override
            public Integer apply(Observable<Integer> v) {
                throw new TestException("Forced failure");
            }
        });
        fail("Should have thrown!");
    } catch (TestException ex) {
        assertEquals("Forced failure", ex.getMessage());
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 62 with Observable

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

the class FlowableSwitchIfEmptyTest method requestsNotLost.

@Test
public void requestsNotLost() throws InterruptedException {
    final TestSubscriber<Long> ts = new TestSubscriber<>(0L);
    Flowable.unsafeCreate(new Publisher<Long>() {

        @Override
        public void subscribe(final Subscriber<? super Long> subscriber) {
            subscriber.onSubscribe(new Subscription() {

                final AtomicBoolean completed = new AtomicBoolean(false);

                @Override
                public void request(long n) {
                    if (n > 0 && completed.compareAndSet(false, true)) {
                        Schedulers.io().createWorker().schedule(new Runnable() {

                            @Override
                            public void run() {
                                subscriber.onComplete();
                            }
                        }, 100, TimeUnit.MILLISECONDS);
                    }
                }

                @Override
                public void cancel() {
                }
            });
        }
    }).switchIfEmpty(Flowable.fromIterable(Arrays.asList(1L, 2L, 3L))).subscribeOn(Schedulers.computation()).subscribe(ts);
    Thread.sleep(50);
    // request while first observable is still finishing (as empty)
    ts.request(1);
    ts.request(1);
    Thread.sleep(500);
    ts.assertNotComplete();
    ts.assertNoErrors();
    ts.assertValueCount(2);
    ts.cancel();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 63 with Observable

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

the class RxJavaPluginsTest method observableCreate.

@SuppressWarnings("rawtypes")
@Test
public void observableCreate() {
    try {
        RxJavaPlugins.setOnObservableAssembly(new Function<Observable, Observable>() {

            @Override
            public Observable apply(Observable t) {
                return new ObservableRange(1, 2);
            }
        });
        Observable.range(10, 3).test().assertValues(1, 2).assertNoErrors().assertComplete();
    } finally {
        RxJavaPlugins.reset();
    }
    // make sure the reset worked
    Observable.range(10, 3).test().assertValues(10, 11, 12).assertNoErrors().assertComplete();
}
Also used : ObservableRange(io.reactivex.rxjava3.internal.operators.observable.ObservableRange) Observable(io.reactivex.rxjava3.core.Observable) ConnectableObservable(io.reactivex.rxjava3.observables.ConnectableObservable) Test(org.junit.Test)

Example 64 with Observable

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

the class ConnectableObservable method connect.

/**
 * Instructs the {@code ConnectableObservable} to begin emitting the items from its underlying
 * {@link Observable} to its {@link Observer}s.
 * <p>
 * To disconnect from a synchronous source, use the {@link #connect(Consumer)} method.
 * <dl>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>The behavior is determined by the implementor of this abstract class.</dd>
 * </dl>
 *
 * @return the {@link Disposable} representing the connection
 * @see <a href="http://reactivex.io/documentation/operators/connect.html">ReactiveX documentation: Connect</a>
 */
@NonNull
@SchedulerSupport(SchedulerSupport.NONE)
public final Disposable connect() {
    ConnectConsumer cc = new ConnectConsumer();
    connect(cc);
    return cc.disposable;
}
Also used : ConnectConsumer(io.reactivex.rxjava3.internal.util.ConnectConsumer)

Example 65 with Observable

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

the class ObservableAllTest method predicateThrows.

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

            @Override
            protected void subscribeActual(Observer<? super Integer> observer) {
                observer.onSubscribe(Disposable.empty());
                observer.onNext(1);
                observer.onNext(2);
                observer.onError(new TestException());
                observer.onComplete();
            }
        }.all(new Predicate<Integer>() {

            @Override
            public boolean test(Integer v) throws Exception {
                throw new TestException();
            }
        }).test().assertFailure(TestException.class);
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)132 TestException (io.reactivex.rxjava3.exceptions.TestException)109 Observable (io.reactivex.rxjava3.core.Observable)83 InOrder (org.mockito.InOrder)60 TestObserver (io.reactivex.rxjava3.observers.TestObserver)38 Disposable (io.reactivex.rxjava3.disposables.Disposable)35 IOException (java.io.IOException)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)25 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)22 Observer (io.reactivex.rxjava3.core.Observer)19 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)17 Function (io.reactivex.rxjava3.functions.Function)10 List (java.util.List)8 TimeUnit (java.util.concurrent.TimeUnit)8 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)7 ConnectableObservable (io.reactivex.rxjava3.observables.ConnectableObservable)6 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)6 Collections (java.util.Collections)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Assert (org.junit.Assert)6