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