Skip to content

Instantly share code, notes, and snippets.

@akarnokd
Created November 10, 2017 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akarnokd/6069fd876a476b32fd0978ae0f0ebb11 to your computer and use it in GitHub Desktop.
Save akarnokd/6069fd876a476b32fd0978ae0f0ebb11 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import rx.Observable;
import rx.functions.Func1;
public class RetryWhenTimedTest {
public class RetryWhenObservable implements Func1<Observable<? extends Throwable>, Observable<?>> {
private static final String TAG = "RetryWhenObservable";
int maxRetry, interval,retryCount = 0;
public RetryWhenObservable(int maxRetry, int interval) {
this.maxRetry = maxRetry;
this.interval = interval;
}
@Override
public Observable<?> call(Observable<? extends Throwable> attempts) {
return attempts.flatMap(throwable -> {
if (++retryCount < this.maxRetry) {
return Observable.timer(interval,TimeUnit.SECONDS);
}else
return Observable.error(throwable);
});
}
}
@Test
public void test() {
Observable.error(new IOException())
.retryWhen(new RetryWhenObservable(5, 1))
.test()
.awaitTerminalEvent()
.assertFailure(IOException.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment