use of io.reactivex.functions.Function in project RxJava by ReactiveX.
the class ObservableDebounceTest method debounceSelectorObservableThrows.
@Test
public void debounceSelectorObservableThrows() {
PublishSubject<Integer> source = PublishSubject.create();
Function<Integer, Observable<Integer>> debounceSel = new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
return Observable.error(new TestException());
}
};
Observer<Object> o = TestHelper.mockObserver();
source.debounce(debounceSel).subscribe(o);
source.onNext(1);
verify(o, never()).onNext(any());
verify(o, never()).onComplete();
verify(o).onError(any(TestException.class));
}
use of io.reactivex.functions.Function in project RxJava by ReactiveX.
the class ObservableOnErrorResumeNextViaFunctionTest method testFunctionThrowsError.
/**
* Test that when a function throws an exception this is propagated through onError.
*/
@Test
public void testFunctionThrowsError() {
Subscription s = mock(Subscription.class);
TestObservable w = new TestObservable(s, "one");
Function<Throwable, Observable<String>> resume = new Function<Throwable, Observable<String>>() {
@Override
public Observable<String> apply(Throwable t1) {
throw new RuntimeException("exception from function");
}
};
Observable<String> o = Observable.unsafeCreate(w).onErrorResumeNext(resume);
@SuppressWarnings("unchecked") DefaultObserver<String> observer = mock(DefaultObserver.class);
o.subscribe(observer);
try {
w.t.join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
// we should get the "one" value before the error
verify(observer, times(1)).onNext("one");
// we should have received an onError call on the Observer since the resume function threw an exception
verify(observer, times(1)).onError(any(Throwable.class));
verify(observer, times(0)).onComplete();
}
use of io.reactivex.functions.Function in project RxJava by ReactiveX.
the class ObservableOnErrorResumeNextViaObservableTest method testMapResumeAsyncNext.
@Test
public void testMapResumeAsyncNext() {
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.onErrorResumeNext(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 io.reactivex.functions.Function in project RxJava by ReactiveX.
the class ObservableTimeoutWithSelectorTest method testTimeoutSelectorWithTimeoutAndOnNextRaceCondition.
@Test
public void testTimeoutSelectorWithTimeoutAndOnNextRaceCondition() 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, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
if (t1 == 1) {
// Force "unsubscribe" run on another thread
return Observable.unsafeCreate(new ObservableSource<Integer>() {
@Override
public void subscribe(Observer<? super Integer> observer) {
observer.onSubscribe(Disposables.empty());
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.
}
}
observer.onNext(1);
timeoutEmittedOne.countDown();
}
}).subscribeOn(Schedulers.newThread());
} else {
return PublishSubject.create();
}
}
};
final Observer<Integer> o = TestHelper.mockObserver();
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
observerReceivedTwo.countDown();
return null;
}
}).when(o).onNext(2);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
observerCompleted.countDown();
return null;
}
}).when(o).onComplete();
final TestObserver<Integer> ts = new TestObserver<Integer>(o);
new Thread(new Runnable() {
@Override
public void run() {
PublishSubject<Integer> source = PublishSubject.create();
source.timeout(timeoutFunc, Observable.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(o);
inOrder.verify(o).onSubscribe((Disposable) notNull());
inOrder.verify(o).onNext(1);
inOrder.verify(o).onNext(2);
inOrder.verify(o, never()).onNext(3);
inOrder.verify(o).onComplete();
inOrder.verifyNoMoreInteractions();
}
use of io.reactivex.functions.Function in project RxJava by ReactiveX.
the class ObservableTimeoutWithSelectorTest method testTimeoutSelectorNormal1.
@Test(timeout = 2000)
public void testTimeoutSelectorNormal1() {
PublishSubject<Integer> source = PublishSubject.create();
final PublishSubject<Integer> timeout = PublishSubject.create();
Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
return timeout;
}
};
Observable<Integer> other = Observable.fromIterable(Arrays.asList(100));
Observer<Object> o = TestHelper.mockObserver();
InOrder inOrder = inOrder(o);
source.timeout(timeout, timeoutFunc, other).subscribe(o);
source.onNext(1);
source.onNext(2);
source.onNext(3);
timeout.onNext(1);
inOrder.verify(o).onNext(1);
inOrder.verify(o).onNext(2);
inOrder.verify(o).onNext(3);
inOrder.verify(o).onNext(100);
inOrder.verify(o).onComplete();
verify(o, never()).onError(any(Throwable.class));
}
Aggregations