| 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.mixed; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.List; |
| 19 |
import java.util.concurrent.atomic.AtomicInteger; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.*; |
| 25 |
import io.reactivex.rxjava3.functions.*; |
| 26 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 27 |
import io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler; |
| 28 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 29 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 30 |
import io.reactivex.rxjava3.subjects.*; |
| 31 |
import io.reactivex.rxjava3.testsupport.*; |
| 32 |
|
| 33 |
public class ObservableConcatMapCompletableTest extends RxJavaTest { |
| 34 |
|
| 35 |
@Test |
| 36 |
public void simple() { |
| 37 |
Observable.range(1, 5) |
| 38 |
.concatMapCompletable(Functions.justFunction(Completable.complete())) |
| 39 |
.test() |
| 40 |
.assertResult(); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void simple2() { |
| 45 |
final AtomicInteger counter = new AtomicInteger(); |
| 46 |
Observable.range(1, 5) |
| 47 |
.concatMapCompletable(Functions.justFunction(Completable.fromAction(new Action() { |
| 48 |
@Override |
| 49 |
public void run() throws Exception { |
| 50 |
counter.incrementAndGet(); |
| 51 |
} |
| 52 |
}))) |
| 53 |
.test() |
| 54 |
.assertResult(); |
| 55 |
|
| 56 |
assertEquals(5, counter.get()); |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void simpleLongPrefetch() { |
| 61 |
Observable.range(1, 1024) |
| 62 |
.concatMapCompletable(Functions.justFunction(Completable.complete()), 32) |
| 63 |
.test() |
| 64 |
.assertResult(); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void mainError() { |
| 69 |
Observable.<Integer>error(new TestException()) |
| 70 |
.concatMapCompletable(Functions.justFunction(Completable.complete())) |
| 71 |
.test() |
| 72 |
.assertFailure(TestException.class); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void innerError() { |
| 77 |
Observable.just(1) |
| 78 |
.concatMapCompletable(Functions.justFunction(Completable.error(new TestException()))) |
| 79 |
.test() |
| 80 |
.assertFailure(TestException.class); |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void innerErrorDelayed() { |
| 85 |
TestObserverEx<Void> to = Observable.range(1, 5) |
| 86 |
.concatMapCompletableDelayError( |
| 87 |
new Function<Integer, CompletableSource>() { |
| 88 |
@Override |
| 89 |
public CompletableSource apply(Integer v) throws Exception { |
| 90 |
return Completable.error(new TestException()); |
| 91 |
} |
| 92 |
} |
| 93 |
) |
| 94 |
.to(TestHelper.<Void>testConsumer()) |
| 95 |
.assertFailure(CompositeException.class) |
| 96 |
; |
| 97 |
|
| 98 |
assertEquals(5, ((CompositeException)to.errors().get(0)).getExceptions().size()); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void mapperCrash() { |
| 103 |
Observable.just(1) |
| 104 |
.concatMapCompletable(new Function<Integer, CompletableSource>() { |
| 105 |
@Override |
| 106 |
public CompletableSource apply(Integer v) throws Exception { |
| 107 |
throw new TestException(); |
| 108 |
} |
| 109 |
}) |
| 110 |
.test() |
| 111 |
.assertFailure(TestException.class); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void mapperCrashHidden() { |
| 116 |
Observable.just(1).hide() |
| 117 |
.concatMapCompletable(new Function<Integer, CompletableSource>() { |
| 118 |
@Override |
| 119 |
public CompletableSource apply(Integer v) throws Exception { |
| 120 |
throw new TestException(); |
| 121 |
} |
| 122 |
}) |
| 123 |
.test() |
| 124 |
.assertFailure(TestException.class); |
| 125 |
} |
| 126 |
|
| 127 |
@Test |
| 128 |
public void immediateError() { |
| 129 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 130 |
CompletableSubject cs = CompletableSubject.create(); |
| 131 |
|
| 132 |
TestObserver<Void> to = ps.concatMapCompletable( |
| 133 |
Functions.justFunction(cs)).test(); |
| 134 |
|
| 135 |
to.assertEmpty(); |
| 136 |
|
| 137 |
assertTrue(ps.hasObservers()); |
| 138 |
assertFalse(cs.hasObservers()); |
| 139 |
|
| 140 |
ps.onNext(1); |
| 141 |
|
| 142 |
assertTrue(cs.hasObservers()); |
| 143 |
|
| 144 |
ps.onError(new TestException()); |
| 145 |
|
| 146 |
assertFalse(cs.hasObservers()); |
| 147 |
|
| 148 |
to.assertFailure(TestException.class); |
| 149 |
} |
| 150 |
|
| 151 |
@Test |
| 152 |
public void immediateError2() { |
| 153 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 154 |
CompletableSubject cs = CompletableSubject.create(); |
| 155 |
|
| 156 |
TestObserver<Void> to = ps.concatMapCompletable( |
| 157 |
Functions.justFunction(cs)).test(); |
| 158 |
|
| 159 |
to.assertEmpty(); |
| 160 |
|
| 161 |
assertTrue(ps.hasObservers()); |
| 162 |
assertFalse(cs.hasObservers()); |
| 163 |
|
| 164 |
ps.onNext(1); |
| 165 |
|
| 166 |
assertTrue(cs.hasObservers()); |
| 167 |
|
| 168 |
cs.onError(new TestException()); |
| 169 |
|
| 170 |
assertFalse(ps.hasObservers()); |
| 171 |
|
| 172 |
to.assertFailure(TestException.class); |
| 173 |
} |
| 174 |
|
| 175 |
@Test |
| 176 |
public void boundaryError() { |
| 177 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 178 |
CompletableSubject cs = CompletableSubject.create(); |
| 179 |
|
| 180 |
TestObserver<Void> to = ps.concatMapCompletableDelayError( |
| 181 |
Functions.justFunction(cs), false).test(); |
| 182 |
|
| 183 |
to.assertEmpty(); |
| 184 |
|
| 185 |
assertTrue(ps.hasObservers()); |
| 186 |
assertFalse(cs.hasObservers()); |
| 187 |
|
| 188 |
ps.onNext(1); |
| 189 |
|
| 190 |
assertTrue(cs.hasObservers()); |
| 191 |
|
| 192 |
ps.onError(new TestException()); |
| 193 |
|
| 194 |
assertTrue(cs.hasObservers()); |
| 195 |
|
| 196 |
to.assertEmpty(); |
| 197 |
|
| 198 |
cs.onComplete(); |
| 199 |
|
| 200 |
to.assertFailure(TestException.class); |
| 201 |
} |
| 202 |
|
| 203 |
@Test |
| 204 |
public void endError() { |
| 205 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 206 |
final CompletableSubject cs = CompletableSubject.create(); |
| 207 |
final CompletableSubject cs2 = CompletableSubject.create(); |
| 208 |
|
| 209 |
TestObserver<Void> to = ps.concatMapCompletableDelayError( |
| 210 |
new Function<Integer, CompletableSource>() { |
| 211 |
@Override |
| 212 |
public CompletableSource apply(Integer v) throws Exception { |
| 213 |
if (v == 1) { |
| 214 |
return cs; |
| 215 |
} |
| 216 |
return cs2; |
| 217 |
} |
| 218 |
}, true, 32 |
| 219 |
) |
| 220 |
.test(); |
| 221 |
|
| 222 |
to.assertEmpty(); |
| 223 |
|
| 224 |
assertTrue(ps.hasObservers()); |
| 225 |
assertFalse(cs.hasObservers()); |
| 226 |
|
| 227 |
ps.onNext(1); |
| 228 |
|
| 229 |
assertTrue(cs.hasObservers()); |
| 230 |
|
| 231 |
cs.onError(new TestException()); |
| 232 |
|
| 233 |
assertTrue(ps.hasObservers()); |
| 234 |
|
| 235 |
ps.onNext(2); |
| 236 |
|
| 237 |
to.assertEmpty(); |
| 238 |
|
| 239 |
cs2.onComplete(); |
| 240 |
|
| 241 |
assertTrue(ps.hasObservers()); |
| 242 |
|
| 243 |
to.assertEmpty(); |
| 244 |
|
| 245 |
ps.onComplete(); |
| 246 |
|
| 247 |
to.assertFailure(TestException.class); |
| 248 |
} |
| 249 |
|
| 250 |
@Test |
| 251 |
public void doubleOnSubscribe() { |
| 252 |
TestHelper.checkDoubleOnSubscribeObservableToCompletable( |
| 253 |
new Function<Observable<Object>, Completable>() { |
| 254 |
@Override |
| 255 |
public Completable apply(Observable<Object> f) |
| 256 |
throws Exception { |
| 257 |
return f.concatMapCompletable( |
| 258 |
Functions.justFunction(Completable.complete())); |
| 259 |
} |
| 260 |
} |
| 261 |
); |
| 262 |
} |
| 263 |
|
| 264 |
@Test |
| 265 |
public void disposed() { |
| 266 |
TestHelper.checkDisposed( |
| 267 |
Observable.never() |
| 268 |
.concatMapCompletable( |
| 269 |
Functions.justFunction(Completable.complete())) |
| 270 |
); |
| 271 |
} |
| 272 |
|
| 273 |
@Test |
| 274 |
public void immediateOuterInnerErrorRace() { |
| 275 |
final TestException ex = new TestException(); |
| 276 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 277 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 278 |
try { |
| 279 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 280 |
final CompletableSubject cs = CompletableSubject.create(); |
| 281 |
|
| 282 |
TestObserver<Void> to = ps.concatMapCompletable( |
| 283 |
Functions.justFunction(cs) |
| 284 |
) |
| 285 |
.test(); |
| 286 |
|
| 287 |
ps.onNext(1); |
| 288 |
|
| 289 |
Runnable r1 = new Runnable() { |
| 290 |
@Override |
| 291 |
public void run() { |
| 292 |
ps.onError(ex); |
| 293 |
} |
| 294 |
}; |
| 295 |
|
| 296 |
Runnable r2 = new Runnable() { |
| 297 |
@Override |
| 298 |
public void run() { |
| 299 |
cs.onError(ex); |
| 300 |
} |
| 301 |
}; |
| 302 |
|
| 303 |
TestHelper.race(r1, r2); |
| 304 |
|
| 305 |
to.assertError(new Predicate<Throwable>() { |
| 306 |
@Override |
| 307 |
public boolean test(Throwable e) throws Exception { |
| 308 |
return e instanceof TestException || e instanceof CompositeException; |
| 309 |
} |
| 310 |
}) |
| 311 |
.assertNotComplete(); |
| 312 |
|
| 313 |
if (!errors.isEmpty()) { |
| 314 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 315 |
} |
| 316 |
} finally { |
| 317 |
RxJavaPlugins.reset(); |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
@Test |
| 323 |
public void disposeInDrainLoop() { |
| 324 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 325 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 326 |
final CompletableSubject cs = CompletableSubject.create(); |
| 327 |
|
| 328 |
final TestObserver<Void> to = ps.concatMapCompletable( |
| 329 |
Functions.justFunction(cs) |
| 330 |
) |
| 331 |
.test(); |
| 332 |
|
| 333 |
ps.onNext(1); |
| 334 |
|
| 335 |
Runnable r1 = new Runnable() { |
| 336 |
@Override |
| 337 |
public void run() { |
| 338 |
ps.onNext(2); |
| 339 |
} |
| 340 |
}; |
| 341 |
|
| 342 |
Runnable r2 = new Runnable() { |
| 343 |
@Override |
| 344 |
public void run() { |
| 345 |
cs.onComplete(); |
| 346 |
to.dispose(); |
| 347 |
} |
| 348 |
}; |
| 349 |
|
| 350 |
TestHelper.race(r1, r2); |
| 351 |
|
| 352 |
to.assertEmpty(); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
@Test |
| 357 |
public void doneButNotEmpty() { |
| 358 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 359 |
final CompletableSubject cs = CompletableSubject.create(); |
| 360 |
|
| 361 |
final TestObserver<Void> to = ps.concatMapCompletable( |
| 362 |
Functions.justFunction(cs) |
| 363 |
) |
| 364 |
.test(); |
| 365 |
|
| 366 |
ps.onNext(1); |
| 367 |
ps.onNext(2); |
| 368 |
ps.onComplete(); |
| 369 |
|
| 370 |
cs.onComplete(); |
| 371 |
|
| 372 |
to.assertResult(); |
| 373 |
} |
| 374 |
|
| 375 |
@Test |
| 376 |
public void asyncFused() { |
| 377 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 378 |
final CompletableSubject cs = CompletableSubject.create(); |
| 379 |
|
| 380 |
final TestObserver<Void> to = ps.observeOn(ImmediateThinScheduler.INSTANCE) |
| 381 |
.concatMapCompletable( |
| 382 |
Functions.justFunction(cs) |
| 383 |
) |
| 384 |
.test(); |
| 385 |
|
| 386 |
ps.onNext(1); |
| 387 |
ps.onComplete(); |
| 388 |
|
| 389 |
cs.onComplete(); |
| 390 |
|
| 391 |
to.assertResult(); |
| 392 |
} |
| 393 |
|
| 394 |
@Test |
| 395 |
public void fusionRejected() { |
| 396 |
final CompletableSubject cs = CompletableSubject.create(); |
| 397 |
|
| 398 |
TestHelper.rejectObservableFusion() |
| 399 |
.concatMapCompletable( |
| 400 |
Functions.justFunction(cs) |
| 401 |
) |
| 402 |
.test() |
| 403 |
.assertEmpty(); |
| 404 |
} |
| 405 |
|
| 406 |
@Test |
| 407 |
public void emptyScalarSource() { |
| 408 |
final CompletableSubject cs = CompletableSubject.create(); |
| 409 |
|
| 410 |
Observable.empty() |
| 411 |
.concatMapCompletable(Functions.justFunction(cs)) |
| 412 |
.test() |
| 413 |
.assertResult(); |
| 414 |
} |
| 415 |
|
| 416 |
@Test |
| 417 |
public void justScalarSource() { |
| 418 |
final CompletableSubject cs = CompletableSubject.create(); |
| 419 |
|
| 420 |
TestObserver<Void> to = Observable.just(1) |
| 421 |
.concatMapCompletable(Functions.justFunction(cs)) |
| 422 |
.test(); |
| 423 |
|
| 424 |
to.assertEmpty(); |
| 425 |
|
| 426 |
assertTrue(cs.hasObservers()); |
| 427 |
|
| 428 |
cs.onComplete(); |
| 429 |
|
| 430 |
to.assertResult(); |
| 431 |
} |
| 432 |
|
| 433 |
@Test |
| 434 |
public void undeliverableUponCancel() { |
| 435 |
TestHelper.checkUndeliverableUponCancel(new ObservableConverter<Integer, Completable>() { |
| 436 |
@Override |
| 437 |
public Completable apply(Observable<Integer> upstream) { |
| 438 |
return upstream.concatMapCompletable(new Function<Integer, Completable>() { |
| 439 |
@Override |
| 440 |
public Completable apply(Integer v) throws Throwable { |
| 441 |
return Completable.complete().hide(); |
| 442 |
} |
| 443 |
}); |
| 444 |
} |
| 445 |
}); |
| 446 |
} |
| 447 |
|
| 448 |
@Test |
| 449 |
public void undeliverableUponCancelDelayError() { |
| 450 |
TestHelper.checkUndeliverableUponCancel(new ObservableConverter<Integer, Completable>() { |
| 451 |
@Override |
| 452 |
public Completable apply(Observable<Integer> upstream) { |
| 453 |
return upstream.concatMapCompletableDelayError(new Function<Integer, Completable>() { |
| 454 |
@Override |
| 455 |
public Completable apply(Integer v) throws Throwable { |
| 456 |
return Completable.complete().hide(); |
| 457 |
} |
| 458 |
}, false, 2); |
| 459 |
} |
| 460 |
}); |
| 461 |
} |
| 462 |
|
| 463 |
@Test |
| 464 |
public void undeliverableUponCancelDelayErrorTillEnd() { |
| 465 |
TestHelper.checkUndeliverableUponCancel(new ObservableConverter<Integer, Completable>() { |
| 466 |
@Override |
| 467 |
public Completable apply(Observable<Integer> upstream) { |
| 468 |
return upstream.concatMapCompletableDelayError(new Function<Integer, Completable>() { |
| 469 |
@Override |
| 470 |
public Completable apply(Integer v) throws Throwable { |
| 471 |
return Completable.complete().hide(); |
| 472 |
} |
| 473 |
}, true, 2); |
| 474 |
} |
| 475 |
}); |
| 476 |
} |
| 477 |
|
| 478 |
@Test |
| 479 |
public void basicNonFused() { |
| 480 |
Observable.range(1, 5).hide() |
| 481 |
.concatMapCompletable(v -> Completable.complete().hide()) |
| 482 |
.test() |
| 483 |
.assertResult(); |
| 484 |
} |
| 485 |
|
| 486 |
@Test |
| 487 |
public void basicSyncFused() { |
| 488 |
Observable.range(1, 5) |
| 489 |
.concatMapCompletable(v -> Completable.complete().hide()) |
| 490 |
.test() |
| 491 |
.assertResult(); |
| 492 |
} |
| 493 |
|
| 494 |
@Test |
| 495 |
public void basicAsyncFused() { |
| 496 |
UnicastSubject<Integer> us = UnicastSubject.create(); |
| 497 |
TestHelper.emit(us, 1, 2, 3, 4, 5); |
| 498 |
|
| 499 |
us |
| 500 |
.concatMapCompletable(v -> Completable.complete().hide()) |
| 501 |
.test() |
| 502 |
.assertResult(); |
| 503 |
} |
| 504 |
|
| 505 |
@Test |
| 506 |
public void basicFusionRejected() { |
| 507 |
TestHelper.<Integer>rejectObservableFusion() |
| 508 |
.concatMapCompletable(v -> Completable.complete().hide()) |
| 509 |
.test() |
| 510 |
.assertEmpty(); |
| 511 |
} |
| 512 |
|
| 513 |
@Test |
| 514 |
public void fusedPollCrash() { |
| 515 |
Observable.range(1, 5) |
| 516 |
.map(v -> { |
| 517 |
if (v == 3) { |
| 518 |
throw new TestException(); |
| 519 |
} |
| 520 |
return v; |
| 521 |
}) |
| 522 |
.compose(TestHelper.observableStripBoundary()) |
| 523 |
.concatMapCompletable(v -> Completable.complete().hide()) |
| 524 |
.test() |
| 525 |
.assertFailure(TestException.class); |
| 526 |
} |
| 527 |
} |