use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class FlowableRetryWithPredicateTest method unsubscribeFromRetry.
@Test
public void unsubscribeFromRetry() {
PublishProcessor<Integer> processor = PublishProcessor.create();
final AtomicInteger count = new AtomicInteger(0);
Disposable sub = processor.retry(retryTwice).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer n) {
count.incrementAndGet();
}
});
processor.onNext(1);
sub.dispose();
processor.onNext(2);
assertEquals(1, count.get());
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class FlowablePublishTest method publish.
@Test
public void publish() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
ConnectableFlowable<String> f = Flowable.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();
}
}).publish();
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();
}
});
Disposable connection = f.connect();
try {
if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
fail("subscriptions did not receive values");
}
assertEquals(1, counter.get());
} finally {
connection.dispose();
}
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class RxJavaPluginsTest method overrideConnectableFlowable.
@SuppressWarnings("rawtypes")
@Test
public void overrideConnectableFlowable() {
try {
RxJavaPlugins.setOnConnectableFlowableAssembly(new Function<ConnectableFlowable, ConnectableFlowable>() {
@Override
public ConnectableFlowable apply(ConnectableFlowable co) throws Exception {
return new ConnectableFlowable() {
@Override
public void connect(Consumer connection) {
}
@Override
public void reset() {
// nothing to do in this test
}
@SuppressWarnings("unchecked")
@Override
protected void subscribeActual(Subscriber subscriber) {
subscriber.onSubscribe(new ScalarSubscription(subscriber, 10));
}
};
}
});
Flowable.just(1).publish().autoConnect().test().assertResult(10);
} finally {
RxJavaPlugins.reset();
}
Flowable.just(1).publish().autoConnect().test().assertResult(1);
}
use of io.reactivex.rxjava3.functions.Consumer 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);
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class MaybeTest method observeOnErrorThread.
@Test
public void observeOnErrorThread() {
String main = Thread.currentThread().getName();
final String[] name = { null };
Maybe.error(new TestException()).observeOn(Schedulers.single()).doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable e) throws Exception {
name[0] = Thread.currentThread().getName();
}
}).test().awaitDone(5, TimeUnit.SECONDS).assertFailure(TestException.class);
assertNotEquals(main, name[0]);
}
Aggregations