use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.
the class SerializedSubjectTest method normal.
@Test
public void normal() {
Subject<Integer> s = PublishSubject.<Integer>create().toSerialized();
TestObserver<Integer> to = s.test();
Observable.range(1, 10).subscribe(s);
to.assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
assertFalse(s.hasObservers());
s.onNext(11);
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
s.onError(new TestException());
TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
s.onComplete();
Disposable bs = Disposable.empty();
s.onSubscribe(bs);
assertTrue(bs.isDisposed());
}
use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.
the class FlowableSubscriberTest method forEachWhile.
@Test
public void forEachWhile() {
PublishProcessor<Integer> pp = PublishProcessor.create();
final List<Integer> list = new ArrayList<>();
Disposable d = pp.forEachWhile(new Predicate<Integer>() {
@Override
public boolean test(Integer v) throws Exception {
list.add(v);
return v < 3;
}
});
assertFalse(d.isDisposed());
pp.onNext(1);
pp.onNext(2);
pp.onNext(3);
assertFalse(pp.hasSubscribers());
assertEquals(Arrays.asList(1, 2, 3), list);
}
use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.
the class SerialDisposableTests 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.set(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();
}
}
use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.
the class SerialDisposableTests method settingUnderlyingWhenUnsubscribedCausesImmediateUnsubscriptionConcurrently.
@Test
public void settingUnderlyingWhenUnsubscribedCausesImmediateUnsubscriptionConcurrently() throws InterruptedException {
final Disposable firstSet = mock(Disposable.class);
serialDisposable.set(firstSet);
final CountDownLatch start = new CountDownLatch(1);
final int count = 10;
final CountDownLatch end = new CountDownLatch(count);
final List<Thread> threads = new ArrayList<>();
for (int i = 0; i < count; i++) {
final Thread t = new Thread() {
@Override
public void run() {
try {
start.await();
serialDisposable.dispose();
} catch (InterruptedException e) {
fail(e.getMessage());
} finally {
end.countDown();
}
}
};
t.start();
threads.add(t);
}
final Disposable underlying = mock(Disposable.class);
start.countDown();
serialDisposable.set(underlying);
end.await();
verify(firstSet).dispose();
verify(underlying).dispose();
for (final Thread t : threads) {
t.join();
}
}
use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.
the class ExceptionsTest method stackOverflowWouldOccur.
@Test
public void stackOverflowWouldOccur() {
final PublishSubject<Integer> a = PublishSubject.create();
final PublishSubject<Integer> b = PublishSubject.create();
final int MAX_STACK_DEPTH = 800;
final AtomicInteger depth = new AtomicInteger();
a.subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(Integer n) {
b.onNext(n + 1);
}
});
b.subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
// TODO Auto-generated method stub
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(Integer n) {
if (depth.get() < MAX_STACK_DEPTH) {
depth.set(Thread.currentThread().getStackTrace().length);
a.onNext(n + 1);
}
}
});
a.onNext(1);
assertTrue(depth.get() >= MAX_STACK_DEPTH);
}
Aggregations