use of org.apache.cassandra.cql3.functions.Function in project RxJava by ReactiveX.
the class FlowableOnErrorResumeNextViaFunctionTest method functionThrowsError.
/**
* Test that when a function throws an exception this is propagated through onError.
*/
@Test
public void functionThrowsError() {
Subscription s = mock(Subscription.class);
TestFlowable w = new TestFlowable(s, "one");
Function<Throwable, Flowable<String>> resume = new Function<Throwable, Flowable<String>>() {
@Override
public Flowable<String> apply(Throwable t1) {
throw new RuntimeException("exception from function");
}
};
Flowable<String> flowable = Flowable.unsafeCreate(w).onErrorResumeNext(resume);
Subscriber<String> subscriber = TestHelper.mockSubscriber();
flowable.subscribe(subscriber);
try {
w.t.join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
// we should get the "one" value before the error
verify(subscriber, times(1)).onNext("one");
// we should have received an onError call on the Observer since the resume function threw an exception
verify(subscriber, times(1)).onError(any(Throwable.class));
verify(subscriber, times(0)).onComplete();
}
use of org.apache.cassandra.cql3.functions.Function in project RxJava by ReactiveX.
the class ObservableOnErrorResumeWithTest method mapResumeAsyncNext.
@Test
public void mapResumeAsyncNext() {
Disposable sr = mock(Disposable.class);
// Trigger multiple failures
Observable<String> w = Observable.just("one", "fail", "two", "three", "fail");
// Resume Observable is async
TestObservable f = new TestObservable(sr, "twoResume", "threeResume");
Observable<String> resume = Observable.unsafeCreate(f);
// Introduce map function that fails intermittently (Map does not prevent this when the Observer is a
// rx.operator incl onErrorResumeNextViaObservable)
w = w.map(new Function<String, String>() {
@Override
public String apply(String s) {
if ("fail".equals(s)) {
throw new RuntimeException("Forced Failure");
}
System.out.println("BadMapper:" + s);
return s;
}
});
Observable<String> observable = w.onErrorResumeWith(resume);
Observer<String> observer = TestHelper.mockObserver();
observable.subscribe(observer);
try {
f.t.join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
verify(observer, Mockito.never()).onError(any(Throwable.class));
verify(observer, times(1)).onComplete();
verify(observer, times(1)).onNext("one");
verify(observer, Mockito.never()).onNext("two");
verify(observer, Mockito.never()).onNext("three");
verify(observer, times(1)).onNext("twoResume");
verify(observer, times(1)).onNext("threeResume");
}
use of org.apache.cassandra.cql3.functions.Function in project RxJava by ReactiveX.
the class FlowableDebounceTest method emitLate.
@Test
public void emitLate() {
final AtomicReference<Subscriber<? super Integer>> ref = new AtomicReference<>();
TestSubscriber<Integer> ts = Flowable.range(1, 2).debounce(new Function<Integer, Flowable<Integer>>() {
@Override
public Flowable<Integer> apply(Integer o) throws Exception {
if (o != 1) {
return Flowable.never();
}
return new Flowable<Integer>() {
@Override
protected void subscribeActual(Subscriber<? super Integer> subscriber) {
subscriber.onSubscribe(new BooleanSubscription());
ref.set(subscriber);
}
};
}
}).test();
ref.get().onNext(1);
ts.assertResult(2);
}
use of org.apache.cassandra.cql3.functions.Function in project RxJava by ReactiveX.
the class MaybeTimerTest 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 = Maybe.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();
}
}
use of org.apache.cassandra.cql3.functions.Function in project RxJava by ReactiveX.
the class ObservableTimerTest 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 = Observable.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