| 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.util.List; |
| 19 |
import java.util.concurrent.*; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.disposables.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.*; |
| 26 |
import io.reactivex.rxjava3.functions.Function; |
| 27 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 28 |
import io.reactivex.rxjava3.operators.QueueDisposable; |
| 29 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 30 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 31 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 32 |
import io.reactivex.rxjava3.testsupport.*; |
| 33 |
|
| 34 |
public class ObservableFlatMapCompletableTest extends RxJavaTest { |
| 35 |
|
| 36 |
@Test |
| 37 |
public void normalObservable() { |
| 38 |
Observable.range(1, 10) |
| 39 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 40 |
@Override |
| 41 |
public CompletableSource apply(Integer v) throws Exception { |
| 42 |
return Completable.complete(); |
| 43 |
} |
| 44 |
}).toObservable() |
| 45 |
.test() |
| 46 |
.assertResult(); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void mapperThrowsObservable() { |
| 51 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 52 |
|
| 53 |
TestObserver<Integer> to = ps |
| 54 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 55 |
@Override |
| 56 |
public CompletableSource apply(Integer v) throws Exception { |
| 57 |
throw new TestException(); |
| 58 |
} |
| 59 |
}).<Integer>toObservable() |
| 60 |
.test(); |
| 61 |
|
| 62 |
assertTrue(ps.hasObservers()); |
| 63 |
|
| 64 |
ps.onNext(1); |
| 65 |
|
| 66 |
to.assertFailure(TestException.class); |
| 67 |
|
| 68 |
assertFalse(ps.hasObservers()); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void mapperReturnsNullObservable() { |
| 73 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 74 |
|
| 75 |
TestObserver<Integer> to = ps |
| 76 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 77 |
@Override |
| 78 |
public CompletableSource apply(Integer v) throws Exception { |
| 79 |
return null; |
| 80 |
} |
| 81 |
}).<Integer>toObservable() |
| 82 |
.test(); |
| 83 |
|
| 84 |
assertTrue(ps.hasObservers()); |
| 85 |
|
| 86 |
ps.onNext(1); |
| 87 |
|
| 88 |
to.assertFailure(NullPointerException.class); |
| 89 |
|
| 90 |
assertFalse(ps.hasObservers()); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void normalDelayErrorObservable() { |
| 95 |
Observable.range(1, 10) |
| 96 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 97 |
@Override |
| 98 |
public CompletableSource apply(Integer v) throws Exception { |
| 99 |
return Completable.complete(); |
| 100 |
} |
| 101 |
}, true).toObservable() |
| 102 |
.test() |
| 103 |
.assertResult(); |
| 104 |
} |
| 105 |
|
| 106 |
@Test |
| 107 |
public void normalAsyncObservable() { |
| 108 |
Observable.range(1, 1000) |
| 109 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 110 |
@Override |
| 111 |
public CompletableSource apply(Integer v) throws Exception { |
| 112 |
return Observable.range(1, 100).subscribeOn(Schedulers.computation()).ignoreElements(); |
| 113 |
} |
| 114 |
}).toObservable() |
| 115 |
.test() |
| 116 |
.awaitDone(5, TimeUnit.SECONDS) |
| 117 |
.assertResult(); |
| 118 |
} |
| 119 |
|
| 120 |
@Test |
| 121 |
public void normalDelayErrorAllObservable() { |
| 122 |
TestObserverEx<Integer> to = Observable.range(1, 10).concatWith(Observable.<Integer>error(new TestException())) |
| 123 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 124 |
@Override |
| 125 |
public CompletableSource apply(Integer v) throws Exception { |
| 126 |
return Completable.error(new TestException()); |
| 127 |
} |
| 128 |
}, true).<Integer>toObservable() |
| 129 |
.to(TestHelper.<Integer>testConsumer()) |
| 130 |
.assertFailure(CompositeException.class); |
| 131 |
|
| 132 |
List<Throwable> errors = TestHelper.compositeList(to.errors().get(0)); |
| 133 |
|
| 134 |
for (int i = 0; i < 11; i++) { |
| 135 |
TestHelper.assertError(errors, i, TestException.class); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
@Test |
| 140 |
public void normalDelayInnerErrorAllObservable() { |
| 141 |
TestObserverEx<Integer> to = Observable.range(1, 10) |
| 142 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 143 |
@Override |
| 144 |
public CompletableSource apply(Integer v) throws Exception { |
| 145 |
return Completable.error(new TestException()); |
| 146 |
} |
| 147 |
}, true).<Integer>toObservable() |
| 148 |
.to(TestHelper.<Integer>testConsumer()) |
| 149 |
.assertFailure(CompositeException.class); |
| 150 |
|
| 151 |
List<Throwable> errors = TestHelper.compositeList(to.errors().get(0)); |
| 152 |
|
| 153 |
for (int i = 0; i < 10; i++) { |
| 154 |
TestHelper.assertError(errors, i, TestException.class); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
@Test |
| 159 |
public void normalNonDelayErrorOuterObservable() { |
| 160 |
Observable.range(1, 10).concatWith(Observable.<Integer>error(new TestException())) |
| 161 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 162 |
@Override |
| 163 |
public CompletableSource apply(Integer v) throws Exception { |
| 164 |
return Completable.complete(); |
| 165 |
} |
| 166 |
}, false).toObservable() |
| 167 |
.test() |
| 168 |
.assertFailure(TestException.class); |
| 169 |
} |
| 170 |
|
| 171 |
@Test |
| 172 |
public void fusedObservable() { |
| 173 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ANY); |
| 174 |
|
| 175 |
Observable.range(1, 10) |
| 176 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 177 |
@Override |
| 178 |
public CompletableSource apply(Integer v) throws Exception { |
| 179 |
return Completable.complete(); |
| 180 |
} |
| 181 |
}).<Integer>toObservable() |
| 182 |
.subscribe(to); |
| 183 |
|
| 184 |
to |
| 185 |
.assertFuseable() |
| 186 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 187 |
.assertResult(); |
| 188 |
} |
| 189 |
|
| 190 |
@Test |
| 191 |
public void disposedObservable() { |
| 192 |
TestHelper.checkDisposed(Observable.range(1, 10) |
| 193 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 194 |
@Override |
| 195 |
public CompletableSource apply(Integer v) throws Exception { |
| 196 |
return Completable.complete(); |
| 197 |
} |
| 198 |
}).toObservable()); |
| 199 |
} |
| 200 |
|
| 201 |
@Test |
| 202 |
public void normal() { |
| 203 |
Observable.range(1, 10) |
| 204 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 205 |
@Override |
| 206 |
public CompletableSource apply(Integer v) throws Exception { |
| 207 |
return Completable.complete(); |
| 208 |
} |
| 209 |
}) |
| 210 |
.test() |
| 211 |
.assertResult(); |
| 212 |
} |
| 213 |
|
| 214 |
@Test |
| 215 |
public void mapperThrows() { |
| 216 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 217 |
|
| 218 |
TestObserver<Void> to = ps |
| 219 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 220 |
@Override |
| 221 |
public CompletableSource apply(Integer v) throws Exception { |
| 222 |
throw new TestException(); |
| 223 |
} |
| 224 |
}) |
| 225 |
.test(); |
| 226 |
|
| 227 |
assertTrue(ps.hasObservers()); |
| 228 |
|
| 229 |
ps.onNext(1); |
| 230 |
|
| 231 |
to.assertFailure(TestException.class); |
| 232 |
|
| 233 |
assertFalse(ps.hasObservers()); |
| 234 |
} |
| 235 |
|
| 236 |
@Test |
| 237 |
public void mapperReturnsNull() { |
| 238 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 239 |
|
| 240 |
TestObserver<Void> to = ps |
| 241 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 242 |
@Override |
| 243 |
public CompletableSource apply(Integer v) throws Exception { |
| 244 |
return null; |
| 245 |
} |
| 246 |
}) |
| 247 |
.test(); |
| 248 |
|
| 249 |
assertTrue(ps.hasObservers()); |
| 250 |
|
| 251 |
ps.onNext(1); |
| 252 |
|
| 253 |
to.assertFailure(NullPointerException.class); |
| 254 |
|
| 255 |
assertFalse(ps.hasObservers()); |
| 256 |
} |
| 257 |
|
| 258 |
@Test |
| 259 |
public void normalDelayError() { |
| 260 |
Observable.range(1, 10) |
| 261 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 262 |
@Override |
| 263 |
public CompletableSource apply(Integer v) throws Exception { |
| 264 |
return Completable.complete(); |
| 265 |
} |
| 266 |
}, true) |
| 267 |
.test() |
| 268 |
.assertResult(); |
| 269 |
} |
| 270 |
|
| 271 |
@Test |
| 272 |
public void normalAsync() { |
| 273 |
Observable.range(1, 1000) |
| 274 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 275 |
@Override |
| 276 |
public CompletableSource apply(Integer v) throws Exception { |
| 277 |
return Observable.range(1, 100).subscribeOn(Schedulers.computation()).ignoreElements(); |
| 278 |
} |
| 279 |
}) |
| 280 |
.test() |
| 281 |
.awaitDone(5, TimeUnit.SECONDS) |
| 282 |
.assertResult(); |
| 283 |
} |
| 284 |
|
| 285 |
@Test |
| 286 |
public void normalDelayErrorAll() { |
| 287 |
TestObserverEx<Void> to = Observable.range(1, 10).concatWith(Observable.<Integer>error(new TestException())) |
| 288 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 289 |
@Override |
| 290 |
public CompletableSource apply(Integer v) throws Exception { |
| 291 |
return Completable.error(new TestException()); |
| 292 |
} |
| 293 |
}, true) |
| 294 |
.to(TestHelper.<Void>testConsumer()) |
| 295 |
.assertFailure(CompositeException.class); |
| 296 |
|
| 297 |
List<Throwable> errors = TestHelper.compositeList(to.errors().get(0)); |
| 298 |
|
| 299 |
for (int i = 0; i < 11; i++) { |
| 300 |
TestHelper.assertError(errors, i, TestException.class); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
@Test |
| 305 |
public void normalDelayInnerErrorAll() { |
| 306 |
TestObserverEx<Void> to = Observable.range(1, 10) |
| 307 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 308 |
@Override |
| 309 |
public CompletableSource apply(Integer v) throws Exception { |
| 310 |
return Completable.error(new TestException()); |
| 311 |
} |
| 312 |
}, true) |
| 313 |
.to(TestHelper.<Void>testConsumer()) |
| 314 |
.assertFailure(CompositeException.class); |
| 315 |
|
| 316 |
List<Throwable> errors = TestHelper.compositeList(to.errors().get(0)); |
| 317 |
|
| 318 |
for (int i = 0; i < 10; i++) { |
| 319 |
TestHelper.assertError(errors, i, TestException.class); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
@Test |
| 324 |
public void normalNonDelayErrorOuter() { |
| 325 |
Observable.range(1, 10).concatWith(Observable.<Integer>error(new TestException())) |
| 326 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 327 |
@Override |
| 328 |
public CompletableSource apply(Integer v) throws Exception { |
| 329 |
return Completable.complete(); |
| 330 |
} |
| 331 |
}, false) |
| 332 |
.test() |
| 333 |
.assertFailure(TestException.class); |
| 334 |
} |
| 335 |
|
| 336 |
@Test |
| 337 |
public void fused() { |
| 338 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ANY); |
| 339 |
|
| 340 |
Observable.range(1, 10) |
| 341 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 342 |
@Override |
| 343 |
public CompletableSource apply(Integer v) throws Exception { |
| 344 |
return Completable.complete(); |
| 345 |
} |
| 346 |
}) |
| 347 |
.<Integer>toObservable() |
| 348 |
.subscribe(to); |
| 349 |
|
| 350 |
to |
| 351 |
.assertFuseable() |
| 352 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 353 |
.assertResult(); |
| 354 |
} |
| 355 |
|
| 356 |
@Test |
| 357 |
public void disposed() { |
| 358 |
TestHelper.checkDisposed(Observable.range(1, 10) |
| 359 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 360 |
@Override |
| 361 |
public CompletableSource apply(Integer v) throws Exception { |
| 362 |
return Completable.complete(); |
| 363 |
} |
| 364 |
})); |
| 365 |
} |
| 366 |
|
| 367 |
@Test |
| 368 |
public void innerObserver() { |
| 369 |
Observable.range(1, 3) |
| 370 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 371 |
@Override |
| 372 |
public CompletableSource apply(Integer v) throws Exception { |
| 373 |
return new Completable() { |
| 374 |
@Override |
| 375 |
protected void subscribeActual(CompletableObserver observer) { |
| 376 |
observer.onSubscribe(Disposable.empty()); |
| 377 |
|
| 378 |
assertFalse(((Disposable)observer).isDisposed()); |
| 379 |
|
| 380 |
((Disposable)observer).dispose(); |
| 381 |
|
| 382 |
assertTrue(((Disposable)observer).isDisposed()); |
| 383 |
} |
| 384 |
}; |
| 385 |
} |
| 386 |
}) |
| 387 |
.test(); |
| 388 |
} |
| 389 |
|
| 390 |
@Test |
| 391 |
public void badSource() { |
| 392 |
TestHelper.checkBadSourceObservable(new Function<Observable<Integer>, Object>() { |
| 393 |
@Override |
| 394 |
public Object apply(Observable<Integer> o) throws Exception { |
| 395 |
return o.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 396 |
@Override |
| 397 |
public CompletableSource apply(Integer v) throws Exception { |
| 398 |
return Completable.complete(); |
| 399 |
} |
| 400 |
}); |
| 401 |
} |
| 402 |
}, false, 1, null); |
| 403 |
} |
| 404 |
|
| 405 |
@Test |
| 406 |
public void fusedInternalsObservable() { |
| 407 |
Observable.range(1, 10) |
| 408 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 409 |
@Override |
| 410 |
public CompletableSource apply(Integer v) throws Exception { |
| 411 |
return Completable.complete(); |
| 412 |
} |
| 413 |
}) |
| 414 |
.toObservable() |
| 415 |
.subscribe(new Observer<Object>() { |
| 416 |
@Override |
| 417 |
public void onSubscribe(Disposable d) { |
| 418 |
QueueDisposable<?> qd = (QueueDisposable<?>)d; |
| 419 |
try { |
| 420 |
assertNull(qd.poll()); |
| 421 |
} catch (Throwable ex) { |
| 422 |
throw new RuntimeException(ex); |
| 423 |
} |
| 424 |
assertTrue(qd.isEmpty()); |
| 425 |
qd.clear(); |
| 426 |
} |
| 427 |
|
| 428 |
@Override |
| 429 |
public void onNext(Object t) { |
| 430 |
} |
| 431 |
|
| 432 |
@Override |
| 433 |
public void onError(Throwable t) { |
| 434 |
} |
| 435 |
|
| 436 |
@Override |
| 437 |
public void onComplete() { |
| 438 |
} |
| 439 |
}); |
| 440 |
} |
| 441 |
|
| 442 |
@Test |
| 443 |
public void innerObserverObservable() { |
| 444 |
Observable.range(1, 3) |
| 445 |
.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 446 |
@Override |
| 447 |
public CompletableSource apply(Integer v) throws Exception { |
| 448 |
return new Completable() { |
| 449 |
@Override |
| 450 |
protected void subscribeActual(CompletableObserver observer) { |
| 451 |
observer.onSubscribe(Disposable.empty()); |
| 452 |
|
| 453 |
assertFalse(((Disposable)observer).isDisposed()); |
| 454 |
|
| 455 |
((Disposable)observer).dispose(); |
| 456 |
|
| 457 |
assertTrue(((Disposable)observer).isDisposed()); |
| 458 |
} |
| 459 |
}; |
| 460 |
} |
| 461 |
}) |
| 462 |
.toObservable() |
| 463 |
.test(); |
| 464 |
} |
| 465 |
|
| 466 |
@Test |
| 467 |
public void badSourceObservable() { |
| 468 |
TestHelper.checkBadSourceObservable(new Function<Observable<Integer>, Object>() { |
| 469 |
@Override |
| 470 |
public Object apply(Observable<Integer> o) throws Exception { |
| 471 |
return o.flatMapCompletable(new Function<Integer, CompletableSource>() { |
| 472 |
@Override |
| 473 |
public CompletableSource apply(Integer v) throws Exception { |
| 474 |
return Completable.complete(); |
| 475 |
} |
| 476 |
}).toObservable(); |
| 477 |
} |
| 478 |
}, false, 1, null); |
| 479 |
} |
| 480 |
|
| 481 |
@Test |
| 482 |
public void undeliverableUponCancel() { |
| 483 |
TestHelper.checkUndeliverableUponCancel(new ObservableConverter<Integer, Completable>() { |
| 484 |
@Override |
| 485 |
public Completable apply(Observable<Integer> upstream) { |
| 486 |
return upstream.flatMapCompletable(new Function<Integer, Completable>() { |
| 487 |
@Override |
| 488 |
public Completable apply(Integer v) throws Throwable { |
| 489 |
return Completable.complete().hide(); |
| 490 |
} |
| 491 |
}); |
| 492 |
} |
| 493 |
}); |
| 494 |
} |
| 495 |
|
| 496 |
@Test |
| 497 |
public void undeliverableUponCancelDelayError() { |
| 498 |
TestHelper.checkUndeliverableUponCancel(new ObservableConverter<Integer, Completable>() { |
| 499 |
@Override |
| 500 |
public Completable apply(Observable<Integer> upstream) { |
| 501 |
return upstream.flatMapCompletable(new Function<Integer, Completable>() { |
| 502 |
@Override |
| 503 |
public Completable apply(Integer v) throws Throwable { |
| 504 |
return Completable.complete().hide(); |
| 505 |
} |
| 506 |
}, true); |
| 507 |
} |
| 508 |
}); |
| 509 |
} |
| 510 |
|
| 511 |
@Test |
| 512 |
public void doubleOnSubscribe() { |
| 513 |
TestHelper.checkDoubleOnSubscribeObservable(o -> o.flatMapCompletable(v -> Completable.never()).toObservable()); |
| 514 |
} |
| 515 |
|
| 516 |
@Test |
| 517 |
public void doubleOnSubscribeCompletable() { |
| 518 |
TestHelper.checkDoubleOnSubscribeObservableToCompletable(o -> o.flatMapCompletable(v -> Completable.never())); |
| 519 |
} |
| 520 |
|
| 521 |
@Test |
| 522 |
public void cancelWhileMapping() throws Throwable { |
| 523 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 524 |
PublishSubject<Integer> ps1 = PublishSubject.create(); |
| 525 |
|
| 526 |
TestObserver<Object> to = new TestObserver<>(); |
| 527 |
CountDownLatch cdl = new CountDownLatch(1); |
| 528 |
|
| 529 |
ps1.flatMapCompletable(v -> { |
| 530 |
TestHelper.raceOther(() -> { |
| 531 |
to.dispose(); |
| 532 |
}, cdl); |
| 533 |
return Completable.complete(); |
| 534 |
}) |
| 535 |
.toObservable() |
| 536 |
.subscribe(to); |
| 537 |
|
| 538 |
ps1.onNext(1); |
| 539 |
|
| 540 |
cdl.await(); |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
@Test |
| 545 |
public void cancelWhileMappingCompletable() throws Throwable { |
| 546 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 547 |
PublishSubject<Integer> ps1 = PublishSubject.create(); |
| 548 |
|
| 549 |
TestObserver<Void> to = new TestObserver<>(); |
| 550 |
CountDownLatch cdl = new CountDownLatch(1); |
| 551 |
|
| 552 |
ps1.flatMapCompletable(v -> { |
| 553 |
TestHelper.raceOther(() -> { |
| 554 |
to.dispose(); |
| 555 |
}, cdl); |
| 556 |
return Completable.complete(); |
| 557 |
}) |
| 558 |
.subscribe(to); |
| 559 |
|
| 560 |
ps1.onNext(1); |
| 561 |
|
| 562 |
cdl.await(); |
| 563 |
} |
| 564 |
} |
| 565 |
} |