Debian Codegraph @

src/test/java/io/reactivex/rxjava3/parallel/ParallelCollectTest.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.parallel;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
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.plugins.RxJavaPlugins;
26 import io.reactivex.rxjava3.processors.PublishProcessor;
27 import io.reactivex.rxjava3.subscribers.TestSubscriber;
28 import io.reactivex.rxjava3.testsupport.TestHelper;
29
30 public class ParallelCollectTest extends RxJavaTest {
31
32 @Test
33 public void subscriberCount() {
34 ParallelFlowableTest.checkSubscriberCount(Flowable.range(1, 5).parallel()
35 .collect(new Supplier<List<Integer>>() {
36 @Override
37 public List<Integer> get() throws Exception {
38 return new ArrayList<>();
39 }
40 }, new BiConsumer<List<Integer>, Integer>() {
41 @Override
42 public void accept(List<Integer> a, Integer b) throws Exception {
43 a.add(b);
44 }
45 }));
46 }
47
48 @Test
49 public void initialCrash() {
50 Flowable.range(1, 5)
51 .parallel()
52 .collect(new Supplier<List<Integer>>() {
53 @Override
54 public List<Integer> get() throws Exception {
55 throw new TestException();
56 }
57 }, new BiConsumer<List<Integer>, Integer>() {
58 @Override
59 public void accept(List<Integer> a, Integer b) throws Exception {
60 a.add(b);
61 }
62 })
63 .sequential()
64 .test()
65 .assertFailure(TestException.class);
66 }
67
68 @Test
69 public void reducerCrash() {
70 Flowable.range(1, 5)
71 .parallel()
72 .collect(new Supplier<List<Integer>>() {
73 @Override
74 public List<Integer> get() throws Exception {
75 return new ArrayList<>();
76 }
77 }, new BiConsumer<List<Integer>, Integer>() {
78 @Override
79 public void accept(List<Integer> a, Integer b) throws Exception {
80 if (b == 3) {
81 throw new TestException();
82 }
83 a.add(b);
84 }
85 })
86 .sequential()
87 .test()
88 .assertFailure(TestException.class);
89 }
90
91 @Test
92 public void cancel() {
93 PublishProcessor<Integer> pp = PublishProcessor.create();
94
95 TestSubscriber<List<Integer>> ts = pp
96 .parallel()
97 .collect(new Supplier<List<Integer>>() {
98 @Override
99 public List<Integer> get() throws Exception {
100 return new ArrayList<>();
101 }
102 }, new BiConsumer<List<Integer>, Integer>() {
103 @Override
104 public void accept(List<Integer> a, Integer b) throws Exception {
105 a.add(b);
106 }
107 })
108 .sequential()
109 .test();
110
111 assertTrue(pp.hasSubscribers());
112
113 ts.cancel();
114
115 assertFalse(pp.hasSubscribers());
116 }
117
118 @Test
119 public void error() {
120 Flowable.<Integer>error(new TestException())
121 .parallel()
122 .collect(new Supplier<List<Integer>>() {
123 @Override
124 public List<Integer> get() throws Exception {
125 return new ArrayList<>();
126 }
127 }, new BiConsumer<List<Integer>, Integer>() {
128 @Override
129 public void accept(List<Integer> a, Integer b) throws Exception {
130 a.add(b);
131 }
132 })
133 .sequential()
134 .test()
135 .assertFailure(TestException.class);
136 }
137
138 @Test
139 public void doubleError() {
140 List<Throwable> errors = TestHelper.trackPluginErrors();
141 try {
142 new ParallelInvalid()
143 .collect(new Supplier<List<Object>>() {
144 @Override
145 public List<Object> get() throws Exception {
146 return new ArrayList<>();
147 }
148 }, new BiConsumer<List<Object>, Object>() {
149 @Override
150 public void accept(List<Object> a, Object b) throws Exception {
151 a.add(b);
152 }
153 })
154 .sequential()
155 .test()
156 .assertFailure(TestException.class);
157
158 assertFalse(errors.isEmpty());
159 for (Throwable ex : errors) {
160 assertTrue(ex.toString(), ex.getCause() instanceof TestException);
161 }
162 } finally {
163 RxJavaPlugins.reset();
164 }
165 }
166
167 @Test
168 public void doubleOnSubscribe() {
169 TestHelper.checkDoubleOnSubscribeParallel(
170 pf -> pf.collect(ArrayList::new, ArrayList::add)
171 );
172 }
173 }