Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableBlockingSubscribeTest.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.internal.operators.completable;
15
16 import static org.junit.Assert.assertTrue;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.util.concurrent.TimeUnit;
21
22 import org.junit.Test;
23
24 import io.reactivex.rxjava3.core.Completable;
25 import io.reactivex.rxjava3.exceptions.TestException;
26 import io.reactivex.rxjava3.functions.*;
27 import io.reactivex.rxjava3.observers.TestObserver;
28 import io.reactivex.rxjava3.schedulers.Schedulers;
29 import io.reactivex.rxjava3.testsupport.TestHelper;
30
31 public class CompletableBlockingSubscribeTest {
32
33 @Test
34 public void noArgComplete() {
35 Completable.complete()
36 .blockingSubscribe();
37 }
38
39 @Test
40 public void noArgCompleteAsync() {
41 Completable.complete()
42 .delay(100, TimeUnit.MILLISECONDS)
43 .blockingSubscribe();
44 }
45
46 @Test
47 public void noArgError() throws Throwable {
48 TestHelper.withErrorTracking(errors -> {
49 Completable.error(new TestException())
50 .blockingSubscribe();
51
52 TestHelper.assertUndeliverable(errors, 0, TestException.class);
53 });
54 }
55
56 @Test
57 public void noArgErrorAsync() throws Throwable {
58 TestHelper.withErrorTracking(errors -> {
59 Completable.error(new TestException())
60 .delay(100, TimeUnit.MILLISECONDS, Schedulers.computation(), true)
61 .blockingSubscribe();
62
63 TestHelper.assertUndeliverable(errors, 0, TestException.class);
64 });
65 }
66
67 @Test
68 public void oneArgComplete() throws Throwable {
69 Action action = mock(Action.class);
70
71 Completable.complete()
72 .blockingSubscribe(action);
73
74 verify(action).run();
75 }
76
77 @Test
78 public void oneArgCompleteAsync() throws Throwable {
79 Action action = mock(Action.class);
80
81 Completable.complete()
82 .delay(50, TimeUnit.MILLISECONDS)
83 .blockingSubscribe(action);
84
85 verify(action).run();
86 }
87
88 @Test
89 public void oneArgCompleteFails() throws Throwable {
90 TestHelper.withErrorTracking(errors -> {
91 Action action = mock(Action.class);
92 doThrow(new TestException()).when(action).run();
93
94 Completable.complete()
95 .blockingSubscribe(action);
96
97 TestHelper.assertUndeliverable(errors, 0, TestException.class);
98
99 verify(action).run();
100 });
101 }
102
103 @Test
104 public void oneArgError() throws Throwable {
105 TestHelper.withErrorTracking(errors -> {
106 Action action = mock(Action.class);
107
108 Completable.error(new TestException())
109 .blockingSubscribe(action);
110
111 TestHelper.assertUndeliverable(errors, 0, TestException.class);
112
113 verify(action, never()).run();
114 });
115 }
116
117 @Test
118 public void oneArgErrorAsync() throws Throwable {
119 TestHelper.withErrorTracking(errors -> {
120 Action action = mock(Action.class);
121
122 Completable.error(new TestException())
123 .delay(50, TimeUnit.MILLISECONDS, Schedulers.computation(), true)
124 .blockingSubscribe(action);
125
126 TestHelper.assertUndeliverable(errors, 0, TestException.class);
127
128 verify(action, never()).run();
129 });
130 }
131
132 @Test
133 public void twoArgComplete() throws Throwable {
134 Action action = mock(Action.class);
135 @SuppressWarnings("unchecked")
136 Consumer<? super Throwable> consumer = mock(Consumer.class);
137
138 Completable.complete()
139 .blockingSubscribe(action, consumer);
140
141 verify(action).run();
142 verify(consumer, never()).accept(any());
143 }
144
145 @Test
146 public void twoArgCompleteAsync() throws Throwable {
147 Action action = mock(Action.class);
148 @SuppressWarnings("unchecked")
149 Consumer<? super Throwable> consumer = mock(Consumer.class);
150
151 Completable.complete()
152 .delay(50, TimeUnit.MILLISECONDS)
153 .blockingSubscribe(action, consumer);
154
155 verify(action).run();
156 verify(consumer, never()).accept(any());
157 }
158
159 @Test
160 public void twoArgCompleteFails() throws Throwable {
161 TestHelper.withErrorTracking(errors -> {
162 Action action = mock(Action.class);
163 doThrow(new TestException()).when(action).run();
164 @SuppressWarnings("unchecked")
165 Consumer<? super Throwable> consumer = mock(Consumer.class);
166
167 Completable.complete()
168 .blockingSubscribe(action, consumer);
169
170 TestHelper.assertUndeliverable(errors, 0, TestException.class);
171
172 verify(action).run();
173 verify(consumer, never()).accept(any());
174 });
175 }
176
177 @Test
178 public void twoArgError() throws Throwable {
179 TestHelper.withErrorTracking(errors -> {
180 Action action = mock(Action.class);
181 @SuppressWarnings("unchecked")
182 Consumer<? super Throwable> consumer = mock(Consumer.class);
183
184 Completable.error(new TestException())
185 .blockingSubscribe(action, consumer);
186
187 assertTrue("" + errors, errors.isEmpty());
188
189 verify(action, never()).run();
190 verify(consumer).accept(any(TestException.class));
191 });
192 }
193
194 @Test
195 public void twoArgErrorAsync() throws Throwable {
196 TestHelper.withErrorTracking(errors -> {
197 Action action = mock(Action.class);
198 @SuppressWarnings("unchecked")
199 Consumer<? super Throwable> consumer = mock(Consumer.class);
200
201 Completable.error(new TestException())
202 .delay(50, TimeUnit.MILLISECONDS, Schedulers.computation(), true)
203 .blockingSubscribe(action, consumer);
204
205 assertTrue("" + errors, errors.isEmpty());
206
207 verify(action, never()).run();
208 verify(consumer).accept(any(TestException.class));
209 });
210 }
211
212 @Test
213 public void twoArgErrorFails() throws Throwable {
214 TestHelper.withErrorTracking(errors -> {
215 Action action = mock(Action.class);
216 @SuppressWarnings("unchecked")
217 Consumer<? super Throwable> consumer = mock(Consumer.class);
218 doThrow(new TestException()).when(consumer).accept(any());
219
220 Completable.error(new TestException())
221 .delay(50, TimeUnit.MILLISECONDS, Schedulers.computation(), true)
222 .blockingSubscribe(action, consumer);
223
224 TestHelper.assertUndeliverable(errors, 0, TestException.class);
225
226 verify(action, never()).run();
227 verify(consumer).accept(any(TestException.class));
228 });
229 }
230
231 @Test
232 public void twoArgInterrupted() throws Throwable {
233 TestHelper.withErrorTracking(errors -> {
234 Action onDispose = mock(Action.class);
235
236 Action action = mock(Action.class);
237 @SuppressWarnings("unchecked")
238 Consumer<? super Throwable> consumer = mock(Consumer.class);
239
240 Thread.currentThread().interrupt();
241
242 Completable.never()
243 .doOnDispose(onDispose)
244 .blockingSubscribe(action, consumer);
245
246 assertTrue("" + errors, errors.isEmpty());
247
248 verify(onDispose).run();
249 verify(action, never()).run();
250 verify(consumer).accept(any(InterruptedException.class));
251 });
252 }
253
254 @Test
255 public void observerComplete() {
256 TestObserver<Void> to = new TestObserver<>();
257
258 Completable.complete()
259 .blockingSubscribe(to);
260
261 to.assertResult();
262 }
263
264 @Test
265 public void observerCompleteAsync() {
266 TestObserver<Void> to = new TestObserver<>();
267
268 Completable.complete()
269 .delay(50, TimeUnit.MILLISECONDS, Schedulers.computation(), true)
270 .blockingSubscribe(to);
271
272 to.assertResult();
273 }
274
275 @Test
276 public void observerError() {
277 TestObserver<Void> to = new TestObserver<>();
278
279 Completable.error(new TestException())
280 .blockingSubscribe(to);
281
282 to.assertFailure(TestException.class);
283 }
284
285 @Test
286 public void observerErrorAsync() {
287 TestObserver<Void> to = new TestObserver<>();
288
289 Completable.error(new TestException())
290 .delay(50, TimeUnit.MILLISECONDS, Schedulers.computation(), true)
291 .blockingSubscribe(to);
292
293 to.assertFailure(TestException.class);
294 }
295
296 @Test
297 public void observerDispose() throws Throwable {
298 Action onDispose = mock(Action.class);
299
300 TestObserver<Void> to = new TestObserver<>();
301 to.dispose();
302
303 Completable.never()
304 .doOnDispose(onDispose)
305 .blockingSubscribe(to);
306
307 to.assertEmpty();
308
309 verify(onDispose).run();
310 }
311
312 @Test
313 public void ovserverInterrupted() throws Throwable {
314 TestHelper.withErrorTracking(errors -> {
315 Action onDispose = mock(Action.class);
316
317 TestObserver<Void> to = new TestObserver<>();
318
319 Thread.currentThread().interrupt();
320
321 Completable.never()
322 .doOnDispose(onDispose)
323 .blockingSubscribe(to);
324
325 assertTrue("" + errors, errors.isEmpty());
326
327 verify(onDispose).run();
328 to.assertFailure(InterruptedException.class);
329 });
330 }
331 }