use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.
the class FlowableCollectWithCollectorTest method collectorAccumulatorDropSignalsToFlowable.
@Test
public void collectorAccumulatorDropSignalsToFlowable() throws Throwable {
TestHelper.withErrorTracking(errors -> {
Flowable<Integer> source = new Flowable<Integer>() {
@Override
protected void subscribeActual(Subscriber<? super Integer> s) {
s.onSubscribe(new BooleanSubscription());
s.onNext(1);
s.onNext(2);
s.onError(new IOException());
s.onComplete();
}
};
source.collect(new Collector<Integer, Integer, Integer>() {
@Override
public Supplier<Integer> supplier() {
return () -> 1;
}
@Override
public BiConsumer<Integer, Integer> accumulator() {
return (a, b) -> {
throw new TestException();
};
}
@Override
public BinaryOperator<Integer> combiner() {
return (a, b) -> a + b;
}
@Override
public Function<Integer, Integer> finisher() {
return a -> a;
}
@Override
public Set<Characteristics> characteristics() {
return Collections.emptySet();
}
}).toFlowable().test().assertFailure(TestException.class);
TestHelper.assertUndeliverable(errors, 0, IOException.class);
});
}
use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.
the class FlowableCollectWithCollectorTest method collectorAccumulatorCrashToFlowable.
@Test
public void collectorAccumulatorCrashToFlowable() {
BehaviorProcessor<Integer> source = BehaviorProcessor.createDefault(1);
source.collect(new Collector<Integer, Integer, Integer>() {
@Override
public Supplier<Integer> supplier() {
return () -> 1;
}
@Override
public BiConsumer<Integer, Integer> accumulator() {
return (a, b) -> {
throw new TestException();
};
}
@Override
public BinaryOperator<Integer> combiner() {
return (a, b) -> a + b;
}
@Override
public Function<Integer, Integer> finisher() {
return a -> a;
}
@Override
public Set<Characteristics> characteristics() {
return Collections.emptySet();
}
}).toFlowable().test().assertFailure(TestException.class);
assertFalse(source.hasSubscribers());
}
use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.
the class FlowableTimeoutWithSelectorTest method timeoutSelectorWithTimeoutAndOnNextRaceCondition.
@Test
public void timeoutSelectorWithTimeoutAndOnNextRaceCondition() throws InterruptedException {
// Thread 1 Thread 2
//
// observer.onNext(1)
// start timeout
// unsubscribe timeout in thread 2 start to do some long-time work in "unsubscribe"
// observer.onNext(2)
// timeout.onNext(1)
// "unsubscribe" done
//
//
// In the above case, the timeout operator should ignore "timeout.onNext(1)"
// since "observer" has already seen 2.
final CountDownLatch observerReceivedTwo = new CountDownLatch(1);
final CountDownLatch timeoutEmittedOne = new CountDownLatch(1);
final CountDownLatch observerCompleted = new CountDownLatch(1);
final CountDownLatch enteredTimeoutOne = new CountDownLatch(1);
final AtomicBoolean latchTimeout = new AtomicBoolean(false);
final Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {
@Override
public Flowable<Integer> apply(Integer t1) {
if (t1 == 1) {
// Force "unsubscribe" run on another thread
return Flowable.unsafeCreate(new Publisher<Integer>() {
@Override
public void subscribe(Subscriber<? super Integer> subscriber) {
subscriber.onSubscribe(new BooleanSubscription());
enteredTimeoutOne.countDown();
// force the timeout message be sent after observer.onNext(2)
while (true) {
try {
if (!observerReceivedTwo.await(30, TimeUnit.SECONDS)) {
// CountDownLatch timeout
// There should be something wrong
latchTimeout.set(true);
}
break;
} catch (InterruptedException e) {
// Since we just want to emulate a busy method,
// we ignore the interrupt signal from Scheduler.
}
}
subscriber.onNext(1);
timeoutEmittedOne.countDown();
}
}).subscribeOn(Schedulers.newThread());
} else {
return PublishProcessor.create();
}
}
};
final Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
observerReceivedTwo.countDown();
return null;
}
}).when(subscriber).onNext(2);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
observerCompleted.countDown();
return null;
}
}).when(subscriber).onComplete();
final TestSubscriber<Integer> ts = new TestSubscriber<>(subscriber);
new Thread(new Runnable() {
@Override
public void run() {
PublishProcessor<Integer> source = PublishProcessor.create();
source.timeout(timeoutFunc, Flowable.just(3)).subscribe(ts);
// start timeout
source.onNext(1);
try {
if (!enteredTimeoutOne.await(30, TimeUnit.SECONDS)) {
latchTimeout.set(true);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
// disable timeout
source.onNext(2);
try {
if (!timeoutEmittedOne.await(30, TimeUnit.SECONDS)) {
latchTimeout.set(true);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
source.onComplete();
}
}).start();
if (!observerCompleted.await(30, TimeUnit.SECONDS)) {
latchTimeout.set(true);
}
assertFalse("CoundDownLatch timeout", latchTimeout.get());
InOrder inOrder = inOrder(subscriber);
inOrder.verify(subscriber).onSubscribe((Subscription) notNull());
inOrder.verify(subscriber).onNext(1);
inOrder.verify(subscriber).onNext(2);
inOrder.verify(subscriber, never()).onNext(3);
inOrder.verify(subscriber).onComplete();
inOrder.verifyNoMoreInteractions();
}
use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.
the class FlowableRepeatTest method shouldDisposeInnerObservable.
@Test
public void shouldDisposeInnerObservable() {
final PublishProcessor<Object> processor = PublishProcessor.create();
final Disposable disposable = Flowable.just("Leak").repeatWhen(new Function<Flowable<Object>, Flowable<Object>>() {
@Override
public Flowable<Object> apply(Flowable<Object> completions) throws Exception {
return completions.switchMap(new Function<Object, Flowable<Object>>() {
@Override
public Flowable<Object> apply(Object ignore) throws Exception {
return processor;
}
});
}
}).subscribe();
assertTrue(processor.hasSubscribers());
disposable.dispose();
assertFalse(processor.hasSubscribers());
}
use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.
the class SingleTimerTest method timerInterruptible.
@Test
public void timerInterruptible() throws Exception {
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
try {
for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec, true) }) {
final AtomicBoolean interrupted = new AtomicBoolean();
TestObserver<Long> to = Single.timer(1, TimeUnit.MILLISECONDS, s).map(new Function<Long, Long>() {
@Override
public Long apply(Long v) throws Exception {
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
interrupted.set(true);
}
return v;
}
}).test();
Thread.sleep(500);
to.dispose();
Thread.sleep(500);
assertTrue(s.getClass().getSimpleName(), interrupted.get());
}
} finally {
exec.shutdown();
}
}
Aggregations