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:
Satyarth Shankar 2022-11-03 13:49:56 +05:30 committed by GitHub
parent c9cd7d4c70
commit 87a562291e
1 changed files with 62 additions and 0 deletions

View File

@ -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;
}
}