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