| 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.observable; |
| 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.*; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.core.Observable; |
| 27 |
import io.reactivex.rxjava3.exceptions.*; |
| 28 |
import io.reactivex.rxjava3.functions.*; |
| 29 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 30 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 31 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 32 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 33 |
import io.reactivex.rxjava3.subjects.*; |
| 34 |
import io.reactivex.rxjava3.testsupport.*; |
| 35 |
|
| 36 |
public class ObservableConcatMapEagerTest extends RxJavaTest { |
| 37 |
|
| 38 |
@Test |
| 39 |
public void normal() { |
| 40 |
Observable.range(1, 5) |
| 41 |
.concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 42 |
@Override |
| 43 |
public ObservableSource<Integer> apply(Integer t) { |
| 44 |
return Observable.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 normalDelayBoundary() { |
| 53 |
Observable.range(1, 5) |
| 54 |
.concatMapEagerDelayError(new Function<Integer, ObservableSource<Integer>>() { |
| 55 |
@Override |
| 56 |
public ObservableSource<Integer> apply(Integer t) { |
| 57 |
return Observable.range(t, 2); |
| 58 |
} |
| 59 |
}, false) |
| 60 |
.test() |
| 61 |
.assertResult(1, 2, 2, 3, 3, 4, 4, 5, 5, 6); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void normalDelayEnd() { |
| 66 |
Observable.range(1, 5) |
| 67 |
.concatMapEagerDelayError(new Function<Integer, ObservableSource<Integer>>() { |
| 68 |
@Override |
| 69 |
public ObservableSource<Integer> apply(Integer t) { |
| 70 |
return Observable.range(t, 2); |
| 71 |
} |
| 72 |
}, true) |
| 73 |
.test() |
| 74 |
.assertResult(1, 2, 2, 3, 3, 4, 4, 5, 5, 6); |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void mainErrorsDelayBoundary() { |
| 79 |
PublishSubject<Integer> main = PublishSubject.create(); |
| 80 |
final PublishSubject<Integer> inner = PublishSubject.create(); |
| 81 |
|
| 82 |
TestObserverEx<Integer> to = main.concatMapEagerDelayError( |
| 83 |
new Function<Integer, ObservableSource<Integer>>() { |
| 84 |
@Override |
| 85 |
public ObservableSource<Integer> apply(Integer t) { |
| 86 |
return inner; |
| 87 |
} |
| 88 |
}, false).to(TestHelper.<Integer>testConsumer()); |
| 89 |
|
| 90 |
main.onNext(1); |
| 91 |
|
| 92 |
inner.onNext(2); |
| 93 |
|
| 94 |
to.assertValue(2); |
| 95 |
|
| 96 |
main.onError(new TestException("Forced failure")); |
| 97 |
|
| 98 |
to.assertNoErrors(); |
| 99 |
|
| 100 |
inner.onNext(3); |
| 101 |
inner.onComplete(); |
| 102 |
|
| 103 |
to.assertFailureAndMessage(TestException.class, "Forced failure", 2, 3); |
| 104 |
} |
| 105 |
|
| 106 |
@Test |
| 107 |
public void mainErrorsDelayEnd() { |
| 108 |
PublishSubject<Integer> main = PublishSubject.create(); |
| 109 |
final PublishSubject<Integer> inner = PublishSubject.create(); |
| 110 |
|
| 111 |
TestObserverEx<Integer> to = main.concatMapEagerDelayError( |
| 112 |
new Function<Integer, ObservableSource<Integer>>() { |
| 113 |
@Override |
| 114 |
public ObservableSource<Integer> apply(Integer t) { |
| 115 |
return inner; |
| 116 |
} |
| 117 |
}, true).to(TestHelper.<Integer>testConsumer()); |
| 118 |
|
| 119 |
main.onNext(1); |
| 120 |
main.onNext(2); |
| 121 |
|
| 122 |
inner.onNext(2); |
| 123 |
|
| 124 |
to.assertValue(2); |
| 125 |
|
| 126 |
main.onError(new TestException("Forced failure")); |
| 127 |
|
| 128 |
to.assertNoErrors(); |
| 129 |
|
| 130 |
inner.onNext(3); |
| 131 |
inner.onComplete(); |
| 132 |
|
| 133 |
to.assertFailureAndMessage(TestException.class, "Forced failure", 2, 3, 2, 3); |
| 134 |
} |
| 135 |
|
| 136 |
@Test |
| 137 |
public void mainErrorsImmediate() { |
| 138 |
PublishSubject<Integer> main = PublishSubject.create(); |
| 139 |
final PublishSubject<Integer> inner = PublishSubject.create(); |
| 140 |
|
| 141 |
TestObserverEx<Integer> to = main.concatMapEager( |
| 142 |
new Function<Integer, ObservableSource<Integer>>() { |
| 143 |
@Override |
| 144 |
public ObservableSource<Integer> apply(Integer t) { |
| 145 |
return inner; |
| 146 |
} |
| 147 |
}).to(TestHelper.<Integer>testConsumer()); |
| 148 |
|
| 149 |
main.onNext(1); |
| 150 |
main.onNext(2); |
| 151 |
|
| 152 |
inner.onNext(2); |
| 153 |
|
| 154 |
to.assertValue(2); |
| 155 |
|
| 156 |
main.onError(new TestException("Forced failure")); |
| 157 |
|
| 158 |
assertFalse("inner has subscribers?", inner.hasObservers()); |
| 159 |
|
| 160 |
inner.onNext(3); |
| 161 |
inner.onComplete(); |
| 162 |
|
| 163 |
to.assertFailureAndMessage(TestException.class, "Forced failure", 2); |
| 164 |
} |
| 165 |
|
| 166 |
@Test |
| 167 |
public void longEager() { |
| 168 |
|
| 169 |
Observable.range(1, 2 * Observable.bufferSize()) |
| 170 |
.concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 171 |
@Override |
| 172 |
public ObservableSource<Integer> apply(Integer v) { |
| 173 |
return Observable.just(1); |
| 174 |
} |
| 175 |
}) |
| 176 |
.test() |
| 177 |
.assertValueCount(2 * Observable.bufferSize()) |
| 178 |
.assertNoErrors() |
| 179 |
.assertComplete(); |
| 180 |
} |
| 181 |
|
| 182 |
TestObserver<Object> to; |
| 183 |
|
| 184 |
Function<Integer, Observable<Integer>> toJust = new Function<Integer, Observable<Integer>>() { |
| 185 |
@Override |
| 186 |
public Observable<Integer> apply(Integer t) { |
| 187 |
return Observable.just(t); |
| 188 |
} |
| 189 |
}; |
| 190 |
|
| 191 |
Function<Integer, Observable<Integer>> toRange = new Function<Integer, Observable<Integer>>() { |
| 192 |
@Override |
| 193 |
public Observable<Integer> apply(Integer t) { |
| 194 |
return Observable.range(t, 2); |
| 195 |
} |
| 196 |
}; |
| 197 |
|
| 198 |
@Before |
| 199 |
public void before() { |
| 200 |
to = new TestObserver<>(); |
| 201 |
} |
| 202 |
|
| 203 |
@Test |
| 204 |
public void simple() { |
| 205 |
Observable.range(1, 100).concatMapEager(toJust).subscribe(to); |
| 206 |
|
| 207 |
to.assertNoErrors(); |
| 208 |
to.assertValueCount(100); |
| 209 |
to.assertComplete(); |
| 210 |
} |
| 211 |
|
| 212 |
@Test |
| 213 |
public void simple2() { |
| 214 |
Observable.range(1, 100).concatMapEager(toRange).subscribe(to); |
| 215 |
|
| 216 |
to.assertNoErrors(); |
| 217 |
to.assertValueCount(200); |
| 218 |
to.assertComplete(); |
| 219 |
} |
| 220 |
|
| 221 |
@Test |
| 222 |
public void eagerness2() { |
| 223 |
final AtomicInteger count = new AtomicInteger(); |
| 224 |
Observable<Integer> source = Observable.just(1).doOnNext(new Consumer<Integer>() { |
| 225 |
@Override |
| 226 |
public void accept(Integer t) { |
| 227 |
count.getAndIncrement(); |
| 228 |
} |
| 229 |
}).hide(); |
| 230 |
|
| 231 |
Observable.concatArrayEager(source, source).subscribe(to); |
| 232 |
|
| 233 |
Assert.assertEquals(2, count.get()); |
| 234 |
|
| 235 |
to.assertValueCount(count.get()); |
| 236 |
to.assertNoErrors(); |
| 237 |
to.assertComplete(); |
| 238 |
} |
| 239 |
|
| 240 |
@Test |
| 241 |
public void eagerness3() { |
| 242 |
final AtomicInteger count = new AtomicInteger(); |
| 243 |
Observable<Integer> source = Observable.just(1).doOnNext(new Consumer<Integer>() { |
| 244 |
@Override |
| 245 |
public void accept(Integer t) { |
| 246 |
count.getAndIncrement(); |
| 247 |
} |
| 248 |
}).hide(); |
| 249 |
|
| 250 |
Observable.concatArrayEager(source, source, source).subscribe(to); |
| 251 |
|
| 252 |
Assert.assertEquals(3, count.get()); |
| 253 |
|
| 254 |
to.assertValueCount(count.get()); |
| 255 |
to.assertNoErrors(); |
| 256 |
to.assertComplete(); |
| 257 |
} |
| 258 |
|
| 259 |
@Test |
| 260 |
public void eagerness4() { |
| 261 |
final AtomicInteger count = new AtomicInteger(); |
| 262 |
Observable<Integer> source = Observable.just(1).doOnNext(new Consumer<Integer>() { |
| 263 |
@Override |
| 264 |
public void accept(Integer t) { |
| 265 |
count.getAndIncrement(); |
| 266 |
} |
| 267 |
}).hide(); |
| 268 |
|
| 269 |
Observable.concatArrayEager(source, source, source, source).subscribe(to); |
| 270 |
|
| 271 |
Assert.assertEquals(4, count.get()); |
| 272 |
|
| 273 |
to.assertValueCount(count.get()); |
| 274 |
to.assertNoErrors(); |
| 275 |
to.assertComplete(); |
| 276 |
} |
| 277 |
|
| 278 |
@Test |
| 279 |
public void eagerness5() { |
| 280 |
final AtomicInteger count = new AtomicInteger(); |
| 281 |
Observable<Integer> source = Observable.just(1).doOnNext(new Consumer<Integer>() { |
| 282 |
@Override |
| 283 |
public void accept(Integer t) { |
| 284 |
count.getAndIncrement(); |
| 285 |
} |
| 286 |
}).hide(); |
| 287 |
|
| 288 |
Observable.concatArrayEager(source, source, source, source, source).subscribe(to); |
| 289 |
|
| 290 |
Assert.assertEquals(5, count.get()); |
| 291 |
|
| 292 |
to.assertValueCount(count.get()); |
| 293 |
to.assertNoErrors(); |
| 294 |
to.assertComplete(); |
| 295 |
} |
| 296 |
|
| 297 |
@Test |
| 298 |
public void eagerness6() { |
| 299 |
final AtomicInteger count = new AtomicInteger(); |
| 300 |
Observable<Integer> source = Observable.just(1).doOnNext(new Consumer<Integer>() { |
| 301 |
@Override |
| 302 |
public void accept(Integer t) { |
| 303 |
count.getAndIncrement(); |
| 304 |
} |
| 305 |
}).hide(); |
| 306 |
|
| 307 |
Observable.concatArrayEager(source, source, source, source, source, source).subscribe(to); |
| 308 |
|
| 309 |
Assert.assertEquals(6, count.get()); |
| 310 |
|
| 311 |
to.assertValueCount(count.get()); |
| 312 |
to.assertNoErrors(); |
| 313 |
to.assertComplete(); |
| 314 |
} |
| 315 |
|
| 316 |
@Test |
| 317 |
public void eagerness7() { |
| 318 |
final AtomicInteger count = new AtomicInteger(); |
| 319 |
Observable<Integer> source = Observable.just(1).doOnNext(new Consumer<Integer>() { |
| 320 |
@Override |
| 321 |
public void accept(Integer t) { |
| 322 |
count.getAndIncrement(); |
| 323 |
} |
| 324 |
}).hide(); |
| 325 |
|
| 326 |
Observable.concatArrayEager(source, source, source, source, source, source, source).subscribe(to); |
| 327 |
|
| 328 |
Assert.assertEquals(7, count.get()); |
| 329 |
|
| 330 |
to.assertValueCount(count.get()); |
| 331 |
to.assertNoErrors(); |
| 332 |
to.assertComplete(); |
| 333 |
} |
| 334 |
|
| 335 |
@Test |
| 336 |
public void eagerness8() { |
| 337 |
final AtomicInteger count = new AtomicInteger(); |
| 338 |
Observable<Integer> source = Observable.just(1).doOnNext(new Consumer<Integer>() { |
| 339 |
@Override |
| 340 |
public void accept(Integer t) { |
| 341 |
count.getAndIncrement(); |
| 342 |
} |
| 343 |
}).hide(); |
| 344 |
|
| 345 |
Observable.concatArrayEager(source, source, source, source, source, source, source, source).subscribe(to); |
| 346 |
|
| 347 |
Assert.assertEquals(8, count.get()); |
| 348 |
|
| 349 |
to.assertValueCount(count.get()); |
| 350 |
to.assertNoErrors(); |
| 351 |
to.assertComplete(); |
| 352 |
} |
| 353 |
|
| 354 |
@Test |
| 355 |
public void eagerness9() { |
| 356 |
final AtomicInteger count = new AtomicInteger(); |
| 357 |
Observable<Integer> source = Observable.just(1).doOnNext(new Consumer<Integer>() { |
| 358 |
@Override |
| 359 |
public void accept(Integer t) { |
| 360 |
count.getAndIncrement(); |
| 361 |
} |
| 362 |
}).hide(); |
| 363 |
|
| 364 |
Observable.concatArrayEager(source, source, source, source, source, source, source, source, source).subscribe(to); |
| 365 |
|
| 366 |
Assert.assertEquals(9, count.get()); |
| 367 |
|
| 368 |
to.assertValueCount(count.get()); |
| 369 |
to.assertNoErrors(); |
| 370 |
to.assertComplete(); |
| 371 |
} |
| 372 |
|
| 373 |
@Test |
| 374 |
public void mainError() { |
| 375 |
Observable.<Integer>error(new TestException()).concatMapEager(toJust).subscribe(to); |
| 376 |
|
| 377 |
to.assertNoValues(); |
| 378 |
to.assertError(TestException.class); |
| 379 |
to.assertNotComplete(); |
| 380 |
} |
| 381 |
|
| 382 |
@Test |
| 383 |
public void innerError() { |
| 384 |
// TODO verify: concatMapEager subscribes first then consumes the sources is okay |
| 385 |
|
| 386 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 387 |
|
| 388 |
Observable.concatArrayEager(Observable.just(1), ps) |
| 389 |
.subscribe(to); |
| 390 |
|
| 391 |
ps.onError(new TestException()); |
| 392 |
|
| 393 |
to.assertValue(1); |
| 394 |
to.assertError(TestException.class); |
| 395 |
to.assertNotComplete(); |
| 396 |
} |
| 397 |
|
| 398 |
@Test |
| 399 |
public void innerEmpty() { |
| 400 |
Observable.concatArrayEager(Observable.empty(), Observable.empty()).subscribe(to); |
| 401 |
|
| 402 |
to.assertNoValues(); |
| 403 |
to.assertNoErrors(); |
| 404 |
to.assertComplete(); |
| 405 |
} |
| 406 |
|
| 407 |
@Test |
| 408 |
public void mapperThrows() { |
| 409 |
Observable.just(1).concatMapEager(new Function<Integer, Observable<Integer>>() { |
| 410 |
@Override |
| 411 |
public Observable<Integer> apply(Integer t) { |
| 412 |
throw new TestException(); |
| 413 |
} |
| 414 |
}).subscribe(to); |
| 415 |
|
| 416 |
to.assertNoValues(); |
| 417 |
to.assertNotComplete(); |
| 418 |
to.assertError(TestException.class); |
| 419 |
} |
| 420 |
|
| 421 |
@Test(expected = IllegalArgumentException.class) |
| 422 |
public void invalidMaxConcurrent() { |
| 423 |
Observable.just(1).concatMapEager(toJust, 0, Observable.bufferSize()); |
| 424 |
} |
| 425 |
|
| 426 |
@Test(expected = IllegalArgumentException.class) |
| 427 |
public void invalidCapacityHint() { |
| 428 |
Observable.just(1).concatMapEager(toJust, Observable.bufferSize(), 0); |
| 429 |
} |
| 430 |
|
| 431 |
@Test |
| 432 |
public void asynchronousRun() { |
| 433 |
Observable.range(1, 2).concatMapEager(new Function<Integer, Observable<Integer>>() { |
| 434 |
@Override |
| 435 |
public Observable<Integer> apply(Integer t) { |
| 436 |
return Observable.range(1, 1000).subscribeOn(Schedulers.computation()); |
| 437 |
} |
| 438 |
}).observeOn(Schedulers.newThread()).subscribe(to); |
| 439 |
|
| 440 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 441 |
to.assertNoErrors(); |
| 442 |
to.assertValueCount(2000); |
| 443 |
} |
| 444 |
|
| 445 |
@Test |
| 446 |
public void reentrantWork() { |
| 447 |
final PublishSubject<Integer> subject = PublishSubject.create(); |
| 448 |
|
| 449 |
final AtomicBoolean once = new AtomicBoolean(); |
| 450 |
|
| 451 |
subject.concatMapEager(new Function<Integer, Observable<Integer>>() { |
| 452 |
@Override |
| 453 |
public Observable<Integer> apply(Integer t) { |
| 454 |
return Observable.just(t); |
| 455 |
} |
| 456 |
}) |
| 457 |
.doOnNext(new Consumer<Integer>() { |
| 458 |
@Override |
| 459 |
public void accept(Integer t) { |
| 460 |
if (once.compareAndSet(false, true)) { |
| 461 |
subject.onNext(2); |
| 462 |
} |
| 463 |
} |
| 464 |
}) |
| 465 |
.subscribe(to); |
| 466 |
|
| 467 |
subject.onNext(1); |
| 468 |
|
| 469 |
to.assertNoErrors(); |
| 470 |
to.assertNotComplete(); |
| 471 |
to.assertValues(1, 2); |
| 472 |
} |
| 473 |
|
| 474 |
@SuppressWarnings("unchecked") |
| 475 |
@Test |
| 476 |
public void concatArrayEager() throws Exception { |
| 477 |
for (int i = 2; i < 10; i++) { |
| 478 |
Observable<Integer>[] obs = new Observable[i]; |
| 479 |
Arrays.fill(obs, Observable.just(1)); |
| 480 |
|
| 481 |
Integer[] expected = new Integer[i]; |
| 482 |
Arrays.fill(expected, 1); |
| 483 |
|
| 484 |
Method m = Observable.class.getMethod("concatArrayEager", ObservableSource[].class); |
| 485 |
|
| 486 |
TestObserver<Integer> to = TestObserver.create(); |
| 487 |
|
| 488 |
((Observable<Integer>)m.invoke(null, new Object[]{obs})).subscribe(to); |
| 489 |
|
| 490 |
to.assertValues(expected); |
| 491 |
to.assertNoErrors(); |
| 492 |
to.assertComplete(); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
@Test |
| 497 |
public void capacityHint() { |
| 498 |
Observable<Integer> source = Observable.just(1); |
| 499 |
TestObserver<Integer> to = TestObserver.create(); |
| 500 |
|
| 501 |
Observable.concatEager(Arrays.asList(source, source, source), 1, 1).subscribe(to); |
| 502 |
|
| 503 |
to.assertValues(1, 1, 1); |
| 504 |
to.assertNoErrors(); |
| 505 |
to.assertComplete(); |
| 506 |
} |
| 507 |
|
| 508 |
@Test |
| 509 |
public void Observable() { |
| 510 |
Observable<Integer> source = Observable.just(1); |
| 511 |
TestObserver<Integer> to = TestObserver.create(); |
| 512 |
|
| 513 |
Observable.concatEager(Observable.just(source, source, source)).subscribe(to); |
| 514 |
|
| 515 |
to.assertValues(1, 1, 1); |
| 516 |
to.assertNoErrors(); |
| 517 |
to.assertComplete(); |
| 518 |
} |
| 519 |
|
| 520 |
@Test |
| 521 |
public void ObservableCapacityHint() { |
| 522 |
Observable<Integer> source = Observable.just(1); |
| 523 |
TestObserver<Integer> to = TestObserver.create(); |
| 524 |
|
| 525 |
Observable.concatEager(Observable.just(source, source, source), 1, 1).subscribe(to); |
| 526 |
|
| 527 |
to.assertValues(1, 1, 1); |
| 528 |
to.assertNoErrors(); |
| 529 |
to.assertComplete(); |
| 530 |
} |
| 531 |
|
| 532 |
@Test |
| 533 |
public void badCapacityHint() throws Exception { |
| 534 |
Observable<Integer> source = Observable.just(1); |
| 535 |
try { |
| 536 |
Observable.concatEager(Arrays.asList(source, source, source), 1, -99); |
| 537 |
} catch (IllegalArgumentException ex) { |
| 538 |
assertEquals("bufferSize > 0 required but it was -99", ex.getMessage()); |
| 539 |
} |
| 540 |
|
| 541 |
} |
| 542 |
|
| 543 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 544 |
@Test |
| 545 |
public void mappingBadCapacityHint() throws Exception { |
| 546 |
Observable<Integer> source = Observable.just(1); |
| 547 |
try { |
| 548 |
Observable.just(source, source, source).concatMapEager((Function)Functions.identity(), 10, -99); |
| 549 |
} catch (IllegalArgumentException ex) { |
| 550 |
assertEquals("bufferSize > 0 required but it was -99", ex.getMessage()); |
| 551 |
} |
| 552 |
|
| 553 |
} |
| 554 |
|
| 555 |
@Test |
| 556 |
public void concatEagerIterable() { |
| 557 |
Observable.concatEager(Arrays.asList(Observable.just(1), Observable.just(2))) |
| 558 |
.test() |
| 559 |
.assertResult(1, 2); |
| 560 |
} |
| 561 |
|
| 562 |
@Test |
| 563 |
public void dispose() { |
| 564 |
TestHelper.checkDisposed(Observable.just(1).hide().concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 565 |
@Override |
| 566 |
public ObservableSource<Integer> apply(Integer v) throws Exception { |
| 567 |
return Observable.range(1, 2); |
| 568 |
} |
| 569 |
})); |
| 570 |
} |
| 571 |
|
| 572 |
@Test |
| 573 |
public void empty() { |
| 574 |
Observable.<Integer>empty().hide().concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 575 |
@Override |
| 576 |
public ObservableSource<Integer> apply(Integer v) throws Exception { |
| 577 |
return Observable.range(1, 2); |
| 578 |
} |
| 579 |
}) |
| 580 |
.test() |
| 581 |
.assertResult(); |
| 582 |
} |
| 583 |
|
| 584 |
@Test |
| 585 |
public void innerError2() { |
| 586 |
Observable.<Integer>just(1).hide().concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 587 |
@Override |
| 588 |
public ObservableSource<Integer> apply(Integer v) throws Exception { |
| 589 |
return Observable.error(new TestException()); |
| 590 |
} |
| 591 |
}) |
| 592 |
.test() |
| 593 |
.assertFailure(TestException.class); |
| 594 |
} |
| 595 |
|
| 596 |
@Test |
| 597 |
public void innerErrorMaxConcurrency() { |
| 598 |
Observable.<Integer>just(1).hide().concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 599 |
@Override |
| 600 |
public ObservableSource<Integer> apply(Integer v) throws Exception { |
| 601 |
return Observable.error(new TestException()); |
| 602 |
} |
| 603 |
}, 1, 128) |
| 604 |
.test() |
| 605 |
.assertFailure(TestException.class); |
| 606 |
} |
| 607 |
|
| 608 |
@Test |
| 609 |
public void innerCallableThrows() { |
| 610 |
Observable.<Integer>just(1).hide().concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 611 |
@Override |
| 612 |
public ObservableSource<Integer> apply(Integer v) throws Exception { |
| 613 |
return Observable.fromCallable(new Callable<Integer>() { |
| 614 |
@Override |
| 615 |
public Integer call() throws Exception { |
| 616 |
throw new TestException(); |
| 617 |
} |
| 618 |
}); |
| 619 |
} |
| 620 |
}) |
| 621 |
.test() |
| 622 |
.assertFailure(TestException.class); |
| 623 |
} |
| 624 |
|
| 625 |
@Test |
| 626 |
public void innerOuterRace() { |
| 627 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 628 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 629 |
try { |
| 630 |
final PublishSubject<Integer> ps1 = PublishSubject.create(); |
| 631 |
final PublishSubject<Integer> ps2 = PublishSubject.create(); |
| 632 |
|
| 633 |
TestObserverEx<Integer> to = ps1.concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 634 |
@Override |
| 635 |
public ObservableSource<Integer> apply(Integer v) throws Exception { |
| 636 |
return ps2; |
| 637 |
} |
| 638 |
}).to(TestHelper.<Integer>testConsumer()); |
| 639 |
|
| 640 |
final TestException ex1 = new TestException(); |
| 641 |
final TestException ex2 = new TestException(); |
| 642 |
|
| 643 |
ps1.onNext(1); |
| 644 |
|
| 645 |
Runnable r1 = new Runnable() { |
| 646 |
@Override |
| 647 |
public void run() { |
| 648 |
ps1.onError(ex1); |
| 649 |
} |
| 650 |
}; |
| 651 |
Runnable r2 = new Runnable() { |
| 652 |
@Override |
| 653 |
public void run() { |
| 654 |
ps2.onError(ex2); |
| 655 |
} |
| 656 |
}; |
| 657 |
|
| 658 |
TestHelper.race(r1, r2); |
| 659 |
|
| 660 |
to.assertSubscribed().assertNoValues().assertNotComplete(); |
| 661 |
|
| 662 |
Throwable ex = to.errors().get(0); |
| 663 |
|
| 664 |
if (ex instanceof CompositeException) { |
| 665 |
List<Throwable> es = TestHelper.errorList(to); |
| 666 |
TestHelper.assertError(es, 0, TestException.class); |
| 667 |
TestHelper.assertError(es, 1, TestException.class); |
| 668 |
} else { |
| 669 |
to.assertError(TestException.class); |
| 670 |
if (!errors.isEmpty()) { |
| 671 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 672 |
} |
| 673 |
} |
| 674 |
} finally { |
| 675 |
RxJavaPlugins.reset(); |
| 676 |
} |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
@Test |
| 681 |
public void nextCancelRace() { |
| 682 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 683 |
final PublishSubject<Integer> ps1 = PublishSubject.create(); |
| 684 |
|
| 685 |
final TestObserver<Integer> to = ps1.concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 686 |
@Override |
| 687 |
public ObservableSource<Integer> apply(Integer v) throws Exception { |
| 688 |
return Observable.never(); |
| 689 |
} |
| 690 |
}).test(); |
| 691 |
|
| 692 |
Runnable r1 = new Runnable() { |
| 693 |
@Override |
| 694 |
public void run() { |
| 695 |
ps1.onNext(1); |
| 696 |
} |
| 697 |
}; |
| 698 |
Runnable r2 = new Runnable() { |
| 699 |
@Override |
| 700 |
public void run() { |
| 701 |
to.dispose(); |
| 702 |
} |
| 703 |
}; |
| 704 |
|
| 705 |
TestHelper.race(r1, r2); |
| 706 |
|
| 707 |
to.assertEmpty(); |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
@Test |
| 712 |
public void mapperCancels() { |
| 713 |
final TestObserver<Integer> to = new TestObserver<>(); |
| 714 |
|
| 715 |
Observable.just(1).hide() |
| 716 |
.concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 717 |
@Override |
| 718 |
public ObservableSource<Integer> apply(Integer v) throws Exception { |
| 719 |
to.dispose(); |
| 720 |
return Observable.never(); |
| 721 |
} |
| 722 |
}, 1, 128) |
| 723 |
.subscribe(to); |
| 724 |
|
| 725 |
to.assertEmpty(); |
| 726 |
} |
| 727 |
|
| 728 |
@Test |
| 729 |
public void innerErrorFused() { |
| 730 |
Observable.<Integer>just(1).hide().concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 731 |
@Override |
| 732 |
public ObservableSource<Integer> apply(Integer v) throws Exception { |
| 733 |
return Observable.range(1, 2).map(new Function<Integer, Integer>() { |
| 734 |
@Override |
| 735 |
public Integer apply(Integer v) throws Exception { |
| 736 |
throw new TestException(); |
| 737 |
} |
| 738 |
}); |
| 739 |
} |
| 740 |
}) |
| 741 |
.test() |
| 742 |
.assertFailure(TestException.class); |
| 743 |
} |
| 744 |
|
| 745 |
@Test |
| 746 |
public void innerErrorAfterPoll() { |
| 747 |
final UnicastSubject<Integer> us = UnicastSubject.create(); |
| 748 |
us.onNext(1); |
| 749 |
|
| 750 |
TestObserver<Integer> to = new TestObserver<Integer>() { |
| 751 |
@Override |
| 752 |
public void onNext(Integer t) { |
| 753 |
super.onNext(t); |
| 754 |
us.onError(new TestException()); |
| 755 |
} |
| 756 |
}; |
| 757 |
|
| 758 |
Observable.<Integer>just(1).hide() |
| 759 |
.concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 760 |
@Override |
| 761 |
public ObservableSource<Integer> apply(Integer v) throws Exception { |
| 762 |
return us; |
| 763 |
} |
| 764 |
}, 1, 128) |
| 765 |
.subscribe(to); |
| 766 |
|
| 767 |
to |
| 768 |
.assertFailure(TestException.class, 1); |
| 769 |
} |
| 770 |
|
| 771 |
@Test |
| 772 |
public void fuseAndTake() { |
| 773 |
UnicastSubject<Integer> us = UnicastSubject.create(); |
| 774 |
|
| 775 |
us.onNext(1); |
| 776 |
us.onComplete(); |
| 777 |
|
| 778 |
us.concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 779 |
@Override |
| 780 |
public ObservableSource<Integer> apply(Integer v) throws Exception { |
| 781 |
return Observable.just(1); |
| 782 |
} |
| 783 |
}) |
| 784 |
.take(1) |
| 785 |
.test() |
| 786 |
.assertResult(1); |
| 787 |
} |
| 788 |
|
| 789 |
@Test |
| 790 |
public void doubleOnSubscribe() { |
| 791 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, ObservableSource<Object>>() { |
| 792 |
@Override |
| 793 |
public ObservableSource<Object> apply(Observable<Object> o) throws Exception { |
| 794 |
return o.concatMapEager(new Function<Object, ObservableSource<Object>>() { |
| 795 |
@Override |
| 796 |
public ObservableSource<Object> apply(Object v) throws Exception { |
| 797 |
return Observable.just(v); |
| 798 |
} |
| 799 |
}); |
| 800 |
} |
| 801 |
}); |
| 802 |
} |
| 803 |
|
| 804 |
@Test |
| 805 |
public void oneDelayed() { |
| 806 |
Observable.just(1, 2, 3, 4, 5) |
| 807 |
.concatMapEager(new Function<Integer, ObservableSource<Integer>>() { |
| 808 |
@Override |
| 809 |
public ObservableSource<Integer> apply(Integer i) throws Exception { |
| 810 |
return i == 3 ? Observable.just(i) : Observable |
| 811 |
.just(i) |
| 812 |
.delay(1, TimeUnit.MILLISECONDS, Schedulers.io()); |
| 813 |
} |
| 814 |
}) |
| 815 |
.observeOn(Schedulers.io()) |
| 816 |
.test() |
| 817 |
.awaitDone(5, TimeUnit.SECONDS) |
| 818 |
.assertResult(1, 2, 3, 4, 5) |
| 819 |
; |
| 820 |
} |
| 821 |
|
| 822 |
@Test |
| 823 |
@SuppressWarnings("unchecked") |
| 824 |
public void maxConcurrencyOf2() { |
| 825 |
List<Integer>[] list = new ArrayList[100]; |
| 826 |
for (int i = 0; i < 100; i++) { |
| 827 |
List<Integer> lst = new ArrayList<>(); |
| 828 |
list[i] = lst; |
| 829 |
for (int k = 1; k <= 10; k++) { |
| 830 |
lst.add((i) * 10 + k); |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
Observable.range(1, 1000) |
| 835 |
.buffer(10) |
| 836 |
.concatMapEager(new Function<List<Integer>, ObservableSource<List<Integer>>>() { |
| 837 |
@Override |
| 838 |
public ObservableSource<List<Integer>> apply(List<Integer> v) |
| 839 |
throws Exception { |
| 840 |
return Observable.just(v) |
| 841 |
.subscribeOn(Schedulers.io()) |
| 842 |
.doOnNext(new Consumer<List<Integer>>() { |
| 843 |
@Override |
| 844 |
public void accept(List<Integer> v) |
| 845 |
throws Exception { |
| 846 |
Thread.sleep(new Random().nextInt(20)); |
| 847 |
} |
| 848 |
}); |
| 849 |
} |
| 850 |
} |
| 851 |
, 2, 3) |
| 852 |
.test() |
| 853 |
.awaitDone(5, TimeUnit.SECONDS) |
| 854 |
.assertResult(list); |
| 855 |
} |
| 856 |
|
| 857 |
@Test |
| 858 |
public void arrayDelayErrorDefault() { |
| 859 |
PublishSubject<Integer> ps1 = PublishSubject.create(); |
| 860 |
PublishSubject<Integer> ps2 = PublishSubject.create(); |
| 861 |
PublishSubject<Integer> ps3 = PublishSubject.create(); |
| 862 |
|
| 863 |
TestObserver<Integer> to = Observable.concatArrayEagerDelayError(ps1, ps2, ps3) |
| 864 |
.test(); |
| 865 |
|
| 866 |
to.assertEmpty(); |
| 867 |
|
| 868 |
assertTrue(ps1.hasObservers()); |
| 869 |
assertTrue(ps2.hasObservers()); |
| 870 |
assertTrue(ps3.hasObservers()); |
| 871 |
|
| 872 |
ps2.onNext(2); |
| 873 |
ps2.onComplete(); |
| 874 |
|
| 875 |
to.assertEmpty(); |
| 876 |
|
| 877 |
ps1.onNext(1); |
| 878 |
|
| 879 |
to.assertValuesOnly(1); |
| 880 |
|
| 881 |
ps1.onComplete(); |
| 882 |
|
| 883 |
to.assertValuesOnly(1, 2); |
| 884 |
|
| 885 |
ps3.onComplete(); |
| 886 |
|
| 887 |
to.assertResult(1, 2); |
| 888 |
} |
| 889 |
|
| 890 |
@Test |
| 891 |
public void arrayDelayErrorMaxConcurrency() { |
| 892 |
PublishSubject<Integer> ps1 = PublishSubject.create(); |
| 893 |
PublishSubject<Integer> ps2 = PublishSubject.create(); |
| 894 |
PublishSubject<Integer> ps3 = PublishSubject.create(); |
| 895 |
|
| 896 |
TestObserver<Integer> to = Observable.concatArrayEagerDelayError(2, 2, ps1, ps2, ps3) |
| 897 |
.test(); |
| 898 |
|
| 899 |
to.assertEmpty(); |
| 900 |
|
| 901 |
assertTrue(ps1.hasObservers()); |
| 902 |
assertTrue(ps2.hasObservers()); |
| 903 |
assertFalse(ps3.hasObservers()); |
| 904 |
|
| 905 |
ps2.onNext(2); |
| 906 |
ps2.onComplete(); |
| 907 |
|
| 908 |
to.assertEmpty(); |
| 909 |
|
| 910 |
ps1.onNext(1); |
| 911 |
|
| 912 |
to.assertValuesOnly(1); |
| 913 |
|
| 914 |
ps1.onComplete(); |
| 915 |
|
| 916 |
assertTrue(ps3.hasObservers()); |
| 917 |
|
| 918 |
to.assertValuesOnly(1, 2); |
| 919 |
|
| 920 |
ps3.onComplete(); |
| 921 |
|
| 922 |
to.assertResult(1, 2); |
| 923 |
} |
| 924 |
|
| 925 |
@Test |
| 926 |
public void arrayDelayErrorMaxConcurrencyErrorDelayed() { |
| 927 |
PublishSubject<Integer> ps1 = PublishSubject.create(); |
| 928 |
PublishSubject<Integer> ps2 = PublishSubject.create(); |
| 929 |
PublishSubject<Integer> ps3 = PublishSubject.create(); |
| 930 |
|
| 931 |
TestObserver<Integer> to = Observable.concatArrayEagerDelayError(2, 2, ps1, ps2, ps3) |
| 932 |
.test(); |
| 933 |
|
| 934 |
to.assertEmpty(); |
| 935 |
|
| 936 |
assertTrue(ps1.hasObservers()); |
| 937 |
assertTrue(ps2.hasObservers()); |
| 938 |
assertFalse(ps3.hasObservers()); |
| 939 |
|
| 940 |
ps2.onNext(2); |
| 941 |
ps2.onError(new TestException()); |
| 942 |
|
| 943 |
to.assertEmpty(); |
| 944 |
|
| 945 |
ps1.onNext(1); |
| 946 |
|
| 947 |
to.assertValuesOnly(1); |
| 948 |
|
| 949 |
ps1.onComplete(); |
| 950 |
|
| 951 |
assertTrue(ps3.hasObservers()); |
| 952 |
|
| 953 |
to.assertValuesOnly(1, 2); |
| 954 |
|
| 955 |
ps3.onComplete(); |
| 956 |
|
| 957 |
to.assertFailure(TestException.class, 1, 2); |
| 958 |
} |
| 959 |
|
| 960 |
@Test |
| 961 |
public void cancelActive() { |
| 962 |
PublishSubject<Integer> ps1 = PublishSubject.create(); |
| 963 |
PublishSubject<Integer> ps2 = PublishSubject.create(); |
| 964 |
|
| 965 |
TestObserver<Integer> to = Observable |
| 966 |
.concatEager(Observable.just(ps1, ps2)) |
| 967 |
.test(); |
| 968 |
|
| 969 |
assertTrue(ps1.hasObservers()); |
| 970 |
assertTrue(ps2.hasObservers()); |
| 971 |
|
| 972 |
to.dispose(); |
| 973 |
|
| 974 |
assertFalse(ps1.hasObservers()); |
| 975 |
assertFalse(ps2.hasObservers()); |
| 976 |
} |
| 977 |
|
| 978 |
@Test |
| 979 |
public void cancelNoInnerYet() { |
| 980 |
PublishSubject<Observable<Integer>> ps1 = PublishSubject.create(); |
| 981 |
|
| 982 |
TestObserver<Integer> to = Observable |
| 983 |
.concatEager(ps1) |
| 984 |
.test(); |
| 985 |
|
| 986 |
assertTrue(ps1.hasObservers()); |
| 987 |
|
| 988 |
to.dispose(); |
| 989 |
|
| 990 |
assertFalse(ps1.hasObservers()); |
| 991 |
} |
| 992 |
|
| 993 |
@Test |
| 994 |
public void undeliverableUponCancel() { |
| 995 |
TestHelper.checkUndeliverableUponCancel(new ObservableConverter<Integer, Observable<Integer>>() { |
| 996 |
@Override |
| 997 |
public Observable<Integer> apply(Observable<Integer> upstream) { |
| 998 |
return upstream.concatMapEager(new Function<Integer, Observable<Integer>>() { |
| 999 |
@Override |
| 1000 |
public Observable<Integer> apply(Integer v) throws Throwable { |
| 1001 |
return Observable.just(v).hide(); |
| 1002 |
} |
| 1003 |
}); |
| 1004 |
} |
| 1005 |
}); |
| 1006 |
} |
| 1007 |
|
| 1008 |
@Test |
| 1009 |
public void undeliverableUponCancelDelayError() { |
| 1010 |
TestHelper.checkUndeliverableUponCancel(new ObservableConverter<Integer, Observable<Integer>>() { |
| 1011 |
@Override |
| 1012 |
public Observable<Integer> apply(Observable<Integer> upstream) { |
| 1013 |
return upstream.concatMapEagerDelayError(new Function<Integer, Observable<Integer>>() { |
| 1014 |
@Override |
| 1015 |
public Observable<Integer> apply(Integer v) throws Throwable { |
| 1016 |
return Observable.just(v).hide(); |
| 1017 |
} |
| 1018 |
}, false); |
| 1019 |
} |
| 1020 |
}); |
| 1021 |
} |
| 1022 |
|
| 1023 |
@Test |
| 1024 |
public void undeliverableUponCancelDelayErrorTillEnd() { |
| 1025 |
TestHelper.checkUndeliverableUponCancel(new ObservableConverter<Integer, Observable<Integer>>() { |
| 1026 |
@Override |
| 1027 |
public Observable<Integer> apply(Observable<Integer> upstream) { |
| 1028 |
return upstream.concatMapEagerDelayError(new Function<Integer, Observable<Integer>>() { |
| 1029 |
@Override |
| 1030 |
public Observable<Integer> apply(Integer v) throws Throwable { |
| 1031 |
return Observable.just(v).hide(); |
| 1032 |
} |
| 1033 |
}, true); |
| 1034 |
} |
| 1035 |
}); |
| 1036 |
} |
| 1037 |
|
| 1038 |
@Test |
| 1039 |
public void iterableDelayError() { |
| 1040 |
Observable.concatEagerDelayError(Arrays.asList( |
| 1041 |
Observable.range(1, 2), |
| 1042 |
Observable.error(new TestException()), |
| 1043 |
Observable.range(3, 3) |
| 1044 |
)) |
| 1045 |
.test() |
| 1046 |
.assertFailure(TestException.class, 1, 2, 3, 4, 5); |
| 1047 |
} |
| 1048 |
|
| 1049 |
@Test |
| 1050 |
public void iterableDelayErrorMaxConcurrency() { |
| 1051 |
Observable.concatEagerDelayError(Arrays.asList( |
| 1052 |
Observable.range(1, 2), |
| 1053 |
Observable.error(new TestException()), |
| 1054 |
Observable.range(3, 3) |
| 1055 |
), 1, 1) |
| 1056 |
.test() |
| 1057 |
.assertFailure(TestException.class, 1, 2, 3, 4, 5); |
| 1058 |
} |
| 1059 |
|
| 1060 |
@Test |
| 1061 |
public void observerDelayError() { |
| 1062 |
Observable.concatEagerDelayError(Observable.fromArray( |
| 1063 |
Observable.range(1, 2), |
| 1064 |
Observable.error(new TestException()), |
| 1065 |
Observable.range(3, 3) |
| 1066 |
)) |
| 1067 |
.test() |
| 1068 |
.assertFailure(TestException.class, 1, 2, 3, 4, 5); |
| 1069 |
} |
| 1070 |
|
| 1071 |
@Test |
| 1072 |
public void observerDelayErrorMaxConcurrency() { |
| 1073 |
Observable.concatEagerDelayError(Observable.fromArray( |
| 1074 |
Observable.range(1, 2), |
| 1075 |
Observable.error(new TestException()), |
| 1076 |
Observable.range(3, 3) |
| 1077 |
), 1, 1) |
| 1078 |
.test() |
| 1079 |
.assertFailure(TestException.class, 1, 2, 3, 4, 5); |
| 1080 |
} |
| 1081 |
|
| 1082 |
@Test |
| 1083 |
public void innerFusionRejected() { |
| 1084 |
Observable.just(1) |
| 1085 |
.hide() |
| 1086 |
.concatMapEager(v -> TestHelper.rejectObservableFusion()) |
| 1087 |
.test() |
| 1088 |
.assertEmpty(); |
| 1089 |
} |
| 1090 |
} |