| 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 java.util.List; |
| 17 |
import java.util.concurrent.atomic.AtomicReference; |
| 18 |
|
| 19 |
import org.junit.Test; |
| 20 |
import org.reactivestreams.Subscriber; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.*; |
| 25 |
import io.reactivex.rxjava3.functions.Function; |
| 26 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 27 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 28 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 29 |
import io.reactivex.rxjava3.testsupport.*; |
| 30 |
|
| 31 |
public class ParallelJoinTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void overflowFastpath() { |
| 35 |
new ParallelFlowable<Integer>() { |
| 36 |
@Override |
| 37 |
public void subscribe(Subscriber<? super Integer>[] subscribers) { |
| 38 |
subscribers[0].onSubscribe(new BooleanSubscription()); |
| 39 |
subscribers[0].onNext(1); |
| 40 |
subscribers[0].onNext(2); |
| 41 |
subscribers[0].onNext(3); |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public int parallelism() { |
| 46 |
return 1; |
| 47 |
} |
| 48 |
} |
| 49 |
.sequential(1) |
| 50 |
.test(0) |
| 51 |
.assertFailure(MissingBackpressureException.class); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void overflowSlowpath() { |
| 56 |
@SuppressWarnings("unchecked") |
| 57 |
final Subscriber<? super Integer>[] subs = new Subscriber[1]; |
| 58 |
|
| 59 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1) { |
| 60 |
@Override |
| 61 |
public void onNext(Integer t) { |
| 62 |
super.onNext(t); |
| 63 |
subs[0].onNext(2); |
| 64 |
subs[0].onNext(3); |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
new ParallelFlowable<Integer>() { |
| 69 |
@Override |
| 70 |
public void subscribe(Subscriber<? super Integer>[] subscribers) { |
| 71 |
subs[0] = subscribers[0]; |
| 72 |
subscribers[0].onSubscribe(new BooleanSubscription()); |
| 73 |
subscribers[0].onNext(1); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public int parallelism() { |
| 78 |
return 1; |
| 79 |
} |
| 80 |
} |
| 81 |
.sequential(1) |
| 82 |
.subscribe(ts); |
| 83 |
|
| 84 |
ts.assertFailure(MissingBackpressureException.class, 1); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void emptyBackpressured() { |
| 89 |
Flowable.empty() |
| 90 |
.parallel() |
| 91 |
.sequential() |
| 92 |
.test(0) |
| 93 |
.assertResult(); |
| 94 |
} |
| 95 |
|
| 96 |
@Test |
| 97 |
public void overflowFastpathDelayError() { |
| 98 |
new ParallelFlowable<Integer>() { |
| 99 |
@Override |
| 100 |
public void subscribe(Subscriber<? super Integer>[] subscribers) { |
| 101 |
subscribers[0].onSubscribe(new BooleanSubscription()); |
| 102 |
subscribers[0].onNext(1); |
| 103 |
subscribers[0].onNext(2); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public int parallelism() { |
| 108 |
return 1; |
| 109 |
} |
| 110 |
} |
| 111 |
.sequentialDelayError(1) |
| 112 |
.test(0) |
| 113 |
.requestMore(1) |
| 114 |
.assertFailure(MissingBackpressureException.class, 1); |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public void overflowSlowpathDelayError() { |
| 119 |
@SuppressWarnings("unchecked") |
| 120 |
final Subscriber<? super Integer>[] subs = new Subscriber[1]; |
| 121 |
|
| 122 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1) { |
| 123 |
@Override |
| 124 |
public void onNext(Integer t) { |
| 125 |
super.onNext(t); |
| 126 |
if (t == 1) { |
| 127 |
subs[0].onNext(2); |
| 128 |
subs[0].onNext(3); |
| 129 |
} |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
new ParallelFlowable<Integer>() { |
| 134 |
@Override |
| 135 |
public void subscribe(Subscriber<? super Integer>[] subscribers) { |
| 136 |
subs[0] = subscribers[0]; |
| 137 |
subscribers[0].onSubscribe(new BooleanSubscription()); |
| 138 |
subscribers[0].onNext(1); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public int parallelism() { |
| 143 |
return 1; |
| 144 |
} |
| 145 |
} |
| 146 |
.sequentialDelayError(1) |
| 147 |
.subscribe(ts); |
| 148 |
|
| 149 |
ts.request(1); |
| 150 |
|
| 151 |
ts.assertFailure(MissingBackpressureException.class, 1, 2); |
| 152 |
} |
| 153 |
|
| 154 |
@Test |
| 155 |
public void emptyBackpressuredDelayError() { |
| 156 |
Flowable.empty() |
| 157 |
.parallel() |
| 158 |
.sequentialDelayError() |
| 159 |
.test(0) |
| 160 |
.assertResult(); |
| 161 |
} |
| 162 |
|
| 163 |
@Test |
| 164 |
public void delayError() { |
| 165 |
TestSubscriberEx<Integer> flow = Flowable.range(1, 2) |
| 166 |
.parallel(2) |
| 167 |
.map(new Function<Integer, Integer>() { |
| 168 |
@Override |
| 169 |
public Integer apply(Integer v) throws Exception { |
| 170 |
throw new TestException(); |
| 171 |
} |
| 172 |
}) |
| 173 |
.sequentialDelayError() |
| 174 |
.to(TestHelper.<Integer>testConsumer()) |
| 175 |
.assertFailure(CompositeException.class); |
| 176 |
|
| 177 |
List<Throwable> error = TestHelper.errorList(flow); |
| 178 |
TestHelper.assertError(error, 0, TestException.class); |
| 179 |
TestHelper.assertError(error, 1, TestException.class); |
| 180 |
} |
| 181 |
|
| 182 |
@Test |
| 183 |
public void normalDelayError() { |
| 184 |
Flowable.just(1) |
| 185 |
.parallel(1) |
| 186 |
.sequentialDelayError(1) |
| 187 |
.test() |
| 188 |
.assertResult(1); |
| 189 |
} |
| 190 |
|
| 191 |
@Test |
| 192 |
public void rangeDelayError() { |
| 193 |
Flowable.range(1, 2) |
| 194 |
.parallel(1) |
| 195 |
.sequentialDelayError(1) |
| 196 |
.take(1) |
| 197 |
.test() |
| 198 |
.assertResult(1); |
| 199 |
} |
| 200 |
|
| 201 |
@Test |
| 202 |
public void rangeDelayErrorBackpressure() { |
| 203 |
Flowable.range(1, 3) |
| 204 |
.parallel(1) |
| 205 |
.sequentialDelayError(1) |
| 206 |
.take(2) |
| 207 |
.rebatchRequests(1) |
| 208 |
.test() |
| 209 |
.assertResult(1, 2); |
| 210 |
} |
| 211 |
|
| 212 |
@Test |
| 213 |
public void rangeDelayErrorBackpressure2() { |
| 214 |
Flowable.range(1, 3) |
| 215 |
.parallel(1) |
| 216 |
.sequentialDelayError(1) |
| 217 |
.rebatchRequests(1) |
| 218 |
.test() |
| 219 |
.assertResult(1, 2, 3); |
| 220 |
} |
| 221 |
|
| 222 |
@Test |
| 223 |
public void delayErrorCancelBackpressured() { |
| 224 |
TestSubscriber<Integer> ts = Flowable.range(1, 3) |
| 225 |
.parallel(1) |
| 226 |
.sequentialDelayError(1) |
| 227 |
.test(0); |
| 228 |
|
| 229 |
ts |
| 230 |
.cancel(); |
| 231 |
|
| 232 |
ts.assertEmpty(); |
| 233 |
} |
| 234 |
|
| 235 |
@Test |
| 236 |
public void delayErrorCancelBackpressured2() { |
| 237 |
TestSubscriber<Integer> ts = Flowable.<Integer>empty() |
| 238 |
.parallel(1) |
| 239 |
.sequentialDelayError(1) |
| 240 |
.test(0); |
| 241 |
|
| 242 |
ts.assertResult(); |
| 243 |
} |
| 244 |
|
| 245 |
@Test |
| 246 |
public void consumerCancelsAfterOne() { |
| 247 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1) { |
| 248 |
@Override |
| 249 |
public void onNext(Integer t) { |
| 250 |
super.onNext(t); |
| 251 |
cancel(); |
| 252 |
onComplete(); |
| 253 |
} |
| 254 |
}; |
| 255 |
|
| 256 |
Flowable.range(1, 3) |
| 257 |
.parallel(1) |
| 258 |
.sequential() |
| 259 |
.subscribe(ts); |
| 260 |
|
| 261 |
ts.assertResult(1); |
| 262 |
} |
| 263 |
|
| 264 |
@Test |
| 265 |
public void delayErrorConsumerCancelsAfterOne() { |
| 266 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1) { |
| 267 |
@Override |
| 268 |
public void onNext(Integer t) { |
| 269 |
super.onNext(t); |
| 270 |
cancel(); |
| 271 |
onComplete(); |
| 272 |
} |
| 273 |
}; |
| 274 |
|
| 275 |
Flowable.range(1, 3) |
| 276 |
.parallel(1) |
| 277 |
.sequentialDelayError() |
| 278 |
.subscribe(ts); |
| 279 |
|
| 280 |
ts.assertResult(1); |
| 281 |
} |
| 282 |
|
| 283 |
@Test |
| 284 |
public void delayErrorDrainTrigger() { |
| 285 |
Flowable.range(1, 3) |
| 286 |
.parallel(1) |
| 287 |
.sequentialDelayError() |
| 288 |
.test(0) |
| 289 |
.requestMore(1) |
| 290 |
.assertValues(1) |
| 291 |
.requestMore(1) |
| 292 |
.assertValues(1, 2) |
| 293 |
.requestMore(1) |
| 294 |
.assertResult(1, 2, 3); |
| 295 |
} |
| 296 |
|
| 297 |
@Test |
| 298 |
public void failedRailIsIgnored() { |
| 299 |
Flowable.range(1, 4) |
| 300 |
.parallel(2) |
| 301 |
.map(new Function<Integer, Integer>() { |
| 302 |
@Override |
| 303 |
public Integer apply(Integer v) throws Exception { |
| 304 |
if (v == 1) { |
| 305 |
throw new TestException(); |
| 306 |
} |
| 307 |
return v; |
| 308 |
} |
| 309 |
}) |
| 310 |
.sequentialDelayError() |
| 311 |
.test() |
| 312 |
.assertFailure(TestException.class, 2, 3, 4); |
| 313 |
} |
| 314 |
|
| 315 |
@Test |
| 316 |
public void failedRailIsIgnoredHidden() { |
| 317 |
Flowable.range(1, 4).hide() |
| 318 |
.parallel(2) |
| 319 |
.map(new Function<Integer, Integer>() { |
| 320 |
@Override |
| 321 |
public Integer apply(Integer v) throws Exception { |
| 322 |
if (v == 1) { |
| 323 |
throw new TestException(); |
| 324 |
} |
| 325 |
return v; |
| 326 |
} |
| 327 |
}) |
| 328 |
.sequentialDelayError() |
| 329 |
.test() |
| 330 |
.assertFailure(TestException.class, 2, 3, 4); |
| 331 |
} |
| 332 |
|
| 333 |
@Test |
| 334 |
public void takeUntil() { |
| 335 |
Flowable.range(1, 10) |
| 336 |
.parallel(1) |
| 337 |
.sequential() |
| 338 |
.takeUntil(v -> true) |
| 339 |
.test(0L) |
| 340 |
.requestMore(100) |
| 341 |
.assertResult(1); |
| 342 |
} |
| 343 |
|
| 344 |
@Test |
| 345 |
public void takeUntilDelayError() { |
| 346 |
Flowable.range(1, 10) |
| 347 |
.parallel(1) |
| 348 |
.sequentialDelayError() |
| 349 |
.takeUntil(v -> true) |
| 350 |
.test(0L) |
| 351 |
.requestMore(100) |
| 352 |
.assertResult(1); |
| 353 |
} |
| 354 |
|
| 355 |
@Test |
| 356 |
public void oneItemNext() { |
| 357 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 358 |
|
| 359 |
TestSubscriber<Integer> ts = pp.parallel(1) |
| 360 |
.sequential() |
| 361 |
.test(0L); |
| 362 |
|
| 363 |
pp.onNext(1); |
| 364 |
|
| 365 |
ts.requestMore(10) |
| 366 |
.assertValuesOnly(1); |
| 367 |
} |
| 368 |
|
| 369 |
@Test |
| 370 |
public void delayErrorOneItemNext() { |
| 371 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 372 |
|
| 373 |
TestSubscriber<Integer> ts = pp.parallel(1) |
| 374 |
.sequentialDelayError() |
| 375 |
.test(0L); |
| 376 |
|
| 377 |
pp.onNext(1); |
| 378 |
|
| 379 |
ts.requestMore(10) |
| 380 |
.assertValuesOnly(1); |
| 381 |
} |
| 382 |
|
| 383 |
@Test |
| 384 |
public void onNextWhileProcessingSlowPath() { |
| 385 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 386 |
|
| 387 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 388 |
@Override |
| 389 |
public void onNext(@NonNull Integer t) { |
| 390 |
super.onNext(t); |
| 391 |
if (t == 1) { |
| 392 |
pp.onNext(2); |
| 393 |
} |
| 394 |
} |
| 395 |
}; |
| 396 |
|
| 397 |
ParallelFlowable.fromArray(pp) |
| 398 |
.sequential() |
| 399 |
.subscribeWith(ts); |
| 400 |
|
| 401 |
pp.onNext(1); |
| 402 |
|
| 403 |
ts |
| 404 |
.assertValuesOnly(1, 2); |
| 405 |
} |
| 406 |
|
| 407 |
@Test |
| 408 |
public void delayErrorOnNextWhileProcessingSlowPath() { |
| 409 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 410 |
|
| 411 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 412 |
@Override |
| 413 |
public void onNext(@NonNull Integer t) { |
| 414 |
super.onNext(t); |
| 415 |
if (t == 1) { |
| 416 |
pp.onNext(2); |
| 417 |
} |
| 418 |
} |
| 419 |
}; |
| 420 |
|
| 421 |
ParallelFlowable.fromArray(pp) |
| 422 |
.sequentialDelayError() |
| 423 |
.subscribeWith(ts); |
| 424 |
|
| 425 |
pp.onNext(1); |
| 426 |
|
| 427 |
ts |
| 428 |
.assertValuesOnly(1, 2); |
| 429 |
} |
| 430 |
|
| 431 |
@Test |
| 432 |
public void badRequest() { |
| 433 |
TestHelper.assertBadRequestReported( |
| 434 |
ParallelFlowable.fromArray(PublishProcessor.create()) |
| 435 |
.sequential() |
| 436 |
); |
| 437 |
} |
| 438 |
|
| 439 |
@Test |
| 440 |
public void onNextMissingBackpressureRace() throws Throwable { |
| 441 |
TestHelper.withErrorTracking(errors -> { |
| 442 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 443 |
|
| 444 |
AtomicReference<Subscriber<? super Integer>> ref1 = new AtomicReference<>(); |
| 445 |
AtomicReference<Subscriber<? super Integer>> ref2 = new AtomicReference<>(); |
| 446 |
|
| 447 |
Flowable<Integer> f1 = new Flowable<Integer>() { |
| 448 |
@Override |
| 449 |
public void subscribeActual(Subscriber<? super Integer> s) { |
| 450 |
s.onSubscribe(new BooleanSubscription()); |
| 451 |
ref1.set(s); |
| 452 |
} |
| 453 |
}; |
| 454 |
Flowable<Integer> f2 = new Flowable<Integer>() { |
| 455 |
@Override |
| 456 |
public void subscribeActual(Subscriber<? super Integer> s) { |
| 457 |
s.onSubscribe(new BooleanSubscription()); |
| 458 |
ref2.set(s); |
| 459 |
} |
| 460 |
}; |
| 461 |
|
| 462 |
ParallelFlowable.fromArray(f1, f2) |
| 463 |
.sequential(1) |
| 464 |
.test(0) |
| 465 |
; |
| 466 |
|
| 467 |
TestHelper.race( |
| 468 |
() -> { |
| 469 |
ref1.get().onNext(1); |
| 470 |
ref1.get().onNext(2); |
| 471 |
}, |
| 472 |
() -> { |
| 473 |
ref2.get().onNext(3); |
| 474 |
ref2.get().onNext(4); |
| 475 |
} |
| 476 |
); |
| 477 |
|
| 478 |
errors.clear(); |
| 479 |
} |
| 480 |
}); |
| 481 |
} |
| 482 |
|
| 483 |
@Test |
| 484 |
public void onNextMissingBackpressureDelayErrorRace() throws Throwable { |
| 485 |
TestHelper.withErrorTracking(errors -> { |
| 486 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 487 |
|
| 488 |
AtomicReference<Subscriber<? super Integer>> ref1 = new AtomicReference<>(); |
| 489 |
AtomicReference<Subscriber<? super Integer>> ref2 = new AtomicReference<>(); |
| 490 |
|
| 491 |
Flowable<Integer> f1 = new Flowable<Integer>() { |
| 492 |
@Override |
| 493 |
public void subscribeActual(Subscriber<? super Integer> s) { |
| 494 |
s.onSubscribe(new BooleanSubscription()); |
| 495 |
ref1.set(s); |
| 496 |
} |
| 497 |
}; |
| 498 |
Flowable<Integer> f2 = new Flowable<Integer>() { |
| 499 |
@Override |
| 500 |
public void subscribeActual(Subscriber<? super Integer> s) { |
| 501 |
s.onSubscribe(new BooleanSubscription()); |
| 502 |
ref2.set(s); |
| 503 |
} |
| 504 |
}; |
| 505 |
|
| 506 |
ParallelFlowable.fromArray(f1, f2) |
| 507 |
.sequentialDelayError(1) |
| 508 |
.test(0) |
| 509 |
; |
| 510 |
|
| 511 |
TestHelper.race( |
| 512 |
() -> { |
| 513 |
ref1.get().onNext(1); |
| 514 |
ref1.get().onNext(2); |
| 515 |
}, |
| 516 |
() -> { |
| 517 |
ref2.get().onNext(3); |
| 518 |
ref2.get().onNext(4); |
| 519 |
} |
| 520 |
); |
| 521 |
|
| 522 |
errors.clear(); |
| 523 |
} |
| 524 |
}); |
| 525 |
} |
| 526 |
} |