use of io.reactivex.rxjava3.disposables.CompositeDisposable in project RxJava by ReactiveX.
the class CompositeDisposableTest method success.
@Test
public void success() {
final AtomicInteger counter = new AtomicInteger();
CompositeDisposable cd = new CompositeDisposable();
cd.add(Disposable.fromRunnable(new Runnable() {
@Override
public void run() {
counter.incrementAndGet();
}
}));
cd.add(Disposable.fromRunnable(new Runnable() {
@Override
public void run() {
counter.incrementAndGet();
}
}));
cd.dispose();
assertEquals(2, counter.get());
}
use of io.reactivex.rxjava3.disposables.CompositeDisposable in project RxJava by ReactiveX.
the class NewThreadSchedulerTest method shutdownRejects.
@Test
@SuppressUndeliverable
public void shutdownRejects() {
final int[] calls = { 0 };
Runnable r = new Runnable() {
@Override
public void run() {
calls[0]++;
}
};
Scheduler s = getScheduler();
Worker w = s.createWorker();
w.dispose();
assertTrue(w.isDisposed());
assertEquals(Disposable.disposed(), w.schedule(r));
assertEquals(Disposable.disposed(), w.schedule(r, 1, TimeUnit.SECONDS));
assertEquals(Disposable.disposed(), w.schedulePeriodically(r, 1, 1, TimeUnit.SECONDS));
NewThreadWorker actual = (NewThreadWorker) w;
CompositeDisposable cd = new CompositeDisposable();
actual.scheduleActual(r, 1, TimeUnit.SECONDS, cd);
assertEquals(0, cd.size());
assertEquals(0, calls[0]);
}
use of io.reactivex.rxjava3.disposables.CompositeDisposable in project RxJava by ReactiveX.
the class TrampolineSchedulerTest method nestedTrampolineWithUnsubscribe.
@Test
public void nestedTrampolineWithUnsubscribe() {
final ArrayList<String> workDone = new ArrayList<>();
final CompositeDisposable workers = new CompositeDisposable();
Worker worker = Schedulers.trampoline().createWorker();
try {
workers.add(worker);
worker.schedule(new Runnable() {
@Override
public void run() {
workers.add(doWorkOnNewTrampoline("A", workDone));
}
});
final Worker worker2 = Schedulers.trampoline().createWorker();
workers.add(worker2);
worker2.schedule(new Runnable() {
@Override
public void run() {
workers.add(doWorkOnNewTrampoline("B", workDone));
// we unsubscribe worker2 ... it should not affect work scheduled on a separate Trampline.Worker
worker2.dispose();
}
});
assertEquals(6, workDone.size());
assertEquals(Arrays.asList("A.1", "A.B.1", "A.B.2", "B.1", "B.B.1", "B.B.2"), workDone);
} finally {
workers.dispose();
}
}
use of io.reactivex.rxjava3.disposables.CompositeDisposable in project RxJava by ReactiveX.
the class SchedulerLifecycleTest method tryOutSchedulers.
private void tryOutSchedulers() throws InterruptedException {
final CountDownLatch cdl = new CountDownLatch(4);
final Runnable countAction = new Runnable() {
@Override
public void run() {
cdl.countDown();
}
};
CompositeDisposable cd = new CompositeDisposable();
try {
Worker w1 = Schedulers.computation().createWorker();
cd.add(w1);
w1.schedule(countAction);
Worker w2 = Schedulers.io().createWorker();
cd.add(w2);
w2.schedule(countAction);
Worker w3 = Schedulers.newThread().createWorker();
cd.add(w3);
w3.schedule(countAction);
Worker w4 = Schedulers.single().createWorker();
cd.add(w4);
w4.schedule(countAction);
if (!cdl.await(3, TimeUnit.SECONDS)) {
fail("countAction was not run by every worker");
}
} finally {
cd.dispose();
}
}
use of io.reactivex.rxjava3.disposables.CompositeDisposable in project Signal-Android by signalapp.
the class SignalWebSocket method getWebSocket.
private synchronized WebSocketConnection getWebSocket() throws WebSocketUnavailableException {
if (!canConnect) {
throw new WebSocketUnavailableException();
}
if (webSocket == null || webSocket.isDead()) {
webSocketStateDisposable.dispose();
webSocket = webSocketFactory.createWebSocket();
webSocketStateDisposable = new CompositeDisposable();
Disposable state = webSocket.connect().subscribeOn(Schedulers.computation()).observeOn(Schedulers.computation()).subscribe(webSocketState::onNext);
webSocketStateDisposable.add(state);
}
return webSocket;
}
Aggregations