Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/ObservableZipIterableTest.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.observable;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.util.*;
21 import java.util.concurrent.atomic.AtomicInteger;
22
23 import io.reactivex.rxjava3.disposables.Disposable;
24 import org.junit.*;
25 import org.mockito.InOrder;
26
27 import io.reactivex.rxjava3.core.*;
28 import io.reactivex.rxjava3.core.Observable;
29 import io.reactivex.rxjava3.core.Observer;
30 import io.reactivex.rxjava3.exceptions.TestException;
31 import io.reactivex.rxjava3.functions.*;
32 import io.reactivex.rxjava3.internal.util.CrashingIterable;
33 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
34 import io.reactivex.rxjava3.subjects.PublishSubject;
35 import io.reactivex.rxjava3.testsupport.TestHelper;
36
37 public class ObservableZipIterableTest extends RxJavaTest {
38 BiFunction<String, String, String> concat2Strings;
39 PublishSubject<String> s1;
40 PublishSubject<String> s2;
41 Observable<String> zipped;
42
43 Observer<String> observer;
44 InOrder inOrder;
45
46 @Before
47 public void setUp() {
48 concat2Strings = new BiFunction<String, String, String>() {
49 @Override
50 public String apply(String t1, String t2) {
51 return t1 + "-" + t2;
52 }
53 };
54
55 s1 = PublishSubject.create();
56 s2 = PublishSubject.create();
57 zipped = Observable.zip(s1, s2, concat2Strings);
58
59 observer = TestHelper.mockObserver();
60 inOrder = inOrder(observer);
61
62 zipped.subscribe(observer);
63 }
64
65 BiFunction<Object, Object, String> zipr2 = new BiFunction<Object, Object, String>() {
66
67 @Override
68 public String apply(Object t1, Object t2) {
69 return "" + t1 + t2;
70 }
71
72 };
73 Function3<Object, Object, Object, String> zipr3 = new Function3<Object, Object, Object, String>() {
74
75 @Override
76 public String apply(Object t1, Object t2, Object t3) {
77 return "" + t1 + t2 + t3;
78 }
79
80 };
81
82 @Test
83 public void zipIterableSameSize() {
84 PublishSubject<String> r1 = PublishSubject.create();
85 /* define an Observer to receive aggregated events */
86 Observer<String> o = TestHelper.mockObserver();
87 InOrder io = inOrder(o);
88
89 Iterable<String> r2 = Arrays.asList("1", "2", "3");
90
91 r1.zipWith(r2, zipr2).subscribe(o);
92
93 r1.onNext("one-");
94 r1.onNext("two-");
95 r1.onNext("three-");
96 r1.onComplete();
97
98 io.verify(o).onNext("one-1");
99 io.verify(o).onNext("two-2");
100 io.verify(o).onNext("three-3");
101 io.verify(o).onComplete();
102
103 verify(o, never()).onError(any(Throwable.class));
104
105 }
106
107 @Test
108 public void zipIterableEmptyFirstSize() {
109 PublishSubject<String> r1 = PublishSubject.create();
110 /* define an Observer to receive aggregated events */
111 Observer<String> o = TestHelper.mockObserver();
112 InOrder io = inOrder(o);
113
114 Iterable<String> r2 = Arrays.asList("1", "2", "3");
115
116 r1.zipWith(r2, zipr2).subscribe(o);
117
118 r1.onComplete();
119
120 io.verify(o).onComplete();
121
122 verify(o, never()).onNext(any(String.class));
123 verify(o, never()).onError(any(Throwable.class));
124
125 }
126
127 @Test
128 public void zipIterableEmptySecond() {
129 PublishSubject<String> r1 = PublishSubject.create();
130 /* define an Observer to receive aggregated events */
131 Observer<String> o = TestHelper.mockObserver();
132 InOrder io = inOrder(o);
133
134 Iterable<String> r2 = Arrays.asList();
135
136 r1.zipWith(r2, zipr2).subscribe(o);
137
138 r1.onNext("one-");
139 r1.onNext("two-");
140 r1.onNext("three-");
141 r1.onComplete();
142
143 io.verify(o).onComplete();
144
145 verify(o, never()).onNext(any(String.class));
146 verify(o, never()).onError(any(Throwable.class));
147 }
148
149 @Test
150 public void zipIterableFirstShorter() {
151 PublishSubject<String> r1 = PublishSubject.create();
152 /* define an Observer to receive aggregated events */
153 Observer<String> o = TestHelper.mockObserver();
154 InOrder io = inOrder(o);
155
156 Iterable<String> r2 = Arrays.asList("1", "2", "3");
157
158 r1.zipWith(r2, zipr2).subscribe(o);
159
160 r1.onNext("one-");
161 r1.onNext("two-");
162 r1.onComplete();
163
164 io.verify(o).onNext("one-1");
165 io.verify(o).onNext("two-2");
166 io.verify(o).onComplete();
167
168 verify(o, never()).onError(any(Throwable.class));
169
170 }
171
172 @Test
173 public void zipIterableSecondShorter() {
174 PublishSubject<String> r1 = PublishSubject.create();
175 /* define an Observer to receive aggregated events */
176 Observer<String> o = TestHelper.mockObserver();
177 InOrder io = inOrder(o);
178
179 Iterable<String> r2 = Arrays.asList("1", "2");
180
181 r1.zipWith(r2, zipr2).subscribe(o);
182
183 r1.onNext("one-");
184 r1.onNext("two-");
185 r1.onNext("three-");
186 r1.onComplete();
187
188 io.verify(o).onNext("one-1");
189 io.verify(o).onNext("two-2");
190 io.verify(o).onComplete();
191
192 verify(o, never()).onError(any(Throwable.class));
193
194 }
195
196 @Test
197 public void zipIterableFirstThrows() {
198 PublishSubject<String> r1 = PublishSubject.create();
199 /* define an Observer to receive aggregated events */
200 Observer<String> o = TestHelper.mockObserver();
201 InOrder io = inOrder(o);
202
203 Iterable<String> r2 = Arrays.asList("1", "2", "3");
204
205 r1.zipWith(r2, zipr2).subscribe(o);
206
207 r1.onNext("one-");
208 r1.onNext("two-");
209 r1.onError(new TestException());
210
211 io.verify(o).onNext("one-1");
212 io.verify(o).onNext("two-2");
213 io.verify(o).onError(any(TestException.class));
214
215 verify(o, never()).onComplete();
216
217 }
218
219 @Test
220 public void zipIterableIteratorThrows() {
221 PublishSubject<String> r1 = PublishSubject.create();
222 /* define an Observer to receive aggregated events */
223 Observer<String> o = TestHelper.mockObserver();
224 InOrder io = inOrder(o);
225
226 Iterable<String> r2 = new Iterable<String>() {
227 @Override
228 public Iterator<String> iterator() {
229 throw new TestException();
230 }
231 };
232
233 r1.zipWith(r2, zipr2).subscribe(o);
234
235 r1.onNext("one-");
236 r1.onNext("two-");
237 r1.onError(new TestException());
238
239 io.verify(o).onError(any(TestException.class));
240
241 verify(o, never()).onComplete();
242 verify(o, never()).onNext(any(String.class));
243
244 }
245
246 @Test
247 public void zipIterableHasNextThrows() {
248 PublishSubject<String> r1 = PublishSubject.create();
249 /* define an Observer to receive aggregated events */
250 Observer<String> o = TestHelper.mockObserver();
251 InOrder io = inOrder(o);
252
253 Iterable<String> r2 = new Iterable<String>() {
254
255 @Override
256 public Iterator<String> iterator() {
257 return new Iterator<String>() {
258 int count;
259
260 @Override
261 public boolean hasNext() {
262 if (count == 0) {
263 return true;
264 }
265 throw new TestException();
266 }
267
268 @Override
269 public String next() {
270 count++;
271 return "1";
272 }
273
274 @Override
275 public void remove() {
276 throw new UnsupportedOperationException("Not supported yet.");
277 }
278
279 };
280 }
281
282 };
283
284 r1.zipWith(r2, zipr2).subscribe(o);
285
286 r1.onNext("one-");
287 r1.onError(new TestException());
288
289 io.verify(o).onNext("one-1");
290 io.verify(o).onError(any(TestException.class));
291
292 verify(o, never()).onComplete();
293
294 }
295
296 @Test
297 public void zipIterableNextThrows() {
298 PublishSubject<String> r1 = PublishSubject.create();
299 /* define an Observer to receive aggregated events */
300 Observer<String> o = TestHelper.mockObserver();
301 InOrder io = inOrder(o);
302
303 Iterable<String> r2 = new Iterable<String>() {
304
305 @Override
306 public Iterator<String> iterator() {
307 return new Iterator<String>() {
308 @Override
309 public boolean hasNext() {
310 return true;
311 }
312
313 @Override
314 public String next() {
315 throw new TestException();
316 }
317
318 @Override
319 public void remove() {
320 throw new UnsupportedOperationException("Not supported yet.");
321 }
322
323 };
324 }
325
326 };
327
328 r1.zipWith(r2, zipr2).subscribe(o);
329
330 r1.onError(new TestException());
331
332 io.verify(o).onError(any(TestException.class));
333
334 verify(o, never()).onNext(any(String.class));
335 verify(o, never()).onComplete();
336
337 }
338
339 Consumer<String> printer = new Consumer<String>() {
340 @Override
341 public void accept(String pv) {
342 System.out.println(pv);
343 }
344 };
345
346 static final class SquareStr implements Function<Integer, String> {
347 final AtomicInteger counter = new AtomicInteger();
348 @Override
349 public String apply(Integer t1) {
350 counter.incrementAndGet();
351 System.out.println("Omg I'm calculating so hard: " + t1 + "*" + t1 + "=" + (t1 * t1));
352 return " " + (t1 * t1);
353 }
354 }
355
356 @Test
357 public void take2() {
358 Observable<Integer> o = Observable.just(1, 2, 3, 4, 5);
359 Iterable<String> it = Arrays.asList("a", "b", "c", "d", "e");
360
361 SquareStr squareStr = new SquareStr();
362
363 o.map(squareStr).zipWith(it, concat2Strings).take(2).subscribe(printer);
364
365 assertEquals(2, squareStr.counter.get());
366 }
367
368 @Test
369 public void dispose() {
370 TestHelper.checkDisposed(Observable.just(1).zipWith(Arrays.asList(1), new BiFunction<Integer, Integer, Object>() {
371 @Override
372 public Object apply(Integer a, Integer b) throws Exception {
373 return a + b;
374 }
375 }));
376 }
377
378 @Test
379 public void doubleOnSubscribe() {
380 TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Integer>, ObservableSource<Object>>() {
381 @Override
382 public ObservableSource<Object> apply(Observable<Integer> o) throws Exception {
383 return o.zipWith(Arrays.asList(1), new BiFunction<Integer, Integer, Object>() {
384 @Override
385 public Object apply(Integer a, Integer b) throws Exception {
386 return a + b;
387 }
388 });
389 }
390 });
391 }
392
393 @Test
394 public void iteratorThrows() {
395 Observable.just(1).zipWith(new CrashingIterable(100, 1, 100), new BiFunction<Integer, Integer, Object>() {
396 @Override
397 public Object apply(Integer a, Integer b) throws Exception {
398 return a + b;
399 }
400 })
401 .to(TestHelper.<Object>testConsumer())
402 .assertFailureAndMessage(TestException.class, "hasNext()");
403 }
404
405 @Test
406 public void badSource() {
407 List<Throwable> errors = TestHelper.trackPluginErrors();
408 try {
409 new Observable<Integer>() {
410 @Override
411 protected void subscribeActual(Observer<? super Integer> observer) {
412 observer.onSubscribe(Disposable.empty());
413 observer.onNext(1);
414 observer.onComplete();
415 observer.onNext(2);
416 observer.onError(new TestException());
417 observer.onComplete();
418 }
419 }
420 .zipWith(Arrays.asList(1), new BiFunction<Integer, Integer, Object>() {
421 @Override
422 public Object apply(Integer a, Integer b) throws Exception {
423 return a + b;
424 }
425 })
426 .test()
427 .assertResult(2);
428
429 TestHelper.assertUndeliverable(errors, 0, TestException.class);
430 } finally {
431 RxJavaPlugins.reset();
432 }
433 }
434 }