Debian Codegraph @

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