Search in sources :

Example 61 with Observer

use of io.reactivex.rxjava3.core.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) {
                    }

                    @Override
                    public void reset() {
                    // nothing to do in this test
                    }

                    @SuppressWarnings("unchecked")
                    @Override
                    protected void subscribeActual(Observer observer) {
                        observer.onSubscribe(Disposable.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.rxjava3.core.Observer) ConnectableObservable(io.reactivex.rxjava3.observables.ConnectableObservable) Test(org.junit.Test)

Example 62 with Observer

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

the class MaybeTest method onTerminateDetach.

@Test
public void onTerminateDetach() throws Exception {
    System.gc();
    Thread.sleep(150);
    long before = usedMemoryNow();
    Maybe<Object> source = Flowable.just((Object) new Object[10000000]).singleElement();
    long middle = usedMemoryNow();
    MaybeObserver<Object> observer = new MaybeObserver<Object>() {

        @SuppressWarnings("unused")
        Disposable u;

        @Override
        public void onSubscribe(Disposable d) {
            this.u = d;
        }

        @Override
        public void onSuccess(Object value) {
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onComplete() {
        }
    };
    source.onTerminateDetach().subscribe(observer);
    source = null;
    System.gc();
    Thread.sleep(250);
    long after = usedMemoryNow();
    String log = String.format("%.2f MB -> %.2f MB -> %.2f MB%n", before / 1024.0 / 1024.0, middle / 1024.0 / 1024.0, after / 1024.0 / 1024.0);
    System.out.printf(log);
    if (before * 1.3 < after) {
        fail("There seems to be a memory leak: " + log);
    }
    // hold onto the reference to prevent premature GC
    assertNotNull(observer);
}
Also used : ArgsToString(io.reactivex.rxjava3.internal.operators.flowable.FlowableZipTest.ArgsToString) Test(org.junit.Test)

Example 63 with Observer

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

the class TestObserverTest method errorDelegateThrows.

@Test
public void errorDelegateThrows() {
    TestObserver<Integer> to = new TestObserver<>(new Observer<Integer>() {

        @Override
        public void onSubscribe(Disposable d) {
        }

        @Override
        public void onNext(Integer value) {
        }

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

        @Override
        public void onComplete() {
            throw new TestException();
        }
    });
    to.onSubscribe(Disposable.empty());
    try {
        to.onError(new IOException());
        throw new RuntimeException("Should have thrown!");
    } catch (TestException ex) {
        to.assertNotComplete().assertError(Throwable.class);
    }
}
Also used : ScalarDisposable(io.reactivex.rxjava3.internal.operators.observable.ObservableScalarXMap.ScalarDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Example 64 with Observer

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

the class CompletableSubscribeOn method subscribeActual.

@Override
protected void subscribeActual(final CompletableObserver observer) {
    final SubscribeOnObserver parent = new SubscribeOnObserver(observer, source);
    observer.onSubscribe(parent);
    Disposable f = scheduler.scheduleDirect(parent);
    parent.task.replace(f);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable)

Example 65 with Observer

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

the class CompletableMergeArrayDelayError method subscribeActual.

@Override
public void subscribeActual(final CompletableObserver observer) {
    final CompositeDisposable set = new CompositeDisposable();
    final AtomicInteger wip = new AtomicInteger(sources.length + 1);
    final AtomicThrowable errors = new AtomicThrowable();
    set.add(new TryTerminateAndReportDisposable(errors));
    observer.onSubscribe(set);
    for (CompletableSource c : sources) {
        if (set.isDisposed()) {
            return;
        }
        if (c == null) {
            Throwable ex = new NullPointerException("A completable source is null");
            errors.tryAddThrowableOrReport(ex);
            wip.decrementAndGet();
            continue;
        }
        c.subscribe(new MergeInnerCompletableObserver(observer, set, errors, wip));
    }
    if (wip.decrementAndGet() == 0) {
        errors.tryTerminateConsumer(observer);
    }
}
Also used : AtomicThrowable(io.reactivex.rxjava3.internal.util.AtomicThrowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicThrowable(io.reactivex.rxjava3.internal.util.AtomicThrowable)

Aggregations

Test (org.junit.Test)139 TestException (io.reactivex.rxjava3.exceptions.TestException)88 InOrder (org.mockito.InOrder)75 Observable (io.reactivex.rxjava3.core.Observable)49 Disposable (io.reactivex.rxjava3.disposables.Disposable)49 TestObserver (io.reactivex.rxjava3.observers.TestObserver)37 IOException (java.io.IOException)29 Observer (io.reactivex.rxjava3.core.Observer)23 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)11 Function (io.reactivex.rxjava3.functions.Function)10 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)6 ConnectableObservable (io.reactivex.rxjava3.observables.ConnectableObservable)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)5 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)5 io.reactivex.rxjava3.subjects (io.reactivex.rxjava3.subjects)5 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5