| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.single; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertEquals; |
| 17 |
|
| 18 |
import java.util.concurrent.Callable; |
| 19 |
import java.util.concurrent.atomic.AtomicInteger; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.functions.Predicate; |
| 26 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 27 |
|
| 28 |
public class SingleRetryTest extends RxJavaTest { |
| 29 |
@Test |
| 30 |
public void retryTimesPredicateWithMatchingPredicate() { |
| 31 |
final AtomicInteger atomicInteger = new AtomicInteger(3); |
| 32 |
final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0); |
| 33 |
|
| 34 |
Single.fromCallable(new Callable<Boolean>() { |
| 35 |
@Override public Boolean call() throws Exception { |
| 36 |
numberOfSubscribeCalls.incrementAndGet(); |
| 37 |
|
| 38 |
if (atomicInteger.decrementAndGet() != 0) { |
| 39 |
throw new RuntimeException(); |
| 40 |
} |
| 41 |
|
| 42 |
throw new IllegalArgumentException(); |
| 43 |
} |
| 44 |
}) |
| 45 |
.retry(Integer.MAX_VALUE, new Predicate<Throwable>() { |
| 46 |
@Override public boolean test(final Throwable throwable) throws Exception { |
| 47 |
return !(throwable instanceof IllegalArgumentException); |
| 48 |
} |
| 49 |
}) |
| 50 |
.test() |
| 51 |
.assertFailure(IllegalArgumentException.class); |
| 52 |
|
| 53 |
assertEquals(3, numberOfSubscribeCalls.get()); |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void retryTimesPredicateWithMatchingRetryAmount() { |
| 58 |
final AtomicInteger atomicInteger = new AtomicInteger(3); |
| 59 |
final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0); |
| 60 |
|
| 61 |
Single.fromCallable(new Callable<Boolean>() { |
| 62 |
@Override public Boolean call() throws Exception { |
| 63 |
numberOfSubscribeCalls.incrementAndGet(); |
| 64 |
|
| 65 |
if (atomicInteger.decrementAndGet() != 0) { |
| 66 |
throw new RuntimeException(); |
| 67 |
} |
| 68 |
|
| 69 |
return true; |
| 70 |
} |
| 71 |
}) |
| 72 |
.retry(2, Functions.alwaysTrue()) |
| 73 |
.test() |
| 74 |
.assertResult(true); |
| 75 |
|
| 76 |
assertEquals(3, numberOfSubscribeCalls.get()); |
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
public void retryTimesPredicateWithNotMatchingRetryAmount() { |
| 81 |
final AtomicInteger atomicInteger = new AtomicInteger(3); |
| 82 |
final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0); |
| 83 |
|
| 84 |
Single.fromCallable(new Callable<Boolean>() { |
| 85 |
@Override public Boolean call() throws Exception { |
| 86 |
numberOfSubscribeCalls.incrementAndGet(); |
| 87 |
|
| 88 |
if (atomicInteger.decrementAndGet() != 0) { |
| 89 |
throw new RuntimeException(); |
| 90 |
} |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
}) |
| 95 |
.retry(1, Functions.alwaysTrue()) |
| 96 |
.test() |
| 97 |
.assertFailure(RuntimeException.class); |
| 98 |
|
| 99 |
assertEquals(2, numberOfSubscribeCalls.get()); |
| 100 |
} |
| 101 |
|
| 102 |
@Test |
| 103 |
public void retryTimesPredicateWithZeroRetries() { |
| 104 |
final AtomicInteger atomicInteger = new AtomicInteger(2); |
| 105 |
final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0); |
| 106 |
|
| 107 |
Single.fromCallable(new Callable<Boolean>() { |
| 108 |
@Override public Boolean call() throws Exception { |
| 109 |
numberOfSubscribeCalls.incrementAndGet(); |
| 110 |
|
| 111 |
if (atomicInteger.decrementAndGet() != 0) { |
| 112 |
throw new RuntimeException(); |
| 113 |
} |
| 114 |
|
| 115 |
return true; |
| 116 |
} |
| 117 |
}) |
| 118 |
.retry(0, Functions.alwaysTrue()) |
| 119 |
.test() |
| 120 |
.assertFailure(RuntimeException.class); |
| 121 |
|
| 122 |
assertEquals(1, numberOfSubscribeCalls.get()); |
| 123 |
} |
| 124 |
|
| 125 |
@Test |
| 126 |
public void untilTrueJust() { |
| 127 |
Single.just(1) |
| 128 |
.retryUntil(() -> true) |
| 129 |
.test() |
| 130 |
.assertResult(1); |
| 131 |
} |
| 132 |
|
| 133 |
@Test |
| 134 |
public void untilFalseJust() { |
| 135 |
Single.just(1) |
| 136 |
.retryUntil(() -> false) |
| 137 |
.test() |
| 138 |
.assertResult(1); |
| 139 |
} |
| 140 |
|
| 141 |
@Test |
| 142 |
public void untilTrueError() { |
| 143 |
Single.error(new TestException()) |
| 144 |
.retryUntil(() -> true) |
| 145 |
.test() |
| 146 |
.assertFailure(TestException.class); |
| 147 |
} |
| 148 |
|
| 149 |
@Test |
| 150 |
public void untilFalseError() { |
| 151 |
AtomicInteger counter = new AtomicInteger(); |
| 152 |
Single.defer(() -> { |
| 153 |
if (counter.getAndIncrement() == 0) { |
| 154 |
return Single.error(new TestException()); |
| 155 |
} |
| 156 |
return Single.just(1); |
| 157 |
}) |
| 158 |
.retryUntil(() -> false) |
| 159 |
.test() |
| 160 |
.assertResult(1); |
| 161 |
} |
| 162 |
} |