| 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.lang.reflect.Method; |
| 19 |
import java.util.*; |
| 20 |
import java.util.concurrent.*; |
| 21 |
import java.util.concurrent.atomic.*; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
import org.reactivestreams.*; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 27 |
import io.reactivex.rxjava3.core.*; |
| 28 |
import io.reactivex.rxjava3.exceptions.*; |
| 29 |
import io.reactivex.rxjava3.functions.*; |
| 30 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 31 |
import io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler; |
| 32 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 33 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 34 |
import io.reactivex.rxjava3.processors.*; |
| 35 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 36 |
import io.reactivex.rxjava3.subscribers.*; |
| 37 |
import io.reactivex.rxjava3.testsupport.*; |
| 38 |
|
| 39 |
public class FlowableConcatMapSchedulerTest extends RxJavaTest { |
| 40 |
|
| 41 |
@Test |
| 42 |
public void boundaryFusion() { |
| 43 |
Flowable.range(1, 10000) |
| 44 |
.observeOn(Schedulers.single()) |
| 45 |
.map(new Function<Integer, String>() { |
| 46 |
@Override |
| 47 |
public String apply(Integer t) throws Exception { |
| 48 |
String name = Thread.currentThread().getName(); |
| 49 |
if (name.contains("RxSingleScheduler")) { |
| 50 |
return "RxSingleScheduler"; |
| 51 |
} |
| 52 |
return name; |
| 53 |
} |
| 54 |
}) |
| 55 |
.concatMap(new Function<String, Publisher<? extends Object>>() { |
| 56 |
@Override |
| 57 |
public Publisher<? extends Object> apply(String v) |
| 58 |
throws Exception { |
| 59 |
return Flowable.just(v); |
| 60 |
} |
| 61 |
}, 2, ImmediateThinScheduler.INSTANCE) |
| 62 |
.observeOn(Schedulers.computation()) |
| 63 |
.distinct() |
| 64 |
.test() |
| 65 |
.awaitDone(5, TimeUnit.SECONDS) |
| 66 |
.assertResult("RxSingleScheduler"); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void innerScalarRequestRace() { |
| 71 |
Flowable<Integer> just = Flowable.just(1); |
| 72 |
int n = 1000; |
| 73 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 74 |
PublishProcessor<Flowable<Integer>> source = PublishProcessor.create(); |
| 75 |
|
| 76 |
TestSubscriber<Integer> ts = source |
| 77 |
.concatMap(v -> v, n + 1, ImmediateThinScheduler.INSTANCE) |
| 78 |
.test(1L); |
| 79 |
|
| 80 |
TestHelper.race(() -> { |
| 81 |
for (int j = 0; j < n; j++) { |
| 82 |
source.onNext(just); |
| 83 |
} |
| 84 |
}, () -> { |
| 85 |
for (int j = 0; j < n; j++) { |
| 86 |
ts.request(1); |
| 87 |
} |
| 88 |
}); |
| 89 |
|
| 90 |
ts.assertValueCount(n); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void innerScalarRequestRaceDelayError() { |
| 96 |
Flowable<Integer> just = Flowable.just(1); |
| 97 |
int n = 1000; |
| 98 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 99 |
PublishProcessor<Flowable<Integer>> source = PublishProcessor.create(); |
| 100 |
|
| 101 |
TestSubscriber<Integer> ts = source |
| 102 |
.concatMapDelayError(v -> v, true, n + 1, ImmediateThinScheduler.INSTANCE) |
| 103 |
.test(1L); |
| 104 |
|
| 105 |
TestHelper.race(() -> { |
| 106 |
for (int j = 0; j < n; j++) { |
| 107 |
source.onNext(just); |
| 108 |
} |
| 109 |
}, () -> { |
| 110 |
for (int j = 0; j < n; j++) { |
| 111 |
ts.request(1); |
| 112 |
} |
| 113 |
}); |
| 114 |
|
| 115 |
ts.assertValueCount(n); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void boundaryFusionDelayError() { |
| 121 |
Flowable.range(1, 10000) |
| 122 |
.observeOn(Schedulers.single()) |
| 123 |
.map(new Function<Integer, String>() { |
| 124 |
@Override |
| 125 |
public String apply(Integer t) throws Exception { |
| 126 |
String name = Thread.currentThread().getName(); |
| 127 |
if (name.contains("RxSingleScheduler")) { |
| 128 |
return "RxSingleScheduler"; |
| 129 |
} |
| 130 |
return name; |
| 131 |
} |
| 132 |
}) |
| 133 |
.concatMapDelayError(new Function<String, Publisher<? extends Object>>() { |
| 134 |
@Override |
| 135 |
public Publisher<? extends Object> apply(String v) |
| 136 |
throws Exception { |
| 137 |
return Flowable.just(v); |
| 138 |
} |
| 139 |
}, true, 2, ImmediateThinScheduler.INSTANCE) |
| 140 |
.observeOn(Schedulers.computation()) |
| 141 |
.distinct() |
| 142 |
.test() |
| 143 |
.awaitDone(5, TimeUnit.SECONDS) |
| 144 |
.assertResult("RxSingleScheduler"); |
| 145 |
} |
| 146 |
|
| 147 |
@Test |
| 148 |
public void pollThrows() { |
| 149 |
Flowable.just(1) |
| 150 |
.map(new Function<Integer, Integer>() { |
| 151 |
@Override |
| 152 |
public Integer apply(Integer v) throws Exception { |
| 153 |
throw new TestException(); |
| 154 |
} |
| 155 |
}) |
| 156 |
.compose(TestHelper.<Integer>flowableStripBoundary()) |
| 157 |
.concatMap(new Function<Integer, Publisher<Integer>>() { |
| 158 |
@Override |
| 159 |
public Publisher<Integer> apply(Integer v) |
| 160 |
throws Exception { |
| 161 |
return Flowable.just(v); |
| 162 |
} |
| 163 |
}, 2, ImmediateThinScheduler.INSTANCE) |
| 164 |
.test() |
| 165 |
.assertFailure(TestException.class); |
| 166 |
} |
| 167 |
|
| 168 |
@Test |
| 169 |
public void pollThrowsDelayError() { |
| 170 |
Flowable.just(1) |
| 171 |
.map(new Function<Integer, Integer>() { |
| 172 |
@Override |
| 173 |
public Integer apply(Integer v) throws Exception { |
| 174 |
throw new TestException(); |
| 175 |
} |
| 176 |
}) |
| 177 |
.compose(TestHelper.<Integer>flowableStripBoundary()) |
| 178 |
.concatMapDelayError(new Function<Integer, Publisher<Integer>>() { |
| 179 |
@Override |
| 180 |
public Publisher<Integer> apply(Integer v) |
| 181 |
throws Exception { |
| 182 |
return Flowable.just(v); |
| 183 |
} |
| 184 |
}, true, 2, ImmediateThinScheduler.INSTANCE) |
| 185 |
.test() |
| 186 |
.assertFailure(TestException.class); |
| 187 |
} |
| 188 |
|
| 189 |
@Test |
| 190 |
public void noCancelPrevious() { |
| 191 |
final AtomicInteger counter = new AtomicInteger(); |
| 192 |
|
| 193 |
Flowable.range(1, 5) |
| 194 |
.concatMap(new Function<Integer, Flowable<Integer>>() { |
| 195 |
@Override |
| 196 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 197 |
return Flowable.just(v).doOnCancel(new Action() { |
| 198 |
@Override |
| 199 |
public void run() throws Exception { |
| 200 |
counter.getAndIncrement(); |
| 201 |
} |
| 202 |
}); |
| 203 |
} |
| 204 |
}, 2, ImmediateThinScheduler.INSTANCE) |
| 205 |
.test() |
| 206 |
.assertResult(1, 2, 3, 4, 5); |
| 207 |
|
| 208 |
assertEquals(0, counter.get()); |
| 209 |
} |
| 210 |
|
| 211 |
@Test |
| 212 |
public void delayErrorCallableTillTheEnd() { |
| 213 |
Flowable.just(1, 2, 3, 101, 102, 23, 890, 120, 32) |
| 214 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 215 |
@Override public Flowable<Integer> apply(final Integer integer) throws Exception { |
| 216 |
return Flowable.fromCallable(new Callable<Integer>() { |
| 217 |
@Override public Integer call() throws Exception { |
| 218 |
if (integer >= 100) { |
| 219 |
throw new NullPointerException("test null exp"); |
| 220 |
} |
| 221 |
return integer; |
| 222 |
} |
| 223 |
}); |
| 224 |
} |
| 225 |
}, true, 2, ImmediateThinScheduler.INSTANCE) |
| 226 |
.test() |
| 227 |
.assertFailure(CompositeException.class, 1, 2, 3, 23, 32); |
| 228 |
} |
| 229 |
|
| 230 |
@Test |
| 231 |
public void delayErrorCallableEager() { |
| 232 |
Flowable.just(1, 2, 3, 101, 102, 23, 890, 120, 32) |
| 233 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 234 |
@Override public Flowable<Integer> apply(final Integer integer) throws Exception { |
| 235 |
return Flowable.fromCallable(new Callable<Integer>() { |
| 236 |
@Override public Integer call() throws Exception { |
| 237 |
if (integer >= 100) { |
| 238 |
throw new NullPointerException("test null exp"); |
| 239 |
} |
| 240 |
return integer; |
| 241 |
} |
| 242 |
}); |
| 243 |
} |
| 244 |
}, false, 2, ImmediateThinScheduler.INSTANCE) |
| 245 |
.test() |
| 246 |
.assertFailure(NullPointerException.class, 1, 2, 3); |
| 247 |
} |
| 248 |
|
| 249 |
@Test |
| 250 |
public void mapperScheduled() { |
| 251 |
TestSubscriber<String> ts = Flowable.just(1) |
| 252 |
.concatMap(new Function<Integer, Flowable<String>>() { |
| 253 |
@Override |
| 254 |
public Flowable<String> apply(Integer t) throws Throwable { |
| 255 |
return Flowable.just(Thread.currentThread().getName()); |
| 256 |
} |
| 257 |
}, 2, Schedulers.single()) |
| 258 |
.test() |
| 259 |
.awaitDone(5, TimeUnit.SECONDS) |
| 260 |
.assertValueCount(1) |
| 261 |
.assertNoErrors() |
| 262 |
.assertComplete(); |
| 263 |
|
| 264 |
assertTrue(ts.values().toString(), ts.values().get(0).startsWith("RxSingleScheduler-")); |
| 265 |
} |
| 266 |
|
| 267 |
@Test |
| 268 |
public void mapperScheduledHidden() { |
| 269 |
TestSubscriber<String> ts = Flowable.just(1) |
| 270 |
.concatMap(new Function<Integer, Flowable<String>>() { |
| 271 |
@Override |
| 272 |
public Flowable<String> apply(Integer t) throws Throwable { |
| 273 |
return Flowable.just(Thread.currentThread().getName()).hide(); |
| 274 |
} |
| 275 |
}, 2, Schedulers.single()) |
| 276 |
.test() |
| 277 |
.awaitDone(5, TimeUnit.SECONDS) |
| 278 |
.assertValueCount(1) |
| 279 |
.assertNoErrors() |
| 280 |
.assertComplete(); |
| 281 |
|
| 282 |
assertTrue(ts.values().toString(), ts.values().get(0).startsWith("RxSingleScheduler-")); |
| 283 |
} |
| 284 |
|
| 285 |
@Test |
| 286 |
public void mapperDelayErrorScheduled() { |
| 287 |
TestSubscriber<String> ts = Flowable.just(1) |
| 288 |
.concatMapDelayError(new Function<Integer, Flowable<String>>() { |
| 289 |
@Override |
| 290 |
public Flowable<String> apply(Integer t) throws Throwable { |
| 291 |
return Flowable.just(Thread.currentThread().getName()); |
| 292 |
} |
| 293 |
}, false, 2, Schedulers.single()) |
| 294 |
.test() |
| 295 |
.awaitDone(5, TimeUnit.SECONDS) |
| 296 |
.assertValueCount(1) |
| 297 |
.assertNoErrors() |
| 298 |
.assertComplete(); |
| 299 |
|
| 300 |
assertTrue(ts.values().toString(), ts.values().get(0).startsWith("RxSingleScheduler-")); |
| 301 |
} |
| 302 |
|
| 303 |
@Test |
| 304 |
public void mapperDelayErrorScheduledHidden() { |
| 305 |
TestSubscriber<String> ts = Flowable.just(1) |
| 306 |
.concatMapDelayError(new Function<Integer, Flowable<String>>() { |
| 307 |
@Override |
| 308 |
public Flowable<String> apply(Integer t) throws Throwable { |
| 309 |
return Flowable.just(Thread.currentThread().getName()).hide(); |
| 310 |
} |
| 311 |
}, false, 2, Schedulers.single()) |
| 312 |
.test() |
| 313 |
.awaitDone(5, TimeUnit.SECONDS) |
| 314 |
.assertValueCount(1) |
| 315 |
.assertNoErrors() |
| 316 |
.assertComplete(); |
| 317 |
|
| 318 |
assertTrue(ts.values().toString(), ts.values().get(0).startsWith("RxSingleScheduler-")); |
| 319 |
} |
| 320 |
|
| 321 |
@Test |
| 322 |
public void mapperDelayError2Scheduled() { |
| 323 |
TestSubscriber<String> ts = Flowable.just(1) |
| 324 |
.concatMapDelayError(new Function<Integer, Flowable<String>>() { |
| 325 |
@Override |
| 326 |
public Flowable<String> apply(Integer t) throws Throwable { |
| 327 |
return Flowable.just(Thread.currentThread().getName()); |
| 328 |
} |
| 329 |
}, true, 2, Schedulers.single()) |
| 330 |
.test() |
| 331 |
.awaitDone(5, TimeUnit.SECONDS) |
| 332 |
.assertValueCount(1) |
| 333 |
.assertNoErrors() |
| 334 |
.assertComplete(); |
| 335 |
|
| 336 |
assertTrue(ts.values().toString(), ts.values().get(0).startsWith("RxSingleScheduler-")); |
| 337 |
} |
| 338 |
|
| 339 |
@Test |
| 340 |
public void mapperDelayError2ScheduledHidden() { |
| 341 |
TestSubscriber<String> ts = Flowable.just(1) |
| 342 |
.concatMapDelayError(new Function<Integer, Flowable<String>>() { |
| 343 |
@Override |
| 344 |
public Flowable<String> apply(Integer t) throws Throwable { |
| 345 |
return Flowable.just(Thread.currentThread().getName()).hide(); |
| 346 |
} |
| 347 |
}, true, 2, Schedulers.single()) |
| 348 |
.test() |
| 349 |
.awaitDone(5, TimeUnit.SECONDS) |
| 350 |
.assertValueCount(1) |
| 351 |
.assertNoErrors() |
| 352 |
.assertComplete(); |
| 353 |
|
| 354 |
assertTrue(ts.values().toString(), ts.values().get(0).startsWith("RxSingleScheduler-")); |
| 355 |
} |
| 356 |
|
| 357 |
@Test |
| 358 |
public void issue2890NoStackoverflow() throws InterruptedException, TimeoutException { |
| 359 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
| 360 |
final Scheduler sch = Schedulers.from(executor); |
| 361 |
|
| 362 |
Function<Integer, Flowable<Integer>> func = new Function<Integer, Flowable<Integer>>() { |
| 363 |
@Override |
| 364 |
public Flowable<Integer> apply(Integer t) { |
| 365 |
Flowable<Integer> flowable = Flowable.just(t) |
| 366 |
.subscribeOn(sch) |
| 367 |
; |
| 368 |
FlowableProcessor<Integer> processor = UnicastProcessor.create(); |
| 369 |
flowable.subscribe(processor); |
| 370 |
return processor; |
| 371 |
} |
| 372 |
}; |
| 373 |
|
| 374 |
int n = 5000; |
| 375 |
final AtomicInteger counter = new AtomicInteger(); |
| 376 |
|
| 377 |
Flowable.range(1, n).concatMap(func, 2, ImmediateThinScheduler.INSTANCE).subscribe(new DefaultSubscriber<Integer>() { |
| 378 |
@Override |
| 379 |
public void onNext(Integer t) { |
| 380 |
// Consume after sleep for 1 ms |
| 381 |
try { |
| 382 |
Thread.sleep(1); |
| 383 |
} catch (InterruptedException e) { |
| 384 |
// ignored |
| 385 |
} |
| 386 |
if (counter.getAndIncrement() % 100 == 0) { |
| 387 |
System.out.print("testIssue2890NoStackoverflow -> "); |
| 388 |
System.out.println(counter.get()); |
| 389 |
}; |
| 390 |
} |
| 391 |
|
| 392 |
@Override |
| 393 |
public void onComplete() { |
| 394 |
executor.shutdown(); |
| 395 |
} |
| 396 |
|
| 397 |
@Override |
| 398 |
public void onError(Throwable e) { |
| 399 |
executor.shutdown(); |
| 400 |
} |
| 401 |
}); |
| 402 |
|
| 403 |
long awaitTerminationTimeoutMillis = 100_000; |
| 404 |
if (!executor.awaitTermination(awaitTerminationTimeoutMillis, TimeUnit.MILLISECONDS)) { |
| 405 |
throw new TimeoutException("Completed " + counter.get() + "/" + n + " before timed out after " |
| 406 |
+ awaitTerminationTimeoutMillis + " milliseconds."); |
| 407 |
} |
| 408 |
|
| 409 |
assertEquals(n, counter.get()); |
| 410 |
} |
| 411 |
|
| 412 |
@Test |
| 413 |
public void concatMapRangeAsyncLoopIssue2876() { |
| 414 |
final long durationSeconds = 2; |
| 415 |
final long startTime = System.currentTimeMillis(); |
| 416 |
for (int i = 0;; i++) { |
| 417 |
//only run this for a max of ten seconds |
| 418 |
if (System.currentTimeMillis() - startTime > TimeUnit.SECONDS.toMillis(durationSeconds)) { |
| 419 |
return; |
| 420 |
} |
| 421 |
if (i % 1000 == 0) { |
| 422 |
System.out.println("concatMapRangeAsyncLoop > " + i); |
| 423 |
} |
| 424 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 425 |
Flowable.range(0, 1000) |
| 426 |
.concatMap(new Function<Integer, Flowable<Integer>>() { |
| 427 |
@Override |
| 428 |
public Flowable<Integer> apply(Integer t) { |
| 429 |
return Flowable.fromIterable(Arrays.asList(t)); |
| 430 |
} |
| 431 |
}, 2, ImmediateThinScheduler.INSTANCE) |
| 432 |
.observeOn(Schedulers.computation()).subscribe(ts); |
| 433 |
|
| 434 |
ts.awaitDone(2500, TimeUnit.MILLISECONDS); |
| 435 |
ts.assertTerminated(); |
| 436 |
ts.assertNoErrors(); |
| 437 |
assertEquals(1000, ts.values().size()); |
| 438 |
assertEquals((Integer)999, ts.values().get(999)); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
@SuppressWarnings("unchecked") |
| 443 |
@Test |
| 444 |
public void concatArray() throws Exception { |
| 445 |
for (int i = 2; i < 10; i++) { |
| 446 |
Flowable<Integer>[] obs = new Flowable[i]; |
| 447 |
Arrays.fill(obs, Flowable.just(1)); |
| 448 |
|
| 449 |
Integer[] expected = new Integer[i]; |
| 450 |
Arrays.fill(expected, 1); |
| 451 |
|
| 452 |
Method m = Flowable.class.getMethod("concatArray", Publisher[].class); |
| 453 |
|
| 454 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 455 |
|
| 456 |
((Flowable<Integer>)m.invoke(null, new Object[]{obs})).subscribe(ts); |
| 457 |
|
| 458 |
ts.assertValues(expected); |
| 459 |
ts.assertNoErrors(); |
| 460 |
ts.assertComplete(); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 465 |
@Test |
| 466 |
public void concatMapJustJust() { |
| 467 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 468 |
|
| 469 |
Flowable.just(Flowable.just(1)).concatMap((Function)Functions.identity(), 2, ImmediateThinScheduler.INSTANCE).subscribe(ts); |
| 470 |
|
| 471 |
ts.assertValue(1); |
| 472 |
ts.assertNoErrors(); |
| 473 |
ts.assertComplete(); |
| 474 |
} |
| 475 |
|
| 476 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 477 |
@Test |
| 478 |
public void concatMapJustRange() { |
| 479 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 480 |
|
| 481 |
Flowable.just(Flowable.range(1, 5)).concatMap((Function)Functions.identity(), 2, ImmediateThinScheduler.INSTANCE).subscribe(ts); |
| 482 |
|
| 483 |
ts.assertValues(1, 2, 3, 4, 5); |
| 484 |
ts.assertNoErrors(); |
| 485 |
ts.assertComplete(); |
| 486 |
} |
| 487 |
|
| 488 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 489 |
@Test |
| 490 |
public void concatMapDelayErrorJustJust() { |
| 491 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 492 |
|
| 493 |
Flowable.just(Flowable.just(1)).concatMapDelayError((Function)Functions.identity(), true, 2, ImmediateThinScheduler.INSTANCE).subscribe(ts); |
| 494 |
|
| 495 |
ts.assertValue(1); |
| 496 |
ts.assertNoErrors(); |
| 497 |
ts.assertComplete(); |
| 498 |
} |
| 499 |
|
| 500 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 501 |
@Test |
| 502 |
public void concatMapDelayErrorJustRange() { |
| 503 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 504 |
|
| 505 |
Flowable.just(Flowable.range(1, 5)).concatMapDelayError((Function)Functions.identity(), true, 2, ImmediateThinScheduler.INSTANCE).subscribe(ts); |
| 506 |
|
| 507 |
ts.assertValues(1, 2, 3, 4, 5); |
| 508 |
ts.assertNoErrors(); |
| 509 |
ts.assertComplete(); |
| 510 |
} |
| 511 |
|
| 512 |
@SuppressWarnings("unchecked") |
| 513 |
@Test |
| 514 |
public void startWithArray() throws Exception { |
| 515 |
for (int i = 2; i < 10; i++) { |
| 516 |
Object[] obs = new Object[i]; |
| 517 |
Arrays.fill(obs, 1); |
| 518 |
|
| 519 |
Integer[] expected = new Integer[i]; |
| 520 |
Arrays.fill(expected, 1); |
| 521 |
|
| 522 |
Method m = Flowable.class.getMethod("startWithArray", Object[].class); |
| 523 |
|
| 524 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 525 |
|
| 526 |
((Flowable<Integer>)m.invoke(Flowable.empty(), new Object[]{obs})).subscribe(ts); |
| 527 |
|
| 528 |
ts.assertValues(expected); |
| 529 |
ts.assertNoErrors(); |
| 530 |
ts.assertComplete(); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
@Test |
| 535 |
public void concatMapDelayError() { |
| 536 |
Flowable.just(Flowable.just(1), Flowable.just(2)) |
| 537 |
.concatMapDelayError(Functions.<Flowable<Integer>>identity(), true, 2, ImmediateThinScheduler.INSTANCE) |
| 538 |
.test() |
| 539 |
.assertResult(1, 2); |
| 540 |
} |
| 541 |
|
| 542 |
@Test |
| 543 |
public void concatMapDelayErrorJustSource() { |
| 544 |
Flowable.just(0) |
| 545 |
.concatMapDelayError(new Function<Object, Flowable<Integer>>() { |
| 546 |
@Override |
| 547 |
public Flowable<Integer> apply(Object v) throws Exception { |
| 548 |
return Flowable.just(1); |
| 549 |
} |
| 550 |
}, true, 16, ImmediateThinScheduler.INSTANCE) |
| 551 |
.test() |
| 552 |
.assertResult(1); |
| 553 |
|
| 554 |
} |
| 555 |
|
| 556 |
@Test |
| 557 |
public void concatMapJustSource() { |
| 558 |
Flowable.just(0).hide() |
| 559 |
.concatMap(new Function<Object, Flowable<Integer>>() { |
| 560 |
@Override |
| 561 |
public Flowable<Integer> apply(Object v) throws Exception { |
| 562 |
return Flowable.just(1); |
| 563 |
} |
| 564 |
}, 16, ImmediateThinScheduler.INSTANCE) |
| 565 |
.test() |
| 566 |
.assertResult(1); |
| 567 |
} |
| 568 |
|
| 569 |
@Test |
| 570 |
public void concatMapJustSourceDelayError() { |
| 571 |
Flowable.just(0).hide() |
| 572 |
.concatMapDelayError(new Function<Object, Flowable<Integer>>() { |
| 573 |
@Override |
| 574 |
public Flowable<Integer> apply(Object v) throws Exception { |
| 575 |
return Flowable.just(1); |
| 576 |
} |
| 577 |
}, false, 16, ImmediateThinScheduler.INSTANCE) |
| 578 |
.test() |
| 579 |
.assertResult(1); |
| 580 |
} |
| 581 |
|
| 582 |
@Test |
| 583 |
public void concatMapScalarBackpressured() { |
| 584 |
Flowable.just(1).hide() |
| 585 |
.concatMap(Functions.justFunction(Flowable.just(2)), 2, ImmediateThinScheduler.INSTANCE) |
| 586 |
.test(1L) |
| 587 |
.assertResult(2); |
| 588 |
} |
| 589 |
|
| 590 |
@Test |
| 591 |
public void concatMapScalarBackpressuredDelayError() { |
| 592 |
Flowable.just(1).hide() |
| 593 |
.concatMapDelayError(Functions.justFunction(Flowable.just(2)), true, 2, ImmediateThinScheduler.INSTANCE) |
| 594 |
.test(1L) |
| 595 |
.assertResult(2); |
| 596 |
} |
| 597 |
|
| 598 |
@Test |
| 599 |
public void concatMapEmpty() { |
| 600 |
Flowable.just(1).hide() |
| 601 |
.concatMap(Functions.justFunction(Flowable.empty()), 2, ImmediateThinScheduler.INSTANCE) |
| 602 |
.test() |
| 603 |
.assertResult(); |
| 604 |
} |
| 605 |
|
| 606 |
@Test |
| 607 |
public void concatMapEmptyDelayError() { |
| 608 |
Flowable.just(1).hide() |
| 609 |
.concatMapDelayError(Functions.justFunction(Flowable.empty()), true, 2, ImmediateThinScheduler.INSTANCE) |
| 610 |
.test() |
| 611 |
.assertResult(); |
| 612 |
} |
| 613 |
|
| 614 |
@Test |
| 615 |
public void ignoreBackpressure() { |
| 616 |
new Flowable<Integer>() { |
| 617 |
@Override |
| 618 |
protected void subscribeActual(Subscriber<? super Integer> s) { |
| 619 |
s.onSubscribe(new BooleanSubscription()); |
| 620 |
for (int i = 0; i < 10; i++) { |
| 621 |
s.onNext(i); |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
.concatMap(Functions.justFunction(Flowable.just(2)), 8, ImmediateThinScheduler.INSTANCE) |
| 626 |
.test(0L) |
| 627 |
.assertFailure(IllegalStateException.class); |
| 628 |
} |
| 629 |
|
| 630 |
@Test |
| 631 |
public void doubleOnSubscribe() { |
| 632 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Integer>>() { |
| 633 |
@Override |
| 634 |
public Publisher<Integer> apply(Flowable<Object> f) throws Exception { |
| 635 |
return f.concatMap(Functions.justFunction(Flowable.just(2)), 2, ImmediateThinScheduler.INSTANCE); |
| 636 |
} |
| 637 |
}); |
| 638 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Integer>>() { |
| 639 |
@Override |
| 640 |
public Publisher<Integer> apply(Flowable<Object> f) throws Exception { |
| 641 |
return f.concatMapDelayError(Functions.justFunction(Flowable.just(2)), true, 2, ImmediateThinScheduler.INSTANCE); |
| 642 |
} |
| 643 |
}); |
| 644 |
} |
| 645 |
|
| 646 |
@Test |
| 647 |
public void immediateInnerNextOuterError() { |
| 648 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 649 |
|
| 650 |
final TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>() { |
| 651 |
@Override |
| 652 |
public void onNext(Integer t) { |
| 653 |
super.onNext(t); |
| 654 |
if (t == 1) { |
| 655 |
pp.onError(new TestException("First")); |
| 656 |
} |
| 657 |
} |
| 658 |
}; |
| 659 |
|
| 660 |
pp.concatMap(Functions.justFunction(Flowable.just(1)), 2, ImmediateThinScheduler.INSTANCE) |
| 661 |
.subscribe(ts); |
| 662 |
|
| 663 |
pp.onNext(1); |
| 664 |
|
| 665 |
assertFalse(pp.hasSubscribers()); |
| 666 |
|
| 667 |
ts.assertFailureAndMessage(TestException.class, "First", 1); |
| 668 |
} |
| 669 |
|
| 670 |
@Test |
| 671 |
public void immediateInnerNextOuterError2() { |
| 672 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 673 |
|
| 674 |
final TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>() { |
| 675 |
@Override |
| 676 |
public void onNext(Integer t) { |
| 677 |
super.onNext(t); |
| 678 |
if (t == 1) { |
| 679 |
pp.onError(new TestException("First")); |
| 680 |
} |
| 681 |
} |
| 682 |
}; |
| 683 |
|
| 684 |
pp.concatMap(Functions.justFunction(Flowable.just(1).hide()), 2, ImmediateThinScheduler.INSTANCE) |
| 685 |
.subscribe(ts); |
| 686 |
|
| 687 |
pp.onNext(1); |
| 688 |
|
| 689 |
assertFalse(pp.hasSubscribers()); |
| 690 |
|
| 691 |
ts.assertFailureAndMessage(TestException.class, "First", 1); |
| 692 |
} |
| 693 |
|
| 694 |
@Test |
| 695 |
public void concatMapInnerError() { |
| 696 |
Flowable.just(1).hide() |
| 697 |
.concatMap(Functions.justFunction(Flowable.error(new TestException())), 2, ImmediateThinScheduler.INSTANCE) |
| 698 |
.test() |
| 699 |
.assertFailure(TestException.class); |
| 700 |
} |
| 701 |
|
| 702 |
@Test |
| 703 |
public void concatMapInnerErrorDelayError() { |
| 704 |
Flowable.just(1).hide() |
| 705 |
.concatMapDelayError(Functions.justFunction(Flowable.error(new TestException())), true, 2, ImmediateThinScheduler.INSTANCE) |
| 706 |
.test() |
| 707 |
.assertFailure(TestException.class); |
| 708 |
} |
| 709 |
|
| 710 |
@Test |
| 711 |
public void badSource() { |
| 712 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() { |
| 713 |
@Override |
| 714 |
public Object apply(Flowable<Integer> f) throws Exception { |
| 715 |
return f.concatMap(Functions.justFunction(Flowable.just(1).hide()), 2, ImmediateThinScheduler.INSTANCE); |
| 716 |
} |
| 717 |
}, true, 1, 1, 1); |
| 718 |
} |
| 719 |
|
| 720 |
@Test |
| 721 |
public void badInnerSource() { |
| 722 |
@SuppressWarnings("rawtypes") |
| 723 |
final Subscriber[] ts0 = { null }; |
| 724 |
TestSubscriberEx<Integer> ts = Flowable.just(1).hide().concatMap(Functions.justFunction(new Flowable<Integer>() { |
| 725 |
@Override |
| 726 |
protected void subscribeActual(Subscriber<? super Integer> s) { |
| 727 |
ts0[0] = s; |
| 728 |
s.onSubscribe(new BooleanSubscription()); |
| 729 |
s.onError(new TestException("First")); |
| 730 |
} |
| 731 |
}), 2, ImmediateThinScheduler.INSTANCE) |
| 732 |
.to(TestHelper.<Integer>testConsumer()); |
| 733 |
|
| 734 |
ts.assertFailureAndMessage(TestException.class, "First"); |
| 735 |
|
| 736 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 737 |
try { |
| 738 |
ts0[0].onError(new TestException("Second")); |
| 739 |
|
| 740 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 741 |
} finally { |
| 742 |
RxJavaPlugins.reset(); |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
@Test |
| 747 |
public void badInnerSourceDelayError() { |
| 748 |
@SuppressWarnings("rawtypes") |
| 749 |
final Subscriber[] ts0 = { null }; |
| 750 |
TestSubscriberEx<Integer> ts = Flowable.just(1).hide().concatMapDelayError(Functions.justFunction(new Flowable<Integer>() { |
| 751 |
@Override |
| 752 |
protected void subscribeActual(Subscriber<? super Integer> s) { |
| 753 |
ts0[0] = s; |
| 754 |
s.onSubscribe(new BooleanSubscription()); |
| 755 |
s.onError(new TestException("First")); |
| 756 |
} |
| 757 |
}), true, 2, ImmediateThinScheduler.INSTANCE) |
| 758 |
.to(TestHelper.<Integer>testConsumer()); |
| 759 |
|
| 760 |
ts.assertFailureAndMessage(TestException.class, "First"); |
| 761 |
|
| 762 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 763 |
try { |
| 764 |
ts0[0].onError(new TestException("Second")); |
| 765 |
|
| 766 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 767 |
} finally { |
| 768 |
RxJavaPlugins.reset(); |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
@Test |
| 773 |
public void badSourceDelayError() { |
| 774 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() { |
| 775 |
@Override |
| 776 |
public Object apply(Flowable<Integer> f) throws Exception { |
| 777 |
return f.concatMapDelayError(Functions.justFunction(Flowable.just(1).hide()), true, 2, ImmediateThinScheduler.INSTANCE); |
| 778 |
} |
| 779 |
}, true, 1, 1, 1); |
| 780 |
} |
| 781 |
|
| 782 |
@Test |
| 783 |
public void fusedCrash() { |
| 784 |
Flowable.range(1, 2) |
| 785 |
.map(new Function<Integer, Object>() { |
| 786 |
@Override |
| 787 |
public Object apply(Integer v) throws Exception { throw new TestException(); } |
| 788 |
}) |
| 789 |
.concatMap(Functions.justFunction(Flowable.just(1)), 2, ImmediateThinScheduler.INSTANCE) |
| 790 |
.test() |
| 791 |
.assertFailure(TestException.class); |
| 792 |
} |
| 793 |
|
| 794 |
@Test |
| 795 |
public void fusedCrashDelayError() { |
| 796 |
Flowable.range(1, 2) |
| 797 |
.map(new Function<Integer, Object>() { |
| 798 |
@Override |
| 799 |
public Object apply(Integer v) throws Exception { throw new TestException(); } |
| 800 |
}) |
| 801 |
.concatMapDelayError(Functions.justFunction(Flowable.just(1)), true, 2, ImmediateThinScheduler.INSTANCE) |
| 802 |
.test() |
| 803 |
.assertFailure(TestException.class); |
| 804 |
} |
| 805 |
|
| 806 |
@Test |
| 807 |
public void callableCrash() { |
| 808 |
Flowable.just(1).hide() |
| 809 |
.concatMap(Functions.justFunction(Flowable.fromCallable(new Callable<Object>() { |
| 810 |
@Override |
| 811 |
public Object call() throws Exception { |
| 812 |
throw new TestException(); |
| 813 |
} |
| 814 |
})), 2, ImmediateThinScheduler.INSTANCE) |
| 815 |
.test() |
| 816 |
.assertFailure(TestException.class); |
| 817 |
} |
| 818 |
|
| 819 |
@Test |
| 820 |
public void callableCrashDelayError() { |
| 821 |
Flowable.just(1).hide() |
| 822 |
.concatMapDelayError(Functions.justFunction(Flowable.fromCallable(new Callable<Object>() { |
| 823 |
@Override |
| 824 |
public Object call() throws Exception { |
| 825 |
throw new TestException(); |
| 826 |
} |
| 827 |
})), true, 2, ImmediateThinScheduler.INSTANCE) |
| 828 |
.test() |
| 829 |
.assertFailure(TestException.class); |
| 830 |
} |
| 831 |
|
| 832 |
@Test |
| 833 |
public void dispose() { |
| 834 |
TestHelper.checkDisposed(Flowable.range(1, 2) |
| 835 |
.concatMap(Functions.justFunction(Flowable.just(1)), 2, ImmediateThinScheduler.INSTANCE)); |
| 836 |
|
| 837 |
TestHelper.checkDisposed(Flowable.range(1, 2) |
| 838 |
.concatMapDelayError(Functions.justFunction(Flowable.just(1)), true, 2, ImmediateThinScheduler.INSTANCE)); |
| 839 |
} |
| 840 |
|
| 841 |
@Test |
| 842 |
public void notVeryEnd() { |
| 843 |
Flowable.range(1, 2) |
| 844 |
.concatMapDelayError(Functions.justFunction(Flowable.error(new TestException())), false, 16, ImmediateThinScheduler.INSTANCE) |
| 845 |
.test() |
| 846 |
.assertFailure(TestException.class); |
| 847 |
} |
| 848 |
|
| 849 |
@Test |
| 850 |
public void error() { |
| 851 |
Flowable.error(new TestException()) |
| 852 |
.concatMapDelayError(Functions.justFunction(Flowable.just(2)), false, 16, ImmediateThinScheduler.INSTANCE) |
| 853 |
.test() |
| 854 |
.assertFailure(TestException.class); |
| 855 |
} |
| 856 |
|
| 857 |
@Test |
| 858 |
public void mapperThrows() { |
| 859 |
Flowable.range(1, 2) |
| 860 |
.concatMap(new Function<Integer, Publisher<Object>>() { |
| 861 |
@Override |
| 862 |
public Publisher<Object> apply(Integer v) throws Exception { |
| 863 |
throw new TestException(); |
| 864 |
} |
| 865 |
}, 2, ImmediateThinScheduler.INSTANCE) |
| 866 |
.test() |
| 867 |
.assertFailure(TestException.class); |
| 868 |
} |
| 869 |
|
| 870 |
@Test |
| 871 |
public void mainErrors() { |
| 872 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 873 |
|
| 874 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 875 |
|
| 876 |
source.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 877 |
@Override |
| 878 |
public Flowable<Integer> apply(Integer v) { |
| 879 |
return Flowable.range(v, 2); |
| 880 |
} |
| 881 |
}, true, 2, ImmediateThinScheduler.INSTANCE).subscribe(ts); |
| 882 |
|
| 883 |
source.onNext(1); |
| 884 |
source.onNext(2); |
| 885 |
source.onError(new TestException()); |
| 886 |
|
| 887 |
ts.assertValues(1, 2, 2, 3); |
| 888 |
ts.assertError(TestException.class); |
| 889 |
ts.assertNotComplete(); |
| 890 |
} |
| 891 |
|
| 892 |
@Test |
| 893 |
public void innerErrors() { |
| 894 |
final Flowable<Integer> inner = Flowable.range(1, 2) |
| 895 |
.concatWith(Flowable.<Integer>error(new TestException())); |
| 896 |
|
| 897 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 898 |
|
| 899 |
Flowable.range(1, 3).concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 900 |
@Override |
| 901 |
public Flowable<Integer> apply(Integer v) { |
| 902 |
return inner; |
| 903 |
} |
| 904 |
}, true, 2, ImmediateThinScheduler.INSTANCE).subscribe(ts); |
| 905 |
|
| 906 |
ts.assertValues(1, 2, 1, 2, 1, 2); |
| 907 |
ts.assertError(CompositeException.class); |
| 908 |
ts.assertNotComplete(); |
| 909 |
} |
| 910 |
|
| 911 |
@Test |
| 912 |
public void singleInnerErrors() { |
| 913 |
final Flowable<Integer> inner = Flowable.range(1, 2).concatWith(Flowable.<Integer>error(new TestException())); |
| 914 |
|
| 915 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 916 |
|
| 917 |
Flowable.just(1) |
| 918 |
.hide() // prevent scalar optimization |
| 919 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 920 |
@Override |
| 921 |
public Flowable<Integer> apply(Integer v) { |
| 922 |
return inner; |
| 923 |
} |
| 924 |
}, true, 2, ImmediateThinScheduler.INSTANCE).subscribe(ts); |
| 925 |
|
| 926 |
ts.assertValues(1, 2); |
| 927 |
ts.assertError(TestException.class); |
| 928 |
ts.assertNotComplete(); |
| 929 |
} |
| 930 |
|
| 931 |
@Test |
| 932 |
public void innerNull() { |
| 933 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 934 |
|
| 935 |
Flowable.just(1) |
| 936 |
.hide() // prevent scalar optimization |
| 937 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 938 |
@Override |
| 939 |
public Flowable<Integer> apply(Integer v) { |
| 940 |
return null; |
| 941 |
} |
| 942 |
}, true, 2, ImmediateThinScheduler.INSTANCE).subscribe(ts); |
| 943 |
|
| 944 |
ts.assertNoValues(); |
| 945 |
ts.assertError(NullPointerException.class); |
| 946 |
ts.assertNotComplete(); |
| 947 |
} |
| 948 |
|
| 949 |
@Test |
| 950 |
public void innerThrows() { |
| 951 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 952 |
|
| 953 |
Flowable.just(1) |
| 954 |
.hide() // prevent scalar optimization |
| 955 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 956 |
@Override |
| 957 |
public Flowable<Integer> apply(Integer v) { |
| 958 |
throw new TestException(); |
| 959 |
} |
| 960 |
}, true, 2, ImmediateThinScheduler.INSTANCE).subscribe(ts); |
| 961 |
|
| 962 |
ts.assertNoValues(); |
| 963 |
ts.assertError(TestException.class); |
| 964 |
ts.assertNotComplete(); |
| 965 |
} |
| 966 |
|
| 967 |
@Test |
| 968 |
public void innerWithEmpty() { |
| 969 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 970 |
|
| 971 |
Flowable.range(1, 3) |
| 972 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 973 |
@Override |
| 974 |
public Flowable<Integer> apply(Integer v) { |
| 975 |
return v == 2 ? Flowable.<Integer>empty() : Flowable.range(1, 2); |
| 976 |
} |
| 977 |
}, true, 2, ImmediateThinScheduler.INSTANCE).subscribe(ts); |
| 978 |
|
| 979 |
ts.assertValues(1, 2, 1, 2); |
| 980 |
ts.assertNoErrors(); |
| 981 |
ts.assertComplete(); |
| 982 |
} |
| 983 |
|
| 984 |
@Test |
| 985 |
public void innerWithScalar() { |
| 986 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 987 |
|
| 988 |
Flowable.range(1, 3) |
| 989 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 990 |
@Override |
| 991 |
public Flowable<Integer> apply(Integer v) { |
| 992 |
return v == 2 ? Flowable.just(3) : Flowable.range(1, 2); |
| 993 |
} |
| 994 |
}, true, 2, ImmediateThinScheduler.INSTANCE).subscribe(ts); |
| 995 |
|
| 996 |
ts.assertValues(1, 2, 3, 1, 2); |
| 997 |
ts.assertNoErrors(); |
| 998 |
ts.assertComplete(); |
| 999 |
} |
| 1000 |
|
| 1001 |
@Test |
| 1002 |
public void backpressure() { |
| 1003 |
TestSubscriber<Integer> ts = TestSubscriber.create(0); |
| 1004 |
|
| 1005 |
Flowable.range(1, 3).concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 1006 |
@Override |
| 1007 |
public Flowable<Integer> apply(Integer v) { |
| 1008 |
return Flowable.range(v, 2); |
| 1009 |
} |
| 1010 |
}, true, 2, ImmediateThinScheduler.INSTANCE).subscribe(ts); |
| 1011 |
|
| 1012 |
ts.assertNoValues(); |
| 1013 |
ts.assertNoErrors(); |
| 1014 |
ts.assertNotComplete(); |
| 1015 |
|
| 1016 |
ts.request(1); |
| 1017 |
ts.assertValues(1); |
| 1018 |
ts.assertNoErrors(); |
| 1019 |
ts.assertNotComplete(); |
| 1020 |
|
| 1021 |
ts.request(3); |
| 1022 |
ts.assertValues(1, 2, 2, 3); |
| 1023 |
ts.assertNoErrors(); |
| 1024 |
ts.assertNotComplete(); |
| 1025 |
|
| 1026 |
ts.request(2); |
| 1027 |
|
| 1028 |
ts.assertValues(1, 2, 2, 3, 3, 4); |
| 1029 |
ts.assertNoErrors(); |
| 1030 |
ts.assertComplete(); |
| 1031 |
} |
| 1032 |
|
| 1033 |
@Test |
| 1034 |
public void mapperScheduledLong() { |
| 1035 |
TestSubscriber<String> ts = Flowable.range(1, 1000) |
| 1036 |
.hide() |
| 1037 |
.observeOn(Schedulers.computation()) |
| 1038 |
.concatMap(new Function<Integer, Flowable<String>>() { |
| 1039 |
@Override |
| 1040 |
public Flowable<String> apply(Integer t) throws Throwable { |
| 1041 |
return Flowable.just(Thread.currentThread().getName()) |
| 1042 |
.repeat(1000) |
| 1043 |
.observeOn(Schedulers.io()); |
| 1044 |
} |
| 1045 |
}, 2, Schedulers.single()) |
| 1046 |
.distinct() |
| 1047 |
.test() |
| 1048 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1049 |
.assertValueCount(1) |
| 1050 |
.assertNoErrors() |
| 1051 |
.assertComplete(); |
| 1052 |
|
| 1053 |
assertTrue(ts.values().toString(), ts.values().get(0).startsWith("RxSingleScheduler-")); |
| 1054 |
} |
| 1055 |
|
| 1056 |
@Test |
| 1057 |
public void mapperDelayErrorScheduledLong() { |
| 1058 |
TestSubscriber<String> ts = Flowable.range(1, 1000) |
| 1059 |
.hide() |
| 1060 |
.observeOn(Schedulers.computation()) |
| 1061 |
.concatMapDelayError(new Function<Integer, Flowable<String>>() { |
| 1062 |
@Override |
| 1063 |
public Flowable<String> apply(Integer t) throws Throwable { |
| 1064 |
return Flowable.just(Thread.currentThread().getName()) |
| 1065 |
.repeat(1000) |
| 1066 |
.observeOn(Schedulers.io()); |
| 1067 |
} |
| 1068 |
}, false, 2, Schedulers.single()) |
| 1069 |
.distinct() |
| 1070 |
.test() |
| 1071 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1072 |
.assertValueCount(1) |
| 1073 |
.assertNoErrors() |
| 1074 |
.assertComplete(); |
| 1075 |
|
| 1076 |
assertTrue(ts.values().toString(), ts.values().get(0).startsWith("RxSingleScheduler-")); |
| 1077 |
} |
| 1078 |
|
| 1079 |
@Test |
| 1080 |
public void mapperDelayError2ScheduledLong() { |
| 1081 |
TestSubscriber<String> ts = Flowable.range(1, 1000) |
| 1082 |
.hide() |
| 1083 |
.observeOn(Schedulers.computation()) |
| 1084 |
.concatMapDelayError(new Function<Integer, Flowable<String>>() { |
| 1085 |
@Override |
| 1086 |
public Flowable<String> apply(Integer t) throws Throwable { |
| 1087 |
return Flowable.just(Thread.currentThread().getName()) |
| 1088 |
.repeat(1000) |
| 1089 |
.observeOn(Schedulers.io()); |
| 1090 |
} |
| 1091 |
}, true, 2, Schedulers.single()) |
| 1092 |
.distinct() |
| 1093 |
.test() |
| 1094 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1095 |
.assertValueCount(1) |
| 1096 |
.assertNoErrors() |
| 1097 |
.assertComplete(); |
| 1098 |
|
| 1099 |
assertTrue(ts.values().toString(), ts.values().get(0).startsWith("RxSingleScheduler-")); |
| 1100 |
} |
| 1101 |
|
| 1102 |
@Test |
| 1103 |
public void undeliverableUponCancel() { |
| 1104 |
TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() { |
| 1105 |
@Override |
| 1106 |
public Flowable<Integer> apply(Flowable<Integer> upstream) { |
| 1107 |
return upstream.concatMap(new Function<Integer, Publisher<Integer>>() { |
| 1108 |
@Override |
| 1109 |
public Publisher<Integer> apply(Integer v) throws Throwable { |
| 1110 |
return Flowable.just(v).hide(); |
| 1111 |
} |
| 1112 |
}, 2, ImmediateThinScheduler.INSTANCE); |
| 1113 |
} |
| 1114 |
}); |
| 1115 |
} |
| 1116 |
|
| 1117 |
@Test |
| 1118 |
public void undeliverableUponCancelDelayError() { |
| 1119 |
TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() { |
| 1120 |
@Override |
| 1121 |
public Flowable<Integer> apply(Flowable<Integer> upstream) { |
| 1122 |
return upstream.concatMapDelayError(new Function<Integer, Publisher<Integer>>() { |
| 1123 |
@Override |
| 1124 |
public Publisher<Integer> apply(Integer v) throws Throwable { |
| 1125 |
return Flowable.just(v).hide(); |
| 1126 |
} |
| 1127 |
}, false, 2, ImmediateThinScheduler.INSTANCE); |
| 1128 |
} |
| 1129 |
}); |
| 1130 |
} |
| 1131 |
|
| 1132 |
@Test |
| 1133 |
public void undeliverableUponCancelDelayErrorTillEnd() { |
| 1134 |
TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() { |
| 1135 |
@Override |
| 1136 |
public Flowable<Integer> apply(Flowable<Integer> upstream) { |
| 1137 |
return upstream.concatMapDelayError(new Function<Integer, Publisher<Integer>>() { |
| 1138 |
@Override |
| 1139 |
public Publisher<Integer> apply(Integer v) throws Throwable { |
| 1140 |
return Flowable.just(v).hide(); |
| 1141 |
} |
| 1142 |
}, true, 2, ImmediateThinScheduler.INSTANCE); |
| 1143 |
} |
| 1144 |
}); |
| 1145 |
} |
| 1146 |
|
| 1147 |
@Test |
| 1148 |
public void fusionRejected() { |
| 1149 |
TestSubscriberEx<Object> ts = new TestSubscriberEx<>(); |
| 1150 |
|
| 1151 |
TestHelper.rejectFlowableFusion() |
| 1152 |
.concatMap(v -> Flowable.never(), 2, ImmediateThinScheduler.INSTANCE) |
| 1153 |
.subscribe(ts); |
| 1154 |
} |
| 1155 |
|
| 1156 |
@Test |
| 1157 |
public void fusionRejectedDelayErrorr() { |
| 1158 |
TestSubscriberEx<Object> ts = new TestSubscriberEx<>(); |
| 1159 |
|
| 1160 |
TestHelper.rejectFlowableFusion() |
| 1161 |
.concatMapDelayError(v -> Flowable.never(), true, 2, ImmediateThinScheduler.INSTANCE) |
| 1162 |
.subscribe(ts); |
| 1163 |
} |
| 1164 |
|
| 1165 |
@Test |
| 1166 |
public void scalarInnerJustDispose() { |
| 1167 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 1168 |
|
| 1169 |
Flowable.just(1) |
| 1170 |
.hide() |
| 1171 |
.concatMap(v -> Flowable.fromCallable(() -> { |
| 1172 |
ts.cancel(); |
| 1173 |
return 1; |
| 1174 |
}), 2, ImmediateThinScheduler.INSTANCE) |
| 1175 |
.subscribe(ts); |
| 1176 |
|
| 1177 |
ts.assertEmpty(); |
| 1178 |
} |
| 1179 |
|
| 1180 |
@Test |
| 1181 |
public void scalarInnerJustDisposeDelayError() { |
| 1182 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 1183 |
|
| 1184 |
Flowable.just(1) |
| 1185 |
.hide() |
| 1186 |
.concatMapDelayError(v -> Flowable.fromCallable(() -> { |
| 1187 |
ts.cancel(); |
| 1188 |
return 1; |
| 1189 |
}), true, 2, ImmediateThinScheduler.INSTANCE) |
| 1190 |
.subscribe(ts); |
| 1191 |
|
| 1192 |
ts.assertEmpty(); |
| 1193 |
} |
| 1194 |
|
| 1195 |
static final class EmptyDisposingFlowable extends Flowable<Object> |
| 1196 |
implements Supplier<Object> { |
| 1197 |
final TestSubscriber<Object> ts; |
| 1198 |
EmptyDisposingFlowable(TestSubscriber<Object> ts) { |
| 1199 |
this.ts = ts; |
| 1200 |
} |
| 1201 |
|
| 1202 |
@Override |
| 1203 |
protected void subscribeActual(@NonNull Subscriber<? super @NonNull Object> subscriber) { |
| 1204 |
EmptySubscription.complete(subscriber); |
| 1205 |
} |
| 1206 |
|
| 1207 |
@Override |
| 1208 |
public @NonNull Object get() throws Throwable { |
| 1209 |
ts.cancel(); |
| 1210 |
return null; |
| 1211 |
} |
| 1212 |
} |
| 1213 |
|
| 1214 |
@Test |
| 1215 |
public void scalarInnerEmptyDisposeDelayError() { |
| 1216 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 1217 |
|
| 1218 |
Flowable.just(1) |
| 1219 |
.hide() |
| 1220 |
.concatMapDelayError(v -> new EmptyDisposingFlowable(ts), |
| 1221 |
true, 2, ImmediateThinScheduler.INSTANCE |
| 1222 |
) |
| 1223 |
.subscribe(ts); |
| 1224 |
|
| 1225 |
ts.assertEmpty(); |
| 1226 |
} |
| 1227 |
|
| 1228 |
@Test |
| 1229 |
public void mainErrorInnerNextIgnoreCancel() { |
| 1230 |
AtomicReference<Subscriber<? super Integer>> ref = new AtomicReference<>(); |
| 1231 |
|
| 1232 |
Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())) |
| 1233 |
.concatMap(v -> Flowable.<Integer>fromPublisher(ref::set), 2, ImmediateThinScheduler.INSTANCE) |
| 1234 |
.doOnError(e -> { |
| 1235 |
ref.get().onSubscribe(new BooleanSubscription()); |
| 1236 |
ref.get().onNext(1); |
| 1237 |
}) |
| 1238 |
.test() |
| 1239 |
.assertFailure(TestException.class); |
| 1240 |
} |
| 1241 |
|
| 1242 |
@Test |
| 1243 |
public void scalarSupplierMainError() { |
| 1244 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1245 |
|
| 1246 |
TestSubscriber<Integer> ts = pp.concatMap(v -> Flowable.fromCallable(() -> { |
| 1247 |
pp.onError(new TestException()); |
| 1248 |
return 2; |
| 1249 |
}), 2, ImmediateThinScheduler.INSTANCE) |
| 1250 |
.test() |
| 1251 |
; |
| 1252 |
|
| 1253 |
pp.onNext(1); |
| 1254 |
|
| 1255 |
ts.assertFailure(TestException.class); |
| 1256 |
} |
| 1257 |
|
| 1258 |
@Test |
| 1259 |
public void mainErrorInnerErrorRace() throws Throwable { |
| 1260 |
TestHelper.withErrorTracking(errors -> { |
| 1261 |
TestException ex1 = new TestException(); |
| 1262 |
TestException ex2 = new TestException(); |
| 1263 |
|
| 1264 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1265 |
AtomicReference<Subscriber<? super Integer>> ref1 = new AtomicReference<>(); |
| 1266 |
AtomicReference<Subscriber<? super Integer>> ref2 = new AtomicReference<>(); |
| 1267 |
|
| 1268 |
TestSubscriber<Integer> ts = Flowable.<Integer>fromPublisher(ref1::set) |
| 1269 |
.concatMap(v -> Flowable.<Integer>fromPublisher(ref2::set), 2, ImmediateThinScheduler.INSTANCE) |
| 1270 |
.test(); |
| 1271 |
|
| 1272 |
ref1.get().onSubscribe(new BooleanSubscription()); |
| 1273 |
ref1.get().onNext(1); |
| 1274 |
ref2.get().onSubscribe(new BooleanSubscription()); |
| 1275 |
|
| 1276 |
TestHelper.race(() -> ref1.get().onError(ex1), () -> ref2.get().onError(ex2)); |
| 1277 |
|
| 1278 |
ts.assertError(RuntimeException.class); |
| 1279 |
errors.clear(); |
| 1280 |
} |
| 1281 |
}); |
| 1282 |
} |
| 1283 |
} |