From a9382cf0f93d3439bd387d9dd3e1b5590208e0d4 Mon Sep 17 00:00:00 2001 From: Satyarth Shankar <55573874+satyarth9@users.noreply.github.com> Date: Thu, 3 Nov 2022 13:49:56 +0530 Subject: [PATCH] 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 --- .../rxjava/RxJavaRetryWithDelayUnitTest.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 rxjava-modules/rxjava-core/src/test/java/com/baeldung/rxjava/RxJavaRetryWithDelayUnitTest.java diff --git a/rxjava-modules/rxjava-core/src/test/java/com/baeldung/rxjava/RxJavaRetryWithDelayUnitTest.java b/rxjava-modules/rxjava-core/src/test/java/com/baeldung/rxjava/RxJavaRetryWithDelayUnitTest.java new file mode 100644 index 0000000000..c6c8a461a3 --- /dev/null +++ b/rxjava-modules/rxjava-core/src/test/java/com/baeldung/rxjava/RxJavaRetryWithDelayUnitTest.java @@ -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; + } +}