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