retry when rxjava (#12961)
* BAEL-5751 test commit for checkin builds * BAEL-5751 compiled with java 8 * BAEL-5751 small update * BAEL-5751 added the core code * BAEL-5751 moved code to a different module * BAEL-5751 using assertArrayEquals * BAEL-5751 new line at the end of file * BAEL-5738 retry with delay in rxJava * Update RxJavaRetryWithDelayUnitTest.java Co-authored-by: Grzegorz Piwowarek <gpiwowarek@gmail.com>
This commit is contained in:
parent
c9cd7d4c70
commit
87a562291e
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.rxjava;
|
||||
import io.reactivex.Observable;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class RxJavaRetryWithDelayUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenObservable_whenSuccess_thenOnNext(){
|
||||
Observable.just(remoteCallSuccess())
|
||||
.subscribe(success -> {
|
||||
System.out.println("Success");
|
||||
System.out.println(success);
|
||||
}, err -> {
|
||||
System.out.println("Error");
|
||||
System.out.println(err);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenObservable_whenError_thenOnError(){
|
||||
Observable.just(remoteCallError())
|
||||
.subscribe(success -> {
|
||||
System.out.println("Success");
|
||||
System.out.println(success);
|
||||
}, err -> {
|
||||
System.out.println("Error");
|
||||
System.out.println(err);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenError_whenRetry_thenCanDelay(){
|
||||
Observable.just(remoteCallError())
|
||||
.retryWhen(attempts -> {
|
||||
return attempts.flatMap(err -> {
|
||||
if (customChecker(err)) {
|
||||
return Observable.timer(5000, TimeUnit.MILLISECONDS);
|
||||
} else {
|
||||
return Observable.error(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private String remoteCallSuccess(){
|
||||
return "Success";
|
||||
}
|
||||
|
||||
private String remoteCallError(){
|
||||
// consider a network call that failed over here.
|
||||
return "Error";
|
||||
}
|
||||
|
||||
private boolean customChecker(Throwable t){
|
||||
// this will include custom logic that decides whether resubscription should occur or not
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue