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