| 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.operators.flowable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.*; |
| 19 |
import java.util.concurrent.*; |
| 20 |
import java.util.concurrent.atomic.*; |
| 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.subscriptions.BooleanSubscription; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
import io.reactivex.rxjava3.processors.*; |
| 32 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 33 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 34 |
import io.reactivex.rxjava3.testsupport.*; |
| 35 |
|
| 36 |
public class FlowableConcatMapEagerTest extends RxJavaTest { |
| 37 |
|
| 38 |
@Test |
| 39 |
public void normal() { |
| 40 |
Flowable.range(1, 5) |
| 41 |
.concatMapEager(new Function<Integer, Publisher<Integer>>() { |
| 42 |
@Override |
| 43 |
public Publisher<Integer> apply(Integer t) { |
| 44 |
return Flowable.range(t, 2); |
| 45 |
} |
| 46 |
}) |
| 47 |
.test() |
| 48 |
.assertResult(1, 2, 2, 3, 3, 4, 4, 5, 5, 6); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void normalBackpressured() { |
| 53 |
TestSubscriber<Integer> ts = Flowable.range(1, 5) |
| 54 |
.concatMapEager(new Function<Integer, Publisher<Integer>>() { |
| 55 |
@Override |
| 56 |
public Publisher<Integer> apply(Integer t) { |
| 57 |
return Flowable.range(t, 2); |
| 58 |
} |
| 59 |
}) |
| 60 |
.test(3); |
| 61 |
|
| 62 |
ts.assertValues(1, 2, 2); |
| 63 |
|
| 64 |
ts.request(1); |
| 65 |
|
| 66 |
ts.assertValues(1, 2, 2, 3); |
| 67 |
|
| 68 |
ts.request(1); |
| 69 |
|
| 70 |
ts.assertValues(1, 2, 2, 3, 3); |
| 71 |
|
| 72 |
ts.request(5); |
| 73 |
|
| 74 |
ts.assertResult(1, 2, 2, 3, 3, 4, 4, 5, 5, 6); |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void normalDelayBoundary() { |
| 79 |
Flowable.range(1, 5) |
| 80 |
.concatMapEagerDelayError(new Function<Integer, Publisher<Integer>>() { |
| 81 |
@Override |
| 82 |
public Publisher<Integer> apply(Integer t) { |
| 83 |
return Flowable.range(t, 2); |
| 84 |
} |
| 85 |
}, false) |
| 86 |
.test() |
| 87 |
.assertResult(1, 2, 2, 3, 3, 4, 4, 5, 5, 6); |
| 88 |
} |
| 89 |
|
| 90 |
@Test |
| 91 |
public void normalDelayBoundaryBackpressured() { |
| 92 |
TestSubscriber<Integer> ts = Flowable.range(1, 5) |
| 93 |
.concatMapEagerDelayError(new Function<Integer, Publisher<Integer>>() { |
| 94 |
@Override |
| 95 |
public Publisher<Integer> apply(Integer t) { |
| 96 |
return Flowable.range(t, 2); |
| 97 |
} |
| 98 |
}, false) |
| 99 |
.test(3); |
| 100 |
|
| 101 |
ts.assertValues(1, 2, 2); |
| 102 |
|
| 103 |
ts.request(1); |
| 104 |
|
| 105 |
ts.assertValues(1, 2, 2, 3); |
| 106 |
|
| 107 |
ts.request(1); |
| 108 |
|
| 109 |
ts.assertValues(1, 2, 2, 3, 3); |
| 110 |
|
| 111 |
ts.request(5); |
| 112 |
|
| 113 |
ts.assertResult(1, 2, 2, 3, 3, 4, 4, 5, 5, 6); |
| 114 |
} |
| 115 |
|
| 116 |
@Test |
| 117 |
public void normalDelayEnd() { |
| 118 |
Flowable.range(1, 5) |
| 119 |
.concatMapEagerDelayError(new Function<Integer, Publisher<Integer>>() { |
| 120 |
@Override |
| 121 |
public Publisher<Integer> apply(Integer t) { |
| 122 |
return Flowable.range(t, 2); |
| 123 |
} |
| 124 |
}, true) |
| 125 |
.test() |
| 126 |
.assertResult(1, 2, 2, 3, 3, 4, 4, 5, 5, 6); |
| 127 |
} |
| 128 |
|
| 129 |
@Test |
| 130 |
public void normalDelayEndBackpressured() { |
| 131 |
TestSubscriber<Integer> ts = Flowable.range(1, 5) |
| 132 |
.concatMapEagerDelayError(new Function<Integer, Publisher<Integer>>() { |
| 133 |
@Override |
| 134 |
public Publisher<Integer> apply(Integer t) { |
| 135 |
return Flowable.range(t, 2); |
| 136 |
} |
| 137 |
}, true) |
| 138 |
.test(3); |
| 139 |
|
| 140 |
ts.assertValues(1, 2, 2); |
| 141 |
|
| 142 |
ts.request(1); |
| 143 |
|
| 144 |
ts.assertValues(1, 2, 2, 3); |
| 145 |
|
| 146 |
ts.request(1); |
| 147 |
|
| 148 |
ts.assertValues(1, 2, 2, 3, 3); |
| 149 |
|
| 150 |
ts.request(5); |
| 151 |
|
| 152 |
ts.assertResult(1, 2, 2, 3, 3, 4, 4, 5, 5, 6); |
| 153 |
} |
| 154 |
|
| 155 |
@Test |
| 156 |
public void mainErrorsDelayBoundary() { |
| 157 |
PublishProcessor<Integer> main = PublishProcessor.create(); |
| 158 |
final PublishProcessor<Integer> inner = PublishProcessor.create(); |
| 159 |
|
| 160 |
TestSubscriberEx<Integer> ts = main.concatMapEagerDelayError( |
| 161 |
new Function<Integer, Publisher<Integer>>() { |
| 162 |
@Override |
| 163 |
public Publisher<Integer> apply(Integer t) { |
| 164 |
return inner; |
| 165 |
} |
| 166 |
}, false).to(TestHelper.<Integer>testConsumer()); |
| 167 |
|
| 168 |
main.onNext(1); |
| 169 |
|
| 170 |
inner.onNext(2); |
| 171 |
|
| 172 |
ts.assertValue(2); |
| 173 |
|
| 174 |
main.onError(new TestException("Forced failure")); |
| 175 |
|
| 176 |
ts.assertNoErrors(); |
| 177 |
|
| 178 |
inner.onNext(3); |
| 179 |
inner.onComplete(); |
| 180 |
|
| 181 |
ts.assertFailureAndMessage(TestException.class, "Forced failure", 2, 3); |
| 182 |
} |
| 183 |
|
| 184 |
@Test |
| 185 |
public void mainErrorsDelayEnd() { |
| 186 |
PublishProcessor<Integer> main = PublishProcessor.create(); |
| 187 |
final PublishProcessor<Integer> inner = PublishProcessor.create(); |
| 188 |
|
| 189 |
TestSubscriberEx<Integer> ts = main.concatMapEagerDelayError( |
| 190 |
new Function<Integer, Publisher<Integer>>() { |
| 191 |
@Override |
| 192 |
public Publisher<Integer> apply(Integer t) { |
| 193 |
return inner; |
| 194 |
} |
| 195 |
}, true).to(TestHelper.<Integer>testConsumer()); |
| 196 |
|
| 197 |
main.onNext(1); |
| 198 |
main.onNext(2); |
| 199 |
|
| 200 |
inner.onNext(2); |
| 201 |
|
| 202 |
ts.assertValue(2); |
| 203 |
|
| 204 |
main.onError(new TestException("Forced failure")); |
| 205 |
|
| 206 |
ts.assertNoErrors(); |
| 207 |
|
| 208 |
inner.onNext(3); |
| 209 |
inner.onComplete(); |
| 210 |
|
| 211 |
ts.assertFailureAndMessage(TestException.class, "Forced failure", 2, 3, 2, 3); |
| 212 |
} |
| 213 |
|
| 214 |
@Test |
| 215 |
public void mainErrorsImmediate() { |
| 216 |
PublishProcessor<Integer> main = PublishProcessor.create(); |
| 217 |
final PublishProcessor<Integer> inner = PublishProcessor.create(); |
| 218 |
|
| 219 |
TestSubscriberEx<Integer> ts = main.concatMapEager( |
| 220 |
new Function<Integer, Publisher<Integer>>() { |
| 221 |
@Override |
| 222 |
public Publisher<Integer> apply(Integer t) { |
| 223 |
return inner; |
| 224 |
} |
| 225 |
}).to(TestHelper.<Integer>testConsumer()); |
| 226 |
|
| 227 |
main.onNext(1); |
| 228 |
main.onNext(2); |
| 229 |
|
| 230 |
inner.onNext(2); |
| 231 |
|
| 232 |
ts.assertValue(2); |
| 233 |
|
| 234 |
main.onError(new TestException("Forced failure")); |
| 235 |
|
| 236 |
assertFalse("inner has subscribers?", inner.hasSubscribers()); |
| 237 |
|
| 238 |
inner.onNext(3); |
| 239 |
inner.onComplete(); |
| 240 |
|
| 241 |
ts.assertFailureAndMessage(TestException.class, "Forced failure", 2); |
| 242 |
} |
| 243 |
|
| 244 |
@Test |
| 245 |
public void longEager() { |
| 246 |
|
| 247 |
Flowable.range(1, 2 * Flowable.bufferSize()) |
| 248 |
.concatMapEager(new Function<Integer, Publisher<Integer>>() { |
| 249 |
@Override |
| 250 |
public Publisher<Integer> apply(Integer v) { |
| 251 |
return Flowable.just(1); |
| 252 |
} |
| 253 |
}) |
| 254 |
.test() |
| 255 |
.assertValueCount(2 * Flowable.bufferSize()) |
| 256 |
.assertNoErrors() |
| 257 |
.assertComplete(); |
| 258 |
} |
| 259 |
|
| 260 |
TestSubscriber<Object> ts; |
| 261 |
TestSubscriber<Object> tsBp; |
| 262 |
|
| 263 |
Function<Integer, Flowable<Integer>> toJust = new Function<Integer, Flowable<Integer>>() { |
| 264 |
@Override |
| 265 |
public Flowable<Integer> apply(Integer t) { |
| 266 |
return Flowable.just(t); |
| 267 |
} |
| 268 |
}; |
| 269 |
|
| 270 |
Function<Integer, Flowable<Integer>> toRange = new Function<Integer, Flowable<Integer>>() { |
| 271 |
@Override |
| 272 |
public Flowable<Integer> apply(Integer t) { |
| 273 |
return Flowable.range(t, 2); |
| 274 |
} |
| 275 |
}; |
| 276 |
|
| 277 |
@Before |
| 278 |
public void before() { |
| 279 |
ts = new TestSubscriber<>(); |
| 280 |
tsBp = new TestSubscriber<>(0L); |
| 281 |
} |
| 282 |
|
| 283 |
@Test |
| 284 |
public void simple() { |
| 285 |
Flowable.range(1, 100).concatMapEager(toJust).subscribe(ts); |
| 286 |
|
| 287 |
ts.assertNoErrors(); |
| 288 |
ts.assertValueCount(100); |
| 289 |
ts.assertComplete(); |
| 290 |
} |
| 291 |
|
| 292 |
@Test |
| 293 |
public void simple2() { |
| 294 |
Flowable.range(1, 100).concatMapEager(toRange).subscribe(ts); |
| 295 |
|
| 296 |
ts.assertNoErrors(); |
| 297 |
ts.assertValueCount(200); |
| 298 |
ts.assertComplete(); |
| 299 |
} |
| 300 |
|
| 301 |
@Test |
| 302 |
public void eagerness2() { |
| 303 |
final AtomicInteger count = new AtomicInteger(); |
| 304 |
Flowable<Integer> source = Flowable.just(1).doOnNext(new Consumer<Integer>() { |
| 305 |
@Override |
| 306 |
public void accept(Integer t) { |
| 307 |
count.getAndIncrement(); |
| 308 |
} |
| 309 |
}).hide(); |
| 310 |
|
| 311 |
Flowable.concatArrayEager(source, source).subscribe(tsBp); |
| 312 |
|
| 313 |
Assert.assertEquals(2, count.get()); |
| 314 |
tsBp.assertNoErrors(); |
| 315 |
tsBp.assertNotComplete(); |
| 316 |
tsBp.assertNoValues(); |
| 317 |
|
| 318 |
tsBp.request(Long.MAX_VALUE); |
| 319 |
|
| 320 |
tsBp.assertValueCount(count.get()); |
| 321 |
tsBp.assertNoErrors(); |
| 322 |
tsBp.assertComplete(); |
| 323 |
} |
| 324 |
|
| 325 |
@Test |
| 326 |
public void eagerness3() { |
| 327 |
final AtomicInteger count = new AtomicInteger(); |
| 328 |
Flowable<Integer> source = Flowable.just(1).doOnNext(new Consumer<Integer>() { |
| 329 |
@Override |
| 330 |
public void accept(Integer t) { |
| 331 |
count.getAndIncrement(); |
| 332 |
} |
| 333 |
}).hide(); |
| 334 |
|
| 335 |
Flowable.concatArrayEager(source, source, source).subscribe(tsBp); |
| 336 |
|
| 337 |
Assert.assertEquals(3, count.get()); |
| 338 |
tsBp.assertNoErrors(); |
| 339 |
tsBp.assertNotComplete(); |
| 340 |
tsBp.assertNoValues(); |
| 341 |
|
| 342 |
tsBp.request(Long.MAX_VALUE); |
| 343 |
|
| 344 |
tsBp.assertValueCount(count.get()); |
| 345 |
tsBp.assertNoErrors(); |
| 346 |
tsBp.assertComplete(); |
| 347 |
} |
| 348 |
|
| 349 |
@Test |
| 350 |
public void eagerness4() { |
| 351 |
final AtomicInteger count = new AtomicInteger(); |
| 352 |
Flowable<Integer> source = Flowable.just(1).doOnNext(new Consumer<Integer>() { |
| 353 |
@Override |
| 354 |
public void accept(Integer t) { |
| 355 |
count.getAndIncrement(); |
| 356 |
} |
| 357 |
}).hide(); |
| 358 |
|
| 359 |
Flowable.concatArrayEager(source, source, source, source).subscribe(tsBp); |
| 360 |
|
| 361 |
Assert.assertEquals(4, count.get()); |
| 362 |
tsBp.assertNoErrors(); |
| 363 |
tsBp.assertNotComplete(); |
| 364 |
tsBp.assertNoValues(); |
| 365 |
|
| 366 |
tsBp.request(Long.MAX_VALUE); |
| 367 |
|
| 368 |
tsBp.assertValueCount(count.get()); |
| 369 |
tsBp.assertNoErrors(); |
| 370 |
tsBp.assertComplete(); |
| 371 |
} |
| 372 |
|
| 373 |
@Test |
| 374 |
public void eagerness5() { |
| 375 |
final AtomicInteger count = new AtomicInteger(); |
| 376 |
Flowable<Integer> source = Flowable.just(1).doOnNext(new Consumer<Integer>() { |
| 377 |
@Override |
| 378 |
public void accept(Integer t) { |
| 379 |
count.getAndIncrement(); |
| 380 |
} |
| 381 |
}).hide(); |
| 382 |
|
| 383 |
Flowable.concatArrayEager(source, source, source, source, source).subscribe(tsBp); |
| 384 |
|
| 385 |
Assert.assertEquals(5, count.get()); |
| 386 |
tsBp.assertNoErrors(); |
| 387 |
tsBp.assertNotComplete(); |
| 388 |
tsBp.assertNoValues(); |
| 389 |
|
| 390 |
tsBp.request(Long.MAX_VALUE); |
| 391 |
|
| 392 |
tsBp.assertValueCount(count.get()); |
| 393 |
tsBp.assertNoErrors(); |
| 394 |
tsBp.assertComplete(); |
| 395 |
} |
| 396 |
|
| 397 |
@Test |
| 398 |
public void eagerness6() { |
| 399 |
final AtomicInteger count = new AtomicInteger(); |
| 400 |
Flowable<Integer> source = Flowable.just(1).doOnNext(new Consumer<Integer>() { |
| 401 |
@Override |
| 402 |
public void accept(Integer t) { |
| 403 |
count.getAndIncrement(); |
| 404 |
} |
| 405 |
}).hide(); |
| 406 |
|
| 407 |
Flowable.concatArrayEager(source, source, source, source, source, source).subscribe(tsBp); |
| 408 |
|
| 409 |
Assert.assertEquals(6, count.get()); |
| 410 |
tsBp.assertNoErrors(); |
| 411 |
tsBp.assertNotComplete(); |
| 412 |
tsBp.assertNoValues(); |
| 413 |
|
| 414 |
tsBp.request(Long.MAX_VALUE); |
| 415 |
|
| 416 |
tsBp.assertValueCount(count.get()); |
| 417 |
tsBp.assertNoErrors(); |
| 418 |
tsBp.assertComplete(); |
| 419 |
} |
| 420 |
|
| 421 |
@Test |
| 422 |
public void eagerness7() { |
| 423 |
final AtomicInteger count = new AtomicInteger(); |
| 424 |
Flowable<Integer> source = Flowable.just(1).doOnNext(new Consumer<Integer>() { |
| 425 |
@Override |
| 426 |
public void accept(Integer t) { |
| 427 |
count.getAndIncrement(); |
| 428 |
} |
| 429 |
}).hide(); |
| 430 |
|
| 431 |
Flowable.concatArrayEager(source, source, source, source, source, source, source).subscribe(tsBp); |
| 432 |
|
| 433 |
Assert.assertEquals(7, count.get()); |
| 434 |
tsBp.assertNoErrors(); |
| 435 |
tsBp.assertNotComplete(); |
| 436 |
tsBp.assertNoValues(); |
| 437 |
|
| 438 |
tsBp.request(Long.MAX_VALUE); |
| 439 |
|
| 440 |
tsBp.assertValueCount(count.get()); |
| 441 |
tsBp.assertNoErrors(); |
| 442 |
tsBp.assertComplete(); |
| 443 |
} |
| 444 |
|
| 445 |
@Test |
| 446 |
public void eagerness8() { |
| 447 |
final AtomicInteger count = new AtomicInteger(); |
| 448 |
Flowable<Integer> source = Flowable.just(1).doOnNext(new Consumer<Integer>() { |
| 449 |
@Override |
| 450 |
public void accept(Integer t) { |
| 451 |
count.getAndIncrement(); |
| 452 |
} |
| 453 |
}).hide(); |
| 454 |
|
| 455 |
Flowable.concatArrayEager(source, source, source, source, source, source, source, source).subscribe(tsBp); |
| 456 |
|
| 457 |
Assert.assertEquals(8, count.get()); |
| 458 |
tsBp.assertNoErrors(); |
| 459 |
tsBp.assertNotComplete(); |
| 460 |
tsBp.assertNoValues(); |
| 461 |
|
| 462 |
tsBp.request(Long.MAX_VALUE); |
| 463 |
|
| 464 |
tsBp.assertValueCount(count.get()); |
| 465 |
tsBp.assertNoErrors(); |
| 466 |
tsBp.assertComplete(); |
| 467 |
} |
| 468 |
|
| 469 |
@Test |
| 470 |
public void eagerness9() { |
| 471 |
final AtomicInteger count = new AtomicInteger(); |
| 472 |
Flowable<Integer> source = Flowable.just(1).doOnNext(new Consumer<Integer>() { |
| 473 |
@Override |
| 474 |
public void accept(Integer t) { |
| 475 |
count.getAndIncrement(); |
| 476 |
} |
| 477 |
}).hide(); |
| 478 |
|
| 479 |
Flowable.concatArrayEager(source, source, source, source, source, source, source, source, source).subscribe(tsBp); |
| 480 |
|
| 481 |
Assert.assertEquals(9, count.get()); |
| 482 |
tsBp.assertNoErrors(); |
| 483 |
tsBp.assertNotComplete(); |
| 484 |
tsBp.assertNoValues(); |
| 485 |
|
| 486 |
tsBp.request(Long.MAX_VALUE); |
| 487 |
|
| 488 |
tsBp.assertValueCount(count.get()); |
| 489 |
tsBp.assertNoErrors(); |
| 490 |
tsBp.assertComplete(); |
| 491 |
} |
| 492 |
|
| 493 |
@Test |
| 494 |
public void mainError() { |
| 495 |
Flowable.<Integer>error(new TestException()).concatMapEager(toJust).subscribe(ts); |
| 496 |
|
| 497 |
ts.assertNoValues(); |
| 498 |
ts.assertError(TestException.class); |
| 499 |
ts.assertNotComplete(); |
| 500 |
} |
| 501 |
|
| 502 |
@Test |
| 503 |
public void innerError() { |
| 504 |
Flowable.concatArrayEager(Flowable.just(1), Flowable.error(new TestException())).subscribe(ts); |
| 505 |
|
| 506 |
ts.assertValue(1); |
| 507 |
ts.assertError(TestException.class); |
| 508 |
ts.assertNotComplete(); |
| 509 |
} |
| 510 |
|
| 511 |
@Test |
| 512 |
public void innerEmpty() { |
| 513 |
Flowable.concatArrayEager(Flowable.empty(), Flowable.empty()).subscribe(ts); |
| 514 |
|
| 515 |
ts.assertNoValues(); |
| 516 |
ts.assertNoErrors(); |
| 517 |
ts.assertComplete(); |
| 518 |
} |
| 519 |
|
| 520 |
@Test |
| 521 |
public void mapperThrows() { |
| 522 |
Flowable.just(1).concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 523 |
@Override |
| 524 |
public Flowable<Integer> apply(Integer t) { |
| 525 |
throw new TestException(); |
| 526 |
} |
| 527 |
}).subscribe(ts); |
| 528 |
|
| 529 |
ts.assertNoValues(); |
| 530 |
ts.assertNotComplete(); |
| 531 |
ts.assertError(TestException.class); |
| 532 |
} |
| 533 |
|
| 534 |
@Test(expected = IllegalArgumentException.class) |
| 535 |
public void invalidMaxConcurrent() { |
| 536 |
Flowable.just(1).concatMapEager(toJust, 0, Flowable.bufferSize()); |
| 537 |
} |
| 538 |
|
| 539 |
@Test(expected = IllegalArgumentException.class) |
| 540 |
public void invalidCapacityHint() { |
| 541 |
Flowable.just(1).concatMapEager(toJust, Flowable.bufferSize(), 0); |
| 542 |
} |
| 543 |
|
| 544 |
@Test |
| 545 |
public void backpressure() { |
| 546 |
Flowable.concatArrayEager(Flowable.just(1), Flowable.just(1)).subscribe(tsBp); |
| 547 |
|
| 548 |
tsBp.assertNoErrors(); |
| 549 |
tsBp.assertNoValues(); |
| 550 |
tsBp.assertNotComplete(); |
| 551 |
|
| 552 |
tsBp.request(1); |
| 553 |
tsBp.assertValue(1); |
| 554 |
tsBp.assertNoErrors(); |
| 555 |
tsBp.assertNotComplete(); |
| 556 |
|
| 557 |
tsBp.request(1); |
| 558 |
tsBp.assertValues(1, 1); |
| 559 |
tsBp.assertNoErrors(); |
| 560 |
tsBp.assertComplete(); |
| 561 |
} |
| 562 |
|
| 563 |
@Test |
| 564 |
public void asynchronousRun() { |
| 565 |
Flowable.range(1, 2).concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 566 |
@Override |
| 567 |
public Flowable<Integer> apply(Integer t) { |
| 568 |
return Flowable.range(1, 1000).subscribeOn(Schedulers.computation()); |
| 569 |
} |
| 570 |
}).observeOn(Schedulers.single()) |
| 571 |
.test() |
| 572 |
.awaitDone(5, TimeUnit.SECONDS) |
| 573 |
.assertNoErrors() |
| 574 |
.assertValueCount(2000) |
| 575 |
.assertComplete(); |
| 576 |
} |
| 577 |
|
| 578 |
@Test |
| 579 |
public void reentrantWork() { |
| 580 |
final PublishProcessor<Integer> processor = PublishProcessor.create(); |
| 581 |
|
| 582 |
final AtomicBoolean once = new AtomicBoolean(); |
| 583 |
|
| 584 |
processor.concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 585 |
@Override |
| 586 |
public Flowable<Integer> apply(Integer t) { |
| 587 |
return Flowable.just(t); |
| 588 |
} |
| 589 |
}) |
| 590 |
.doOnNext(new Consumer<Integer>() { |
| 591 |
@Override |
| 592 |
public void accept(Integer t) { |
| 593 |
if (once.compareAndSet(false, true)) { |
| 594 |
processor.onNext(2); |
| 595 |
} |
| 596 |
} |
| 597 |
}) |
| 598 |
.subscribe(ts); |
| 599 |
|
| 600 |
processor.onNext(1); |
| 601 |
|
| 602 |
ts.assertNoErrors(); |
| 603 |
ts.assertNotComplete(); |
| 604 |
ts.assertValues(1, 2); |
| 605 |
} |
| 606 |
|
| 607 |
@Test |
| 608 |
public void prefetchIsBounded() { |
| 609 |
final AtomicInteger count = new AtomicInteger(); |
| 610 |
|
| 611 |
TestSubscriber<Object> ts = TestSubscriber.create(0); |
| 612 |
|
| 613 |
Flowable.just(1).concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 614 |
@Override |
| 615 |
public Flowable<Integer> apply(Integer t) { |
| 616 |
return Flowable.range(1, Flowable.bufferSize() * 2) |
| 617 |
.doOnNext(new Consumer<Integer>() { |
| 618 |
@Override |
| 619 |
public void accept(Integer t) { |
| 620 |
count.getAndIncrement(); |
| 621 |
} |
| 622 |
}).hide(); |
| 623 |
} |
| 624 |
}).subscribe(ts); |
| 625 |
|
| 626 |
ts.assertNoErrors(); |
| 627 |
ts.assertNoValues(); |
| 628 |
ts.assertNotComplete(); |
| 629 |
Assert.assertEquals(Flowable.bufferSize(), count.get()); |
| 630 |
} |
| 631 |
|
| 632 |
@Test |
| 633 |
public void maxConcurrent5() { |
| 634 |
final List<Long> requests = new ArrayList<>(); |
| 635 |
Flowable.range(1, 100).doOnRequest(new LongConsumer() { |
| 636 |
@Override |
| 637 |
public void accept(long reqCount) { |
| 638 |
requests.add(reqCount); |
| 639 |
} |
| 640 |
}).concatMapEager(toJust, 5, Flowable.bufferSize()).subscribe(ts); |
| 641 |
|
| 642 |
ts.assertNoErrors(); |
| 643 |
ts.assertValueCount(100); |
| 644 |
ts.assertComplete(); |
| 645 |
|
| 646 |
Assert.assertEquals(5, (long) requests.get(0)); |
| 647 |
Assert.assertEquals(1, (long) requests.get(1)); |
| 648 |
Assert.assertEquals(1, (long) requests.get(2)); |
| 649 |
Assert.assertEquals(1, (long) requests.get(3)); |
| 650 |
Assert.assertEquals(1, (long) requests.get(4)); |
| 651 |
Assert.assertEquals(1, (long) requests.get(5)); |
| 652 |
} |
| 653 |
|
| 654 |
@Test |
| 655 |
public void capacityHint() { |
| 656 |
Flowable<Integer> source = Flowable.just(1); |
| 657 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 658 |
|
| 659 |
Flowable.concatEager(Arrays.asList(source, source, source), 1, 1).subscribe(ts); |
| 660 |
|
| 661 |
ts.assertValues(1, 1, 1); |
| 662 |
ts.assertNoErrors(); |
| 663 |
ts.assertComplete(); |
| 664 |
} |
| 665 |
|
| 666 |
@Test |
| 667 |
public void flowable() { |
| 668 |
Flowable<Integer> source = Flowable.just(1); |
| 669 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 670 |
|
| 671 |
Flowable.concatEager(Flowable.just(source, source, source)).subscribe(ts); |
| 672 |
|
| 673 |
ts.assertValues(1, 1, 1); |
| 674 |
ts.assertNoErrors(); |
| 675 |
ts.assertComplete(); |
| 676 |
} |
| 677 |
|
| 678 |
@Test |
| 679 |
public void flowableCapacityHint() { |
| 680 |
Flowable<Integer> source = Flowable.just(1); |
| 681 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 682 |
|
| 683 |
Flowable.concatEager(Flowable.just(source, source, source), 1, 1).subscribe(ts); |
| 684 |
|
| 685 |
ts.assertValues(1, 1, 1); |
| 686 |
ts.assertNoErrors(); |
| 687 |
ts.assertComplete(); |
| 688 |
} |
| 689 |
|
| 690 |
@Test |
| 691 |
public void badCapacityHint() throws Exception { |
| 692 |
Flowable<Integer> source = Flowable.just(1); |
| 693 |
try { |
| 694 |
Flowable.concatEager(Arrays.asList(source, source, source), 1, -99); |
| 695 |
} catch (IllegalArgumentException ex) { |
| 696 |
assertEquals("prefetch > 0 required but it was -99", ex.getMessage()); |
| 697 |
} |
| 698 |
|
| 699 |
} |
| 700 |
|
| 701 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 702 |
@Test |
| 703 |
public void mappingBadCapacityHint() throws Exception { |
| 704 |
Flowable<Integer> source = Flowable.just(1); |
| 705 |
try { |
| 706 |
Flowable.just(source, source, source).concatMapEager((Function)Functions.identity(), 10, -99); |
| 707 |
} catch (IllegalArgumentException ex) { |
| 708 |
assertEquals("prefetch > 0 required but it was -99", ex.getMessage()); |
| 709 |
} |
| 710 |
|
| 711 |
} |
| 712 |
|
| 713 |
@Test |
| 714 |
public void concatEagerZero() { |
| 715 |
Flowable.concatEager(Collections.<Flowable<Integer>>emptyList()) |
| 716 |
.test() |
| 717 |
.assertResult(); |
| 718 |
} |
| 719 |
|
| 720 |
@Test |
| 721 |
public void concatEagerOne() { |
| 722 |
Flowable.concatEager(Arrays.asList(Flowable.just(1))) |
| 723 |
.test() |
| 724 |
.assertResult(1); |
| 725 |
} |
| 726 |
|
| 727 |
@Test |
| 728 |
public void concatEagerTwo() { |
| 729 |
Flowable.concatEager(Arrays.asList(Flowable.just(1), Flowable.just(2))) |
| 730 |
.test() |
| 731 |
.assertResult(1, 2); |
| 732 |
} |
| 733 |
|
| 734 |
@Test |
| 735 |
public void Flowable() { |
| 736 |
Flowable<Integer> source = Flowable.just(1); |
| 737 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 738 |
|
| 739 |
Flowable.concatEager(Flowable.just(source, source, source)).subscribe(ts); |
| 740 |
|
| 741 |
ts.assertValues(1, 1, 1); |
| 742 |
ts.assertNoErrors(); |
| 743 |
ts.assertComplete(); |
| 744 |
} |
| 745 |
|
| 746 |
@Test |
| 747 |
public void publisherCapacityHint() { |
| 748 |
Flowable<Integer> source = Flowable.just(1); |
| 749 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 750 |
|
| 751 |
Flowable.concatEager(Flowable.just(source, source, source), 1, 1).subscribe(ts); |
| 752 |
|
| 753 |
ts.assertValues(1, 1, 1); |
| 754 |
ts.assertNoErrors(); |
| 755 |
ts.assertComplete(); |
| 756 |
} |
| 757 |
|
| 758 |
@Test |
| 759 |
public void concatEagerIterable() { |
| 760 |
Flowable.concatEager(Arrays.asList(Flowable.just(1), Flowable.just(2))) |
| 761 |
.test() |
| 762 |
.assertResult(1, 2); |
| 763 |
} |
| 764 |
|
| 765 |
@Test |
| 766 |
public void empty() { |
| 767 |
Flowable.<Integer>empty().hide().concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 768 |
@Override |
| 769 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 770 |
return Flowable.range(1, 2); |
| 771 |
} |
| 772 |
}) |
| 773 |
.test() |
| 774 |
.assertResult(); |
| 775 |
} |
| 776 |
|
| 777 |
@Test |
| 778 |
public void dispose() { |
| 779 |
TestHelper.checkDisposed(Flowable.just(1).hide().concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 780 |
@Override |
| 781 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 782 |
return Flowable.range(1, 2); |
| 783 |
} |
| 784 |
})); |
| 785 |
} |
| 786 |
|
| 787 |
@Test |
| 788 |
public void innerError2() { |
| 789 |
Flowable.<Integer>just(1).hide().concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 790 |
@Override |
| 791 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 792 |
return Flowable.error(new TestException()); |
| 793 |
} |
| 794 |
}) |
| 795 |
.test() |
| 796 |
.assertFailure(TestException.class); |
| 797 |
} |
| 798 |
|
| 799 |
@Test |
| 800 |
public void innerOuterRace() { |
| 801 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 802 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 803 |
try { |
| 804 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 805 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 806 |
|
| 807 |
TestSubscriberEx<Integer> ts = pp1.concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 808 |
@Override |
| 809 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 810 |
return pp2; |
| 811 |
} |
| 812 |
}).to(TestHelper.<Integer>testConsumer()); |
| 813 |
|
| 814 |
final TestException ex1 = new TestException(); |
| 815 |
final TestException ex2 = new TestException(); |
| 816 |
|
| 817 |
pp1.onNext(1); |
| 818 |
|
| 819 |
Runnable r1 = new Runnable() { |
| 820 |
@Override |
| 821 |
public void run() { |
| 822 |
pp1.onError(ex1); |
| 823 |
} |
| 824 |
}; |
| 825 |
Runnable r2 = new Runnable() { |
| 826 |
@Override |
| 827 |
public void run() { |
| 828 |
pp2.onError(ex2); |
| 829 |
} |
| 830 |
}; |
| 831 |
|
| 832 |
TestHelper.race(r1, r2); |
| 833 |
|
| 834 |
ts.assertSubscribed().assertNoValues().assertNotComplete(); |
| 835 |
|
| 836 |
Throwable ex = ts.errors().get(0); |
| 837 |
|
| 838 |
if (ex instanceof CompositeException) { |
| 839 |
List<Throwable> es = TestHelper.errorList(ts); |
| 840 |
TestHelper.assertError(es, 0, TestException.class); |
| 841 |
TestHelper.assertError(es, 1, TestException.class); |
| 842 |
} else { |
| 843 |
ts.assertError(TestException.class); |
| 844 |
if (!errors.isEmpty()) { |
| 845 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 846 |
} |
| 847 |
} |
| 848 |
} finally { |
| 849 |
RxJavaPlugins.reset(); |
| 850 |
} |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
@Test |
| 855 |
public void innerErrorMaxConcurrency() { |
| 856 |
Flowable.<Integer>just(1).hide().concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 857 |
@Override |
| 858 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 859 |
return Flowable.error(new TestException()); |
| 860 |
} |
| 861 |
}, 1, 128) |
| 862 |
.test() |
| 863 |
.assertFailure(TestException.class); |
| 864 |
} |
| 865 |
|
| 866 |
@Test |
| 867 |
public void innerCallableThrows() { |
| 868 |
Flowable.<Integer>just(1).hide().concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 869 |
@Override |
| 870 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 871 |
return Flowable.fromCallable(new Callable<Integer>() { |
| 872 |
@Override |
| 873 |
public Integer call() throws Exception { |
| 874 |
throw new TestException(); |
| 875 |
} |
| 876 |
}); |
| 877 |
} |
| 878 |
}) |
| 879 |
.test() |
| 880 |
.assertFailure(TestException.class); |
| 881 |
} |
| 882 |
|
| 883 |
@Test |
| 884 |
public void innerErrorAfterPoll() { |
| 885 |
final UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 886 |
up.onNext(1); |
| 887 |
|
| 888 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 889 |
@Override |
| 890 |
public void onNext(Integer t) { |
| 891 |
super.onNext(t); |
| 892 |
up.onError(new TestException()); |
| 893 |
} |
| 894 |
}; |
| 895 |
|
| 896 |
Flowable.<Integer>just(1).hide() |
| 897 |
.concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 898 |
@Override |
| 899 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 900 |
return up; |
| 901 |
} |
| 902 |
}, 1, 128) |
| 903 |
.subscribe(ts); |
| 904 |
|
| 905 |
ts |
| 906 |
.assertFailure(TestException.class, 1); |
| 907 |
} |
| 908 |
|
| 909 |
@Test |
| 910 |
public void nextCancelRace() { |
| 911 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 912 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 913 |
|
| 914 |
final TestSubscriber<Integer> ts = pp1.concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 915 |
@Override |
| 916 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 917 |
return Flowable.never(); |
| 918 |
} |
| 919 |
}).test(); |
| 920 |
|
| 921 |
Runnable r1 = new Runnable() { |
| 922 |
@Override |
| 923 |
public void run() { |
| 924 |
pp1.onNext(1); |
| 925 |
} |
| 926 |
}; |
| 927 |
Runnable r2 = new Runnable() { |
| 928 |
@Override |
| 929 |
public void run() { |
| 930 |
ts.cancel(); |
| 931 |
} |
| 932 |
}; |
| 933 |
|
| 934 |
TestHelper.race(r1, r2); |
| 935 |
|
| 936 |
ts.assertEmpty(); |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
@Test |
| 941 |
public void mapperCancels() { |
| 942 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 943 |
|
| 944 |
Flowable.just(1).hide() |
| 945 |
.concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 946 |
@Override |
| 947 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 948 |
ts.cancel(); |
| 949 |
return Flowable.never(); |
| 950 |
} |
| 951 |
}, 1, 128) |
| 952 |
.subscribe(ts); |
| 953 |
|
| 954 |
ts.assertEmpty(); |
| 955 |
} |
| 956 |
|
| 957 |
@Test |
| 958 |
public void innerErrorFused() { |
| 959 |
Flowable.<Integer>just(1).hide().concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 960 |
@Override |
| 961 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 962 |
return Flowable.range(1, 2).map(new Function<Integer, Integer>() { |
| 963 |
@Override |
| 964 |
public Integer apply(Integer v) throws Exception { |
| 965 |
throw new TestException(); |
| 966 |
} |
| 967 |
}); |
| 968 |
} |
| 969 |
}) |
| 970 |
.test() |
| 971 |
.assertFailure(TestException.class); |
| 972 |
} |
| 973 |
|
| 974 |
@Test |
| 975 |
public void fuseAndTake() { |
| 976 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 977 |
|
| 978 |
up.onNext(1); |
| 979 |
up.onComplete(); |
| 980 |
|
| 981 |
up.concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 982 |
@Override |
| 983 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 984 |
return Flowable.just(1); |
| 985 |
} |
| 986 |
}) |
| 987 |
.take(1) |
| 988 |
.test() |
| 989 |
.assertResult(1); |
| 990 |
} |
| 991 |
|
| 992 |
@Test |
| 993 |
public void doubleOnSubscribe() { |
| 994 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 995 |
@Override |
| 996 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 997 |
return f.concatMapEager(new Function<Object, Flowable<Object>>() { |
| 998 |
@Override |
| 999 |
public Flowable<Object> apply(Object v) throws Exception { |
| 1000 |
return Flowable.just(v); |
| 1001 |
} |
| 1002 |
}); |
| 1003 |
} |
| 1004 |
}); |
| 1005 |
} |
| 1006 |
|
| 1007 |
@Test |
| 1008 |
public void doubleOnError() { |
| 1009 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1010 |
try { |
| 1011 |
@SuppressWarnings("rawtypes") |
| 1012 |
final Subscriber[] sub = { null }; |
| 1013 |
|
| 1014 |
new Flowable<Integer>() { |
| 1015 |
@Override |
| 1016 |
protected void subscribeActual(Subscriber<? super Integer> s) { |
| 1017 |
sub[0] = s; |
| 1018 |
s.onSubscribe(new BooleanSubscription()); |
| 1019 |
s.onNext(1); |
| 1020 |
s.onError(new TestException("First")); |
| 1021 |
} |
| 1022 |
} |
| 1023 |
.concatMapEager(Functions.justFunction(Flowable.just(1))) |
| 1024 |
.to(TestHelper.<Integer>testConsumer()) |
| 1025 |
.assertFailureAndMessage(TestException.class, "First", 1); |
| 1026 |
|
| 1027 |
sub[0].onError(new TestException("Second")); |
| 1028 |
|
| 1029 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second"); |
| 1030 |
} finally { |
| 1031 |
RxJavaPlugins.reset(); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
@Test |
| 1036 |
public void innerOverflow() { |
| 1037 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1038 |
try { |
| 1039 |
Flowable.just(1) |
| 1040 |
.concatMapEager(new Function<Integer, Publisher<Integer>>() { |
| 1041 |
@Override |
| 1042 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 1043 |
return new Flowable<Integer>() { |
| 1044 |
@Override |
| 1045 |
protected void subscribeActual(Subscriber<? super Integer> s) { |
| 1046 |
s.onSubscribe(new BooleanSubscription()); |
| 1047 |
s.onNext(1); |
| 1048 |
s.onNext(2); |
| 1049 |
s.onError(new TestException()); |
| 1050 |
} |
| 1051 |
}; |
| 1052 |
} |
| 1053 |
}, 1, 1) |
| 1054 |
.test(0L) |
| 1055 |
.assertFailure(MissingBackpressureException.class); |
| 1056 |
|
| 1057 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 1058 |
} finally { |
| 1059 |
RxJavaPlugins.reset(); |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
@Test |
| 1064 |
public void unboundedIn() { |
| 1065 |
int n = Flowable.bufferSize() * 2; |
| 1066 |
Flowable.range(1, n) |
| 1067 |
.concatMapEager(new Function<Integer, Publisher<Integer>>() { |
| 1068 |
@Override |
| 1069 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 1070 |
return Flowable.just(1); |
| 1071 |
} |
| 1072 |
}, Integer.MAX_VALUE, 16) |
| 1073 |
.test() |
| 1074 |
.assertValueCount(n) |
| 1075 |
.assertComplete() |
| 1076 |
.assertNoErrors(); |
| 1077 |
} |
| 1078 |
|
| 1079 |
@Test |
| 1080 |
public void drainCancelRaceOnEmpty() { |
| 1081 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1082 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1083 |
|
| 1084 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 1085 |
|
| 1086 |
Flowable.just(1) |
| 1087 |
.concatMapEager(Functions.justFunction(pp)) |
| 1088 |
.subscribe(ts); |
| 1089 |
|
| 1090 |
Runnable r1 = new Runnable() { |
| 1091 |
@Override |
| 1092 |
public void run() { |
| 1093 |
pp.onComplete(); |
| 1094 |
} |
| 1095 |
}; |
| 1096 |
|
| 1097 |
Runnable r2 = new Runnable() { |
| 1098 |
@Override |
| 1099 |
public void run() { |
| 1100 |
ts.cancel(); |
| 1101 |
} |
| 1102 |
}; |
| 1103 |
|
| 1104 |
TestHelper.race(r1, r2); |
| 1105 |
} |
| 1106 |
} |
| 1107 |
|
| 1108 |
@Test |
| 1109 |
public void innerLong() { |
| 1110 |
int n = Flowable.bufferSize() * 2; |
| 1111 |
|
| 1112 |
Flowable.just(1).hide() |
| 1113 |
.concatMapEager(Functions.justFunction(Flowable.range(1, n).hide())) |
| 1114 |
.rebatchRequests(1) |
| 1115 |
.test() |
| 1116 |
.assertValueCount(n) |
| 1117 |
.assertComplete() |
| 1118 |
.assertNoErrors(); |
| 1119 |
} |
| 1120 |
|
| 1121 |
@Test |
| 1122 |
public void oneDelayed() { |
| 1123 |
Flowable.just(1, 2, 3, 4, 5) |
| 1124 |
.concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 1125 |
@Override |
| 1126 |
public Flowable<Integer> apply(Integer i) throws Exception { |
| 1127 |
return i == 3 ? Flowable.just(i) : Flowable |
| 1128 |
.just(i) |
| 1129 |
.delay(1, TimeUnit.MILLISECONDS, Schedulers.io()); |
| 1130 |
} |
| 1131 |
}) |
| 1132 |
.observeOn(Schedulers.io()) |
| 1133 |
.test() |
| 1134 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1135 |
.assertResult(1, 2, 3, 4, 5) |
| 1136 |
; |
| 1137 |
} |
| 1138 |
|
| 1139 |
@Test |
| 1140 |
@SuppressWarnings("unchecked") |
| 1141 |
public void maxConcurrencyOf2() { |
| 1142 |
List<Integer>[] list = new ArrayList[100]; |
| 1143 |
for (int i = 0; i < 100; i++) { |
| 1144 |
List<Integer> lst = new ArrayList<>(); |
| 1145 |
list[i] = lst; |
| 1146 |
for (int k = 1; k <= 10; k++) { |
| 1147 |
lst.add((i) * 10 + k); |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
Flowable.range(1, 1000) |
| 1152 |
.buffer(10) |
| 1153 |
.concatMapEager(new Function<List<Integer>, Flowable<List<Integer>>>() { |
| 1154 |
@Override |
| 1155 |
public Flowable<List<Integer>> apply(List<Integer> v) |
| 1156 |
throws Exception { |
| 1157 |
return Flowable.just(v) |
| 1158 |
.subscribeOn(Schedulers.io()) |
| 1159 |
.doOnNext(new Consumer<List<Integer>>() { |
| 1160 |
@Override |
| 1161 |
public void accept(List<Integer> v) |
| 1162 |
throws Exception { |
| 1163 |
Thread.sleep(new Random().nextInt(20)); |
| 1164 |
} |
| 1165 |
}); |
| 1166 |
} |
| 1167 |
} |
| 1168 |
, 2, 3) |
| 1169 |
.test() |
| 1170 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1171 |
.assertResult(list); |
| 1172 |
} |
| 1173 |
|
| 1174 |
@Test |
| 1175 |
public void arrayDelayErrorDefault() { |
| 1176 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1177 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1178 |
PublishProcessor<Integer> pp3 = PublishProcessor.create(); |
| 1179 |
|
| 1180 |
TestSubscriber<Integer> ts = Flowable.concatArrayEagerDelayError(pp1, pp2, pp3) |
| 1181 |
.test(); |
| 1182 |
|
| 1183 |
ts.assertEmpty(); |
| 1184 |
|
| 1185 |
assertTrue(pp1.hasSubscribers()); |
| 1186 |
assertTrue(pp2.hasSubscribers()); |
| 1187 |
assertTrue(pp3.hasSubscribers()); |
| 1188 |
|
| 1189 |
pp2.onNext(2); |
| 1190 |
pp2.onComplete(); |
| 1191 |
|
| 1192 |
ts.assertEmpty(); |
| 1193 |
|
| 1194 |
pp1.onNext(1); |
| 1195 |
|
| 1196 |
ts.assertValuesOnly(1); |
| 1197 |
|
| 1198 |
pp1.onComplete(); |
| 1199 |
|
| 1200 |
ts.assertValuesOnly(1, 2); |
| 1201 |
|
| 1202 |
pp3.onComplete(); |
| 1203 |
|
| 1204 |
ts.assertResult(1, 2); |
| 1205 |
} |
| 1206 |
|
| 1207 |
@Test |
| 1208 |
public void arrayDelayErrorMaxConcurrency() { |
| 1209 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1210 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1211 |
PublishProcessor<Integer> pp3 = PublishProcessor.create(); |
| 1212 |
|
| 1213 |
TestSubscriber<Integer> ts = Flowable.concatArrayEagerDelayError(2, 2, pp1, pp2, pp3) |
| 1214 |
.test(); |
| 1215 |
|
| 1216 |
ts.assertEmpty(); |
| 1217 |
|
| 1218 |
assertTrue(pp1.hasSubscribers()); |
| 1219 |
assertTrue(pp2.hasSubscribers()); |
| 1220 |
assertFalse(pp3.hasSubscribers()); |
| 1221 |
|
| 1222 |
pp2.onNext(2); |
| 1223 |
pp2.onComplete(); |
| 1224 |
|
| 1225 |
ts.assertEmpty(); |
| 1226 |
|
| 1227 |
pp1.onNext(1); |
| 1228 |
|
| 1229 |
ts.assertValuesOnly(1); |
| 1230 |
|
| 1231 |
pp1.onComplete(); |
| 1232 |
|
| 1233 |
assertTrue(pp3.hasSubscribers()); |
| 1234 |
|
| 1235 |
ts.assertValuesOnly(1, 2); |
| 1236 |
|
| 1237 |
pp3.onComplete(); |
| 1238 |
|
| 1239 |
ts.assertResult(1, 2); |
| 1240 |
} |
| 1241 |
|
| 1242 |
@Test |
| 1243 |
public void arrayDelayErrorMaxConcurrencyErrorDelayed() { |
| 1244 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1245 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1246 |
PublishProcessor<Integer> pp3 = PublishProcessor.create(); |
| 1247 |
|
| 1248 |
TestSubscriber<Integer> ts = Flowable.concatArrayEagerDelayError(2, 2, pp1, pp2, pp3) |
| 1249 |
.test(); |
| 1250 |
|
| 1251 |
ts.assertEmpty(); |
| 1252 |
|
| 1253 |
assertTrue(pp1.hasSubscribers()); |
| 1254 |
assertTrue(pp2.hasSubscribers()); |
| 1255 |
assertFalse(pp3.hasSubscribers()); |
| 1256 |
|
| 1257 |
pp2.onNext(2); |
| 1258 |
pp2.onError(new TestException()); |
| 1259 |
|
| 1260 |
ts.assertEmpty(); |
| 1261 |
|
| 1262 |
pp1.onNext(1); |
| 1263 |
|
| 1264 |
ts.assertValuesOnly(1); |
| 1265 |
|
| 1266 |
pp1.onComplete(); |
| 1267 |
|
| 1268 |
assertTrue(pp3.hasSubscribers()); |
| 1269 |
|
| 1270 |
ts.assertValuesOnly(1, 2); |
| 1271 |
|
| 1272 |
pp3.onComplete(); |
| 1273 |
|
| 1274 |
ts.assertFailure(TestException.class, 1, 2); |
| 1275 |
} |
| 1276 |
|
| 1277 |
@Test |
| 1278 |
public void cancelActive() { |
| 1279 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1280 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1281 |
|
| 1282 |
TestSubscriber<Integer> ts = Flowable |
| 1283 |
.concatEager(Flowable.just(pp1, pp2)) |
| 1284 |
.test(); |
| 1285 |
|
| 1286 |
assertTrue(pp1.hasSubscribers()); |
| 1287 |
assertTrue(pp2.hasSubscribers()); |
| 1288 |
|
| 1289 |
ts.cancel(); |
| 1290 |
|
| 1291 |
assertFalse(pp1.hasSubscribers()); |
| 1292 |
assertFalse(pp2.hasSubscribers()); |
| 1293 |
} |
| 1294 |
|
| 1295 |
@Test |
| 1296 |
public void cancelNoInnerYet() { |
| 1297 |
PublishProcessor<Flowable<Integer>> pp1 = PublishProcessor.create(); |
| 1298 |
|
| 1299 |
TestSubscriber<Integer> ts = Flowable |
| 1300 |
.concatEager(pp1) |
| 1301 |
.test(); |
| 1302 |
|
| 1303 |
assertTrue(pp1.hasSubscribers()); |
| 1304 |
|
| 1305 |
ts.cancel(); |
| 1306 |
|
| 1307 |
assertFalse(pp1.hasSubscribers()); |
| 1308 |
} |
| 1309 |
|
| 1310 |
@Test |
| 1311 |
public void undeliverableUponCancel() { |
| 1312 |
TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() { |
| 1313 |
@Override |
| 1314 |
public Flowable<Integer> apply(Flowable<Integer> upstream) { |
| 1315 |
return upstream.concatMapEager(new Function<Integer, Flowable<Integer>>() { |
| 1316 |
@Override |
| 1317 |
public Flowable<Integer> apply(Integer v) throws Throwable { |
| 1318 |
return Flowable.just(v).hide(); |
| 1319 |
} |
| 1320 |
}); |
| 1321 |
} |
| 1322 |
}); |
| 1323 |
} |
| 1324 |
|
| 1325 |
@Test |
| 1326 |
public void undeliverableUponCancelDelayError() { |
| 1327 |
TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() { |
| 1328 |
@Override |
| 1329 |
public Flowable<Integer> apply(Flowable<Integer> upstream) { |
| 1330 |
return upstream.concatMapEagerDelayError(new Function<Integer, Flowable<Integer>>() { |
| 1331 |
@Override |
| 1332 |
public Flowable<Integer> apply(Integer v) throws Throwable { |
| 1333 |
return Flowable.just(v).hide(); |
| 1334 |
} |
| 1335 |
}, false); |
| 1336 |
} |
| 1337 |
}); |
| 1338 |
} |
| 1339 |
|
| 1340 |
@Test |
| 1341 |
public void undeliverableUponCancelDelayErrorTillEnd() { |
| 1342 |
TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() { |
| 1343 |
@Override |
| 1344 |
public Flowable<Integer> apply(Flowable<Integer> upstream) { |
| 1345 |
return upstream.concatMapEagerDelayError(new Function<Integer, Flowable<Integer>>() { |
| 1346 |
@Override |
| 1347 |
public Flowable<Integer> apply(Integer v) throws Throwable { |
| 1348 |
return Flowable.just(v).hide(); |
| 1349 |
} |
| 1350 |
}, true); |
| 1351 |
} |
| 1352 |
}); |
| 1353 |
} |
| 1354 |
|
| 1355 |
@Test |
| 1356 |
public void iterableDelayError() { |
| 1357 |
Flowable.concatEagerDelayError(Arrays.asList( |
| 1358 |
Flowable.range(1, 2), |
| 1359 |
Flowable.error(new TestException()), |
| 1360 |
Flowable.range(3, 3) |
| 1361 |
)) |
| 1362 |
.test() |
| 1363 |
.assertFailure(TestException.class, 1, 2, 3, 4, 5); |
| 1364 |
} |
| 1365 |
|
| 1366 |
@Test |
| 1367 |
public void iterableDelayErrorMaxConcurrency() { |
| 1368 |
Flowable.concatEagerDelayError(Arrays.asList( |
| 1369 |
Flowable.range(1, 2), |
| 1370 |
Flowable.error(new TestException()), |
| 1371 |
Flowable.range(3, 3) |
| 1372 |
), 1, 1) |
| 1373 |
.test() |
| 1374 |
.assertFailure(TestException.class, 1, 2, 3, 4, 5); |
| 1375 |
} |
| 1376 |
|
| 1377 |
@Test |
| 1378 |
public void publisherDelayError() { |
| 1379 |
Flowable.concatEagerDelayError(Flowable.fromArray( |
| 1380 |
Flowable.range(1, 2), |
| 1381 |
Flowable.error(new TestException()), |
| 1382 |
Flowable.range(3, 3) |
| 1383 |
)) |
| 1384 |
.test() |
| 1385 |
.assertFailure(TestException.class, 1, 2, 3, 4, 5); |
| 1386 |
} |
| 1387 |
|
| 1388 |
@Test |
| 1389 |
public void publisherDelayErrorMaxConcurrency() { |
| 1390 |
Flowable.concatEagerDelayError(Flowable.fromArray( |
| 1391 |
Flowable.range(1, 2), |
| 1392 |
Flowable.error(new TestException()), |
| 1393 |
Flowable.range(3, 3) |
| 1394 |
), 1, 1) |
| 1395 |
.test() |
| 1396 |
.assertFailure(TestException.class, 1, 2, 3, 4, 5); |
| 1397 |
} |
| 1398 |
|
| 1399 |
@Test |
| 1400 |
public void innerSyncFused() { |
| 1401 |
Flowable.just(1) |
| 1402 |
.hide() |
| 1403 |
.concatMapEagerDelayError(v -> Flowable.range(1, 10), true, 1, 1) |
| 1404 |
.test() |
| 1405 |
.assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 1406 |
} |
| 1407 |
|
| 1408 |
@Test |
| 1409 |
public void badRequest() { |
| 1410 |
TestHelper.assertBadRequestReported(Flowable.never().concatMapEagerDelayError(v -> Flowable.never(), false)); |
| 1411 |
} |
| 1412 |
|
| 1413 |
@Test |
| 1414 |
public void cancelAfterOnNext() { |
| 1415 |
Flowable.just(1) |
| 1416 |
.hide() |
| 1417 |
.concatMapEagerDelayError(v -> Flowable.range(1, 5).hide(), true) |
| 1418 |
.takeUntil(v -> true) |
| 1419 |
.test() |
| 1420 |
.assertResult(1); |
| 1421 |
} |
| 1422 |
|
| 1423 |
@Test |
| 1424 |
public void noInnerQueue() { |
| 1425 |
Flowable.just(1) |
| 1426 |
.hide() |
| 1427 |
.concatMapEagerDelayError(v -> Flowable.fromPublisher(s -> { }), true) |
| 1428 |
.test(0L) |
| 1429 |
.assertEmpty() |
| 1430 |
.requestMore(1L) |
| 1431 |
.assertEmpty() |
| 1432 |
; |
| 1433 |
} |
| 1434 |
} |