Debian Codegraph @

src/test/java/io/reactivex/rxjava3/completable/CompletableRetryTest.java

raw
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.completable;
15
16 import static org.junit.Assert.assertEquals;
17
18 import java.util.concurrent.atomic.AtomicInteger;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.exceptions.TestException;
24 import io.reactivex.rxjava3.functions.*;
25 import io.reactivex.rxjava3.internal.functions.Functions;
26
27 public class CompletableRetryTest extends RxJavaTest {
28 @Test
29 public void retryTimesPredicateWithMatchingPredicate() {
30 final AtomicInteger atomicInteger = new AtomicInteger(3);
31 final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0);
32
33 Completable.fromAction(new Action() {
34 @Override public void run() throws Exception {
35 numberOfSubscribeCalls.incrementAndGet();
36
37 if (atomicInteger.decrementAndGet() != 0) {
38 throw new RuntimeException();
39 }
40
41 throw new IllegalArgumentException();
42 }
43 })
44 .retry(Integer.MAX_VALUE, new Predicate<Throwable>() {
45 @Override public boolean test(final Throwable throwable) throws Exception {
46 return !(throwable instanceof IllegalArgumentException);
47 }
48 })
49 .test()
50 .assertFailure(IllegalArgumentException.class);
51
52 assertEquals(3, numberOfSubscribeCalls.get());
53 }
54
55 @Test
56 public void retryTimesPredicateWithMatchingRetryAmount() {
57 final AtomicInteger atomicInteger = new AtomicInteger(3);
58 final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0);
59
60 Completable.fromAction(new Action() {
61 @Override public void run() throws Exception {
62 numberOfSubscribeCalls.incrementAndGet();
63
64 if (atomicInteger.decrementAndGet() != 0) {
65 throw new RuntimeException();
66 }
67 }
68 })
69 .retry(2, Functions.alwaysTrue())
70 .test()
71 .assertResult();
72
73 assertEquals(3, numberOfSubscribeCalls.get());
74 }
75
76 @Test
77 public void retryTimesPredicateWithNotMatchingRetryAmount() {
78 final AtomicInteger atomicInteger = new AtomicInteger(3);
79 final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0);
80
81 Completable.fromAction(new Action() {
82 @Override public void run() throws Exception {
83 numberOfSubscribeCalls.incrementAndGet();
84
85 if (atomicInteger.decrementAndGet() != 0) {
86 throw new RuntimeException();
87 }
88 }
89 })
90 .retry(1, Functions.alwaysTrue())
91 .test()
92 .assertFailure(RuntimeException.class);
93
94 assertEquals(2, numberOfSubscribeCalls.get());
95 }
96
97 @Test
98 public void retryTimesPredicateWithZeroRetries() {
99 final AtomicInteger atomicInteger = new AtomicInteger(2);
100 final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0);
101
102 Completable.fromAction(new Action() {
103 @Override public void run() throws Exception {
104 numberOfSubscribeCalls.incrementAndGet();
105
106 if (atomicInteger.decrementAndGet() != 0) {
107 throw new RuntimeException();
108 }
109 }
110 })
111 .retry(0, Functions.alwaysTrue())
112 .test()
113 .assertFailure(RuntimeException.class);
114
115 assertEquals(1, numberOfSubscribeCalls.get());
116 }
117
118 @Test
119 public void untilTrueEmpty() {
120 Completable.complete()
121 .retryUntil(() -> true)
122 .test()
123 .assertResult();
124 }
125
126 @Test
127 public void untilFalseEmpty() {
128 Completable.complete()
129 .retryUntil(() -> false)
130 .test()
131 .assertResult();
132 }
133
134 @Test
135 public void untilTrueError() {
136 Completable.error(new TestException())
137 .retryUntil(() -> true)
138 .test()
139 .assertFailure(TestException.class);
140 }
141
142 @Test
143 public void untilFalseError() {
144 AtomicInteger counter = new AtomicInteger();
145 Completable.defer(() -> {
146 if (counter.getAndIncrement() == 0) {
147 return Completable.error(new TestException());
148 }
149 return Completable.complete();
150 })
151 .retryUntil(() -> false)
152 .test()
153 .assertResult();
154 }
155 }