Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/jdk8/ParallelMapOptionalTest.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.jdk8;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19 import java.util.concurrent.TimeUnit;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.internal.functions.Functions;
27 import io.reactivex.rxjava3.parallel.*;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.schedulers.Schedulers;
30 import io.reactivex.rxjava3.testsupport.TestHelper;
31
32 public class ParallelMapOptionalTest extends RxJavaTest {
33
34 @Test
35 public void doubleFilter() {
36 Flowable.range(1, 10)
37 .parallel()
38 .mapOptional(Optional::of)
39 .filter(new Predicate<Integer>() {
40 @Override
41 public boolean test(Integer v) throws Exception {
42 return v % 2 == 0;
43 }
44 })
45 .filter(new Predicate<Integer>() {
46 @Override
47 public boolean test(Integer v) throws Exception {
48 return v % 3 == 0;
49 }
50 })
51 .sequential()
52 .test()
53 .assertResult(6);
54 }
55
56 @Test
57 public void doubleFilterAsync() {
58 Flowable.range(1, 10)
59 .parallel()
60 .runOn(Schedulers.computation())
61 .mapOptional(Optional::of)
62 .filter(new Predicate<Integer>() {
63 @Override
64 public boolean test(Integer v) throws Exception {
65 return v % 2 == 0;
66 }
67 })
68 .filter(new Predicate<Integer>() {
69 @Override
70 public boolean test(Integer v) throws Exception {
71 return v % 3 == 0;
72 }
73 })
74 .sequential()
75 .test()
76 .awaitDone(5, TimeUnit.SECONDS)
77 .assertResult(6);
78 }
79
80 @Test
81 public void doubleError() {
82 List<Throwable> errors = TestHelper.trackPluginErrors();
83 try {
84 new ParallelInvalid()
85 .mapOptional(Optional::of)
86 .sequential()
87 .test()
88 .assertFailure(TestException.class);
89
90 assertFalse(errors.isEmpty());
91 for (Throwable ex : errors) {
92 assertTrue(ex.toString(), ex.getCause() instanceof TestException);
93 }
94 } finally {
95 RxJavaPlugins.reset();
96 }
97 }
98
99 @Test
100 public void doubleError2() {
101 List<Throwable> errors = TestHelper.trackPluginErrors();
102 try {
103 new ParallelInvalid()
104 .mapOptional(Optional::of)
105 .filter(Functions.alwaysTrue())
106 .sequential()
107 .test()
108 .assertFailure(TestException.class);
109
110 assertFalse(errors.isEmpty());
111 for (Throwable ex : errors) {
112 assertTrue(ex.toString(), ex.getCause() instanceof TestException);
113 }
114 } finally {
115 RxJavaPlugins.reset();
116 }
117 }
118
119 @Test
120 public void error() {
121 Flowable.error(new TestException())
122 .parallel()
123 .mapOptional(Optional::of)
124 .sequential()
125 .test()
126 .assertFailure(TestException.class);
127 }
128
129 @Test
130 public void mapCrash() {
131 Flowable.just(1)
132 .parallel()
133 .mapOptional(v -> { throw new TestException(); })
134 .sequential()
135 .test()
136 .assertFailure(TestException.class);
137 }
138
139 @Test
140 public void mapCrashConditional() {
141 Flowable.just(1)
142 .parallel()
143 .mapOptional(v -> { throw new TestException(); })
144 .filter(Functions.alwaysTrue())
145 .sequential()
146 .test()
147 .assertFailure(TestException.class);
148 }
149
150 @Test
151 public void mapCrashConditional2() {
152 Flowable.just(1)
153 .parallel()
154 .runOn(Schedulers.computation())
155 .mapOptional(v -> { throw new TestException(); })
156 .filter(Functions.alwaysTrue())
157 .sequential()
158 .test()
159 .awaitDone(5, TimeUnit.SECONDS)
160 .assertFailure(TestException.class);
161 }
162
163 @Test
164 public void allNone() {
165 Flowable.range(1, 1000)
166 .parallel()
167 .mapOptional(v -> Optional.empty())
168 .sequential()
169 .test()
170 .assertResult();
171 }
172
173 @Test
174 public void allNoneConditional() {
175 Flowable.range(1, 1000)
176 .parallel()
177 .mapOptional(v -> Optional.empty())
178 .filter(v -> true)
179 .sequential()
180 .test()
181 .assertResult();
182 }
183
184 @Test
185 public void mixed() {
186 Flowable.range(1, 1000)
187 .parallel()
188 .mapOptional(v -> v % 2 == 0 ? Optional.of(v) : Optional.empty())
189 .sequential()
190 .test()
191 .assertValueCount(500)
192 .assertNoErrors()
193 .assertComplete();
194 }
195
196 @Test
197 public void mixedConditional() {
198 Flowable.range(1, 1000)
199 .parallel()
200 .mapOptional(v -> v % 2 == 0 ? Optional.of(v) : Optional.empty())
201 .filter(v -> true)
202 .sequential()
203 .test()
204 .assertValueCount(500)
205 .assertNoErrors()
206 .assertComplete();
207 }
208
209 @Test
210 public void invalidSubscriberCount() {
211 TestHelper.checkInvalidParallelSubscribers(
212 Flowable.range(1, 10).parallel()
213 .mapOptional(Optional::of)
214 );
215 }
216
217 @Test
218 public void doubleOnSubscribe() {
219 TestHelper.checkDoubleOnSubscribeParallel(
220 p -> p.mapOptional(Optional::of)
221 );
222
223 TestHelper.checkDoubleOnSubscribeParallel(
224 p -> p.mapOptional(Optional::of)
225 .filter(v -> true)
226 );
227 }
228 }