| 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 java.util.*; |
| 17 |
|
| 18 |
import org.junit.Test; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.exceptions.*; |
| 22 |
import io.reactivex.rxjava3.functions.*; |
| 23 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 24 |
import io.reactivex.rxjava3.parallel.*; |
| 25 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 26 |
import io.reactivex.rxjava3.testsupport.*; |
| 27 |
|
| 28 |
public class ParallelMapTryOptionalTest extends RxJavaTest implements Consumer<Object> { |
| 29 |
|
| 30 |
volatile int calls; |
| 31 |
|
| 32 |
@Override |
| 33 |
public void accept(Object t) throws Exception { |
| 34 |
calls++; |
| 35 |
} |
| 36 |
|
| 37 |
@Test |
| 38 |
public void mapNoError() { |
| 39 |
for (ParallelFailureHandling e : ParallelFailureHandling.values()) { |
| 40 |
Flowable.just(1) |
| 41 |
.parallel(1) |
| 42 |
.mapOptional(Optional::of, e) |
| 43 |
.sequential() |
| 44 |
.test() |
| 45 |
.assertResult(1); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void mapErrorNoError() { |
| 51 |
for (ParallelFailureHandling e : ParallelFailureHandling.values()) { |
| 52 |
Flowable.<Integer>error(new TestException()) |
| 53 |
.parallel(1) |
| 54 |
.mapOptional(Optional::of, e) |
| 55 |
.sequential() |
| 56 |
.test() |
| 57 |
.assertFailure(TestException.class); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void mapConditionalNoError() { |
| 63 |
for (ParallelFailureHandling e : ParallelFailureHandling.values()) { |
| 64 |
Flowable.just(1) |
| 65 |
.parallel(1) |
| 66 |
.mapOptional(Optional::of, e) |
| 67 |
.filter(Functions.alwaysTrue()) |
| 68 |
.sequential() |
| 69 |
.test() |
| 70 |
.assertResult(1); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
public void mapErrorConditionalNoError() { |
| 76 |
for (ParallelFailureHandling e : ParallelFailureHandling.values()) { |
| 77 |
Flowable.<Integer>error(new TestException()) |
| 78 |
.parallel(1) |
| 79 |
.mapOptional(Optional::of, e) |
| 80 |
.filter(Functions.alwaysTrue()) |
| 81 |
.sequential() |
| 82 |
.test() |
| 83 |
.assertFailure(TestException.class); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void mapFailWithError() { |
| 89 |
Flowable.range(0, 2) |
| 90 |
.parallel(1) |
| 91 |
.mapOptional(v -> Optional.of(1 / v), ParallelFailureHandling.ERROR) |
| 92 |
.sequential() |
| 93 |
.test() |
| 94 |
.assertFailure(ArithmeticException.class); |
| 95 |
} |
| 96 |
|
| 97 |
@Test |
| 98 |
public void mapFailWithStop() { |
| 99 |
Flowable.range(0, 2) |
| 100 |
.parallel(1) |
| 101 |
.mapOptional(v -> Optional.of(1 / v), ParallelFailureHandling.STOP) |
| 102 |
.sequential() |
| 103 |
.test() |
| 104 |
.assertResult(); |
| 105 |
} |
| 106 |
|
| 107 |
@Test |
| 108 |
public void mapFailWithRetry() { |
| 109 |
Flowable.range(0, 2) |
| 110 |
.parallel(1) |
| 111 |
.mapOptional(new Function<Integer, Optional<? extends Integer>>() { |
| 112 |
int count; |
| 113 |
@Override |
| 114 |
public Optional<? extends Integer> apply(Integer v) throws Exception { |
| 115 |
if (count++ == 1) { |
| 116 |
return Optional.of(-1); |
| 117 |
} |
| 118 |
return Optional.of(1 / v); |
| 119 |
} |
| 120 |
}, ParallelFailureHandling.RETRY) |
| 121 |
.sequential() |
| 122 |
.test() |
| 123 |
.assertResult(-1, 1); |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void mapFailWithRetryLimited() { |
| 128 |
Flowable.range(0, 2) |
| 129 |
.parallel(1) |
| 130 |
.mapOptional(v -> Optional.of(1 / v), new BiFunction<Long, Throwable, ParallelFailureHandling>() { |
| 131 |
@Override |
| 132 |
public ParallelFailureHandling apply(Long n, Throwable e) throws Exception { |
| 133 |
return n < 5 ? ParallelFailureHandling.RETRY : ParallelFailureHandling.SKIP; |
| 134 |
} |
| 135 |
}) |
| 136 |
.sequential() |
| 137 |
.test() |
| 138 |
.assertResult(1); |
| 139 |
} |
| 140 |
|
| 141 |
@Test |
| 142 |
public void mapFailWithSkip() { |
| 143 |
Flowable.range(0, 2) |
| 144 |
.parallel(1) |
| 145 |
.mapOptional(v -> Optional.of(1 / v), ParallelFailureHandling.SKIP) |
| 146 |
.sequential() |
| 147 |
.test() |
| 148 |
.assertResult(1); |
| 149 |
} |
| 150 |
|
| 151 |
@Test |
| 152 |
public void mapFailHandlerThrows() { |
| 153 |
TestSubscriberEx<Integer> ts = Flowable.range(0, 2) |
| 154 |
.parallel(1) |
| 155 |
.mapOptional(v -> Optional.of(1 / v), new BiFunction<Long, Throwable, ParallelFailureHandling>() { |
| 156 |
@Override |
| 157 |
public ParallelFailureHandling apply(Long n, Throwable e) throws Exception { |
| 158 |
throw new TestException(); |
| 159 |
} |
| 160 |
}) |
| 161 |
.sequential() |
| 162 |
.to(TestHelper.<Integer>testConsumer()) |
| 163 |
.assertFailure(CompositeException.class); |
| 164 |
|
| 165 |
TestHelper.assertCompositeExceptions(ts, ArithmeticException.class, TestException.class); |
| 166 |
} |
| 167 |
|
| 168 |
@Test |
| 169 |
public void mapInvalidSource() { |
| 170 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 171 |
try { |
| 172 |
new ParallelInvalid() |
| 173 |
.mapOptional(Optional::of, ParallelFailureHandling.ERROR) |
| 174 |
.sequential() |
| 175 |
.test(); |
| 176 |
|
| 177 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 178 |
} finally { |
| 179 |
RxJavaPlugins.reset(); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
@Test |
| 184 |
public void mapFailWithErrorConditional() { |
| 185 |
Flowable.range(0, 2) |
| 186 |
.parallel(1) |
| 187 |
.mapOptional(v -> Optional.of(1 / v), ParallelFailureHandling.ERROR) |
| 188 |
.filter(Functions.alwaysTrue()) |
| 189 |
.sequential() |
| 190 |
.test() |
| 191 |
.assertFailure(ArithmeticException.class); |
| 192 |
} |
| 193 |
|
| 194 |
@Test |
| 195 |
public void mapFailWithStopConditional() { |
| 196 |
Flowable.range(0, 2) |
| 197 |
.parallel(1) |
| 198 |
.mapOptional(v -> Optional.of(1 / v), ParallelFailureHandling.STOP) |
| 199 |
.filter(Functions.alwaysTrue()) |
| 200 |
.sequential() |
| 201 |
.test() |
| 202 |
.assertResult(); |
| 203 |
} |
| 204 |
|
| 205 |
@Test |
| 206 |
public void mapFailWithRetryConditional() { |
| 207 |
Flowable.range(0, 2) |
| 208 |
.parallel(1) |
| 209 |
.mapOptional(new Function<Integer, Optional<? extends Integer>>() { |
| 210 |
int count; |
| 211 |
@Override |
| 212 |
public Optional<? extends Integer> apply(Integer v) throws Exception { |
| 213 |
if (count++ == 1) { |
| 214 |
return Optional.of(-1); |
| 215 |
} |
| 216 |
return Optional.of(1 / v); |
| 217 |
} |
| 218 |
}, ParallelFailureHandling.RETRY) |
| 219 |
.filter(Functions.alwaysTrue()) |
| 220 |
.sequential() |
| 221 |
.test() |
| 222 |
.assertResult(-1, 1); |
| 223 |
} |
| 224 |
|
| 225 |
@Test |
| 226 |
public void mapFailWithRetryLimitedConditional() { |
| 227 |
Flowable.range(0, 2) |
| 228 |
.parallel(1) |
| 229 |
.mapOptional(v -> Optional.of(1 / v), new BiFunction<Long, Throwable, ParallelFailureHandling>() { |
| 230 |
@Override |
| 231 |
public ParallelFailureHandling apply(Long n, Throwable e) throws Exception { |
| 232 |
return n < 5 ? ParallelFailureHandling.RETRY : ParallelFailureHandling.SKIP; |
| 233 |
} |
| 234 |
}) |
| 235 |
.filter(Functions.alwaysTrue()) |
| 236 |
.sequential() |
| 237 |
.test() |
| 238 |
.assertResult(1); |
| 239 |
} |
| 240 |
|
| 241 |
@Test |
| 242 |
public void mapFailWithSkipConditional() { |
| 243 |
Flowable.range(0, 2) |
| 244 |
.parallel(1) |
| 245 |
.mapOptional(v -> Optional.of(1 / v), ParallelFailureHandling.SKIP) |
| 246 |
.filter(Functions.alwaysTrue()) |
| 247 |
.sequential() |
| 248 |
.test() |
| 249 |
.assertResult(1); |
| 250 |
} |
| 251 |
|
| 252 |
@Test |
| 253 |
public void mapFailHandlerThrowsConditional() { |
| 254 |
TestSubscriberEx<Integer> ts = Flowable.range(0, 2) |
| 255 |
.parallel(1) |
| 256 |
.mapOptional(v -> Optional.of(1 / v), new BiFunction<Long, Throwable, ParallelFailureHandling>() { |
| 257 |
@Override |
| 258 |
public ParallelFailureHandling apply(Long n, Throwable e) throws Exception { |
| 259 |
throw new TestException(); |
| 260 |
} |
| 261 |
}) |
| 262 |
.filter(Functions.alwaysTrue()) |
| 263 |
.sequential() |
| 264 |
.to(TestHelper.<Integer>testConsumer()) |
| 265 |
.assertFailure(CompositeException.class); |
| 266 |
|
| 267 |
TestHelper.assertCompositeExceptions(ts, ArithmeticException.class, TestException.class); |
| 268 |
} |
| 269 |
|
| 270 |
@Test |
| 271 |
public void mapWrongParallelismConditional() { |
| 272 |
TestHelper.checkInvalidParallelSubscribers( |
| 273 |
Flowable.just(1).parallel(1) |
| 274 |
.mapOptional(Optional::of, ParallelFailureHandling.ERROR) |
| 275 |
.filter(Functions.alwaysTrue()) |
| 276 |
); |
| 277 |
} |
| 278 |
|
| 279 |
@Test |
| 280 |
public void mapInvalidSourceConditional() { |
| 281 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 282 |
try { |
| 283 |
new ParallelInvalid() |
| 284 |
.mapOptional(Optional::of, ParallelFailureHandling.ERROR) |
| 285 |
.filter(Functions.alwaysTrue()) |
| 286 |
.sequential() |
| 287 |
.test(); |
| 288 |
|
| 289 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 290 |
} finally { |
| 291 |
RxJavaPlugins.reset(); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
@Test |
| 296 |
public void failureHandlingEnum() { |
| 297 |
TestHelper.checkEnum(ParallelFailureHandling.class); |
| 298 |
} |
| 299 |
|
| 300 |
@Test |
| 301 |
public void allNone() { |
| 302 |
Flowable.range(1, 1000) |
| 303 |
.parallel() |
| 304 |
.mapOptional(v -> Optional.empty(), ParallelFailureHandling.SKIP) |
| 305 |
.sequential() |
| 306 |
.test() |
| 307 |
.assertResult(); |
| 308 |
} |
| 309 |
|
| 310 |
@Test |
| 311 |
public void allNoneConditional() { |
| 312 |
Flowable.range(1, 1000) |
| 313 |
.parallel() |
| 314 |
.mapOptional(v -> Optional.empty(), ParallelFailureHandling.SKIP) |
| 315 |
.filter(v -> true) |
| 316 |
.sequential() |
| 317 |
.test() |
| 318 |
.assertResult(); |
| 319 |
} |
| 320 |
|
| 321 |
@Test |
| 322 |
public void mixed() { |
| 323 |
Flowable.range(1, 1000) |
| 324 |
.parallel() |
| 325 |
.mapOptional(v -> v % 2 == 0 ? Optional.of(v) : Optional.empty(), ParallelFailureHandling.SKIP) |
| 326 |
.sequential() |
| 327 |
.test() |
| 328 |
.assertValueCount(500) |
| 329 |
.assertNoErrors() |
| 330 |
.assertComplete(); |
| 331 |
} |
| 332 |
|
| 333 |
@Test |
| 334 |
public void mixedConditional() { |
| 335 |
Flowable.range(1, 1000) |
| 336 |
.parallel() |
| 337 |
.mapOptional(v -> v % 2 == 0 ? Optional.of(v) : Optional.empty(), ParallelFailureHandling.SKIP) |
| 338 |
.filter(v -> true) |
| 339 |
.sequential() |
| 340 |
.test() |
| 341 |
.assertValueCount(500) |
| 342 |
.assertNoErrors() |
| 343 |
.assertComplete(); |
| 344 |
} |
| 345 |
|
| 346 |
@Test |
| 347 |
public void mixedConditional2() { |
| 348 |
Flowable.range(1, 1000) |
| 349 |
.parallel() |
| 350 |
.mapOptional(v -> v % 2 == 0 ? Optional.of(v) : Optional.empty(), ParallelFailureHandling.SKIP) |
| 351 |
.filter(v -> v % 4 == 0) |
| 352 |
.sequential() |
| 353 |
.test() |
| 354 |
.assertValueCount(250) |
| 355 |
.assertNoErrors() |
| 356 |
.assertComplete(); |
| 357 |
} |
| 358 |
|
| 359 |
@Test |
| 360 |
public void invalidSubscriberCount() { |
| 361 |
TestHelper.checkInvalidParallelSubscribers( |
| 362 |
Flowable.range(1, 10).parallel() |
| 363 |
.mapOptional(Optional::of, ParallelFailureHandling.SKIP) |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
@Test |
| 368 |
public void doubleOnSubscribe() { |
| 369 |
TestHelper.checkDoubleOnSubscribeParallel( |
| 370 |
p -> p.mapOptional(Optional::of, ParallelFailureHandling.ERROR) |
| 371 |
); |
| 372 |
|
| 373 |
TestHelper.checkDoubleOnSubscribeParallel( |
| 374 |
p -> p.mapOptional(Optional::of, ParallelFailureHandling.ERROR) |
| 375 |
.filter(v -> true) |
| 376 |
); |
| 377 |
} |
| 378 |
} |