Debian Codegraph @

src/test/java/io/reactivex/rxjava3/parallel/ParallelSortedJoinTest.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.io.IOException;
19 import java.util.*;
20 import java.util.concurrent.TimeUnit;
21
22 import org.junit.Test;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.exceptions.TestException;
26 import io.reactivex.rxjava3.internal.functions.Functions;
27 import io.reactivex.rxjava3.internal.operators.parallel.ParallelSortedJoin;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.processors.*;
30 import io.reactivex.rxjava3.schedulers.Schedulers;
31 import io.reactivex.rxjava3.subscribers.TestSubscriber;
32 import io.reactivex.rxjava3.testsupport.TestHelper;
33
34 public class ParallelSortedJoinTest extends RxJavaTest {
35
36 @Test
37 public void cancel() {
38 PublishProcessor<Integer> pp = PublishProcessor.create();
39
40 TestSubscriber<Integer> ts = pp
41 .parallel()
42 .sorted(Functions.<Integer>naturalComparator())
43 .test();
44
45 assertTrue(pp.hasSubscribers());
46
47 ts.cancel();
48
49 assertFalse(pp.hasSubscribers());
50 }
51
52 @Test
53 public void error() {
54 List<Throwable> errors = TestHelper.trackPluginErrors();
55
56 try {
57 Flowable.<Integer>error(new TestException())
58 .parallel()
59 .sorted(Functions.<Integer>naturalComparator())
60 .test()
61 .assertFailure(TestException.class);
62
63 assertTrue(errors.isEmpty());
64 } finally {
65 RxJavaPlugins.reset();
66 }
67 }
68
69 @Test
70 public void error3() {
71 List<Throwable> errors = TestHelper.trackPluginErrors();
72
73 try {
74 Flowable.<Integer>error(new TestException())
75 .parallel()
76 .sorted(Functions.<Integer>naturalComparator())
77 .test(0)
78 .assertFailure(TestException.class);
79
80 assertTrue(errors.isEmpty());
81 } finally {
82 RxJavaPlugins.reset();
83 }
84 }
85
86 @Test
87 public void error2() {
88 List<Throwable> errors = TestHelper.trackPluginErrors();
89
90 try {
91 ParallelFlowable.fromArray(Flowable.<Integer>error(new IOException()), Flowable.<Integer>error(new TestException()))
92 .sorted(Functions.<Integer>naturalComparator())
93 .test()
94 .assertFailure(IOException.class);
95
96 TestHelper.assertUndeliverable(errors, 0, TestException.class);
97 } finally {
98 RxJavaPlugins.reset();
99 }
100 }
101
102 @Test
103 public void comparerCrash() {
104 Flowable.fromArray(4, 3, 2, 1)
105 .parallel(2)
106 .sorted(new Comparator<Integer>() {
107 @Override
108 public int compare(Integer o1, Integer o2) {
109 if (o1 == 4 && o2 == 3) {
110 throw new TestException();
111 }
112 return o1.compareTo(o2);
113 }
114 })
115 .test()
116 .assertFailure(TestException.class, 1, 2);
117 }
118
119 @Test
120 public void empty() {
121 Flowable.<Integer>empty()
122 .parallel()
123 .sorted(Functions.<Integer>naturalComparator())
124 .test()
125 .assertResult();
126 }
127
128 @Test
129 public void asyncDrain() {
130 Integer[] values = new Integer[100 * 1000];
131 for (int i = 0; i < values.length; i++) {
132 values[i] = values.length - i;
133 }
134
135 TestSubscriber<Integer> ts = Flowable.fromArray(values)
136 .parallel(2)
137 .runOn(Schedulers.computation())
138 .sorted(Functions.naturalComparator())
139 .observeOn(Schedulers.single())
140 .test();
141
142 ts
143 .awaitDone(5, TimeUnit.SECONDS)
144 .assertValueCount(values.length)
145 .assertNoErrors()
146 .assertComplete();
147
148 List<Integer> list = ts.values();
149 for (int i = 0; i < values.length; i++) {
150 assertEquals(i + 1, list.get(i).intValue());
151 }
152 }
153
154 @Test
155 public void sortCancelRace() {
156 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
157 final ReplayProcessor<Integer> pp = ReplayProcessor.create();
158 pp.onNext(1);
159 pp.onNext(2);
160
161 final TestSubscriber<Integer> ts = pp.parallel(2)
162 .sorted(Functions.naturalComparator())
163 .test();
164
165 Runnable r1 = new Runnable() {
166 @Override
167 public void run() {
168 pp.onComplete();
169 }
170 };
171
172 Runnable r2 = new Runnable() {
173 @Override
174 public void run() {
175 ts.cancel();
176 }
177 };
178
179 TestHelper.race(r1, r2);
180 }
181 }
182
183 @Test
184 public void sortCancelRace2() {
185 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
186 final ReplayProcessor<Integer> pp = ReplayProcessor.create();
187 pp.onNext(1);
188 pp.onNext(2);
189
190 final TestSubscriber<Integer> ts = pp.parallel(2)
191 .sorted(Functions.naturalComparator())
192 .test(0);
193
194 Runnable r1 = new Runnable() {
195 @Override
196 public void run() {
197 pp.onComplete();
198 }
199 };
200
201 Runnable r2 = new Runnable() {
202 @Override
203 public void run() {
204 ts.cancel();
205 }
206 };
207
208 TestHelper.race(r1, r2);
209 }
210 }
211
212 @Test
213 public void badRequest() {
214 TestHelper.assertBadRequestReported(PublishProcessor.<Integer>create().parallel().sorted(Functions.naturalComparator()));
215 }
216
217 @Test
218 public void comparatorCrashWhileMainOnError() throws Throwable {
219 TestHelper.withErrorTracking(errors -> {
220 PublishProcessor<List<Integer>> pp1 = PublishProcessor.create();
221 PublishProcessor<List<Integer>> pp2 = PublishProcessor.create();
222
223 new ParallelSortedJoin<>(ParallelFlowable.fromArray(pp1, pp2)
224 , (a, b) -> {
225 pp1.onError(new IOException());
226 throw new TestException();
227 })
228 .test();
229
230 pp1.onNext(Arrays.asList(1));
231 pp2.onNext(Arrays.asList(2));
232
233 pp1.onComplete();
234 pp2.onComplete();
235
236 TestHelper.assertUndeliverable(errors, 0, TestException.class);
237 });
238 }
239 }