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();
}
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();
}
}
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();
}
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);
}
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();
}
}
Aggregations