Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleBlockingSubscribeTest.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.single;
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.Single;
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 SingleBlockingSubscribeTest {
32
33 @Test
34 public void noArgSuccess() {
35 Single.just(1)
36 .blockingSubscribe();
37 }
38
39 @Test
40 public void noArgSuccessAsync() {
41 Single.just(1)
42 .delay(100, TimeUnit.MILLISECONDS)
43 .blockingSubscribe();
44 }
45
46 @Test
47 public void noArgError() throws Throwable {
48 TestHelper.withErrorTracking(errors -> {
49 Single.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 Single.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 oneArgSuccess() throws Throwable {
69 @SuppressWarnings("unchecked")
70 Consumer<Integer> success = mock(Consumer.class);
71
72 Single.just(1)
73 .blockingSubscribe(success);
74
75 verify(success).accept(1);
76 }
77
78 @Test
79 public void oneArgSuccessAsync() throws Throwable {
80 @SuppressWarnings("unchecked")
81 Consumer<Integer> success = mock(Consumer.class);
82
83 Single.just(1)
84 .delay(50, TimeUnit.MILLISECONDS)
85 .blockingSubscribe(success);
86
87 verify(success).accept(1);
88 }
89
90 @Test
91 public void oneArgSuccessFails() throws Throwable {
92 TestHelper.withErrorTracking(errors -> {
93 @SuppressWarnings("unchecked")
94 Consumer<Integer> success = mock(Consumer.class);
95 doThrow(new TestException()).when(success).accept(any());
96
97 Single.just(1)
98 .blockingSubscribe(success);
99
100 TestHelper.assertUndeliverable(errors, 0, TestException.class);
101
102 verify(success).accept(1);
103 });
104 }
105
106 @Test
107 public void oneArgError() throws Throwable {
108 TestHelper.withErrorTracking(errors -> {
109 @SuppressWarnings("unchecked")
110 Consumer<Integer> success = mock(Consumer.class);
111
112 Single.<Integer>error(new TestException())
113 .blockingSubscribe(success);
114
115 TestHelper.assertUndeliverable(errors, 0, TestException.class);
116
117 verify(success, never()).accept(any());
118 });
119 }
120
121 @Test
122 public void oneArgErrorAsync() throws Throwable {
123 TestHelper.withErrorTracking(errors -> {
124 @SuppressWarnings("unchecked")
125 Consumer<Integer> success = mock(Consumer.class);
126
127 Single.<Integer>error(new TestException())
128 .delay(50, TimeUnit.MILLISECONDS, Schedulers.computation(), true)
129 .blockingSubscribe(success);
130
131 TestHelper.assertUndeliverable(errors, 0, TestException.class);
132
133 verify(success, never()).accept(any());
134 });
135 }
136
137 @Test
138 public void twoArgSuccess() throws Throwable {
139 @SuppressWarnings("unchecked")
140 Consumer<Integer> success = mock(Consumer.class);
141 @SuppressWarnings("unchecked")
142 Consumer<? super Throwable> consumer = mock(Consumer.class);
143
144 Single.just(1)
145 .blockingSubscribe(success, consumer);
146
147 verify(success).accept(1);
148 verify(consumer, never()).accept(any());
149 }
150
151 @Test
152 public void twoArgSuccessAsync() throws Throwable {
153 @SuppressWarnings("unchecked")
154 Consumer<Integer> success = mock(Consumer.class);
155 @SuppressWarnings("unchecked")
156 Consumer<? super Throwable> consumer = mock(Consumer.class);
157
158 Single.just(1)
159 .delay(50, TimeUnit.MILLISECONDS)
160 .blockingSubscribe(success, consumer);
161
162 verify(success).accept(any());
163 verify(consumer, never()).accept(any());
164 }
165
166 @Test
167 public void twoArgSuccessFails() throws Throwable {
168 TestHelper.withErrorTracking(errors -> {
169 @SuppressWarnings("unchecked")
170 Consumer<Integer> success = mock(Consumer.class);
171 doThrow(new TestException()).when(success).accept(any());
172 @SuppressWarnings("unchecked")
173 Consumer<? super Throwable> consumer = mock(Consumer.class);
174
175 Single.just(1)
176 .blockingSubscribe(success, consumer);
177
178 TestHelper.assertUndeliverable(errors, 0, TestException.class);
179
180 verify(success).accept(any());
181 verify(consumer, never()).accept(any());
182 });
183 }
184
185 @Test
186 public void twoArgError() throws Throwable {
187 TestHelper.withErrorTracking(errors -> {
188 @SuppressWarnings("unchecked")
189 Consumer<Integer> success = mock(Consumer.class);
190 @SuppressWarnings("unchecked")
191 Consumer<? super Throwable> consumer = mock(Consumer.class);
192
193 Single.<Integer>error(new TestException())
194 .blockingSubscribe(success, consumer);
195
196 assertTrue("" + errors, errors.isEmpty());
197
198 verify(success, never()).accept(any());
199 verify(consumer).accept(any(TestException.class));
200 });
201 }
202
203 @Test
204 public void twoArgErrorAsync() throws Throwable {
205 TestHelper.withErrorTracking(errors -> {
206 @SuppressWarnings("unchecked")
207 Consumer<Integer> success = mock(Consumer.class);
208 @SuppressWarnings("unchecked")
209 Consumer<? super Throwable> consumer = mock(Consumer.class);
210
211 Single.<Integer>error(new TestException())
212 .delay(50, TimeUnit.MILLISECONDS, Schedulers.computation(), true)
213 .blockingSubscribe(success, consumer);
214
215 assertTrue("" + errors, errors.isEmpty());
216
217 verify(success, never()).accept(any());
218 verify(consumer).accept(any(TestException.class));
219 });
220 }
221
222 @Test
223 public void twoArgErrorFails() throws Throwable {
224 TestHelper.withErrorTracking(errors -> {
225 @SuppressWarnings("unchecked")
226 Consumer<Integer> success = mock(Consumer.class);
227 @SuppressWarnings("unchecked")
228 Consumer<? super Throwable> consumer = mock(Consumer.class);
229 doThrow(new TestException()).when(consumer).accept(any());
230
231 Single.<Integer>error(new TestException())
232 .delay(50, TimeUnit.MILLISECONDS, Schedulers.computation(), true)
233 .blockingSubscribe(success, consumer);
234
235 TestHelper.assertUndeliverable(errors, 0, TestException.class);
236
237 verify(success, never()).accept(any());
238 verify(consumer).accept(any(TestException.class));
239 });
240 }
241
242 @Test
243 public void twoArgInterrupted() throws Throwable {
244 TestHelper.withErrorTracking(errors -> {
245 Action onDispose = mock(Action.class);
246
247 @SuppressWarnings("unchecked")
248 Consumer<Integer> success = mock(Consumer.class);
249 @SuppressWarnings("unchecked")
250 Consumer<? super Throwable> consumer = mock(Consumer.class);
251
252 Thread.currentThread().interrupt();
253
254 Single.<Integer>never()
255 .doOnDispose(onDispose)
256 .blockingSubscribe(success, consumer);
257
258 assertTrue("" + errors, errors.isEmpty());
259
260 verify(onDispose).run();
261 verify(success, never()).accept(any());
262 verify(consumer).accept(any(InterruptedException.class));
263 });
264 }
265
266 @Test
267 public void observerSuccess() {
268 TestObserver<Integer> to = new TestObserver<>();
269
270 Single.just(1)
271 .blockingSubscribe(to);
272
273 to.assertResult(1);
274 }
275
276 @Test
277 public void observerSuccessAsync() {
278 TestObserver<Integer> to = new TestObserver<>();
279
280 Single.just(1)
281 .delay(50, TimeUnit.MILLISECONDS, Schedulers.computation(), true)
282 .blockingSubscribe(to);
283
284 to.assertResult(1);
285 }
286
287 @Test
288 public void observerError() {
289 TestObserver<Object> to = new TestObserver<>();
290
291 Single.error(new TestException())
292 .blockingSubscribe(to);
293
294 to.assertFailure(TestException.class);
295 }
296
297 @Test
298 public void observerErrorAsync() {
299 TestObserver<Object> to = new TestObserver<>();
300
301 Single.error(new TestException())
302 .delay(50, TimeUnit.MILLISECONDS, Schedulers.computation(), true)
303 .blockingSubscribe(to);
304
305 to.assertFailure(TestException.class);
306 }
307
308 @Test
309 public void observerDispose() throws Throwable {
310 Action onDispose = mock(Action.class);
311
312 TestObserver<Object> to = new TestObserver<>();
313 to.dispose();
314
315 Single.never()
316 .doOnDispose(onDispose)
317 .blockingSubscribe(to);
318
319 to.assertEmpty();
320
321 verify(onDispose).run();
322 }
323
324 @Test
325 public void ovserverInterrupted() throws Throwable {
326 TestHelper.withErrorTracking(errors -> {
327 Action onDispose = mock(Action.class);
328
329 TestObserver<Object> to = new TestObserver<>();
330
331 Thread.currentThread().interrupt();
332
333 Single.never()
334 .doOnDispose(onDispose)
335 .blockingSubscribe(to);
336
337 assertTrue("" + errors, errors.isEmpty());
338
339 verify(onDispose).run();
340 to.assertFailure(InterruptedException.class);
341 });
342 }
343 }