Search in sources :

Example 96 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class FlowableTests method publishLast.

@Test
public void publishLast() throws InterruptedException {
    final AtomicInteger count = new AtomicInteger();
    ConnectableFlowable<String> connectable = Flowable.<String>unsafeCreate(new Publisher<String>() {

        @Override
        public void subscribe(final Subscriber<? super String> subscriber) {
            subscriber.onSubscribe(new BooleanSubscription());
            count.incrementAndGet();
            new Thread(new Runnable() {

                @Override
                public void run() {
                    subscriber.onNext("first");
                    subscriber.onNext("last");
                    subscriber.onComplete();
                }
            }).start();
        }
    }).takeLast(1).publish();
    // subscribe once
    final CountDownLatch latch = new CountDownLatch(1);
    connectable.subscribe(new Consumer<String>() {

        @Override
        public void accept(String value) {
            assertEquals("last", value);
            latch.countDown();
        }
    });
    // subscribe twice
    connectable.subscribe();
    Disposable subscription = connectable.connect();
    assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
    assertEquals(1, count.get());
    subscription.dispose();
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)

Example 97 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class FlowableTests method replay.

@Test
public void replay() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    ConnectableFlowable<String> f = Flowable.<String>unsafeCreate(new Publisher<String>() {

        @Override
        public void subscribe(final Subscriber<? super String> subscriber) {
            subscriber.onSubscribe(new BooleanSubscription());
            new Thread(new Runnable() {

                @Override
                public void run() {
                    counter.incrementAndGet();
                    subscriber.onNext("one");
                    subscriber.onComplete();
                }
            }).start();
        }
    }).replay();
    // we connect immediately and it will emit the value
    Disposable connection = f.connect();
    try {
        // we then expect the following 2 subscriptions to get that same value
        final CountDownLatch latch = new CountDownLatch(2);
        // subscribe once
        f.subscribe(new Consumer<String>() {

            @Override
            public void accept(String v) {
                assertEquals("one", v);
                latch.countDown();
            }
        });
        // subscribe again
        f.subscribe(new Consumer<String>() {

            @Override
            public void accept(String v) {
                assertEquals("one", v);
                latch.countDown();
            }
        });
        if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
            fail("subscriptions did not receive values");
        }
        assertEquals(1, counter.get());
    } finally {
        connection.dispose();
    }
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)

Example 98 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class DisposableTest method fromAction.

@Test
public void fromAction() throws Throwable {
    Action action = mock(Action.class);
    Disposable d = Disposable.fromAction(action);
    assertTrue(d.toString(), d.toString().contains("ActionDisposable(disposed=false, "));
    d.dispose();
    assertTrue(d.toString(), d.toString().contains("ActionDisposable(disposed=true, "));
    d.dispose();
    assertTrue(d.toString(), d.toString().contains("ActionDisposable(disposed=true, "));
    verify(action, times(1)).run();
}
Also used : Action(io.reactivex.rxjava3.functions.Action) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 99 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class SequentialDisposableTest method unsubscribingTwiceDoesUnsubscribeOnce.

@Test
public void unsubscribingTwiceDoesUnsubscribeOnce() {
    Disposable underlying = mock(Disposable.class);
    serialDisposable.update(underlying);
    serialDisposable.dispose();
    verify(underlying).dispose();
    serialDisposable.dispose();
    verifyNoMoreInteractions(underlying);
}
Also used : SequentialDisposable(io.reactivex.rxjava3.internal.disposables.SequentialDisposable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest)

Example 100 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class SequentialDisposableTest method concurrentSetDisposableShouldNotInterleave.

@Test
public void concurrentSetDisposableShouldNotInterleave() throws InterruptedException {
    final int count = 10;
    final List<Disposable> subscriptions = new ArrayList<>();
    final CountDownLatch start = new CountDownLatch(1);
    final CountDownLatch end = new CountDownLatch(count);
    final List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        final Disposable subscription = mock(Disposable.class);
        subscriptions.add(subscription);
        final Thread t = new Thread() {

            @Override
            public void run() {
                try {
                    start.await();
                    serialDisposable.update(subscription);
                } catch (InterruptedException e) {
                    fail(e.getMessage());
                } finally {
                    end.countDown();
                }
            }
        };
        t.start();
        threads.add(t);
    }
    start.countDown();
    end.await();
    serialDisposable.dispose();
    for (final Disposable subscription : subscriptions) {
        verify(subscription).dispose();
    }
    for (final Thread t : threads) {
        t.join();
    }
}
Also used : SequentialDisposable(io.reactivex.rxjava3.internal.disposables.SequentialDisposable) CountDownLatch(java.util.concurrent.CountDownLatch) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest)

Aggregations

Test (org.junit.Test)250 Disposable (io.reactivex.rxjava3.disposables.Disposable)219 TestException (io.reactivex.rxjava3.exceptions.TestException)74 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)56 IOException (java.io.IOException)37 Scheduler (io.reactivex.rxjava3.core.Scheduler)36 EmptyDisposable (io.reactivex.rxjava3.internal.disposables.EmptyDisposable)35 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)34 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)32 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 TestObserver (io.reactivex.rxjava3.observers.TestObserver)18 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)15 QueueDisposable (io.reactivex.rxjava3.operators.QueueDisposable)13 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)10 TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)9 Observable (io.reactivex.rxjava3.core.Observable)6 Observer (io.reactivex.rxjava3.core.Observer)6 ScalarDisposable (io.reactivex.rxjava3.internal.operators.observable.ObservableScalarXMap.ScalarDisposable)6 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)6