| 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.*; |
| 21 |
|
| 22 |
import org.junit.*; |
| 23 |
import org.reactivestreams.*; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.exceptions.*; |
| 27 |
import io.reactivex.rxjava3.functions.*; |
| 28 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 29 |
import io.reactivex.rxjava3.internal.util.*; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
import io.reactivex.rxjava3.processors.UnicastProcessor; |
| 32 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 33 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 34 |
import io.reactivex.rxjava3.testsupport.*; |
| 35 |
|
| 36 |
public class ParallelFlowableTest extends RxJavaTest { |
| 37 |
|
| 38 |
@Test |
| 39 |
public void sequentialMode() { |
| 40 |
Flowable<Integer> source = Flowable.range(1, 1000000).hide(); |
| 41 |
for (int i = 1; i < 33; i++) { |
| 42 |
Flowable<Integer> result = ParallelFlowable.from(source, i) |
| 43 |
.map(new Function<Integer, Integer>() { |
| 44 |
@Override |
| 45 |
public Integer apply(Integer v) throws Exception { |
| 46 |
return v + 1; |
| 47 |
} |
| 48 |
}) |
| 49 |
.sequential() |
| 50 |
; |
| 51 |
|
| 52 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 53 |
|
| 54 |
result.subscribe(ts); |
| 55 |
|
| 56 |
ts |
| 57 |
.assertSubscribed() |
| 58 |
.assertValueCount(1000000) |
| 59 |
.assertComplete() |
| 60 |
.assertNoErrors() |
| 61 |
; |
| 62 |
} |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void sequentialModeFused() { |
| 68 |
Flowable<Integer> source = Flowable.range(1, 1000000); |
| 69 |
for (int i = 1; i < 33; i++) { |
| 70 |
Flowable<Integer> result = ParallelFlowable.from(source, i) |
| 71 |
.map(new Function<Integer, Integer>() { |
| 72 |
@Override |
| 73 |
public Integer apply(Integer v) throws Exception { |
| 74 |
return v + 1; |
| 75 |
} |
| 76 |
}) |
| 77 |
.sequential() |
| 78 |
; |
| 79 |
|
| 80 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 81 |
|
| 82 |
result.subscribe(ts); |
| 83 |
|
| 84 |
ts |
| 85 |
.assertSubscribed() |
| 86 |
.assertValueCount(1000000) |
| 87 |
.assertComplete() |
| 88 |
.assertNoErrors() |
| 89 |
; |
| 90 |
} |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void parallelMode() { |
| 96 |
Flowable<Integer> source = Flowable.range(1, 1000000).hide(); |
| 97 |
int ncpu = Math.max(8, Runtime.getRuntime().availableProcessors()); |
| 98 |
for (int i = 1; i < ncpu + 1; i++) { |
| 99 |
|
| 100 |
ExecutorService exec = Executors.newFixedThreadPool(i); |
| 101 |
|
| 102 |
Scheduler scheduler = Schedulers.from(exec); |
| 103 |
|
| 104 |
try { |
| 105 |
Flowable<Integer> result = ParallelFlowable.from(source, i) |
| 106 |
.runOn(scheduler) |
| 107 |
.map(new Function<Integer, Integer>() { |
| 108 |
@Override |
| 109 |
public Integer apply(Integer v) throws Exception { |
| 110 |
return v + 1; |
| 111 |
} |
| 112 |
}) |
| 113 |
.sequential() |
| 114 |
; |
| 115 |
|
| 116 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 117 |
|
| 118 |
result.subscribe(ts); |
| 119 |
|
| 120 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 121 |
|
| 122 |
ts |
| 123 |
.assertSubscribed() |
| 124 |
.assertValueCount(1000000) |
| 125 |
.assertComplete() |
| 126 |
.assertNoErrors() |
| 127 |
; |
| 128 |
} finally { |
| 129 |
exec.shutdown(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
@Test |
| 136 |
public void parallelModeFused() { |
| 137 |
Flowable<Integer> source = Flowable.range(1, 1000000); |
| 138 |
int ncpu = Math.max(8, Runtime.getRuntime().availableProcessors()); |
| 139 |
for (int i = 1; i < ncpu + 1; i++) { |
| 140 |
|
| 141 |
ExecutorService exec = Executors.newFixedThreadPool(i); |
| 142 |
|
| 143 |
Scheduler scheduler = Schedulers.from(exec); |
| 144 |
|
| 145 |
try { |
| 146 |
Flowable<Integer> result = ParallelFlowable.from(source, i) |
| 147 |
.runOn(scheduler) |
| 148 |
.map(new Function<Integer, Integer>() { |
| 149 |
@Override |
| 150 |
public Integer apply(Integer v) throws Exception { |
| 151 |
return v + 1; |
| 152 |
} |
| 153 |
}) |
| 154 |
.sequential() |
| 155 |
; |
| 156 |
|
| 157 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 158 |
|
| 159 |
result.subscribe(ts); |
| 160 |
|
| 161 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 162 |
|
| 163 |
ts |
| 164 |
.assertSubscribed() |
| 165 |
.assertValueCount(1000000) |
| 166 |
.assertComplete() |
| 167 |
.assertNoErrors() |
| 168 |
; |
| 169 |
} finally { |
| 170 |
exec.shutdown(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
@Test |
| 177 |
public void reduceFull() { |
| 178 |
for (int i = 1; i <= Runtime.getRuntime().availableProcessors() * 2; i++) { |
| 179 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 180 |
|
| 181 |
Flowable.range(1, 10) |
| 182 |
.parallel(i) |
| 183 |
.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 184 |
@Override |
| 185 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 186 |
return a + b; |
| 187 |
} |
| 188 |
}) |
| 189 |
.subscribe(ts); |
| 190 |
|
| 191 |
ts.assertResult(55); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
@Test |
| 196 |
public void parallelReduceFull() { |
| 197 |
int m = 100000; |
| 198 |
for (int n = 1; n <= m; n *= 10) { |
| 199 |
// System.out.println(n); |
| 200 |
for (int i = 1; i <= Runtime.getRuntime().availableProcessors(); i++) { |
| 201 |
// System.out.println(" " + i); |
| 202 |
|
| 203 |
ExecutorService exec = Executors.newFixedThreadPool(i); |
| 204 |
|
| 205 |
Scheduler scheduler = Schedulers.from(exec); |
| 206 |
|
| 207 |
try { |
| 208 |
TestSubscriber<Long> ts = new TestSubscriber<>(); |
| 209 |
|
| 210 |
Flowable.range(1, n) |
| 211 |
.map(new Function<Integer, Long>() { |
| 212 |
@Override |
| 213 |
public Long apply(Integer v) throws Exception { |
| 214 |
return (long)v; |
| 215 |
} |
| 216 |
}) |
| 217 |
.parallel(i) |
| 218 |
.runOn(scheduler) |
| 219 |
.reduce(new BiFunction<Long, Long, Long>() { |
| 220 |
@Override |
| 221 |
public Long apply(Long a, Long b) throws Exception { |
| 222 |
return a + b; |
| 223 |
} |
| 224 |
}) |
| 225 |
.subscribe(ts); |
| 226 |
|
| 227 |
ts.awaitDone(500, TimeUnit.SECONDS); |
| 228 |
|
| 229 |
long e = ((long)n) * (1 + n) / 2; |
| 230 |
|
| 231 |
ts.assertResult(e); |
| 232 |
} finally { |
| 233 |
exec.shutdown(); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
@Test |
| 240 |
public void toSortedList() { |
| 241 |
TestSubscriber<List<Integer>> ts = new TestSubscriber<>(); |
| 242 |
|
| 243 |
Flowable.fromArray(10, 9, 8, 7, 6, 5, 4, 3, 2, 1) |
| 244 |
.parallel() |
| 245 |
.toSortedList(Functions.naturalComparator()) |
| 246 |
.subscribe(ts); |
| 247 |
|
| 248 |
ts.assertResult(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); |
| 249 |
} |
| 250 |
|
| 251 |
@Test |
| 252 |
public void sorted() { |
| 253 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0); |
| 254 |
|
| 255 |
Flowable.fromArray(10, 9, 8, 7, 6, 5, 4, 3, 2, 1) |
| 256 |
.parallel() |
| 257 |
.sorted(Functions.naturalComparator()) |
| 258 |
.subscribe(ts); |
| 259 |
|
| 260 |
ts.assertNoValues(); |
| 261 |
|
| 262 |
ts.request(2); |
| 263 |
|
| 264 |
ts.assertValues(1, 2); |
| 265 |
|
| 266 |
ts.request(5); |
| 267 |
|
| 268 |
ts.assertValues(1, 2, 3, 4, 5, 6, 7); |
| 269 |
|
| 270 |
ts.request(3); |
| 271 |
|
| 272 |
ts.assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 273 |
} |
| 274 |
|
| 275 |
@Test |
| 276 |
public void collect() { |
| 277 |
Supplier<List<Integer>> as = new Supplier<List<Integer>>() { |
| 278 |
@Override |
| 279 |
public List<Integer> get() throws Exception { |
| 280 |
return new ArrayList<>(); |
| 281 |
} |
| 282 |
}; |
| 283 |
|
| 284 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 285 |
Flowable.range(1, 10) |
| 286 |
.parallel() |
| 287 |
.collect(as, new BiConsumer<List<Integer>, Integer>() { |
| 288 |
@Override |
| 289 |
public void accept(List<Integer> a, Integer b) throws Exception { |
| 290 |
a.add(b); |
| 291 |
} |
| 292 |
}) |
| 293 |
.sequential() |
| 294 |
.flatMapIterable(new Function<List<Integer>, Iterable<Integer>>() { |
| 295 |
@Override |
| 296 |
public Iterable<Integer> apply(List<Integer> v) throws Exception { |
| 297 |
return v; |
| 298 |
} |
| 299 |
}) |
| 300 |
.subscribe(ts); |
| 301 |
|
| 302 |
ts |
| 303 |
.assertNoErrors() |
| 304 |
.assertComplete(); |
| 305 |
|
| 306 |
TestHelper.assertValueSet(ts, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 307 |
} |
| 308 |
|
| 309 |
@Test |
| 310 |
public void from() { |
| 311 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 312 |
|
| 313 |
ParallelFlowable.fromArray(Flowable.range(1, 5), Flowable.range(6, 5)) |
| 314 |
.sequential() |
| 315 |
.subscribe(ts); |
| 316 |
|
| 317 |
ts |
| 318 |
.assertNoErrors() |
| 319 |
.assertComplete(); |
| 320 |
|
| 321 |
TestHelper.assertValueSet(ts, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 322 |
} |
| 323 |
|
| 324 |
@Test |
| 325 |
public void concatMapUnordered() { |
| 326 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 327 |
|
| 328 |
Flowable.range(1, 5) |
| 329 |
.parallel() |
| 330 |
.concatMap(new Function<Integer, Publisher<Integer>>() { |
| 331 |
@Override |
| 332 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 333 |
return Flowable.range(v * 10 + 1, 3); |
| 334 |
} |
| 335 |
}) |
| 336 |
.sequential() |
| 337 |
.subscribe(ts); |
| 338 |
|
| 339 |
ts |
| 340 |
.assertNoErrors() |
| 341 |
.assertComplete(); |
| 342 |
|
| 343 |
TestHelper.assertValueSet(ts, 11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 42, 43, 51, 52, 53); |
| 344 |
} |
| 345 |
|
| 346 |
@Test |
| 347 |
public void flatMapUnordered() { |
| 348 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 349 |
|
| 350 |
Flowable.range(1, 5) |
| 351 |
.parallel() |
| 352 |
.flatMap(new Function<Integer, Publisher<Integer>>() { |
| 353 |
@Override |
| 354 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 355 |
return Flowable.range(v * 10 + 1, 3); |
| 356 |
} |
| 357 |
}) |
| 358 |
.sequential() |
| 359 |
.subscribe(ts); |
| 360 |
|
| 361 |
ts |
| 362 |
.assertNoErrors() |
| 363 |
.assertComplete(); |
| 364 |
|
| 365 |
TestHelper.assertValueSet(ts, 11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 42, 43, 51, 52, 53); |
| 366 |
} |
| 367 |
|
| 368 |
@Test |
| 369 |
public void collectAsyncFused() { |
| 370 |
ExecutorService exec = Executors.newFixedThreadPool(3); |
| 371 |
|
| 372 |
Scheduler s = Schedulers.from(exec); |
| 373 |
|
| 374 |
try { |
| 375 |
Supplier<List<Integer>> as = new Supplier<List<Integer>>() { |
| 376 |
@Override |
| 377 |
public List<Integer> get() throws Exception { |
| 378 |
return new ArrayList<>(); |
| 379 |
} |
| 380 |
}; |
| 381 |
TestSubscriber<List<Integer>> ts = new TestSubscriber<>(); |
| 382 |
|
| 383 |
Flowable.range(1, 100000) |
| 384 |
.parallel(3) |
| 385 |
.runOn(s) |
| 386 |
.collect(as, new BiConsumer<List<Integer>, Integer>() { |
| 387 |
@Override |
| 388 |
public void accept(List<Integer> a, Integer b) throws Exception { |
| 389 |
a.add(b); |
| 390 |
} |
| 391 |
}) |
| 392 |
.doOnNext(new Consumer<List<Integer>>() { |
| 393 |
@Override |
| 394 |
public void accept(List<Integer> v) throws Exception { |
| 395 |
System.out.println(v.size()); |
| 396 |
} |
| 397 |
}) |
| 398 |
.sequential() |
| 399 |
.subscribe(ts); |
| 400 |
|
| 401 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 402 |
ts.assertValueCount(3) |
| 403 |
.assertNoErrors() |
| 404 |
.assertComplete() |
| 405 |
; |
| 406 |
|
| 407 |
List<List<Integer>> list = ts.values(); |
| 408 |
|
| 409 |
Assert.assertEquals(100000, list.get(0).size() + list.get(1).size() + list.get(2).size()); |
| 410 |
} finally { |
| 411 |
exec.shutdown(); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
@Test |
| 416 |
public void collectAsync() { |
| 417 |
ExecutorService exec = Executors.newFixedThreadPool(3); |
| 418 |
|
| 419 |
Scheduler s = Schedulers.from(exec); |
| 420 |
|
| 421 |
try { |
| 422 |
Supplier<List<Integer>> as = new Supplier<List<Integer>>() { |
| 423 |
@Override |
| 424 |
public List<Integer> get() throws Exception { |
| 425 |
return new ArrayList<>(); |
| 426 |
} |
| 427 |
}; |
| 428 |
TestSubscriber<List<Integer>> ts = new TestSubscriber<>(); |
| 429 |
|
| 430 |
Flowable.range(1, 100000).hide() |
| 431 |
.parallel(3) |
| 432 |
.runOn(s) |
| 433 |
.collect(as, new BiConsumer<List<Integer>, Integer>() { |
| 434 |
@Override |
| 435 |
public void accept(List<Integer> a, Integer b) throws Exception { |
| 436 |
a.add(b); |
| 437 |
} |
| 438 |
}) |
| 439 |
.doOnNext(new Consumer<List<Integer>>() { |
| 440 |
@Override |
| 441 |
public void accept(List<Integer> v) throws Exception { |
| 442 |
System.out.println(v.size()); |
| 443 |
} |
| 444 |
}) |
| 445 |
.sequential() |
| 446 |
.subscribe(ts); |
| 447 |
|
| 448 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 449 |
ts.assertValueCount(3) |
| 450 |
.assertNoErrors() |
| 451 |
.assertComplete() |
| 452 |
; |
| 453 |
|
| 454 |
List<List<Integer>> list = ts.values(); |
| 455 |
|
| 456 |
Assert.assertEquals(100000, list.get(0).size() + list.get(1).size() + list.get(2).size()); |
| 457 |
} finally { |
| 458 |
exec.shutdown(); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
@Test |
| 463 |
public void collectAsync2() { |
| 464 |
ExecutorService exec = Executors.newFixedThreadPool(3); |
| 465 |
|
| 466 |
Scheduler s = Schedulers.from(exec); |
| 467 |
|
| 468 |
try { |
| 469 |
Supplier<List<Integer>> as = new Supplier<List<Integer>>() { |
| 470 |
@Override |
| 471 |
public List<Integer> get() throws Exception { |
| 472 |
return new ArrayList<>(); |
| 473 |
} |
| 474 |
}; |
| 475 |
TestSubscriber<List<Integer>> ts = new TestSubscriber<>(); |
| 476 |
|
| 477 |
Flowable.range(1, 100000).hide() |
| 478 |
.observeOn(s) |
| 479 |
.parallel(3) |
| 480 |
.runOn(s) |
| 481 |
.collect(as, new BiConsumer<List<Integer>, Integer>() { |
| 482 |
@Override |
| 483 |
public void accept(List<Integer> a, Integer b) throws Exception { |
| 484 |
a.add(b); |
| 485 |
} |
| 486 |
}) |
| 487 |
.doOnNext(new Consumer<List<Integer>>() { |
| 488 |
@Override |
| 489 |
public void accept(List<Integer> v) throws Exception { |
| 490 |
System.out.println(v.size()); |
| 491 |
} |
| 492 |
}) |
| 493 |
.sequential() |
| 494 |
.subscribe(ts); |
| 495 |
|
| 496 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 497 |
ts.assertValueCount(3) |
| 498 |
.assertNoErrors() |
| 499 |
.assertComplete() |
| 500 |
; |
| 501 |
|
| 502 |
List<List<Integer>> list = ts.values(); |
| 503 |
|
| 504 |
Assert.assertEquals(100000, list.get(0).size() + list.get(1).size() + list.get(2).size()); |
| 505 |
} finally { |
| 506 |
exec.shutdown(); |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
@Test |
| 511 |
public void collectAsync3() { |
| 512 |
ExecutorService exec = Executors.newFixedThreadPool(3); |
| 513 |
|
| 514 |
Scheduler s = Schedulers.from(exec); |
| 515 |
|
| 516 |
try { |
| 517 |
Supplier<List<Integer>> as = new Supplier<List<Integer>>() { |
| 518 |
@Override |
| 519 |
public List<Integer> get() throws Exception { |
| 520 |
return new ArrayList<>(); |
| 521 |
} |
| 522 |
}; |
| 523 |
TestSubscriber<List<Integer>> ts = new TestSubscriber<>(); |
| 524 |
|
| 525 |
Flowable.range(1, 100000).hide() |
| 526 |
.observeOn(s) |
| 527 |
.parallel(3) |
| 528 |
.runOn(s) |
| 529 |
.collect(as, new BiConsumer<List<Integer>, Integer>() { |
| 530 |
@Override |
| 531 |
public void accept(List<Integer> a, Integer b) throws Exception { |
| 532 |
a.add(b); |
| 533 |
} |
| 534 |
}) |
| 535 |
.doOnNext(new Consumer<List<Integer>>() { |
| 536 |
@Override |
| 537 |
public void accept(List<Integer> v) throws Exception { |
| 538 |
System.out.println(v.size()); |
| 539 |
} |
| 540 |
}) |
| 541 |
.sequential() |
| 542 |
.subscribe(ts); |
| 543 |
|
| 544 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 545 |
ts.assertValueCount(3) |
| 546 |
.assertNoErrors() |
| 547 |
.assertComplete() |
| 548 |
; |
| 549 |
|
| 550 |
List<List<Integer>> list = ts.values(); |
| 551 |
|
| 552 |
Assert.assertEquals(100000, list.get(0).size() + list.get(1).size() + list.get(2).size()); |
| 553 |
} finally { |
| 554 |
exec.shutdown(); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
@Test |
| 559 |
public void collectAsync3Fused() { |
| 560 |
ExecutorService exec = Executors.newFixedThreadPool(3); |
| 561 |
|
| 562 |
Scheduler s = Schedulers.from(exec); |
| 563 |
|
| 564 |
try { |
| 565 |
Supplier<List<Integer>> as = new Supplier<List<Integer>>() { |
| 566 |
@Override |
| 567 |
public List<Integer> get() throws Exception { |
| 568 |
return new ArrayList<>(); |
| 569 |
} |
| 570 |
}; |
| 571 |
TestSubscriber<List<Integer>> ts = new TestSubscriber<>(); |
| 572 |
|
| 573 |
Flowable.range(1, 100000) |
| 574 |
.observeOn(s) |
| 575 |
.parallel(3) |
| 576 |
.runOn(s) |
| 577 |
.collect(as, new BiConsumer<List<Integer>, Integer>() { |
| 578 |
@Override |
| 579 |
public void accept(List<Integer> a, Integer b) throws Exception { |
| 580 |
a.add(b); |
| 581 |
} |
| 582 |
}) |
| 583 |
.doOnNext(new Consumer<List<Integer>>() { |
| 584 |
@Override |
| 585 |
public void accept(List<Integer> v) throws Exception { |
| 586 |
System.out.println(v.size()); |
| 587 |
} |
| 588 |
}) |
| 589 |
.sequential() |
| 590 |
.subscribe(ts); |
| 591 |
|
| 592 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 593 |
ts.assertValueCount(3) |
| 594 |
.assertNoErrors() |
| 595 |
.assertComplete() |
| 596 |
; |
| 597 |
|
| 598 |
List<List<Integer>> list = ts.values(); |
| 599 |
|
| 600 |
Assert.assertEquals(100000, list.get(0).size() + list.get(1).size() + list.get(2).size()); |
| 601 |
} finally { |
| 602 |
exec.shutdown(); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
@Test |
| 607 |
public void collectAsync3Take() { |
| 608 |
ExecutorService exec = Executors.newFixedThreadPool(4); |
| 609 |
|
| 610 |
Scheduler s = Schedulers.from(exec); |
| 611 |
|
| 612 |
try { |
| 613 |
Supplier<List<Integer>> as = new Supplier<List<Integer>>() { |
| 614 |
@Override |
| 615 |
public List<Integer> get() throws Exception { |
| 616 |
return new ArrayList<>(); |
| 617 |
} |
| 618 |
}; |
| 619 |
TestSubscriber<List<Integer>> ts = new TestSubscriber<>(); |
| 620 |
|
| 621 |
Flowable.range(1, 100000) |
| 622 |
.take(1000) |
| 623 |
.observeOn(s) |
| 624 |
.parallel(3) |
| 625 |
.runOn(s) |
| 626 |
.collect(as, new BiConsumer<List<Integer>, Integer>() { |
| 627 |
@Override |
| 628 |
public void accept(List<Integer> a, Integer b) throws Exception { |
| 629 |
a.add(b); |
| 630 |
} |
| 631 |
}) |
| 632 |
.doOnNext(new Consumer<List<Integer>>() { |
| 633 |
@Override |
| 634 |
public void accept(List<Integer> v) throws Exception { |
| 635 |
System.out.println(v.size()); |
| 636 |
} |
| 637 |
}) |
| 638 |
.sequential() |
| 639 |
.subscribe(ts); |
| 640 |
|
| 641 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 642 |
ts.assertValueCount(3) |
| 643 |
.assertNoErrors() |
| 644 |
.assertComplete() |
| 645 |
; |
| 646 |
|
| 647 |
List<List<Integer>> list = ts.values(); |
| 648 |
|
| 649 |
Assert.assertEquals(1000, list.get(0).size() + list.get(1).size() + list.get(2).size()); |
| 650 |
} finally { |
| 651 |
exec.shutdown(); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
@Test |
| 656 |
public void collectAsync4Take() { |
| 657 |
ExecutorService exec = Executors.newFixedThreadPool(3); |
| 658 |
|
| 659 |
Scheduler s = Schedulers.from(exec); |
| 660 |
|
| 661 |
try { |
| 662 |
Supplier<List<Integer>> as = new Supplier<List<Integer>>() { |
| 663 |
@Override |
| 664 |
public List<Integer> get() throws Exception { |
| 665 |
return new ArrayList<>(); |
| 666 |
} |
| 667 |
}; |
| 668 |
TestSubscriber<List<Integer>> ts = new TestSubscriber<>(); |
| 669 |
|
| 670 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 671 |
|
| 672 |
for (int i = 0; i < 1000; i++) { |
| 673 |
up.onNext(i); |
| 674 |
} |
| 675 |
|
| 676 |
up |
| 677 |
.take(1000) |
| 678 |
.observeOn(s) |
| 679 |
.parallel(3) |
| 680 |
.runOn(s) |
| 681 |
.collect(as, new BiConsumer<List<Integer>, Integer>() { |
| 682 |
@Override |
| 683 |
public void accept(List<Integer> a, Integer b) throws Exception { |
| 684 |
a.add(b); |
| 685 |
} |
| 686 |
}) |
| 687 |
.doOnNext(new Consumer<List<Integer>>() { |
| 688 |
@Override |
| 689 |
public void accept(List<Integer> v) throws Exception { |
| 690 |
System.out.println(v.size()); |
| 691 |
} |
| 692 |
}) |
| 693 |
.sequential() |
| 694 |
.subscribe(ts); |
| 695 |
|
| 696 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 697 |
ts.assertValueCount(3) |
| 698 |
.assertNoErrors() |
| 699 |
.assertComplete() |
| 700 |
; |
| 701 |
|
| 702 |
List<List<Integer>> list = ts.values(); |
| 703 |
|
| 704 |
Assert.assertEquals(1000, list.get(0).size() + list.get(1).size() + list.get(2).size()); |
| 705 |
} finally { |
| 706 |
exec.shutdown(); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
@Test |
| 711 |
public void emptySourceZeroRequest() { |
| 712 |
TestSubscriber<Object> ts = new TestSubscriber<>(0); |
| 713 |
|
| 714 |
Flowable.range(1, 3).parallel(3).sequential().subscribe(ts); |
| 715 |
|
| 716 |
ts.request(1); |
| 717 |
|
| 718 |
ts.assertValue(1); |
| 719 |
} |
| 720 |
|
| 721 |
@Test |
| 722 |
public void parallelismAndPrefetch() { |
| 723 |
for (int parallelism = 1; parallelism <= 8; parallelism++) { |
| 724 |
for (int prefetch = 1; prefetch <= 1024; prefetch *= 2) { |
| 725 |
Flowable.range(1, 1024 * 1024) |
| 726 |
.parallel(parallelism, prefetch) |
| 727 |
.map(Functions.<Integer>identity()) |
| 728 |
.sequential() |
| 729 |
.to(TestHelper.<Integer>testConsumer()) |
| 730 |
.assertSubscribed() |
| 731 |
.assertValueCount(1024 * 1024) |
| 732 |
.assertNoErrors() |
| 733 |
.assertComplete(); |
| 734 |
} |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
@Test |
| 739 |
public void parallelismAndPrefetchAsync() { |
| 740 |
for (int parallelism = 1; parallelism <= 8; parallelism *= 2) { |
| 741 |
for (int prefetch = 1; prefetch <= 1024; prefetch *= 2) { |
| 742 |
System.out.println("parallelismAndPrefetchAsync >> " + parallelism + ", " + prefetch); |
| 743 |
|
| 744 |
Flowable.range(1, 1024 * 1024) |
| 745 |
.parallel(parallelism, prefetch) |
| 746 |
.runOn(Schedulers.computation()) |
| 747 |
.map(Functions.<Integer>identity()) |
| 748 |
.sequential(prefetch) |
| 749 |
.to(TestHelper.<Integer>testConsumer()) |
| 750 |
.withTag("parallelism = " + parallelism + ", prefetch = " + prefetch) |
| 751 |
.awaitDone(30, TimeUnit.SECONDS) |
| 752 |
.assertSubscribed() |
| 753 |
.assertValueCount(1024 * 1024) |
| 754 |
.assertNoErrors() |
| 755 |
.assertComplete(); |
| 756 |
} |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
@SuppressWarnings("unchecked") |
| 761 |
@Test |
| 762 |
public void badParallelismStage() { |
| 763 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 764 |
|
| 765 |
Flowable.range(1, 10) |
| 766 |
.parallel(2) |
| 767 |
.subscribe(new Subscriber[] { ts }); |
| 768 |
|
| 769 |
ts.assertFailure(IllegalArgumentException.class); |
| 770 |
} |
| 771 |
|
| 772 |
@SuppressWarnings("unchecked") |
| 773 |
@Test |
| 774 |
public void badParallelismStage2() { |
| 775 |
TestSubscriber<Integer> ts1 = new TestSubscriber<>(); |
| 776 |
TestSubscriber<Integer> ts2 = new TestSubscriber<>(); |
| 777 |
TestSubscriber<Integer> ts3 = new TestSubscriber<>(); |
| 778 |
|
| 779 |
Flowable.range(1, 10) |
| 780 |
.parallel(2) |
| 781 |
.subscribe(new Subscriber[] { ts1, ts2, ts3 }); |
| 782 |
|
| 783 |
ts1.assertFailure(IllegalArgumentException.class); |
| 784 |
ts2.assertFailure(IllegalArgumentException.class); |
| 785 |
ts3.assertFailure(IllegalArgumentException.class); |
| 786 |
} |
| 787 |
|
| 788 |
@Test |
| 789 |
public void filter() { |
| 790 |
TestSubscriberEx<Integer> ts = Flowable.range(1, 20) |
| 791 |
.parallel() |
| 792 |
.runOn(Schedulers.computation()) |
| 793 |
.filter(new Predicate<Integer>() { |
| 794 |
@Override |
| 795 |
public boolean test(Integer v) throws Exception { |
| 796 |
return v % 2 == 0; |
| 797 |
} |
| 798 |
}) |
| 799 |
.sequential() |
| 800 |
.to(TestHelper.<Integer>testConsumer()) |
| 801 |
.awaitDone(5, TimeUnit.SECONDS) |
| 802 |
.assertNoErrors() |
| 803 |
.assertComplete(); |
| 804 |
|
| 805 |
TestHelper.assertValueSet(ts, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20); |
| 806 |
} |
| 807 |
|
| 808 |
@Test |
| 809 |
public void filterThrows() throws Exception { |
| 810 |
final boolean[] cancelled = { false }; |
| 811 |
Flowable.range(1, 20).concatWith(Flowable.<Integer>never()) |
| 812 |
.doOnCancel(new Action() { |
| 813 |
@Override |
| 814 |
public void run() throws Exception { |
| 815 |
cancelled[0] = true; |
| 816 |
} |
| 817 |
}) |
| 818 |
.parallel() |
| 819 |
.runOn(Schedulers.computation()) |
| 820 |
.filter(new Predicate<Integer>() { |
| 821 |
@Override |
| 822 |
public boolean test(Integer v) throws Exception { |
| 823 |
if (v == 10) { |
| 824 |
throw new TestException(); |
| 825 |
} |
| 826 |
return v % 2 == 0; |
| 827 |
} |
| 828 |
}) |
| 829 |
.sequential() |
| 830 |
.test() |
| 831 |
.awaitDone(5, TimeUnit.SECONDS) |
| 832 |
.assertError(TestException.class) |
| 833 |
.assertNotComplete(); |
| 834 |
|
| 835 |
Thread.sleep(100); |
| 836 |
|
| 837 |
assertTrue(cancelled[0]); |
| 838 |
} |
| 839 |
|
| 840 |
@Test |
| 841 |
public void doAfterNext() { |
| 842 |
final int[] count = { 0 }; |
| 843 |
|
| 844 |
Flowable.range(1, 5) |
| 845 |
.parallel() |
| 846 |
.doAfterNext(new Consumer<Integer>() { |
| 847 |
@Override |
| 848 |
public void accept(Integer v) throws Exception { |
| 849 |
count[0]++; |
| 850 |
} |
| 851 |
}) |
| 852 |
.sequential() |
| 853 |
.test() |
| 854 |
.assertResult(1, 2, 3, 4, 5); |
| 855 |
} |
| 856 |
|
| 857 |
@Test |
| 858 |
public void doOnNextThrows() { |
| 859 |
final int[] count = { 0 }; |
| 860 |
|
| 861 |
Flowable.range(1, 5) |
| 862 |
.parallel() |
| 863 |
.doOnNext(new Consumer<Integer>() { |
| 864 |
@Override |
| 865 |
public void accept(Integer v) throws Exception { |
| 866 |
if (v == 3) { |
| 867 |
throw new TestException(); |
| 868 |
} else { |
| 869 |
count[0]++; |
| 870 |
} |
| 871 |
} |
| 872 |
}) |
| 873 |
.sequential() |
| 874 |
.test() |
| 875 |
.assertError(TestException.class) |
| 876 |
.assertNotComplete(); |
| 877 |
|
| 878 |
assertTrue("" + count[0], count[0] < 5); |
| 879 |
} |
| 880 |
|
| 881 |
@Test |
| 882 |
public void doAfterNextThrows() { |
| 883 |
final int[] count = { 0 }; |
| 884 |
|
| 885 |
Flowable.range(1, 5) |
| 886 |
.parallel() |
| 887 |
.doAfterNext(new Consumer<Integer>() { |
| 888 |
@Override |
| 889 |
public void accept(Integer v) throws Exception { |
| 890 |
if (v == 3) { |
| 891 |
throw new TestException(); |
| 892 |
} else { |
| 893 |
count[0]++; |
| 894 |
} |
| 895 |
} |
| 896 |
}) |
| 897 |
.sequential() |
| 898 |
.test() |
| 899 |
.assertError(TestException.class) |
| 900 |
.assertNotComplete(); |
| 901 |
|
| 902 |
assertTrue("" + count[0], count[0] < 5); |
| 903 |
} |
| 904 |
|
| 905 |
@Test |
| 906 |
public void errorNotRepeating() throws Exception { |
| 907 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 908 |
try { |
| 909 |
Flowable.error(new TestException()) |
| 910 |
.parallel() |
| 911 |
.runOn(Schedulers.computation()) |
| 912 |
.sequential() |
| 913 |
.test() |
| 914 |
.awaitDone(5, TimeUnit.SECONDS) |
| 915 |
.assertFailure(TestException.class) |
| 916 |
; |
| 917 |
|
| 918 |
Thread.sleep(300); |
| 919 |
|
| 920 |
for (Throwable ex : errors) { |
| 921 |
ex.printStackTrace(); |
| 922 |
} |
| 923 |
assertTrue(errors.toString(), errors.isEmpty()); |
| 924 |
} finally { |
| 925 |
RxJavaPlugins.reset(); |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
@Test |
| 930 |
public void doOnError() { |
| 931 |
final int[] count = { 0 }; |
| 932 |
|
| 933 |
Flowable.range(1, 5) |
| 934 |
.parallel(2) |
| 935 |
.map(new Function<Integer, Integer>() { |
| 936 |
@Override |
| 937 |
public Integer apply(Integer v) throws Exception { |
| 938 |
if (v == 3) { |
| 939 |
throw new TestException(); |
| 940 |
} |
| 941 |
return v; |
| 942 |
} |
| 943 |
}) |
| 944 |
.doOnError(new Consumer<Throwable>() { |
| 945 |
@Override |
| 946 |
public void accept(Throwable e) throws Exception { |
| 947 |
if (e instanceof TestException) { |
| 948 |
count[0]++; |
| 949 |
} |
| 950 |
} |
| 951 |
}) |
| 952 |
.sequential() |
| 953 |
.test() |
| 954 |
.assertError(TestException.class) |
| 955 |
.assertNotComplete(); |
| 956 |
|
| 957 |
assertEquals(1, count[0]); |
| 958 |
} |
| 959 |
|
| 960 |
@Test |
| 961 |
public void doOnErrorThrows() { |
| 962 |
TestSubscriberEx<Integer> ts = Flowable.range(1, 5) |
| 963 |
.parallel(2) |
| 964 |
.map(new Function<Integer, Integer>() { |
| 965 |
@Override |
| 966 |
public Integer apply(Integer v) throws Exception { |
| 967 |
if (v == 3) { |
| 968 |
throw new TestException(); |
| 969 |
} |
| 970 |
return v; |
| 971 |
} |
| 972 |
}) |
| 973 |
.doOnError(new Consumer<Throwable>() { |
| 974 |
@Override |
| 975 |
public void accept(Throwable e) throws Exception { |
| 976 |
if (e instanceof TestException) { |
| 977 |
throw new IOException(); |
| 978 |
} |
| 979 |
} |
| 980 |
}) |
| 981 |
.sequential() |
| 982 |
.to(TestHelper.<Integer>testConsumer()) |
| 983 |
.assertError(CompositeException.class) |
| 984 |
.assertNotComplete(); |
| 985 |
|
| 986 |
List<Throwable> errors = TestHelper.errorList(ts); |
| 987 |
TestHelper.assertError(errors, 0, TestException.class); |
| 988 |
TestHelper.assertError(errors, 1, IOException.class); |
| 989 |
} |
| 990 |
|
| 991 |
@Test |
| 992 |
public void doOnComplete() { |
| 993 |
final int[] count = { 0 }; |
| 994 |
|
| 995 |
Flowable.range(1, 5) |
| 996 |
.parallel(2) |
| 997 |
.doOnComplete(new Action() { |
| 998 |
@Override |
| 999 |
public void run() throws Exception { |
| 1000 |
count[0]++; |
| 1001 |
} |
| 1002 |
}) |
| 1003 |
.sequential() |
| 1004 |
.test() |
| 1005 |
.assertResult(1, 2, 3, 4, 5); |
| 1006 |
|
| 1007 |
assertEquals(2, count[0]); |
| 1008 |
} |
| 1009 |
|
| 1010 |
@Test |
| 1011 |
public void doAfterTerminate() { |
| 1012 |
final int[] count = { 0 }; |
| 1013 |
|
| 1014 |
Flowable.range(1, 5) |
| 1015 |
.parallel(2) |
| 1016 |
.doAfterTerminated(new Action() { |
| 1017 |
@Override |
| 1018 |
public void run() throws Exception { |
| 1019 |
count[0]++; |
| 1020 |
} |
| 1021 |
}) |
| 1022 |
.sequential() |
| 1023 |
.test() |
| 1024 |
.assertResult(1, 2, 3, 4, 5); |
| 1025 |
|
| 1026 |
assertEquals(2, count[0]); |
| 1027 |
} |
| 1028 |
|
| 1029 |
@Test |
| 1030 |
public void doOnSubscribe() { |
| 1031 |
final int[] count = { 0 }; |
| 1032 |
|
| 1033 |
Flowable.range(1, 5) |
| 1034 |
.parallel(2) |
| 1035 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 1036 |
@Override |
| 1037 |
public void accept(Subscription s) throws Exception { |
| 1038 |
count[0]++; |
| 1039 |
} |
| 1040 |
}) |
| 1041 |
.sequential() |
| 1042 |
.test() |
| 1043 |
.assertResult(1, 2, 3, 4, 5); |
| 1044 |
|
| 1045 |
assertEquals(2, count[0]); |
| 1046 |
} |
| 1047 |
|
| 1048 |
@Test |
| 1049 |
public void doOnRequest() { |
| 1050 |
final int[] count = { 0 }; |
| 1051 |
|
| 1052 |
Flowable.range(1, 5) |
| 1053 |
.parallel(2) |
| 1054 |
.doOnRequest(new LongConsumer() { |
| 1055 |
@Override |
| 1056 |
public void accept(long s) throws Exception { |
| 1057 |
count[0]++; |
| 1058 |
} |
| 1059 |
}) |
| 1060 |
.sequential() |
| 1061 |
.test() |
| 1062 |
.assertResult(1, 2, 3, 4, 5); |
| 1063 |
|
| 1064 |
assertEquals(2, count[0]); |
| 1065 |
} |
| 1066 |
|
| 1067 |
@Test |
| 1068 |
public void doOnCancel() { |
| 1069 |
final int[] count = { 0 }; |
| 1070 |
|
| 1071 |
Flowable.range(1, 5) |
| 1072 |
.parallel(2) |
| 1073 |
.doOnCancel(new Action() { |
| 1074 |
@Override |
| 1075 |
public void run() throws Exception { |
| 1076 |
count[0]++; |
| 1077 |
} |
| 1078 |
}) |
| 1079 |
.sequential() |
| 1080 |
.take(2) |
| 1081 |
.test() |
| 1082 |
.assertResult(1, 2); |
| 1083 |
|
| 1084 |
assertEquals(2, count[0]); |
| 1085 |
} |
| 1086 |
|
| 1087 |
@SuppressWarnings("unchecked") |
| 1088 |
@Test(expected = IllegalArgumentException.class) |
| 1089 |
public void fromPublishers() { |
| 1090 |
ParallelFlowable.fromArray(new Publisher[0]); |
| 1091 |
} |
| 1092 |
|
| 1093 |
@Test |
| 1094 |
public void to() { |
| 1095 |
Flowable.range(1, 5) |
| 1096 |
.parallel() |
| 1097 |
.to(new ParallelFlowableConverter<Integer, Flowable<Integer>>() { |
| 1098 |
@Override |
| 1099 |
public Flowable<Integer> apply(ParallelFlowable<Integer> pf) { |
| 1100 |
return pf.sequential(); |
| 1101 |
} |
| 1102 |
}) |
| 1103 |
.test() |
| 1104 |
.assertResult(1, 2, 3, 4, 5); |
| 1105 |
} |
| 1106 |
|
| 1107 |
@Test(expected = TestException.class) |
| 1108 |
public void toThrows() { |
| 1109 |
Flowable.range(1, 5) |
| 1110 |
.parallel() |
| 1111 |
.to(new ParallelFlowableConverter<Integer, Flowable<Integer>>() { |
| 1112 |
@Override |
| 1113 |
public Flowable<Integer> apply(ParallelFlowable<Integer> pf) { |
| 1114 |
throw new TestException(); |
| 1115 |
} |
| 1116 |
}); |
| 1117 |
} |
| 1118 |
|
| 1119 |
@Test |
| 1120 |
public void compose() { |
| 1121 |
Flowable.range(1, 5) |
| 1122 |
.parallel() |
| 1123 |
.compose(new ParallelTransformer<Integer, Integer>() { |
| 1124 |
@Override |
| 1125 |
public ParallelFlowable<Integer> apply(ParallelFlowable<Integer> pf) { |
| 1126 |
return pf.map(new Function<Integer, Integer>() { |
| 1127 |
@Override |
| 1128 |
public Integer apply(Integer v) throws Exception { |
| 1129 |
return v + 1; |
| 1130 |
} |
| 1131 |
}); |
| 1132 |
} |
| 1133 |
}) |
| 1134 |
.sequential() |
| 1135 |
.test() |
| 1136 |
.assertResult(2, 3, 4, 5, 6); |
| 1137 |
} |
| 1138 |
|
| 1139 |
@Test |
| 1140 |
public void flatMapDelayError() { |
| 1141 |
final int[] count = { 0 }; |
| 1142 |
|
| 1143 |
Flowable.range(1, 5) |
| 1144 |
.parallel(2) |
| 1145 |
.flatMap(new Function<Integer, Flowable<Integer>>() { |
| 1146 |
@Override |
| 1147 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 1148 |
if (v == 3) { |
| 1149 |
return Flowable.error(new TestException()); |
| 1150 |
} |
| 1151 |
return Flowable.just(v); |
| 1152 |
} |
| 1153 |
}, true) |
| 1154 |
.doOnError(new Consumer<Throwable>() { |
| 1155 |
@Override |
| 1156 |
public void accept(Throwable e) throws Exception { |
| 1157 |
if (e instanceof TestException) { |
| 1158 |
count[0]++; |
| 1159 |
} |
| 1160 |
} |
| 1161 |
}) |
| 1162 |
.sequential() |
| 1163 |
.test() |
| 1164 |
.assertValues(1, 2, 4, 5) |
| 1165 |
.assertError(TestException.class) |
| 1166 |
.assertNotComplete(); |
| 1167 |
|
| 1168 |
assertEquals(1, count[0]); |
| 1169 |
} |
| 1170 |
|
| 1171 |
@Test |
| 1172 |
public void flatMapDelayErrorMaxConcurrency() { |
| 1173 |
final int[] count = { 0 }; |
| 1174 |
|
| 1175 |
Flowable.range(1, 5) |
| 1176 |
.parallel(2) |
| 1177 |
.flatMap(new Function<Integer, Flowable<Integer>>() { |
| 1178 |
@Override |
| 1179 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 1180 |
if (v == 3) { |
| 1181 |
return Flowable.error(new TestException()); |
| 1182 |
} |
| 1183 |
return Flowable.just(v); |
| 1184 |
} |
| 1185 |
}, true, 1) |
| 1186 |
.doOnError(new Consumer<Throwable>() { |
| 1187 |
@Override |
| 1188 |
public void accept(Throwable e) throws Exception { |
| 1189 |
if (e instanceof TestException) { |
| 1190 |
count[0]++; |
| 1191 |
} |
| 1192 |
} |
| 1193 |
}) |
| 1194 |
.sequential() |
| 1195 |
.test() |
| 1196 |
.assertValues(1, 2, 4, 5) |
| 1197 |
.assertError(TestException.class) |
| 1198 |
.assertNotComplete(); |
| 1199 |
|
| 1200 |
assertEquals(1, count[0]); |
| 1201 |
} |
| 1202 |
|
| 1203 |
@Test |
| 1204 |
public void concatMapDelayError() { |
| 1205 |
final int[] count = { 0 }; |
| 1206 |
|
| 1207 |
Flowable.range(1, 5) |
| 1208 |
.parallel(2) |
| 1209 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 1210 |
@Override |
| 1211 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 1212 |
if (v == 3) { |
| 1213 |
return Flowable.error(new TestException()); |
| 1214 |
} |
| 1215 |
return Flowable.just(v); |
| 1216 |
} |
| 1217 |
}, true) |
| 1218 |
.doOnError(new Consumer<Throwable>() { |
| 1219 |
@Override |
| 1220 |
public void accept(Throwable e) throws Exception { |
| 1221 |
if (e instanceof TestException) { |
| 1222 |
count[0]++; |
| 1223 |
} |
| 1224 |
} |
| 1225 |
}) |
| 1226 |
.sequential() |
| 1227 |
.test() |
| 1228 |
.assertValues(1, 2, 4, 5) |
| 1229 |
.assertError(TestException.class) |
| 1230 |
.assertNotComplete(); |
| 1231 |
|
| 1232 |
assertEquals(1, count[0]); |
| 1233 |
} |
| 1234 |
|
| 1235 |
@Test |
| 1236 |
public void concatMapDelayErrorPrefetch() { |
| 1237 |
final int[] count = { 0 }; |
| 1238 |
|
| 1239 |
Flowable.range(1, 5) |
| 1240 |
.parallel(2) |
| 1241 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 1242 |
@Override |
| 1243 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 1244 |
if (v == 3) { |
| 1245 |
return Flowable.error(new TestException()); |
| 1246 |
} |
| 1247 |
return Flowable.just(v); |
| 1248 |
} |
| 1249 |
}, 1, true) |
| 1250 |
.doOnError(new Consumer<Throwable>() { |
| 1251 |
@Override |
| 1252 |
public void accept(Throwable e) throws Exception { |
| 1253 |
if (e instanceof TestException) { |
| 1254 |
count[0]++; |
| 1255 |
} |
| 1256 |
} |
| 1257 |
}) |
| 1258 |
.sequential() |
| 1259 |
.test() |
| 1260 |
.assertValues(1, 2, 4, 5) |
| 1261 |
.assertError(TestException.class) |
| 1262 |
.assertNotComplete(); |
| 1263 |
|
| 1264 |
assertEquals(1, count[0]); |
| 1265 |
} |
| 1266 |
|
| 1267 |
@Test |
| 1268 |
public void concatMapDelayErrorBoundary() { |
| 1269 |
final int[] count = { 0 }; |
| 1270 |
|
| 1271 |
Flowable.range(1, 5) |
| 1272 |
.parallel(2) |
| 1273 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 1274 |
@Override |
| 1275 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 1276 |
if (v == 3) { |
| 1277 |
return Flowable.error(new TestException()); |
| 1278 |
} |
| 1279 |
return Flowable.just(v); |
| 1280 |
} |
| 1281 |
}, false) |
| 1282 |
.doOnError(new Consumer<Throwable>() { |
| 1283 |
@Override |
| 1284 |
public void accept(Throwable e) throws Exception { |
| 1285 |
if (e instanceof TestException) { |
| 1286 |
count[0]++; |
| 1287 |
} |
| 1288 |
} |
| 1289 |
}) |
| 1290 |
.sequential() |
| 1291 |
.test() |
| 1292 |
.assertValues(1, 2) |
| 1293 |
.assertError(TestException.class) |
| 1294 |
.assertNotComplete(); |
| 1295 |
|
| 1296 |
assertEquals(1, count[0]); |
| 1297 |
} |
| 1298 |
|
| 1299 |
public static void checkSubscriberCount(ParallelFlowable<?> source) { |
| 1300 |
int n = source.parallelism(); |
| 1301 |
|
| 1302 |
@SuppressWarnings("unchecked") |
| 1303 |
TestSubscriber<Object>[] consumers = new TestSubscriber[n + 1]; |
| 1304 |
|
| 1305 |
for (int i = 0; i <= n; i++) { |
| 1306 |
consumers[i] = new TestSubscriber<>(); |
| 1307 |
} |
| 1308 |
|
| 1309 |
source.subscribe(consumers); |
| 1310 |
|
| 1311 |
for (int i = 0; i <= n; i++) { |
| 1312 |
consumers[i].awaitDone(5, TimeUnit.SECONDS) |
| 1313 |
.assertFailure(IllegalArgumentException.class); |
| 1314 |
} |
| 1315 |
} |
| 1316 |
|
| 1317 |
@Test |
| 1318 |
public void checkAddBiConsumer() { |
| 1319 |
TestHelper.checkEnum(ListAddBiConsumer.class); |
| 1320 |
} |
| 1321 |
|
| 1322 |
@Test |
| 1323 |
public void mergeBiFunction() throws Exception { |
| 1324 |
MergerBiFunction<Integer> f = new MergerBiFunction<>(Functions.<Integer>naturalComparator()); |
| 1325 |
|
| 1326 |
assertEquals(0, f.apply(Collections.<Integer>emptyList(), Collections.<Integer>emptyList()).size()); |
| 1327 |
|
| 1328 |
assertEquals(Arrays.asList(1, 2), f.apply(Collections.<Integer>emptyList(), Arrays.asList(1, 2))); |
| 1329 |
|
| 1330 |
for (int i = 0; i < 4; i++) { |
| 1331 |
int k = 0; |
| 1332 |
List<Integer> list1 = new ArrayList<>(); |
| 1333 |
for (int j = 0; j < i; j++) { |
| 1334 |
list1.add(k++); |
| 1335 |
} |
| 1336 |
|
| 1337 |
List<Integer> list2 = new ArrayList<>(); |
| 1338 |
for (int j = i; j < 4; j++) { |
| 1339 |
list2.add(k++); |
| 1340 |
} |
| 1341 |
|
| 1342 |
assertEquals(Arrays.asList(0, 1, 2, 3), f.apply(list1, list2)); |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
@Test |
| 1347 |
public void concatMapSubscriberCount() { |
| 1348 |
ParallelFlowableTest.checkSubscriberCount(Flowable.range(1, 5).parallel() |
| 1349 |
.concatMap(Functions.justFunction(Flowable.just(1)))); |
| 1350 |
} |
| 1351 |
|
| 1352 |
@Test |
| 1353 |
public void flatMapSubscriberCount() { |
| 1354 |
ParallelFlowableTest.checkSubscriberCount(Flowable.range(1, 5).parallel() |
| 1355 |
.flatMap(Functions.justFunction(Flowable.just(1)))); |
| 1356 |
} |
| 1357 |
|
| 1358 |
@SuppressWarnings("unchecked") |
| 1359 |
@Test |
| 1360 |
public void fromArraySubscriberCount() { |
| 1361 |
ParallelFlowableTest.checkSubscriberCount(ParallelFlowable.fromArray(new Publisher[] { Flowable.just(1) })); |
| 1362 |
} |
| 1363 |
} |