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