| 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.maybe; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.lang.management.*; |
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.*; |
| 22 |
import java.util.concurrent.atomic.AtomicInteger; |
| 23 |
|
| 24 |
import org.junit.Test; |
| 25 |
import org.reactivestreams.Publisher; |
| 26 |
|
| 27 |
import io.reactivex.rxjava3.core.*; |
| 28 |
import io.reactivex.rxjava3.core.Observable; |
| 29 |
import io.reactivex.rxjava3.disposables.*; |
| 30 |
import io.reactivex.rxjava3.exceptions.*; |
| 31 |
import io.reactivex.rxjava3.functions.*; |
| 32 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 33 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableZipTest.ArgsToString; |
| 34 |
import io.reactivex.rxjava3.internal.operators.maybe.*; |
| 35 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 36 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 37 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 38 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 39 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 40 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 41 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 42 |
import io.reactivex.rxjava3.testsupport.*; |
| 43 |
|
| 44 |
public class MaybeTest extends RxJavaTest { |
| 45 |
@Test |
| 46 |
public void fromFlowableEmpty() { |
| 47 |
|
| 48 |
Flowable.empty() |
| 49 |
.singleElement() |
| 50 |
.test() |
| 51 |
.assertResult(); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void fromFlowableJust() { |
| 56 |
|
| 57 |
Flowable.just(1) |
| 58 |
.singleElement() |
| 59 |
.test() |
| 60 |
.assertResult(1); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void fromFlowableError() { |
| 65 |
|
| 66 |
Flowable.error(new TestException()) |
| 67 |
.singleElement() |
| 68 |
.test() |
| 69 |
.assertFailure(TestException.class); |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
public void fromFlowableValueAndError() { |
| 74 |
Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())) |
| 75 |
.singleElement() |
| 76 |
.test() |
| 77 |
.assertFailure(TestException.class); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void fromFlowableMany() { |
| 82 |
|
| 83 |
Flowable.range(1, 2) |
| 84 |
.singleElement() |
| 85 |
.test() |
| 86 |
.assertFailure(IllegalArgumentException.class); |
| 87 |
} |
| 88 |
|
| 89 |
@Test |
| 90 |
public void fromFlowableDisposeComposesThrough() { |
| 91 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 92 |
|
| 93 |
TestObserver<Integer> to = pp.singleElement().test(); |
| 94 |
|
| 95 |
assertTrue(pp.hasSubscribers()); |
| 96 |
|
| 97 |
to.dispose(); |
| 98 |
|
| 99 |
assertFalse(pp.hasSubscribers()); |
| 100 |
} |
| 101 |
|
| 102 |
@Test |
| 103 |
public void fromObservableEmpty() { |
| 104 |
|
| 105 |
Observable.empty() |
| 106 |
.singleElement() |
| 107 |
.test() |
| 108 |
.assertResult(); |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void fromObservableJust() { |
| 113 |
|
| 114 |
Observable.just(1) |
| 115 |
.singleElement() |
| 116 |
.test() |
| 117 |
.assertResult(1); |
| 118 |
} |
| 119 |
|
| 120 |
@Test |
| 121 |
public void fromObservableError() { |
| 122 |
|
| 123 |
Observable.error(new TestException()) |
| 124 |
.singleElement() |
| 125 |
.test() |
| 126 |
.assertFailure(TestException.class); |
| 127 |
} |
| 128 |
|
| 129 |
@Test |
| 130 |
public void fromObservableValueAndError() { |
| 131 |
Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())) |
| 132 |
.singleElement() |
| 133 |
.test() |
| 134 |
.assertFailure(TestException.class); |
| 135 |
} |
| 136 |
|
| 137 |
@Test |
| 138 |
public void fromObservableMany() { |
| 139 |
|
| 140 |
Observable.range(1, 2) |
| 141 |
.singleElement() |
| 142 |
.test() |
| 143 |
.assertFailure(IllegalArgumentException.class); |
| 144 |
} |
| 145 |
|
| 146 |
@Test |
| 147 |
public void fromObservableDisposeComposesThrough() { |
| 148 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 149 |
|
| 150 |
TestObserver<Integer> to = ps.singleElement().test(false); |
| 151 |
|
| 152 |
assertTrue(ps.hasObservers()); |
| 153 |
|
| 154 |
to.dispose(); |
| 155 |
|
| 156 |
assertFalse(ps.hasObservers()); |
| 157 |
} |
| 158 |
|
| 159 |
@Test |
| 160 |
public void fromObservableDisposeComposesThroughImmediatelyCancelled() { |
| 161 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 162 |
|
| 163 |
ps.singleElement().test(true); |
| 164 |
|
| 165 |
assertFalse(ps.hasObservers()); |
| 166 |
} |
| 167 |
|
| 168 |
@Test |
| 169 |
public void just() { |
| 170 |
Maybe.just(1) |
| 171 |
.test() |
| 172 |
.assertResult(1); |
| 173 |
} |
| 174 |
|
| 175 |
@Test |
| 176 |
public void empty() { |
| 177 |
Maybe.empty() |
| 178 |
.test() |
| 179 |
.assertResult(); |
| 180 |
} |
| 181 |
|
| 182 |
@Test |
| 183 |
public void never() { |
| 184 |
Maybe.never() |
| 185 |
.to(TestHelper.testConsumer()) |
| 186 |
.assertSubscribed() |
| 187 |
.assertNoValues() |
| 188 |
.assertNoErrors() |
| 189 |
.assertNotComplete(); |
| 190 |
} |
| 191 |
|
| 192 |
@Test |
| 193 |
public void error() { |
| 194 |
Maybe.error(new TestException()) |
| 195 |
.test() |
| 196 |
.assertFailure(TestException.class); |
| 197 |
} |
| 198 |
|
| 199 |
@Test |
| 200 |
public void errorCallable() { |
| 201 |
Maybe.error(Functions.justSupplier(new TestException())) |
| 202 |
.test() |
| 203 |
.assertFailure(TestException.class); |
| 204 |
} |
| 205 |
|
| 206 |
@Test |
| 207 |
public void errorCallableReturnsNull() { |
| 208 |
Maybe.error(Functions.justSupplier((Throwable)null)) |
| 209 |
.test() |
| 210 |
.assertFailure(NullPointerException.class); |
| 211 |
} |
| 212 |
|
| 213 |
@Test |
| 214 |
public void wrapCustom() { |
| 215 |
Maybe.wrap(new MaybeSource<Integer>() { |
| 216 |
@Override |
| 217 |
public void subscribe(MaybeObserver<? super Integer> observer) { |
| 218 |
observer.onSubscribe(Disposable.empty()); |
| 219 |
observer.onSuccess(1); |
| 220 |
} |
| 221 |
}) |
| 222 |
.test() |
| 223 |
.assertResult(1); |
| 224 |
} |
| 225 |
|
| 226 |
@Test |
| 227 |
public void wrapMaybe() { |
| 228 |
assertSame(Maybe.empty(), Maybe.wrap(Maybe.empty())); |
| 229 |
} |
| 230 |
|
| 231 |
@Test |
| 232 |
public void emptySingleton() { |
| 233 |
assertSame(Maybe.empty(), Maybe.empty()); |
| 234 |
} |
| 235 |
|
| 236 |
@Test |
| 237 |
public void neverSingleton() { |
| 238 |
assertSame(Maybe.never(), Maybe.never()); |
| 239 |
} |
| 240 |
|
| 241 |
@Test |
| 242 |
public void liftJust() { |
| 243 |
Maybe.just(1).lift(new MaybeOperator<Integer, Integer>() { |
| 244 |
@Override |
| 245 |
public MaybeObserver<? super Integer> apply(MaybeObserver<? super Integer> t) throws Exception { |
| 246 |
return t; |
| 247 |
} |
| 248 |
}) |
| 249 |
.test() |
| 250 |
.assertResult(1); |
| 251 |
} |
| 252 |
|
| 253 |
@Test |
| 254 |
public void liftThrows() { |
| 255 |
Maybe.just(1).lift(new MaybeOperator<Integer, Integer>() { |
| 256 |
@Override |
| 257 |
public MaybeObserver<? super Integer> apply(MaybeObserver<? super Integer> t) throws Exception { |
| 258 |
throw new TestException(); |
| 259 |
} |
| 260 |
}) |
| 261 |
.test() |
| 262 |
.assertFailure(TestException.class); |
| 263 |
} |
| 264 |
|
| 265 |
@Test |
| 266 |
public void deferThrows() { |
| 267 |
Maybe.defer(new Supplier<Maybe<Integer>>() { |
| 268 |
@Override |
| 269 |
public Maybe<Integer> get() throws Exception { |
| 270 |
throw new TestException(); |
| 271 |
} |
| 272 |
}) |
| 273 |
.test() |
| 274 |
.assertFailure(TestException.class); |
| 275 |
} |
| 276 |
|
| 277 |
@Test |
| 278 |
public void deferReturnsNull() { |
| 279 |
Maybe.defer(new Supplier<Maybe<Integer>>() { |
| 280 |
@Override |
| 281 |
public Maybe<Integer> get() throws Exception { |
| 282 |
return null; |
| 283 |
} |
| 284 |
}) |
| 285 |
.test() |
| 286 |
.assertFailure(NullPointerException.class); |
| 287 |
} |
| 288 |
|
| 289 |
@Test |
| 290 |
public void defer() { |
| 291 |
Maybe<Integer> source = Maybe.defer(new Supplier<Maybe<Integer>>() { |
| 292 |
int count; |
| 293 |
@Override |
| 294 |
public Maybe<Integer> get() throws Exception { |
| 295 |
return Maybe.just(count++); |
| 296 |
} |
| 297 |
}); |
| 298 |
|
| 299 |
for (int i = 0; i < 128; i++) { |
| 300 |
source.test().assertResult(i); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
@Test |
| 305 |
public void flowableMaybeFlowable() { |
| 306 |
Flowable.just(1).singleElement().toFlowable().test().assertResult(1); |
| 307 |
} |
| 308 |
|
| 309 |
@Test |
| 310 |
public void obervableMaybeobervable() { |
| 311 |
Observable.just(1).singleElement().toObservable().test().assertResult(1); |
| 312 |
} |
| 313 |
|
| 314 |
@Test |
| 315 |
public void singleMaybeSingle() { |
| 316 |
Single.just(1).toMaybe().toSingle().test().assertResult(1); |
| 317 |
} |
| 318 |
|
| 319 |
@Test |
| 320 |
public void completableMaybeCompletable() { |
| 321 |
Completable.complete().toMaybe().ignoreElement().test().assertResult(); |
| 322 |
} |
| 323 |
|
| 324 |
@Test |
| 325 |
public void unsafeCreate() { |
| 326 |
Maybe.unsafeCreate(new MaybeSource<Integer>() { |
| 327 |
@Override |
| 328 |
public void subscribe(MaybeObserver<? super Integer> observer) { |
| 329 |
observer.onSubscribe(Disposable.empty()); |
| 330 |
observer.onSuccess(1); |
| 331 |
} |
| 332 |
}) |
| 333 |
.test() |
| 334 |
.assertResult(1); |
| 335 |
} |
| 336 |
|
| 337 |
@Test |
| 338 |
public void to() { |
| 339 |
Maybe.just(1).to(new MaybeConverter<Integer, Flowable<Integer>>() { |
| 340 |
@Override |
| 341 |
public Flowable<Integer> apply(Maybe<Integer> v) { |
| 342 |
return v.toFlowable(); |
| 343 |
} |
| 344 |
}) |
| 345 |
.test() |
| 346 |
.assertResult(1); |
| 347 |
} |
| 348 |
|
| 349 |
@Test |
| 350 |
public void as() { |
| 351 |
Maybe.just(1).to(new MaybeConverter<Integer, Flowable<Integer>>() { |
| 352 |
@Override |
| 353 |
public Flowable<Integer> apply(Maybe<Integer> v) { |
| 354 |
return v.toFlowable(); |
| 355 |
} |
| 356 |
}) |
| 357 |
.test() |
| 358 |
.assertResult(1); |
| 359 |
} |
| 360 |
|
| 361 |
@Test |
| 362 |
public void compose() { |
| 363 |
Maybe.just(1).compose(new MaybeTransformer<Integer, Integer>() { |
| 364 |
@Override |
| 365 |
public MaybeSource<Integer> apply(Maybe<Integer> m) { |
| 366 |
return m.map(new Function<Integer, Integer>() { |
| 367 |
@Override |
| 368 |
public Integer apply(Integer w) throws Exception { |
| 369 |
return w + 1; |
| 370 |
} |
| 371 |
}); |
| 372 |
} |
| 373 |
}) |
| 374 |
.test() |
| 375 |
.assertResult(2); |
| 376 |
} |
| 377 |
|
| 378 |
@Test |
| 379 |
public void mapReturnNull() { |
| 380 |
Maybe.just(1).map(new Function<Integer, Object>() { |
| 381 |
@Override |
| 382 |
public Object apply(Integer v) throws Exception { |
| 383 |
return null; |
| 384 |
} |
| 385 |
}).test().assertFailure(NullPointerException.class); |
| 386 |
} |
| 387 |
|
| 388 |
@Test |
| 389 |
public void mapThrows() { |
| 390 |
Maybe.just(1).map(new Function<Integer, Object>() { |
| 391 |
@Override |
| 392 |
public Object apply(Integer v) throws Exception { |
| 393 |
throw new IOException(); |
| 394 |
} |
| 395 |
}).test().assertFailure(IOException.class); |
| 396 |
} |
| 397 |
|
| 398 |
@Test |
| 399 |
public void map() { |
| 400 |
Maybe.just(1).map(new Function<Integer, String>() { |
| 401 |
@Override |
| 402 |
public String apply(Integer v) throws Exception { |
| 403 |
return v.toString(); |
| 404 |
} |
| 405 |
}).test().assertResult("1"); |
| 406 |
} |
| 407 |
|
| 408 |
@Test |
| 409 |
public void filterThrows() { |
| 410 |
Maybe.just(1).filter(new Predicate<Integer>() { |
| 411 |
@Override |
| 412 |
public boolean test(Integer v) throws Exception { |
| 413 |
throw new IOException(); |
| 414 |
} |
| 415 |
}).test().assertFailure(IOException.class); |
| 416 |
} |
| 417 |
|
| 418 |
@Test |
| 419 |
public void filterTrue() { |
| 420 |
Maybe.just(1).filter(new Predicate<Integer>() { |
| 421 |
@Override |
| 422 |
public boolean test(Integer v) throws Exception { |
| 423 |
return v == 1; |
| 424 |
} |
| 425 |
}).test().assertResult(1); |
| 426 |
} |
| 427 |
|
| 428 |
@Test |
| 429 |
public void filterFalse() { |
| 430 |
Maybe.just(2).filter(new Predicate<Integer>() { |
| 431 |
@Override |
| 432 |
public boolean test(Integer v) throws Exception { |
| 433 |
return v == 1; |
| 434 |
} |
| 435 |
}).test().assertResult(); |
| 436 |
} |
| 437 |
|
| 438 |
@Test |
| 439 |
public void filterEmpty() { |
| 440 |
Maybe.<Integer>empty().filter(new Predicate<Integer>() { |
| 441 |
@Override |
| 442 |
public boolean test(Integer v) throws Exception { |
| 443 |
return v == 1; |
| 444 |
} |
| 445 |
}).test().assertResult(); |
| 446 |
} |
| 447 |
|
| 448 |
@Test |
| 449 |
public void singleFilterThrows() { |
| 450 |
Single.just(1).filter(new Predicate<Integer>() { |
| 451 |
@Override |
| 452 |
public boolean test(Integer v) throws Exception { |
| 453 |
throw new IOException(); |
| 454 |
} |
| 455 |
}).test().assertFailure(IOException.class); |
| 456 |
} |
| 457 |
|
| 458 |
@Test |
| 459 |
public void singleFilterTrue() { |
| 460 |
Single.just(1).filter(new Predicate<Integer>() { |
| 461 |
@Override |
| 462 |
public boolean test(Integer v) throws Exception { |
| 463 |
return v == 1; |
| 464 |
} |
| 465 |
}).test().assertResult(1); |
| 466 |
} |
| 467 |
|
| 468 |
@Test |
| 469 |
public void singleFilterFalse() { |
| 470 |
Single.just(2).filter(new Predicate<Integer>() { |
| 471 |
@Override |
| 472 |
public boolean test(Integer v) throws Exception { |
| 473 |
return v == 1; |
| 474 |
} |
| 475 |
}).test().assertResult(); |
| 476 |
} |
| 477 |
|
| 478 |
@Test |
| 479 |
public void cast() { |
| 480 |
TestObserver<Number> to = Maybe.just(1).cast(Number.class).test(); |
| 481 |
// don'n inline this due to the generic type |
| 482 |
to.assertResult((Number)1); |
| 483 |
} |
| 484 |
|
| 485 |
@Test |
| 486 |
public void observeOnSuccess() { |
| 487 |
String main = Thread.currentThread().getName(); |
| 488 |
TestObserver<String> to = Maybe.just(1) |
| 489 |
.observeOn(Schedulers.single()) |
| 490 |
.map(new Function<Integer, String>() { |
| 491 |
@Override |
| 492 |
public String apply(Integer v) throws Exception { |
| 493 |
return v + ": " + Thread.currentThread().getName(); |
| 494 |
} |
| 495 |
}) |
| 496 |
.test() |
| 497 |
.awaitDone(5, TimeUnit.SECONDS) |
| 498 |
.assertValueCount(1) |
| 499 |
; |
| 500 |
|
| 501 |
assertNotEquals("1: " + main, to.values().get(0)); |
| 502 |
} |
| 503 |
|
| 504 |
@Test |
| 505 |
public void observeOnError() { |
| 506 |
Maybe.error(new TestException()) |
| 507 |
.observeOn(Schedulers.single()) |
| 508 |
.test() |
| 509 |
.awaitDone(5, TimeUnit.SECONDS) |
| 510 |
.assertFailure(TestException.class) |
| 511 |
; |
| 512 |
} |
| 513 |
|
| 514 |
@Test |
| 515 |
public void observeOnComplete() { |
| 516 |
Maybe.empty() |
| 517 |
.observeOn(Schedulers.single()) |
| 518 |
.test() |
| 519 |
.awaitDone(5, TimeUnit.SECONDS) |
| 520 |
.assertResult() |
| 521 |
; |
| 522 |
} |
| 523 |
|
| 524 |
@Test |
| 525 |
public void observeOnDispose2() { |
| 526 |
TestHelper.checkDisposed(Maybe.empty().observeOn(Schedulers.single())); |
| 527 |
} |
| 528 |
|
| 529 |
@Test |
| 530 |
public void observeOnDoubleSubscribe() { |
| 531 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, MaybeSource<Object>>() { |
| 532 |
@Override |
| 533 |
public MaybeSource<Object> apply(Maybe<Object> m) throws Exception { |
| 534 |
return m.observeOn(Schedulers.single()); |
| 535 |
} |
| 536 |
}); |
| 537 |
} |
| 538 |
|
| 539 |
@Test |
| 540 |
public void subscribeOnSuccess() { |
| 541 |
String main = Thread.currentThread().getName(); |
| 542 |
TestObserver<String> to = Maybe.fromCallable(new Callable<String>() { |
| 543 |
@Override |
| 544 |
public String call() throws Exception { |
| 545 |
return Thread.currentThread().getName(); |
| 546 |
} |
| 547 |
}) |
| 548 |
.subscribeOn(Schedulers.single()) |
| 549 |
.test() |
| 550 |
.awaitDone(5, TimeUnit.SECONDS) |
| 551 |
.assertValueCount(1) |
| 552 |
; |
| 553 |
|
| 554 |
assertNotEquals(main, to.values().get(0)); |
| 555 |
} |
| 556 |
|
| 557 |
@Test |
| 558 |
public void observeOnErrorThread() { |
| 559 |
String main = Thread.currentThread().getName(); |
| 560 |
|
| 561 |
final String[] name = { null }; |
| 562 |
|
| 563 |
Maybe.error(new TestException()).observeOn(Schedulers.single()) |
| 564 |
.doOnError(new Consumer<Throwable>() { |
| 565 |
@Override |
| 566 |
public void accept(Throwable e) throws Exception { |
| 567 |
name[0] = Thread.currentThread().getName(); |
| 568 |
} |
| 569 |
}) |
| 570 |
.test() |
| 571 |
.awaitDone(5, TimeUnit.SECONDS) |
| 572 |
.assertFailure(TestException.class) |
| 573 |
; |
| 574 |
|
| 575 |
assertNotEquals(main, name[0]); |
| 576 |
} |
| 577 |
|
| 578 |
@Test |
| 579 |
public void observeOnCompleteThread() { |
| 580 |
String main = Thread.currentThread().getName(); |
| 581 |
|
| 582 |
final String[] name = { null }; |
| 583 |
|
| 584 |
Maybe.empty().observeOn(Schedulers.single()) |
| 585 |
.doOnComplete(new Action() { |
| 586 |
@Override |
| 587 |
public void run() throws Exception { |
| 588 |
name[0] = Thread.currentThread().getName(); |
| 589 |
} |
| 590 |
}) |
| 591 |
.test() |
| 592 |
.awaitDone(5, TimeUnit.SECONDS) |
| 593 |
.assertResult() |
| 594 |
; |
| 595 |
|
| 596 |
assertNotEquals(main, name[0]); |
| 597 |
} |
| 598 |
|
| 599 |
@Test |
| 600 |
public void subscribeOnError() { |
| 601 |
Maybe.error(new TestException()) |
| 602 |
.subscribeOn(Schedulers.single()) |
| 603 |
.test() |
| 604 |
.awaitDone(5, TimeUnit.SECONDS) |
| 605 |
.assertFailure(TestException.class) |
| 606 |
; |
| 607 |
} |
| 608 |
|
| 609 |
@Test |
| 610 |
public void subscribeOnComplete() { |
| 611 |
Maybe.empty() |
| 612 |
.subscribeOn(Schedulers.single()) |
| 613 |
.test() |
| 614 |
.awaitDone(5, TimeUnit.SECONDS) |
| 615 |
.assertResult() |
| 616 |
; |
| 617 |
} |
| 618 |
|
| 619 |
@Test |
| 620 |
public void fromAction() { |
| 621 |
final int[] call = { 0 }; |
| 622 |
|
| 623 |
Maybe.fromAction(new Action() { |
| 624 |
@Override |
| 625 |
public void run() throws Exception { |
| 626 |
call[0]++; |
| 627 |
} |
| 628 |
}) |
| 629 |
.test() |
| 630 |
.assertResult(); |
| 631 |
|
| 632 |
assertEquals(1, call[0]); |
| 633 |
} |
| 634 |
|
| 635 |
@Test |
| 636 |
public void fromActionThrows() { |
| 637 |
Maybe.fromAction(new Action() { |
| 638 |
@Override |
| 639 |
public void run() throws Exception { |
| 640 |
throw new TestException(); |
| 641 |
} |
| 642 |
}) |
| 643 |
.test() |
| 644 |
.assertFailure(TestException.class); |
| 645 |
} |
| 646 |
|
| 647 |
@Test |
| 648 |
public void fromRunnable() { |
| 649 |
final int[] call = { 0 }; |
| 650 |
|
| 651 |
Maybe.fromRunnable(new Runnable() { |
| 652 |
@Override |
| 653 |
public void run() { |
| 654 |
call[0]++; |
| 655 |
} |
| 656 |
}) |
| 657 |
.test() |
| 658 |
.assertResult(); |
| 659 |
|
| 660 |
assertEquals(1, call[0]); |
| 661 |
} |
| 662 |
|
| 663 |
@Test |
| 664 |
public void fromRunnableThrows() { |
| 665 |
Maybe.fromRunnable(new Runnable() { |
| 666 |
@Override |
| 667 |
public void run() { |
| 668 |
throw new TestException(); |
| 669 |
} |
| 670 |
}) |
| 671 |
.test() |
| 672 |
.assertFailure(TestException.class); |
| 673 |
} |
| 674 |
|
| 675 |
@Test |
| 676 |
public void fromCallableThrows() { |
| 677 |
Maybe.fromCallable(new Callable<Object>() { |
| 678 |
@Override |
| 679 |
public Object call() throws Exception { |
| 680 |
throw new TestException(); |
| 681 |
} |
| 682 |
}) |
| 683 |
.test() |
| 684 |
.assertFailure(TestException.class); |
| 685 |
} |
| 686 |
|
| 687 |
@Test |
| 688 |
public void doOnSuccess() { |
| 689 |
final Integer[] value = { null }; |
| 690 |
|
| 691 |
Maybe.just(1).doOnSuccess(new Consumer<Integer>() { |
| 692 |
@Override |
| 693 |
public void accept(Integer v) throws Exception { |
| 694 |
value[0] = v; |
| 695 |
} |
| 696 |
}) |
| 697 |
.test() |
| 698 |
.assertResult(1); |
| 699 |
|
| 700 |
assertEquals(1, value[0].intValue()); |
| 701 |
} |
| 702 |
|
| 703 |
@Test |
| 704 |
public void doOnSuccessEmpty() { |
| 705 |
final Integer[] value = { null }; |
| 706 |
|
| 707 |
Maybe.<Integer>empty().doOnSuccess(new Consumer<Integer>() { |
| 708 |
@Override |
| 709 |
public void accept(Integer v) throws Exception { |
| 710 |
value[0] = v; |
| 711 |
} |
| 712 |
}) |
| 713 |
.test() |
| 714 |
.assertResult(); |
| 715 |
|
| 716 |
assertNull(value[0]); |
| 717 |
} |
| 718 |
|
| 719 |
@Test |
| 720 |
public void doOnSuccessThrows() { |
| 721 |
Maybe.just(1).doOnSuccess(new Consumer<Integer>() { |
| 722 |
@Override |
| 723 |
public void accept(Integer v) throws Exception { |
| 724 |
throw new TestException(); |
| 725 |
} |
| 726 |
}) |
| 727 |
.test() |
| 728 |
.assertFailure(TestException.class); |
| 729 |
} |
| 730 |
|
| 731 |
@Test |
| 732 |
public void doOnSubscribe() { |
| 733 |
final Disposable[] value = { null }; |
| 734 |
|
| 735 |
Maybe.just(1).doOnSubscribe(new Consumer<Disposable>() { |
| 736 |
@Override |
| 737 |
public void accept(Disposable v) throws Exception { |
| 738 |
value[0] = v; |
| 739 |
} |
| 740 |
}) |
| 741 |
.test() |
| 742 |
.assertResult(1); |
| 743 |
|
| 744 |
assertNotNull(value[0]); |
| 745 |
} |
| 746 |
|
| 747 |
@Test |
| 748 |
public void doOnSubscribeThrows() { |
| 749 |
Maybe.just(1).doOnSubscribe(new Consumer<Disposable>() { |
| 750 |
@Override |
| 751 |
public void accept(Disposable v) throws Exception { |
| 752 |
throw new TestException(); |
| 753 |
} |
| 754 |
}) |
| 755 |
.test() |
| 756 |
.assertFailure(TestException.class); |
| 757 |
} |
| 758 |
|
| 759 |
@Test |
| 760 |
public void doOnCompleteThrows() { |
| 761 |
Maybe.empty().doOnComplete(new Action() { |
| 762 |
@Override |
| 763 |
public void run() throws Exception { |
| 764 |
throw new TestException(); |
| 765 |
} |
| 766 |
}) |
| 767 |
.test() |
| 768 |
.assertFailure(TestException.class); |
| 769 |
} |
| 770 |
|
| 771 |
@Test |
| 772 |
public void doOnDispose() { |
| 773 |
final int[] call = { 0 }; |
| 774 |
|
| 775 |
Maybe.just(1).doOnDispose(new Action() { |
| 776 |
@Override |
| 777 |
public void run() throws Exception { |
| 778 |
call[0]++; |
| 779 |
} |
| 780 |
}) |
| 781 |
.to(TestHelper.<Integer>testConsumer(true)) |
| 782 |
.assertSubscribed() |
| 783 |
.assertNoValues() |
| 784 |
.assertNoErrors() |
| 785 |
.assertNotComplete(); |
| 786 |
|
| 787 |
assertEquals(1, call[0]); |
| 788 |
} |
| 789 |
|
| 790 |
@Test |
| 791 |
public void doOnDisposeThrows() { |
| 792 |
List<Throwable> list = TestHelper.trackPluginErrors(); |
| 793 |
|
| 794 |
try { |
| 795 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 796 |
|
| 797 |
TestObserverEx<Integer> to = pp.singleElement().doOnDispose(new Action() { |
| 798 |
@Override |
| 799 |
public void run() throws Exception { |
| 800 |
throw new TestException(); |
| 801 |
} |
| 802 |
}) |
| 803 |
.to(TestHelper.<Integer>testConsumer()); |
| 804 |
|
| 805 |
assertTrue(pp.hasSubscribers()); |
| 806 |
|
| 807 |
to.dispose(); |
| 808 |
|
| 809 |
assertFalse(pp.hasSubscribers()); |
| 810 |
|
| 811 |
to.assertSubscribed() |
| 812 |
.assertNoValues() |
| 813 |
.assertNoErrors() |
| 814 |
.assertNotComplete(); |
| 815 |
|
| 816 |
TestHelper.assertUndeliverable(list, 0, TestException.class); |
| 817 |
} finally { |
| 818 |
RxJavaPlugins.reset(); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
@Test |
| 823 |
public void observeOnDispose() throws Exception { |
| 824 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 825 |
|
| 826 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 827 |
|
| 828 |
Maybe.just(1) |
| 829 |
.observeOn(Schedulers.single()) |
| 830 |
.doOnSuccess(new Consumer<Integer>() { |
| 831 |
@Override |
| 832 |
public void accept(Integer v) throws Exception { |
| 833 |
if (!cdl.await(5, TimeUnit.SECONDS)) { |
| 834 |
throw new TimeoutException(); |
| 835 |
} |
| 836 |
} |
| 837 |
}) |
| 838 |
.toFlowable().subscribe(ts); |
| 839 |
|
| 840 |
Thread.sleep(250); |
| 841 |
|
| 842 |
ts.cancel(); |
| 843 |
|
| 844 |
ts.awaitDone(5, TimeUnit.SECONDS) |
| 845 |
.assertFailure(InterruptedException.class); |
| 846 |
} |
| 847 |
|
| 848 |
@Test |
| 849 |
public void doAfterTerminateSuccess() { |
| 850 |
final int[] call = { 0 }; |
| 851 |
|
| 852 |
Maybe.just(1) |
| 853 |
.doOnSuccess(new Consumer<Integer>() { |
| 854 |
@Override |
| 855 |
public void accept(Integer v) throws Exception { |
| 856 |
call[0]++; |
| 857 |
} |
| 858 |
}) |
| 859 |
.doAfterTerminate(new Action() { |
| 860 |
@Override |
| 861 |
public void run() throws Exception { |
| 862 |
if (call[0] == 1) { |
| 863 |
call[0] = -1; |
| 864 |
} |
| 865 |
} |
| 866 |
}) |
| 867 |
.test() |
| 868 |
.assertResult(1); |
| 869 |
|
| 870 |
assertEquals(-1, call[0]); |
| 871 |
} |
| 872 |
|
| 873 |
@Test |
| 874 |
public void doAfterTerminateError() { |
| 875 |
final int[] call = { 0 }; |
| 876 |
|
| 877 |
Maybe.error(new TestException()) |
| 878 |
.doOnError(new Consumer<Object>() { |
| 879 |
@Override |
| 880 |
public void accept(Object v) throws Exception { |
| 881 |
call[0]++; |
| 882 |
} |
| 883 |
}) |
| 884 |
.doAfterTerminate(new Action() { |
| 885 |
@Override |
| 886 |
public void run() throws Exception { |
| 887 |
if (call[0] == 1) { |
| 888 |
call[0] = -1; |
| 889 |
} |
| 890 |
} |
| 891 |
}) |
| 892 |
.test() |
| 893 |
.assertFailure(TestException.class); |
| 894 |
|
| 895 |
assertEquals(-1, call[0]); |
| 896 |
} |
| 897 |
|
| 898 |
@Test |
| 899 |
public void doAfterTerminateComplete() { |
| 900 |
final int[] call = { 0 }; |
| 901 |
|
| 902 |
Maybe.empty() |
| 903 |
.doOnComplete(new Action() { |
| 904 |
@Override |
| 905 |
public void run() throws Exception { |
| 906 |
call[0]++; |
| 907 |
} |
| 908 |
}) |
| 909 |
.doAfterTerminate(new Action() { |
| 910 |
@Override |
| 911 |
public void run() throws Exception { |
| 912 |
if (call[0] == 1) { |
| 913 |
call[0] = -1; |
| 914 |
} |
| 915 |
} |
| 916 |
}) |
| 917 |
.test() |
| 918 |
.assertResult(); |
| 919 |
|
| 920 |
assertEquals(-1, call[0]); |
| 921 |
} |
| 922 |
|
| 923 |
@Test |
| 924 |
public void sourceThrowsNPE() { |
| 925 |
try { |
| 926 |
Maybe.unsafeCreate(new MaybeSource<Object>() { |
| 927 |
@Override |
| 928 |
public void subscribe(MaybeObserver<? super Object> observer) { |
| 929 |
throw new NullPointerException("Forced failure"); |
| 930 |
} |
| 931 |
}).test(); |
| 932 |
|
| 933 |
fail("Should have thrown!"); |
| 934 |
} catch (NullPointerException ex) { |
| 935 |
assertEquals("Forced failure", ex.getMessage()); |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
@Test |
| 940 |
public void sourceThrowsIAE() { |
| 941 |
try { |
| 942 |
Maybe.unsafeCreate(new MaybeSource<Object>() { |
| 943 |
@Override |
| 944 |
public void subscribe(MaybeObserver<? super Object> observer) { |
| 945 |
throw new IllegalArgumentException("Forced failure"); |
| 946 |
} |
| 947 |
}).test(); |
| 948 |
|
| 949 |
fail("Should have thrown!"); |
| 950 |
} catch (NullPointerException ex) { |
| 951 |
assertTrue(ex.toString(), ex.getCause() instanceof IllegalArgumentException); |
| 952 |
assertEquals("Forced failure", ex.getCause().getMessage()); |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
@Test |
| 957 |
public void flatMap() { |
| 958 |
Maybe.just(1).flatMap(new Function<Integer, MaybeSource<Integer>>() { |
| 959 |
@Override |
| 960 |
public MaybeSource<Integer> apply(Integer v) throws Exception { |
| 961 |
return Maybe.just(v * 10); |
| 962 |
} |
| 963 |
}) |
| 964 |
.test() |
| 965 |
.assertResult(10); |
| 966 |
} |
| 967 |
|
| 968 |
@Test |
| 969 |
public void concatMap() { |
| 970 |
Maybe.just(1).concatMap(new Function<Integer, MaybeSource<Integer>>() { |
| 971 |
@Override |
| 972 |
public MaybeSource<Integer> apply(Integer v) throws Exception { |
| 973 |
return Maybe.just(v * 10); |
| 974 |
} |
| 975 |
}) |
| 976 |
.test() |
| 977 |
.assertResult(10); |
| 978 |
} |
| 979 |
|
| 980 |
@Test |
| 981 |
public void flatMapEmpty() { |
| 982 |
Maybe.just(1).flatMap(new Function<Integer, MaybeSource<Integer>>() { |
| 983 |
@Override |
| 984 |
public MaybeSource<Integer> apply(Integer v) throws Exception { |
| 985 |
return Maybe.empty(); |
| 986 |
} |
| 987 |
}) |
| 988 |
.test() |
| 989 |
.assertResult(); |
| 990 |
} |
| 991 |
|
| 992 |
@Test |
| 993 |
public void flatMapError() { |
| 994 |
Maybe.just(1).flatMap(new Function<Integer, MaybeSource<Integer>>() { |
| 995 |
@Override |
| 996 |
public MaybeSource<Integer> apply(Integer v) throws Exception { |
| 997 |
return Maybe.error(new TestException()); |
| 998 |
} |
| 999 |
}) |
| 1000 |
.test() |
| 1001 |
.assertFailure(TestException.class); |
| 1002 |
} |
| 1003 |
|
| 1004 |
@Test |
| 1005 |
public void flatMapNotifySuccess() { |
| 1006 |
Maybe.just(1) |
| 1007 |
.flatMap(new Function<Integer, MaybeSource<Integer>>() { |
| 1008 |
@Override |
| 1009 |
public MaybeSource<Integer> apply(Integer v) throws Exception { |
| 1010 |
return Maybe.just(v * 10); |
| 1011 |
} |
| 1012 |
}, |
| 1013 |
new Function<Throwable, MaybeSource<Integer>>() { |
| 1014 |
@Override |
| 1015 |
public MaybeSource<Integer> apply(Throwable v) throws Exception { |
| 1016 |
return Maybe.just(100); |
| 1017 |
} |
| 1018 |
}, |
| 1019 |
|
| 1020 |
new Supplier<MaybeSource<Integer>>() { |
| 1021 |
@Override |
| 1022 |
public MaybeSource<Integer> get() throws Exception { |
| 1023 |
return Maybe.just(200); |
| 1024 |
} |
| 1025 |
}) |
| 1026 |
.test() |
| 1027 |
.assertResult(10); |
| 1028 |
} |
| 1029 |
|
| 1030 |
@Test |
| 1031 |
public void flatMapNotifyError() { |
| 1032 |
Maybe.<Integer>error(new TestException()) |
| 1033 |
.flatMap(new Function<Integer, MaybeSource<Integer>>() { |
| 1034 |
@Override |
| 1035 |
public MaybeSource<Integer> apply(Integer v) throws Exception { |
| 1036 |
return Maybe.just(v * 10); |
| 1037 |
} |
| 1038 |
}, |
| 1039 |
new Function<Throwable, MaybeSource<Integer>>() { |
| 1040 |
@Override |
| 1041 |
public MaybeSource<Integer> apply(Throwable v) throws Exception { |
| 1042 |
return Maybe.just(100); |
| 1043 |
} |
| 1044 |
}, |
| 1045 |
|
| 1046 |
new Supplier<MaybeSource<Integer>>() { |
| 1047 |
@Override |
| 1048 |
public MaybeSource<Integer> get() throws Exception { |
| 1049 |
return Maybe.just(200); |
| 1050 |
} |
| 1051 |
}) |
| 1052 |
.test() |
| 1053 |
.assertResult(100); |
| 1054 |
} |
| 1055 |
|
| 1056 |
@Test |
| 1057 |
public void flatMapNotifyComplete() { |
| 1058 |
Maybe.<Integer>empty() |
| 1059 |
.flatMap(new Function<Integer, MaybeSource<Integer>>() { |
| 1060 |
@Override |
| 1061 |
public MaybeSource<Integer> apply(Integer v) throws Exception { |
| 1062 |
return Maybe.just(v * 10); |
| 1063 |
} |
| 1064 |
}, |
| 1065 |
new Function<Throwable, MaybeSource<Integer>>() { |
| 1066 |
@Override |
| 1067 |
public MaybeSource<Integer> apply(Throwable v) throws Exception { |
| 1068 |
return Maybe.just(100); |
| 1069 |
} |
| 1070 |
}, |
| 1071 |
|
| 1072 |
new Supplier<MaybeSource<Integer>>() { |
| 1073 |
@Override |
| 1074 |
public MaybeSource<Integer> get() throws Exception { |
| 1075 |
return Maybe.just(200); |
| 1076 |
} |
| 1077 |
}) |
| 1078 |
.test() |
| 1079 |
.assertResult(200); |
| 1080 |
} |
| 1081 |
|
| 1082 |
@Test |
| 1083 |
public void ignoreElementSuccess() { |
| 1084 |
Maybe.just(1) |
| 1085 |
.ignoreElement() |
| 1086 |
.test() |
| 1087 |
.assertResult(); |
| 1088 |
} |
| 1089 |
|
| 1090 |
@Test |
| 1091 |
public void ignoreElementError() { |
| 1092 |
Maybe.error(new TestException()) |
| 1093 |
.ignoreElement() |
| 1094 |
.test() |
| 1095 |
.assertFailure(TestException.class); |
| 1096 |
} |
| 1097 |
|
| 1098 |
@Test |
| 1099 |
public void ignoreElementComplete() { |
| 1100 |
Maybe.empty() |
| 1101 |
.ignoreElement() |
| 1102 |
.test() |
| 1103 |
.assertResult(); |
| 1104 |
} |
| 1105 |
|
| 1106 |
@Test |
| 1107 |
public void ignoreElementSuccessMaybe() { |
| 1108 |
Maybe.just(1) |
| 1109 |
.ignoreElement() |
| 1110 |
.toMaybe() |
| 1111 |
.test() |
| 1112 |
.assertResult(); |
| 1113 |
} |
| 1114 |
|
| 1115 |
@Test |
| 1116 |
public void ignoreElementErrorMaybe() { |
| 1117 |
Maybe.error(new TestException()) |
| 1118 |
.ignoreElement() |
| 1119 |
.toMaybe() |
| 1120 |
.test() |
| 1121 |
.assertFailure(TestException.class); |
| 1122 |
} |
| 1123 |
|
| 1124 |
@Test |
| 1125 |
public void ignoreElementCompleteMaybe() { |
| 1126 |
Maybe.empty() |
| 1127 |
.ignoreElement() |
| 1128 |
.toMaybe() |
| 1129 |
.test() |
| 1130 |
.assertResult(); |
| 1131 |
} |
| 1132 |
|
| 1133 |
@Test |
| 1134 |
public void singleToMaybe() { |
| 1135 |
Single.just(1) |
| 1136 |
.toMaybe() |
| 1137 |
.test() |
| 1138 |
.assertResult(1); |
| 1139 |
} |
| 1140 |
|
| 1141 |
@Test |
| 1142 |
public void singleToMaybeError() { |
| 1143 |
Single.error(new TestException()) |
| 1144 |
.toMaybe() |
| 1145 |
.test() |
| 1146 |
.assertFailure(TestException.class); |
| 1147 |
} |
| 1148 |
|
| 1149 |
@Test |
| 1150 |
public void completableToMaybe() { |
| 1151 |
Completable.complete() |
| 1152 |
.toMaybe() |
| 1153 |
.test() |
| 1154 |
.assertResult(); |
| 1155 |
} |
| 1156 |
|
| 1157 |
@Test |
| 1158 |
public void completableToMaybeError() { |
| 1159 |
Completable.error(new TestException()) |
| 1160 |
.toMaybe() |
| 1161 |
.test() |
| 1162 |
.assertFailure(TestException.class); |
| 1163 |
} |
| 1164 |
|
| 1165 |
@Test |
| 1166 |
public void emptyToSingle() { |
| 1167 |
Maybe.empty() |
| 1168 |
.toSingle() |
| 1169 |
.test() |
| 1170 |
.assertFailure(NoSuchElementException.class); |
| 1171 |
} |
| 1172 |
|
| 1173 |
@Test |
| 1174 |
public void errorToSingle() { |
| 1175 |
Maybe.error(new TestException()) |
| 1176 |
.toSingle() |
| 1177 |
.test() |
| 1178 |
.assertFailure(TestException.class); |
| 1179 |
} |
| 1180 |
|
| 1181 |
@Test |
| 1182 |
public void emptyToCompletable() { |
| 1183 |
Maybe.empty() |
| 1184 |
.ignoreElement() |
| 1185 |
.test() |
| 1186 |
.assertResult(); |
| 1187 |
} |
| 1188 |
|
| 1189 |
@Test |
| 1190 |
public void errorToCompletable() { |
| 1191 |
Maybe.error(new TestException()) |
| 1192 |
.ignoreElement() |
| 1193 |
.test() |
| 1194 |
.assertFailure(TestException.class); |
| 1195 |
} |
| 1196 |
|
| 1197 |
@Test |
| 1198 |
public void concat2() { |
| 1199 |
Maybe.concat(Maybe.just(1), Maybe.just(2)) |
| 1200 |
.test() |
| 1201 |
.assertResult(1, 2); |
| 1202 |
} |
| 1203 |
|
| 1204 |
@Test |
| 1205 |
public void concat2Empty() { |
| 1206 |
Maybe.concat(Maybe.empty(), Maybe.empty()) |
| 1207 |
.test() |
| 1208 |
.assertResult(); |
| 1209 |
} |
| 1210 |
|
| 1211 |
@Test |
| 1212 |
public void concat2Backpressured() { |
| 1213 |
TestSubscriber<Integer> ts = Maybe.concat(Maybe.just(1), Maybe.just(2)) |
| 1214 |
.test(0L); |
| 1215 |
|
| 1216 |
ts.assertEmpty(); |
| 1217 |
|
| 1218 |
ts.request(1); |
| 1219 |
|
| 1220 |
ts.assertValue(1); |
| 1221 |
|
| 1222 |
ts.request(1); |
| 1223 |
|
| 1224 |
ts.assertResult(1, 2); |
| 1225 |
} |
| 1226 |
|
| 1227 |
@Test |
| 1228 |
public void concat2BackpressuredNonEager() { |
| 1229 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1230 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1231 |
|
| 1232 |
TestSubscriber<Integer> ts = Maybe.concat(pp1.singleElement(), pp2.singleElement()) |
| 1233 |
.test(0L); |
| 1234 |
|
| 1235 |
assertTrue(pp1.hasSubscribers()); |
| 1236 |
assertFalse(pp2.hasSubscribers()); |
| 1237 |
|
| 1238 |
ts.assertEmpty(); |
| 1239 |
|
| 1240 |
ts.request(1); |
| 1241 |
|
| 1242 |
assertTrue(pp1.hasSubscribers()); |
| 1243 |
assertFalse(pp2.hasSubscribers()); |
| 1244 |
|
| 1245 |
pp1.onNext(1); |
| 1246 |
pp1.onComplete(); |
| 1247 |
|
| 1248 |
ts.assertValue(1); |
| 1249 |
|
| 1250 |
assertFalse(pp1.hasSubscribers()); |
| 1251 |
assertTrue(pp2.hasSubscribers()); |
| 1252 |
|
| 1253 |
ts.request(1); |
| 1254 |
|
| 1255 |
ts.assertValue(1); |
| 1256 |
|
| 1257 |
pp2.onNext(2); |
| 1258 |
pp2.onComplete(); |
| 1259 |
|
| 1260 |
ts.assertResult(1, 2); |
| 1261 |
|
| 1262 |
assertFalse(pp1.hasSubscribers()); |
| 1263 |
assertFalse(pp2.hasSubscribers()); |
| 1264 |
} |
| 1265 |
|
| 1266 |
@Test |
| 1267 |
public void concat3() { |
| 1268 |
Maybe.concat(Maybe.just(1), Maybe.just(2), Maybe.just(3)) |
| 1269 |
.test() |
| 1270 |
.assertResult(1, 2, 3); |
| 1271 |
} |
| 1272 |
|
| 1273 |
@Test |
| 1274 |
public void concat3Empty() { |
| 1275 |
Maybe.concat(Maybe.empty(), Maybe.empty(), Maybe.empty()) |
| 1276 |
.test() |
| 1277 |
.assertResult(); |
| 1278 |
} |
| 1279 |
|
| 1280 |
@Test |
| 1281 |
public void concat3Mixed1() { |
| 1282 |
Maybe.concat(Maybe.just(1), Maybe.empty(), Maybe.just(3)) |
| 1283 |
.test() |
| 1284 |
.assertResult(1, 3); |
| 1285 |
} |
| 1286 |
|
| 1287 |
@Test |
| 1288 |
public void concat3Mixed2() { |
| 1289 |
Maybe.concat(Maybe.just(1), Maybe.just(2), Maybe.empty()) |
| 1290 |
.test() |
| 1291 |
.assertResult(1, 2); |
| 1292 |
} |
| 1293 |
|
| 1294 |
@Test |
| 1295 |
public void concat3Backpressured() { |
| 1296 |
TestSubscriber<Integer> ts = Maybe.concat(Maybe.just(1), Maybe.just(2), Maybe.just(3)) |
| 1297 |
.test(0L); |
| 1298 |
|
| 1299 |
ts.assertEmpty(); |
| 1300 |
|
| 1301 |
ts.request(1); |
| 1302 |
|
| 1303 |
ts.assertValue(1); |
| 1304 |
|
| 1305 |
ts.request(2); |
| 1306 |
|
| 1307 |
ts.assertResult(1, 2, 3); |
| 1308 |
} |
| 1309 |
|
| 1310 |
@Test |
| 1311 |
public void concatArrayZero() { |
| 1312 |
assertSame(Flowable.empty(), Maybe.concatArray()); |
| 1313 |
} |
| 1314 |
|
| 1315 |
@Test |
| 1316 |
public void concatArrayOne() { |
| 1317 |
Maybe.concatArray(Maybe.just(1)).test().assertResult(1); |
| 1318 |
} |
| 1319 |
|
| 1320 |
@Test |
| 1321 |
public void concat4() { |
| 1322 |
Maybe.concat(Maybe.just(1), Maybe.just(2), Maybe.just(3), Maybe.just(4)) |
| 1323 |
.test() |
| 1324 |
.assertResult(1, 2, 3, 4); |
| 1325 |
} |
| 1326 |
|
| 1327 |
@Test |
| 1328 |
public void concatIterable() { |
| 1329 |
Maybe.concat(Arrays.asList(Maybe.just(1), Maybe.just(2))) |
| 1330 |
.test() |
| 1331 |
.assertResult(1, 2); |
| 1332 |
} |
| 1333 |
|
| 1334 |
@Test |
| 1335 |
public void concatIterableEmpty() { |
| 1336 |
Maybe.concat(Arrays.asList(Maybe.empty(), Maybe.empty())) |
| 1337 |
.test() |
| 1338 |
.assertResult(); |
| 1339 |
} |
| 1340 |
|
| 1341 |
@Test |
| 1342 |
public void concatIterableBackpressured() { |
| 1343 |
TestSubscriber<Integer> ts = Maybe.concat(Arrays.asList(Maybe.just(1), Maybe.just(2))) |
| 1344 |
.test(0L); |
| 1345 |
|
| 1346 |
ts.assertEmpty(); |
| 1347 |
|
| 1348 |
ts.request(1); |
| 1349 |
|
| 1350 |
ts.assertValue(1); |
| 1351 |
|
| 1352 |
ts.request(1); |
| 1353 |
|
| 1354 |
ts.assertResult(1, 2); |
| 1355 |
} |
| 1356 |
|
| 1357 |
@Test |
| 1358 |
public void concatIterableBackpressuredNonEager() { |
| 1359 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1360 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1361 |
|
| 1362 |
TestSubscriber<Integer> ts = Maybe.concat(Arrays.asList(pp1.singleElement(), pp2.singleElement())) |
| 1363 |
.test(0L); |
| 1364 |
|
| 1365 |
assertTrue(pp1.hasSubscribers()); |
| 1366 |
assertFalse(pp2.hasSubscribers()); |
| 1367 |
|
| 1368 |
ts.assertEmpty(); |
| 1369 |
|
| 1370 |
ts.request(1); |
| 1371 |
|
| 1372 |
assertTrue(pp1.hasSubscribers()); |
| 1373 |
assertFalse(pp2.hasSubscribers()); |
| 1374 |
|
| 1375 |
pp1.onNext(1); |
| 1376 |
pp1.onComplete(); |
| 1377 |
|
| 1378 |
ts.assertValue(1); |
| 1379 |
|
| 1380 |
assertFalse(pp1.hasSubscribers()); |
| 1381 |
assertTrue(pp2.hasSubscribers()); |
| 1382 |
|
| 1383 |
ts.request(1); |
| 1384 |
|
| 1385 |
ts.assertValue(1); |
| 1386 |
|
| 1387 |
pp2.onNext(2); |
| 1388 |
pp2.onComplete(); |
| 1389 |
|
| 1390 |
ts.assertResult(1, 2); |
| 1391 |
|
| 1392 |
assertFalse(pp1.hasSubscribers()); |
| 1393 |
assertFalse(pp2.hasSubscribers()); |
| 1394 |
} |
| 1395 |
|
| 1396 |
@Test |
| 1397 |
public void concatIterableZero() { |
| 1398 |
Maybe.concat(Collections.<Maybe<Integer>>emptyList()).test().assertResult(); |
| 1399 |
} |
| 1400 |
|
| 1401 |
@Test |
| 1402 |
public void concatIterableOne() { |
| 1403 |
Maybe.concat(Collections.<Maybe<Integer>>singleton(Maybe.just(1))).test().assertResult(1); |
| 1404 |
} |
| 1405 |
|
| 1406 |
@Test |
| 1407 |
public void concatPublisher() { |
| 1408 |
Maybe.concat(Flowable.just(Maybe.just(1), Maybe.just(2))).test().assertResult(1, 2); |
| 1409 |
} |
| 1410 |
|
| 1411 |
@Test |
| 1412 |
public void concatPublisherPrefetch() { |
| 1413 |
Maybe.concat(Flowable.just(Maybe.just(1), Maybe.just(2)), 1).test().assertResult(1, 2); |
| 1414 |
} |
| 1415 |
|
| 1416 |
@Test |
| 1417 |
public void basic() { |
| 1418 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1419 |
try { |
| 1420 |
final Disposable d = Disposable.empty(); |
| 1421 |
|
| 1422 |
Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() { |
| 1423 |
@Override |
| 1424 |
public void subscribe(MaybeEmitter<Integer> e) throws Exception { |
| 1425 |
e.setDisposable(d); |
| 1426 |
|
| 1427 |
e.onSuccess(1); |
| 1428 |
e.onError(new TestException()); |
| 1429 |
e.onSuccess(2); |
| 1430 |
e.onError(new TestException()); |
| 1431 |
e.onComplete(); |
| 1432 |
} |
| 1433 |
}) |
| 1434 |
.test() |
| 1435 |
.assertResult(1); |
| 1436 |
|
| 1437 |
assertTrue(d.isDisposed()); |
| 1438 |
|
| 1439 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 1440 |
TestHelper.assertUndeliverable(errors, 1, TestException.class); |
| 1441 |
} finally { |
| 1442 |
RxJavaPlugins.reset(); |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
@Test |
| 1447 |
public void basicWithError() { |
| 1448 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1449 |
try { |
| 1450 |
final Disposable d = Disposable.empty(); |
| 1451 |
|
| 1452 |
Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() { |
| 1453 |
@Override |
| 1454 |
public void subscribe(MaybeEmitter<Integer> e) throws Exception { |
| 1455 |
e.setDisposable(d); |
| 1456 |
|
| 1457 |
e.onError(new TestException()); |
| 1458 |
e.onSuccess(2); |
| 1459 |
e.onError(new TestException()); |
| 1460 |
e.onComplete(); |
| 1461 |
} |
| 1462 |
}) |
| 1463 |
.test() |
| 1464 |
.assertFailure(TestException.class); |
| 1465 |
|
| 1466 |
assertTrue(d.isDisposed()); |
| 1467 |
|
| 1468 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 1469 |
} finally { |
| 1470 |
RxJavaPlugins.reset(); |
| 1471 |
} |
| 1472 |
} |
| 1473 |
|
| 1474 |
@Test |
| 1475 |
public void basicWithComplete() { |
| 1476 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1477 |
try { |
| 1478 |
final Disposable d = Disposable.empty(); |
| 1479 |
|
| 1480 |
Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() { |
| 1481 |
@Override |
| 1482 |
public void subscribe(MaybeEmitter<Integer> e) throws Exception { |
| 1483 |
e.setDisposable(d); |
| 1484 |
|
| 1485 |
e.onComplete(); |
| 1486 |
e.onSuccess(1); |
| 1487 |
e.onError(new TestException()); |
| 1488 |
e.onComplete(); |
| 1489 |
e.onSuccess(2); |
| 1490 |
e.onError(new TestException()); |
| 1491 |
} |
| 1492 |
}) |
| 1493 |
.test() |
| 1494 |
.assertResult(); |
| 1495 |
|
| 1496 |
assertTrue(d.isDisposed()); |
| 1497 |
|
| 1498 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 1499 |
TestHelper.assertUndeliverable(errors, 1, TestException.class); |
| 1500 |
} finally { |
| 1501 |
RxJavaPlugins.reset(); |
| 1502 |
} |
| 1503 |
} |
| 1504 |
|
| 1505 |
@Test(expected = IllegalArgumentException.class) |
| 1506 |
public void unsafeCreateWithMaybe() { |
| 1507 |
Maybe.unsafeCreate(Maybe.just(1)); |
| 1508 |
} |
| 1509 |
|
| 1510 |
@Test |
| 1511 |
public void maybeToPublisherEnum() { |
| 1512 |
TestHelper.checkEnum(MaybeToPublisher.class); |
| 1513 |
} |
| 1514 |
|
| 1515 |
@Test |
| 1516 |
public void ambArrayOneIsNull() { |
| 1517 |
Maybe.ambArray(null, Maybe.just(1)) |
| 1518 |
.test() |
| 1519 |
.assertError(NullPointerException.class); |
| 1520 |
} |
| 1521 |
|
| 1522 |
@Test |
| 1523 |
public void ambArrayEmpty() { |
| 1524 |
assertSame(Maybe.empty(), Maybe.ambArray()); |
| 1525 |
} |
| 1526 |
|
| 1527 |
@Test |
| 1528 |
public void ambArrayOne() { |
| 1529 |
assertSame(Maybe.never(), Maybe.ambArray(Maybe.never())); |
| 1530 |
} |
| 1531 |
|
| 1532 |
@Test |
| 1533 |
public void ambWithOrder() { |
| 1534 |
Maybe<Integer> error = Maybe.error(new RuntimeException()); |
| 1535 |
Maybe.just(1).ambWith(error).test().assertValue(1); |
| 1536 |
} |
| 1537 |
|
| 1538 |
@Test |
| 1539 |
public void ambIterableOrder() { |
| 1540 |
Maybe<Integer> error = Maybe.error(new RuntimeException()); |
| 1541 |
Maybe.amb(Arrays.asList(Maybe.just(1), error)).test().assertValue(1); |
| 1542 |
} |
| 1543 |
|
| 1544 |
@Test |
| 1545 |
public void ambArrayOrder() { |
| 1546 |
Maybe<Integer> error = Maybe.error(new RuntimeException()); |
| 1547 |
Maybe.ambArray(Maybe.just(1), error).test().assertValue(1); |
| 1548 |
} |
| 1549 |
|
| 1550 |
@Test |
| 1551 |
public void ambArray1SignalsSuccess() { |
| 1552 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1553 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1554 |
|
| 1555 |
TestObserver<Integer> to = Maybe.ambArray(pp1.singleElement(), pp2.singleElement()) |
| 1556 |
.test(); |
| 1557 |
|
| 1558 |
to.assertEmpty(); |
| 1559 |
|
| 1560 |
assertTrue(pp1.hasSubscribers()); |
| 1561 |
assertTrue(pp2.hasSubscribers()); |
| 1562 |
|
| 1563 |
pp1.onNext(1); |
| 1564 |
pp1.onComplete(); |
| 1565 |
|
| 1566 |
assertFalse(pp1.hasSubscribers()); |
| 1567 |
assertFalse(pp2.hasSubscribers()); |
| 1568 |
|
| 1569 |
to.assertResult(1); |
| 1570 |
} |
| 1571 |
|
| 1572 |
@Test |
| 1573 |
public void ambArray2SignalsSuccess() { |
| 1574 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1575 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1576 |
|
| 1577 |
TestObserver<Integer> to = Maybe.ambArray(pp1.singleElement(), pp2.singleElement()) |
| 1578 |
.test(); |
| 1579 |
|
| 1580 |
to.assertEmpty(); |
| 1581 |
|
| 1582 |
assertTrue(pp1.hasSubscribers()); |
| 1583 |
assertTrue(pp2.hasSubscribers()); |
| 1584 |
|
| 1585 |
pp2.onNext(2); |
| 1586 |
pp2.onComplete(); |
| 1587 |
|
| 1588 |
assertFalse(pp1.hasSubscribers()); |
| 1589 |
assertFalse(pp2.hasSubscribers()); |
| 1590 |
|
| 1591 |
to.assertResult(2); |
| 1592 |
} |
| 1593 |
|
| 1594 |
@Test |
| 1595 |
public void ambArray1SignalsError() { |
| 1596 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1597 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1598 |
|
| 1599 |
TestObserver<Integer> to = Maybe.ambArray(pp1.singleElement(), pp2.singleElement()) |
| 1600 |
.test(); |
| 1601 |
|
| 1602 |
to.assertEmpty(); |
| 1603 |
|
| 1604 |
assertTrue(pp1.hasSubscribers()); |
| 1605 |
assertTrue(pp2.hasSubscribers()); |
| 1606 |
|
| 1607 |
pp1.onError(new TestException()); |
| 1608 |
|
| 1609 |
assertFalse(pp1.hasSubscribers()); |
| 1610 |
assertFalse(pp2.hasSubscribers()); |
| 1611 |
|
| 1612 |
to.assertFailure(TestException.class); |
| 1613 |
} |
| 1614 |
|
| 1615 |
@Test |
| 1616 |
public void ambArray2SignalsError() { |
| 1617 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1618 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1619 |
|
| 1620 |
TestObserver<Integer> to = Maybe.ambArray(pp1.singleElement(), pp2.singleElement()) |
| 1621 |
.test(); |
| 1622 |
|
| 1623 |
to.assertEmpty(); |
| 1624 |
|
| 1625 |
assertTrue(pp1.hasSubscribers()); |
| 1626 |
assertTrue(pp2.hasSubscribers()); |
| 1627 |
|
| 1628 |
pp2.onError(new TestException()); |
| 1629 |
|
| 1630 |
assertFalse(pp1.hasSubscribers()); |
| 1631 |
assertFalse(pp2.hasSubscribers()); |
| 1632 |
|
| 1633 |
to.assertFailure(TestException.class); |
| 1634 |
} |
| 1635 |
|
| 1636 |
@Test |
| 1637 |
public void ambArray1SignalsComplete() { |
| 1638 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1639 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1640 |
|
| 1641 |
TestObserver<Integer> to = Maybe.ambArray(pp1.singleElement(), pp2.singleElement()) |
| 1642 |
.test(); |
| 1643 |
|
| 1644 |
to.assertEmpty(); |
| 1645 |
|
| 1646 |
assertTrue(pp1.hasSubscribers()); |
| 1647 |
assertTrue(pp2.hasSubscribers()); |
| 1648 |
|
| 1649 |
pp1.onComplete(); |
| 1650 |
|
| 1651 |
assertFalse(pp1.hasSubscribers()); |
| 1652 |
assertFalse(pp2.hasSubscribers()); |
| 1653 |
|
| 1654 |
to.assertResult(); |
| 1655 |
} |
| 1656 |
|
| 1657 |
@Test |
| 1658 |
public void ambArray2SignalsComplete() { |
| 1659 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1660 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1661 |
|
| 1662 |
TestObserver<Integer> to = Maybe.ambArray(pp1.singleElement(), pp2.singleElement()) |
| 1663 |
.test(); |
| 1664 |
|
| 1665 |
to.assertEmpty(); |
| 1666 |
|
| 1667 |
assertTrue(pp1.hasSubscribers()); |
| 1668 |
assertTrue(pp2.hasSubscribers()); |
| 1669 |
|
| 1670 |
pp2.onComplete(); |
| 1671 |
|
| 1672 |
assertFalse(pp1.hasSubscribers()); |
| 1673 |
assertFalse(pp2.hasSubscribers()); |
| 1674 |
|
| 1675 |
to.assertResult(); |
| 1676 |
} |
| 1677 |
|
| 1678 |
@Test |
| 1679 |
public void ambIterable1SignalsSuccess() { |
| 1680 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1681 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1682 |
|
| 1683 |
TestObserver<Integer> to = Maybe.amb(Arrays.asList(pp1.singleElement(), pp2.singleElement())) |
| 1684 |
.test(); |
| 1685 |
|
| 1686 |
to.assertEmpty(); |
| 1687 |
|
| 1688 |
assertTrue(pp1.hasSubscribers()); |
| 1689 |
assertTrue(pp2.hasSubscribers()); |
| 1690 |
|
| 1691 |
pp1.onNext(1); |
| 1692 |
pp1.onComplete(); |
| 1693 |
|
| 1694 |
assertFalse(pp1.hasSubscribers()); |
| 1695 |
assertFalse(pp2.hasSubscribers()); |
| 1696 |
|
| 1697 |
to.assertResult(1); |
| 1698 |
} |
| 1699 |
|
| 1700 |
@Test |
| 1701 |
public void ambIterable2SignalsSuccess() { |
| 1702 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1703 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1704 |
|
| 1705 |
TestObserver<Integer> to = Maybe.amb(Arrays.asList(pp1.singleElement(), pp2.singleElement())) |
| 1706 |
.test(); |
| 1707 |
|
| 1708 |
to.assertEmpty(); |
| 1709 |
|
| 1710 |
assertTrue(pp1.hasSubscribers()); |
| 1711 |
assertTrue(pp2.hasSubscribers()); |
| 1712 |
|
| 1713 |
pp2.onNext(2); |
| 1714 |
pp2.onComplete(); |
| 1715 |
|
| 1716 |
assertFalse(pp1.hasSubscribers()); |
| 1717 |
assertFalse(pp2.hasSubscribers()); |
| 1718 |
|
| 1719 |
to.assertResult(2); |
| 1720 |
} |
| 1721 |
|
| 1722 |
@Test |
| 1723 |
public void ambIterable2SignalsSuccessWithOverlap() { |
| 1724 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1725 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1726 |
|
| 1727 |
TestObserver<Integer> to = Maybe.amb(Arrays.asList(pp1.singleElement(), pp2.singleElement())) |
| 1728 |
.test(); |
| 1729 |
|
| 1730 |
to.assertEmpty(); |
| 1731 |
|
| 1732 |
assertTrue(pp1.hasSubscribers()); |
| 1733 |
assertTrue(pp2.hasSubscribers()); |
| 1734 |
|
| 1735 |
pp2.onNext(2); |
| 1736 |
pp1.onNext(1); |
| 1737 |
pp2.onComplete(); |
| 1738 |
|
| 1739 |
assertFalse(pp1.hasSubscribers()); |
| 1740 |
assertFalse(pp2.hasSubscribers()); |
| 1741 |
|
| 1742 |
to.assertResult(2); |
| 1743 |
} |
| 1744 |
|
| 1745 |
@Test |
| 1746 |
public void ambIterable1SignalsError() { |
| 1747 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1748 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1749 |
|
| 1750 |
TestObserver<Integer> to = Maybe.amb(Arrays.asList(pp1.singleElement(), pp2.singleElement())) |
| 1751 |
.test(); |
| 1752 |
|
| 1753 |
to.assertEmpty(); |
| 1754 |
|
| 1755 |
assertTrue(pp1.hasSubscribers()); |
| 1756 |
assertTrue(pp2.hasSubscribers()); |
| 1757 |
|
| 1758 |
pp1.onError(new TestException()); |
| 1759 |
|
| 1760 |
assertFalse(pp1.hasSubscribers()); |
| 1761 |
assertFalse(pp2.hasSubscribers()); |
| 1762 |
|
| 1763 |
to.assertFailure(TestException.class); |
| 1764 |
} |
| 1765 |
|
| 1766 |
@Test |
| 1767 |
public void ambIterable2SignalsError() { |
| 1768 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1769 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1770 |
|
| 1771 |
TestObserver<Integer> to = Maybe.amb(Arrays.asList(pp1.singleElement(), pp2.singleElement())) |
| 1772 |
.test(); |
| 1773 |
|
| 1774 |
to.assertEmpty(); |
| 1775 |
|
| 1776 |
assertTrue(pp1.hasSubscribers()); |
| 1777 |
assertTrue(pp2.hasSubscribers()); |
| 1778 |
|
| 1779 |
pp2.onError(new TestException()); |
| 1780 |
|
| 1781 |
assertFalse(pp1.hasSubscribers()); |
| 1782 |
assertFalse(pp2.hasSubscribers()); |
| 1783 |
|
| 1784 |
to.assertFailure(TestException.class); |
| 1785 |
} |
| 1786 |
|
| 1787 |
@Test |
| 1788 |
public void ambIterable2SignalsErrorWithOverlap() { |
| 1789 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1790 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1791 |
|
| 1792 |
TestObserverEx<Integer> to = Maybe.amb(Arrays.asList(pp1.singleElement(), pp2.singleElement())) |
| 1793 |
.to(TestHelper.<Integer>testConsumer()); |
| 1794 |
|
| 1795 |
to.assertEmpty(); |
| 1796 |
|
| 1797 |
assertTrue(pp1.hasSubscribers()); |
| 1798 |
assertTrue(pp2.hasSubscribers()); |
| 1799 |
|
| 1800 |
pp2.onError(new TestException("2")); |
| 1801 |
pp1.onError(new TestException("1")); |
| 1802 |
|
| 1803 |
assertFalse(pp1.hasSubscribers()); |
| 1804 |
assertFalse(pp2.hasSubscribers()); |
| 1805 |
|
| 1806 |
to.assertFailureAndMessage(TestException.class, "2"); |
| 1807 |
} |
| 1808 |
|
| 1809 |
@Test |
| 1810 |
public void ambIterable1SignalsComplete() { |
| 1811 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1812 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1813 |
|
| 1814 |
TestObserver<Integer> to = Maybe.amb(Arrays.asList(pp1.singleElement(), pp2.singleElement())) |
| 1815 |
.test(); |
| 1816 |
|
| 1817 |
to.assertEmpty(); |
| 1818 |
|
| 1819 |
assertTrue(pp1.hasSubscribers()); |
| 1820 |
assertTrue(pp2.hasSubscribers()); |
| 1821 |
|
| 1822 |
pp1.onComplete(); |
| 1823 |
|
| 1824 |
assertFalse(pp1.hasSubscribers()); |
| 1825 |
assertFalse(pp2.hasSubscribers()); |
| 1826 |
|
| 1827 |
to.assertResult(); |
| 1828 |
} |
| 1829 |
|
| 1830 |
@Test |
| 1831 |
public void ambIterable2SignalsComplete() { |
| 1832 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1833 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1834 |
|
| 1835 |
TestObserver<Integer> to = Maybe.amb(Arrays.asList(pp1.singleElement(), pp2.singleElement())) |
| 1836 |
.test(); |
| 1837 |
|
| 1838 |
to.assertEmpty(); |
| 1839 |
|
| 1840 |
assertTrue(pp1.hasSubscribers()); |
| 1841 |
assertTrue(pp2.hasSubscribers()); |
| 1842 |
|
| 1843 |
pp2.onComplete(); |
| 1844 |
|
| 1845 |
assertFalse(pp1.hasSubscribers()); |
| 1846 |
assertFalse(pp2.hasSubscribers()); |
| 1847 |
|
| 1848 |
to.assertResult(); |
| 1849 |
} |
| 1850 |
|
| 1851 |
@Test |
| 1852 |
public void ambIterableIteratorNull() { |
| 1853 |
Maybe.amb(new Iterable<Maybe<Object>>() { |
| 1854 |
@Override |
| 1855 |
public Iterator<Maybe<Object>> iterator() { |
| 1856 |
return null; |
| 1857 |
} |
| 1858 |
}).test().assertError(NullPointerException.class); |
| 1859 |
} |
| 1860 |
|
| 1861 |
@Test |
| 1862 |
public void ambIterableOneIsNull() { |
| 1863 |
Maybe.amb(Arrays.asList(null, Maybe.just(1))) |
| 1864 |
.test() |
| 1865 |
.assertError(NullPointerException.class); |
| 1866 |
} |
| 1867 |
|
| 1868 |
@Test |
| 1869 |
public void ambIterableEmpty() { |
| 1870 |
Maybe.amb(Collections.<Maybe<Integer>>emptyList()).test().assertResult(); |
| 1871 |
} |
| 1872 |
|
| 1873 |
@Test |
| 1874 |
public void ambIterableOne() { |
| 1875 |
Maybe.amb(Collections.singleton(Maybe.just(1))).test().assertResult(1); |
| 1876 |
} |
| 1877 |
|
| 1878 |
@Test |
| 1879 |
public void mergeArray() { |
| 1880 |
Maybe.mergeArray(Maybe.just(1), Maybe.just(2), Maybe.just(3)) |
| 1881 |
.test() |
| 1882 |
.assertResult(1, 2, 3); |
| 1883 |
} |
| 1884 |
|
| 1885 |
@Test |
| 1886 |
public void merge2() { |
| 1887 |
Maybe.merge(Maybe.just(1), Maybe.just(2)) |
| 1888 |
.test() |
| 1889 |
.assertResult(1, 2); |
| 1890 |
} |
| 1891 |
|
| 1892 |
@Test |
| 1893 |
public void merge3() { |
| 1894 |
Maybe.merge(Maybe.just(1), Maybe.just(2), Maybe.just(3)) |
| 1895 |
.test() |
| 1896 |
.assertResult(1, 2, 3); |
| 1897 |
} |
| 1898 |
|
| 1899 |
@Test |
| 1900 |
public void merge4() { |
| 1901 |
Maybe.merge(Maybe.just(1), Maybe.just(2), Maybe.just(3), Maybe.just(4)) |
| 1902 |
.test() |
| 1903 |
.assertResult(1, 2, 3, 4); |
| 1904 |
} |
| 1905 |
|
| 1906 |
@Test |
| 1907 |
public void merge4Take2() { |
| 1908 |
Maybe.merge(Maybe.just(1), Maybe.just(2), Maybe.just(3), Maybe.just(4)) |
| 1909 |
.take(2) |
| 1910 |
.test() |
| 1911 |
.assertResult(1, 2); |
| 1912 |
} |
| 1913 |
|
| 1914 |
@Test |
| 1915 |
public void mergeArrayBackpressured() { |
| 1916 |
TestSubscriber<Integer> ts = Maybe.mergeArray(Maybe.just(1), Maybe.just(2), Maybe.just(3)) |
| 1917 |
.test(0L); |
| 1918 |
|
| 1919 |
ts.assertEmpty(); |
| 1920 |
|
| 1921 |
ts.request(1); |
| 1922 |
|
| 1923 |
ts.assertValue(1); |
| 1924 |
|
| 1925 |
ts.request(1); |
| 1926 |
|
| 1927 |
ts.assertValues(1, 2); |
| 1928 |
|
| 1929 |
ts.request(1); |
| 1930 |
ts.assertResult(1, 2, 3); |
| 1931 |
} |
| 1932 |
|
| 1933 |
@Test |
| 1934 |
public void mergeArrayBackpressuredMixed1() { |
| 1935 |
TestSubscriber<Integer> ts = Maybe.mergeArray(Maybe.just(1), Maybe.<Integer>empty(), Maybe.just(3)) |
| 1936 |
.test(0L); |
| 1937 |
|
| 1938 |
ts.assertEmpty(); |
| 1939 |
|
| 1940 |
ts.request(1); |
| 1941 |
|
| 1942 |
ts.assertValue(1); |
| 1943 |
|
| 1944 |
ts.request(1); |
| 1945 |
|
| 1946 |
ts.assertResult(1, 3); |
| 1947 |
} |
| 1948 |
|
| 1949 |
@Test |
| 1950 |
public void mergeArrayBackpressuredMixed2() { |
| 1951 |
TestSubscriber<Integer> ts = Maybe.mergeArray(Maybe.just(1), Maybe.just(2), Maybe.<Integer>empty()) |
| 1952 |
.test(0L); |
| 1953 |
|
| 1954 |
ts.assertEmpty(); |
| 1955 |
|
| 1956 |
ts.request(1); |
| 1957 |
|
| 1958 |
ts.assertValue(1); |
| 1959 |
|
| 1960 |
ts.request(1); |
| 1961 |
|
| 1962 |
ts.assertResult(1, 2); |
| 1963 |
} |
| 1964 |
|
| 1965 |
@Test |
| 1966 |
public void mergeArrayBackpressuredMixed3() { |
| 1967 |
TestSubscriber<Integer> ts = Maybe.mergeArray(Maybe.<Integer>empty(), Maybe.just(2), Maybe.just(3)) |
| 1968 |
.test(0L); |
| 1969 |
|
| 1970 |
ts.assertEmpty(); |
| 1971 |
|
| 1972 |
ts.request(1); |
| 1973 |
|
| 1974 |
ts.assertValue(2); |
| 1975 |
|
| 1976 |
ts.request(1); |
| 1977 |
|
| 1978 |
ts.assertResult(2, 3); |
| 1979 |
} |
| 1980 |
|
| 1981 |
@Test |
| 1982 |
public void mergeArrayFused() { |
| 1983 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 1984 |
|
| 1985 |
Maybe.mergeArray(Maybe.just(1), Maybe.just(2), Maybe.just(3)).subscribe(ts); |
| 1986 |
|
| 1987 |
ts.assertSubscribed() |
| 1988 |
.assertFuseable() |
| 1989 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 1990 |
.assertResult(1, 2, 3); |
| 1991 |
} |
| 1992 |
|
| 1993 |
@Test |
| 1994 |
public void mergeArrayFusedRace() { |
| 1995 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1996 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1997 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1998 |
|
| 1999 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 2000 |
|
| 2001 |
Maybe.mergeArray(pp1.singleElement(), pp2.singleElement()).subscribe(ts); |
| 2002 |
|
| 2003 |
ts.assertSubscribed() |
| 2004 |
.assertFuseable() |
| 2005 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 2006 |
; |
| 2007 |
|
| 2008 |
TestHelper.race(new Runnable() { |
| 2009 |
@Override |
| 2010 |
public void run() { |
| 2011 |
pp1.onNext(1); |
| 2012 |
pp1.onComplete(); |
| 2013 |
} |
| 2014 |
}, new Runnable() { |
| 2015 |
@Override |
| 2016 |
public void run() { |
| 2017 |
pp2.onNext(1); |
| 2018 |
pp2.onComplete(); |
| 2019 |
} |
| 2020 |
}); |
| 2021 |
|
| 2022 |
ts |
| 2023 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2024 |
.assertResult(1, 1); |
| 2025 |
} |
| 2026 |
} |
| 2027 |
|
| 2028 |
@Test |
| 2029 |
public void mergeArrayZero() { |
| 2030 |
assertSame(Flowable.empty(), Maybe.mergeArray()); |
| 2031 |
} |
| 2032 |
|
| 2033 |
@Test |
| 2034 |
public void mergeArrayOne() { |
| 2035 |
Maybe.mergeArray(Maybe.just(1)).test().assertResult(1); |
| 2036 |
} |
| 2037 |
|
| 2038 |
@Test |
| 2039 |
public void mergePublisher() { |
| 2040 |
Maybe.merge(Flowable.just(Maybe.just(1), Maybe.just(2), Maybe.just(3))) |
| 2041 |
.test() |
| 2042 |
.assertResult(1, 2, 3); |
| 2043 |
} |
| 2044 |
|
| 2045 |
@Test |
| 2046 |
public void mergePublisherMaxConcurrent() { |
| 2047 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 2048 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 2049 |
|
| 2050 |
TestSubscriber<Integer> ts = Maybe.merge(Flowable.just(pp1.singleElement(), pp2.singleElement()), 1).test(0L); |
| 2051 |
|
| 2052 |
assertTrue(pp1.hasSubscribers()); |
| 2053 |
assertFalse(pp2.hasSubscribers()); |
| 2054 |
|
| 2055 |
pp1.onNext(1); |
| 2056 |
pp1.onComplete(); |
| 2057 |
|
| 2058 |
ts.request(1); |
| 2059 |
|
| 2060 |
assertFalse(pp1.hasSubscribers()); |
| 2061 |
assertTrue(pp2.hasSubscribers()); |
| 2062 |
} |
| 2063 |
|
| 2064 |
@Test |
| 2065 |
public void mergeMaybe() { |
| 2066 |
Maybe.merge(Maybe.just(Maybe.just(1))) |
| 2067 |
.test() |
| 2068 |
.assertResult(1); |
| 2069 |
} |
| 2070 |
|
| 2071 |
@Test |
| 2072 |
public void mergeIterable() { |
| 2073 |
Maybe.merge(Arrays.asList(Maybe.just(1), Maybe.just(2), Maybe.just(3))) |
| 2074 |
.test() |
| 2075 |
.assertResult(1, 2, 3); |
| 2076 |
} |
| 2077 |
|
| 2078 |
@Test |
| 2079 |
public void mergeALot() { |
| 2080 |
@SuppressWarnings("unchecked") |
| 2081 |
Maybe<Integer>[] sources = new Maybe[Flowable.bufferSize() * 2]; |
| 2082 |
Arrays.fill(sources, Maybe.just(1)); |
| 2083 |
|
| 2084 |
Maybe.mergeArray(sources) |
| 2085 |
.to(TestHelper.<Integer>testConsumer()) |
| 2086 |
.assertSubscribed() |
| 2087 |
.assertValueCount(sources.length) |
| 2088 |
.assertNoErrors() |
| 2089 |
.assertComplete(); |
| 2090 |
} |
| 2091 |
|
| 2092 |
@Test |
| 2093 |
public void mergeALotLastEmpty() { |
| 2094 |
@SuppressWarnings("unchecked") |
| 2095 |
Maybe<Integer>[] sources = new Maybe[Flowable.bufferSize() * 2]; |
| 2096 |
Arrays.fill(sources, Maybe.just(1)); |
| 2097 |
sources[sources.length - 1] = Maybe.empty(); |
| 2098 |
|
| 2099 |
Maybe.mergeArray(sources) |
| 2100 |
.to(TestHelper.<Integer>testConsumer()) |
| 2101 |
.assertSubscribed() |
| 2102 |
.assertValueCount(sources.length - 1) |
| 2103 |
.assertNoErrors() |
| 2104 |
.assertComplete(); |
| 2105 |
} |
| 2106 |
|
| 2107 |
@Test |
| 2108 |
public void mergeALotFused() { |
| 2109 |
@SuppressWarnings("unchecked") |
| 2110 |
Maybe<Integer>[] sources = new Maybe[Flowable.bufferSize() * 2]; |
| 2111 |
Arrays.fill(sources, Maybe.just(1)); |
| 2112 |
|
| 2113 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 2114 |
|
| 2115 |
Maybe.mergeArray(sources).subscribe(ts); |
| 2116 |
|
| 2117 |
ts |
| 2118 |
.assertSubscribed() |
| 2119 |
.assertFuseable() |
| 2120 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 2121 |
.assertValueCount(sources.length) |
| 2122 |
.assertNoErrors() |
| 2123 |
.assertComplete(); |
| 2124 |
} |
| 2125 |
|
| 2126 |
@Test |
| 2127 |
public void mergeErrorSuccess() { |
| 2128 |
Maybe.merge(Maybe.error(new TestException()), Maybe.just(1)) |
| 2129 |
.test() |
| 2130 |
.assertFailure(TestException.class); |
| 2131 |
} |
| 2132 |
|
| 2133 |
@Test |
| 2134 |
public void mergeSuccessError() { |
| 2135 |
Maybe.merge(Maybe.just(1), Maybe.error(new TestException())) |
| 2136 |
.test() |
| 2137 |
.assertFailure(TestException.class, 1); |
| 2138 |
} |
| 2139 |
|
| 2140 |
@Test |
| 2141 |
public void subscribeZero() { |
| 2142 |
assertTrue(Maybe.just(1) |
| 2143 |
.subscribe().isDisposed()); |
| 2144 |
} |
| 2145 |
|
| 2146 |
@Test |
| 2147 |
public void subscribeZeroError() { |
| 2148 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 2149 |
|
| 2150 |
try { |
| 2151 |
assertTrue(Maybe.error(new TestException()) |
| 2152 |
.subscribe().isDisposed()); |
| 2153 |
|
| 2154 |
TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class); |
| 2155 |
Throwable c = errors.get(0).getCause(); |
| 2156 |
assertTrue("" + c, c instanceof TestException); |
| 2157 |
} finally { |
| 2158 |
RxJavaPlugins.reset(); |
| 2159 |
} |
| 2160 |
} |
| 2161 |
|
| 2162 |
@Test |
| 2163 |
public void subscribeToOnSuccess() { |
| 2164 |
final List<Integer> values = new ArrayList<>(); |
| 2165 |
|
| 2166 |
Consumer<Integer> onSuccess = new Consumer<Integer>() { |
| 2167 |
@Override |
| 2168 |
public void accept(Integer e) throws Exception { |
| 2169 |
values.add(e); |
| 2170 |
} |
| 2171 |
}; |
| 2172 |
|
| 2173 |
Maybe<Integer> source = Maybe.just(1); |
| 2174 |
|
| 2175 |
source.subscribe(onSuccess); |
| 2176 |
source.subscribe(onSuccess, Functions.emptyConsumer()); |
| 2177 |
source.subscribe(onSuccess, Functions.emptyConsumer(), Functions.EMPTY_ACTION); |
| 2178 |
|
| 2179 |
assertEquals(Arrays.asList(1, 1, 1), values); |
| 2180 |
} |
| 2181 |
|
| 2182 |
@Test |
| 2183 |
public void subscribeToOnError() { |
| 2184 |
final List<Throwable> values = new ArrayList<>(); |
| 2185 |
|
| 2186 |
Consumer<Throwable> onError = new Consumer<Throwable>() { |
| 2187 |
@Override |
| 2188 |
public void accept(Throwable e) throws Exception { |
| 2189 |
values.add(e); |
| 2190 |
} |
| 2191 |
}; |
| 2192 |
|
| 2193 |
TestException ex = new TestException(); |
| 2194 |
|
| 2195 |
Maybe<Integer> source = Maybe.error(ex); |
| 2196 |
|
| 2197 |
source.subscribe(Functions.emptyConsumer(), onError); |
| 2198 |
source.subscribe(Functions.emptyConsumer(), onError, Functions.EMPTY_ACTION); |
| 2199 |
|
| 2200 |
assertEquals(Arrays.asList(ex, ex), values); |
| 2201 |
} |
| 2202 |
|
| 2203 |
@Test |
| 2204 |
public void subscribeToOnComplete() { |
| 2205 |
final List<Integer> values = new ArrayList<>(); |
| 2206 |
|
| 2207 |
Action onComplete = new Action() { |
| 2208 |
@Override |
| 2209 |
public void run() throws Exception { |
| 2210 |
values.add(100); |
| 2211 |
} |
| 2212 |
}; |
| 2213 |
|
| 2214 |
Maybe<Integer> source = Maybe.empty(); |
| 2215 |
|
| 2216 |
source.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer(), onComplete); |
| 2217 |
|
| 2218 |
assertEquals(Arrays.asList(100), values); |
| 2219 |
} |
| 2220 |
|
| 2221 |
@Test |
| 2222 |
public void subscribeWith() { |
| 2223 |
MaybeObserver<Integer> mo = new MaybeObserver<Integer>() { |
| 2224 |
|
| 2225 |
@Override |
| 2226 |
public void onSubscribe(Disposable d) { |
| 2227 |
|
| 2228 |
} |
| 2229 |
|
| 2230 |
@Override |
| 2231 |
public void onSuccess(Integer value) { |
| 2232 |
|
| 2233 |
} |
| 2234 |
|
| 2235 |
@Override |
| 2236 |
public void onError(Throwable e) { |
| 2237 |
|
| 2238 |
} |
| 2239 |
|
| 2240 |
@Override |
| 2241 |
public void onComplete() { |
| 2242 |
|
| 2243 |
} |
| 2244 |
|
| 2245 |
}; |
| 2246 |
|
| 2247 |
assertSame(mo, Maybe.just(1).subscribeWith(mo)); |
| 2248 |
} |
| 2249 |
|
| 2250 |
@Test |
| 2251 |
public void doOnEventSuccess() { |
| 2252 |
final List<Object> list = new ArrayList<>(); |
| 2253 |
|
| 2254 |
assertTrue(Maybe.just(1) |
| 2255 |
.doOnEvent(new BiConsumer<Integer, Throwable>() { |
| 2256 |
@Override |
| 2257 |
public void accept(Integer v, Throwable e) throws Exception { |
| 2258 |
list.add(v); |
| 2259 |
list.add(e); |
| 2260 |
} |
| 2261 |
}) |
| 2262 |
.subscribe().isDisposed()); |
| 2263 |
|
| 2264 |
assertEquals(Arrays.asList(1, null), list); |
| 2265 |
} |
| 2266 |
|
| 2267 |
@Test |
| 2268 |
public void doOnEventError() { |
| 2269 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 2270 |
try { |
| 2271 |
final List<Object> list = new ArrayList<>(); |
| 2272 |
|
| 2273 |
TestException ex = new TestException(); |
| 2274 |
|
| 2275 |
assertTrue(Maybe.<Integer>error(ex) |
| 2276 |
.doOnEvent(new BiConsumer<Integer, Throwable>() { |
| 2277 |
@Override |
| 2278 |
public void accept(Integer v, Throwable e) throws Exception { |
| 2279 |
list.add(v); |
| 2280 |
list.add(e); |
| 2281 |
} |
| 2282 |
}) |
| 2283 |
.subscribe().isDisposed()); |
| 2284 |
|
| 2285 |
assertEquals(Arrays.asList(null, ex), list); |
| 2286 |
|
| 2287 |
TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class); |
| 2288 |
} finally { |
| 2289 |
RxJavaPlugins.reset(); |
| 2290 |
} |
| 2291 |
} |
| 2292 |
|
| 2293 |
@Test |
| 2294 |
public void doOnEventComplete() { |
| 2295 |
final List<Object> list = new ArrayList<>(); |
| 2296 |
|
| 2297 |
assertTrue(Maybe.<Integer>empty() |
| 2298 |
.doOnEvent(new BiConsumer<Integer, Throwable>() { |
| 2299 |
@Override |
| 2300 |
public void accept(Integer v, Throwable e) throws Exception { |
| 2301 |
list.add(v); |
| 2302 |
list.add(e); |
| 2303 |
} |
| 2304 |
}) |
| 2305 |
.subscribe().isDisposed()); |
| 2306 |
|
| 2307 |
assertEquals(Arrays.asList(null, null), list); |
| 2308 |
} |
| 2309 |
|
| 2310 |
@Test |
| 2311 |
public void doOnEventSuccessThrows() { |
| 2312 |
Maybe.just(1) |
| 2313 |
.doOnEvent(new BiConsumer<Integer, Throwable>() { |
| 2314 |
@Override |
| 2315 |
public void accept(Integer v, Throwable e) throws Exception { |
| 2316 |
throw new TestException(); |
| 2317 |
} |
| 2318 |
}) |
| 2319 |
.test() |
| 2320 |
.assertFailure(TestException.class); |
| 2321 |
} |
| 2322 |
|
| 2323 |
@Test |
| 2324 |
public void doOnEventErrorThrows() { |
| 2325 |
TestObserverEx<Integer> to = Maybe.<Integer>error(new TestException("Outer")) |
| 2326 |
.doOnEvent(new BiConsumer<Integer, Throwable>() { |
| 2327 |
@Override |
| 2328 |
public void accept(Integer v, Throwable e) throws Exception { |
| 2329 |
throw new TestException("Inner"); |
| 2330 |
} |
| 2331 |
}) |
| 2332 |
.to(TestHelper.<Integer>testConsumer()) |
| 2333 |
.assertFailure(CompositeException.class); |
| 2334 |
|
| 2335 |
List<Throwable> list = TestHelper.compositeList(to.errors().get(0)); |
| 2336 |
TestHelper.assertError(list, 0, TestException.class, "Outer"); |
| 2337 |
TestHelper.assertError(list, 1, TestException.class, "Inner"); |
| 2338 |
assertEquals(2, list.size()); |
| 2339 |
} |
| 2340 |
|
| 2341 |
@Test |
| 2342 |
public void doOnEventCompleteThrows() { |
| 2343 |
Maybe.<Integer>empty() |
| 2344 |
.doOnEvent(new BiConsumer<Integer, Throwable>() { |
| 2345 |
@Override |
| 2346 |
public void accept(Integer v, Throwable e) throws Exception { |
| 2347 |
throw new TestException(); |
| 2348 |
} |
| 2349 |
}) |
| 2350 |
.test() |
| 2351 |
.assertFailure(TestException.class); |
| 2352 |
} |
| 2353 |
|
| 2354 |
@Test |
| 2355 |
public void concatArrayDelayError() { |
| 2356 |
Maybe.concatArrayDelayError(Maybe.empty(), Maybe.just(1), Maybe.error(new TestException())) |
| 2357 |
.test() |
| 2358 |
.assertFailure(TestException.class, 1); |
| 2359 |
|
| 2360 |
Maybe.concatArrayDelayError(Maybe.error(new TestException()), Maybe.empty(), Maybe.just(1)) |
| 2361 |
.test() |
| 2362 |
.assertFailure(TestException.class, 1); |
| 2363 |
|
| 2364 |
assertSame(Flowable.empty(), Maybe.concatArrayDelayError()); |
| 2365 |
|
| 2366 |
assertFalse(Maybe.concatArrayDelayError(Maybe.never()) instanceof MaybeConcatArrayDelayError); |
| 2367 |
} |
| 2368 |
|
| 2369 |
@Test |
| 2370 |
public void concatIterableDelayError() { |
| 2371 |
Maybe.concatDelayError(Arrays.asList(Maybe.empty(), Maybe.just(1), Maybe.error(new TestException()))) |
| 2372 |
.test() |
| 2373 |
.assertFailure(TestException.class, 1); |
| 2374 |
|
| 2375 |
Maybe.concatDelayError(Arrays.asList(Maybe.error(new TestException()), Maybe.empty(), Maybe.just(1))) |
| 2376 |
.test() |
| 2377 |
.assertFailure(TestException.class, 1); |
| 2378 |
} |
| 2379 |
|
| 2380 |
@Test |
| 2381 |
public void concatPublisherDelayError() { |
| 2382 |
Maybe.concatDelayError(Flowable.just(Maybe.empty(), Maybe.just(1), Maybe.error(new TestException()))) |
| 2383 |
.test() |
| 2384 |
.assertFailure(TestException.class, 1); |
| 2385 |
|
| 2386 |
Maybe.concatDelayError(Flowable.just(Maybe.error(new TestException()), Maybe.empty(), Maybe.just(1))) |
| 2387 |
.test() |
| 2388 |
.assertFailure(TestException.class, 1); |
| 2389 |
} |
| 2390 |
|
| 2391 |
@Test |
| 2392 |
public void concatPublisherDelayErrorPrefetch() { |
| 2393 |
Maybe.concatDelayError(Flowable.just(Maybe.empty(), Maybe.just(1), Maybe.error(new TestException())), 1) |
| 2394 |
.test() |
| 2395 |
.assertFailure(TestException.class, 1); |
| 2396 |
|
| 2397 |
Maybe.concatDelayError(Flowable.just(Maybe.error(new TestException()), Maybe.empty(), Maybe.just(1)), 1) |
| 2398 |
.test() |
| 2399 |
.assertFailure(TestException.class, 1); |
| 2400 |
} |
| 2401 |
|
| 2402 |
@Test |
| 2403 |
public void concatEagerArray() { |
| 2404 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 2405 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 2406 |
|
| 2407 |
TestSubscriber<Integer> ts = Maybe.concatArrayEager(pp1.singleElement(), pp2.singleElement()).test(); |
| 2408 |
|
| 2409 |
assertTrue(pp1.hasSubscribers()); |
| 2410 |
assertTrue(pp2.hasSubscribers()); |
| 2411 |
|
| 2412 |
pp2.onNext(2); |
| 2413 |
pp2.onComplete(); |
| 2414 |
|
| 2415 |
ts.assertEmpty(); |
| 2416 |
|
| 2417 |
pp1.onNext(1); |
| 2418 |
pp1.onComplete(); |
| 2419 |
|
| 2420 |
ts.assertResult(1, 2); |
| 2421 |
|
| 2422 |
} |
| 2423 |
|
| 2424 |
@Test |
| 2425 |
public void concatEagerIterable() { |
| 2426 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 2427 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 2428 |
|
| 2429 |
TestSubscriber<Integer> ts = Maybe.concatEager(Arrays.asList(pp1.singleElement(), pp2.singleElement())).test(); |
| 2430 |
|
| 2431 |
assertTrue(pp1.hasSubscribers()); |
| 2432 |
assertTrue(pp2.hasSubscribers()); |
| 2433 |
|
| 2434 |
pp2.onNext(2); |
| 2435 |
pp2.onComplete(); |
| 2436 |
|
| 2437 |
ts.assertEmpty(); |
| 2438 |
|
| 2439 |
pp1.onNext(1); |
| 2440 |
pp1.onComplete(); |
| 2441 |
|
| 2442 |
ts.assertResult(1, 2); |
| 2443 |
|
| 2444 |
} |
| 2445 |
|
| 2446 |
@Test |
| 2447 |
public void concatEagerPublisher() { |
| 2448 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 2449 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 2450 |
|
| 2451 |
TestSubscriber<Integer> ts = Maybe.concatEager(Flowable.just(pp1.singleElement(), pp2.singleElement())).test(); |
| 2452 |
|
| 2453 |
assertTrue(pp1.hasSubscribers()); |
| 2454 |
assertTrue(pp2.hasSubscribers()); |
| 2455 |
|
| 2456 |
pp2.onNext(2); |
| 2457 |
pp2.onComplete(); |
| 2458 |
|
| 2459 |
ts.assertEmpty(); |
| 2460 |
|
| 2461 |
pp1.onNext(1); |
| 2462 |
pp1.onComplete(); |
| 2463 |
|
| 2464 |
ts.assertResult(1, 2); |
| 2465 |
|
| 2466 |
} |
| 2467 |
|
| 2468 |
static Future<Integer> emptyFuture() { |
| 2469 |
final ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); |
| 2470 |
|
| 2471 |
return exec.schedule(new Callable<Integer>() { |
| 2472 |
|
| 2473 |
@Override |
| 2474 |
public Integer call() throws Exception { |
| 2475 |
exec.shutdown(); |
| 2476 |
return null; |
| 2477 |
} |
| 2478 |
}, 200, TimeUnit.MILLISECONDS); |
| 2479 |
} |
| 2480 |
|
| 2481 |
@Test |
| 2482 |
public void fromFuture() { |
| 2483 |
Maybe.fromFuture(Flowable.just(1).delay(200, TimeUnit.MILLISECONDS).toFuture()) |
| 2484 |
.test() |
| 2485 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2486 |
.assertResult(1) |
| 2487 |
; |
| 2488 |
|
| 2489 |
Maybe.fromFuture(emptyFuture()) |
| 2490 |
.test() |
| 2491 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2492 |
.assertResult() |
| 2493 |
; |
| 2494 |
|
| 2495 |
Maybe.fromFuture(Flowable.error(new TestException()).delay(200, TimeUnit.MILLISECONDS, true).toFuture()) |
| 2496 |
.test() |
| 2497 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2498 |
.assertFailure(TestException.class) |
| 2499 |
; |
| 2500 |
|
| 2501 |
Maybe.fromFuture(Flowable.empty().delay(10, TimeUnit.SECONDS).toFuture(), 100, TimeUnit.MILLISECONDS) |
| 2502 |
.test() |
| 2503 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2504 |
.assertFailure(TimeoutException.class) |
| 2505 |
; |
| 2506 |
} |
| 2507 |
|
| 2508 |
@Test |
| 2509 |
public void mergeArrayDelayError() { |
| 2510 |
Maybe.mergeArrayDelayError(Maybe.empty(), Maybe.just(1), Maybe.error(new TestException())) |
| 2511 |
.test() |
| 2512 |
.assertFailure(TestException.class, 1); |
| 2513 |
|
| 2514 |
Maybe.mergeArrayDelayError(Maybe.error(new TestException()), Maybe.empty(), Maybe.just(1)) |
| 2515 |
.test() |
| 2516 |
.assertFailure(TestException.class, 1); |
| 2517 |
} |
| 2518 |
|
| 2519 |
@Test |
| 2520 |
public void mergeIterableDelayError() { |
| 2521 |
Maybe.mergeDelayError(Arrays.asList(Maybe.empty(), Maybe.just(1), Maybe.error(new TestException()))) |
| 2522 |
.test() |
| 2523 |
.assertFailure(TestException.class, 1); |
| 2524 |
|
| 2525 |
Maybe.mergeDelayError(Arrays.asList(Maybe.error(new TestException()), Maybe.empty(), Maybe.just(1))) |
| 2526 |
.test() |
| 2527 |
.assertFailure(TestException.class, 1); |
| 2528 |
} |
| 2529 |
|
| 2530 |
@Test |
| 2531 |
public void mergePublisherDelayError() { |
| 2532 |
Maybe.mergeDelayError(Flowable.just(Maybe.empty(), Maybe.just(1), Maybe.error(new TestException()))) |
| 2533 |
.test() |
| 2534 |
.assertFailure(TestException.class, 1); |
| 2535 |
|
| 2536 |
Maybe.mergeDelayError(Flowable.just(Maybe.error(new TestException()), Maybe.empty(), Maybe.just(1))) |
| 2537 |
.test() |
| 2538 |
.assertFailure(TestException.class, 1); |
| 2539 |
} |
| 2540 |
|
| 2541 |
@Test |
| 2542 |
public void mergeDelayError2() { |
| 2543 |
Maybe.mergeDelayError(Maybe.just(1), Maybe.error(new TestException())) |
| 2544 |
.test() |
| 2545 |
.assertFailure(TestException.class, 1); |
| 2546 |
|
| 2547 |
Maybe.mergeDelayError(Maybe.error(new TestException()), Maybe.just(1)) |
| 2548 |
.test() |
| 2549 |
.assertFailure(TestException.class, 1); |
| 2550 |
} |
| 2551 |
|
| 2552 |
@Test |
| 2553 |
public void mergeDelayError3() { |
| 2554 |
Maybe.mergeDelayError(Maybe.just(1), Maybe.error(new TestException()), Maybe.just(2)) |
| 2555 |
.test() |
| 2556 |
.assertFailure(TestException.class, 1, 2); |
| 2557 |
|
| 2558 |
Maybe.mergeDelayError(Maybe.error(new TestException()), Maybe.just(1), Maybe.just(2)) |
| 2559 |
.test() |
| 2560 |
.assertFailure(TestException.class, 1, 2); |
| 2561 |
|
| 2562 |
Maybe.mergeDelayError(Maybe.just(1), Maybe.just(2), Maybe.error(new TestException())) |
| 2563 |
.test() |
| 2564 |
.assertFailure(TestException.class, 1, 2); |
| 2565 |
} |
| 2566 |
|
| 2567 |
@Test |
| 2568 |
public void mergeDelayError4() { |
| 2569 |
Maybe.mergeDelayError(Maybe.just(1), Maybe.error(new TestException()), Maybe.just(2), Maybe.just(3)) |
| 2570 |
.test() |
| 2571 |
.assertFailure(TestException.class, 1, 2, 3); |
| 2572 |
|
| 2573 |
Maybe.mergeDelayError(Maybe.error(new TestException()), Maybe.just(1), Maybe.just(2), Maybe.just(3)) |
| 2574 |
.test() |
| 2575 |
.assertFailure(TestException.class, 1, 2, 3); |
| 2576 |
|
| 2577 |
Maybe.mergeDelayError(Maybe.just(1), Maybe.just(2), Maybe.just(3), Maybe.error(new TestException())) |
| 2578 |
.test() |
| 2579 |
.assertFailure(TestException.class, 1, 2, 3); |
| 2580 |
} |
| 2581 |
|
| 2582 |
@Test |
| 2583 |
public void sequenceEqual() { |
| 2584 |
Maybe.sequenceEqual(Maybe.just(1_000_000), Maybe.just(Integer.valueOf(1_000_000))).test().assertResult(true); |
| 2585 |
|
| 2586 |
Maybe.sequenceEqual(Maybe.just(1), Maybe.just(2)).test().assertResult(false); |
| 2587 |
|
| 2588 |
Maybe.sequenceEqual(Maybe.just(1), Maybe.empty()).test().assertResult(false); |
| 2589 |
|
| 2590 |
Maybe.sequenceEqual(Maybe.empty(), Maybe.just(2)).test().assertResult(false); |
| 2591 |
|
| 2592 |
Maybe.sequenceEqual(Maybe.empty(), Maybe.empty()).test().assertResult(true); |
| 2593 |
|
| 2594 |
Maybe.sequenceEqual(Maybe.just(1), Maybe.error(new TestException())).test().assertFailure(TestException.class); |
| 2595 |
|
| 2596 |
Maybe.sequenceEqual(Maybe.error(new TestException()), Maybe.just(1)).test().assertFailure(TestException.class); |
| 2597 |
|
| 2598 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 2599 |
|
| 2600 |
try { |
| 2601 |
Maybe.sequenceEqual(Maybe.error(new TestException("One")), Maybe.error(new TestException("Two"))) |
| 2602 |
.to(TestHelper.<Boolean>testConsumer()) |
| 2603 |
.assertFailureAndMessage(TestException.class, "One"); |
| 2604 |
|
| 2605 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "Two"); |
| 2606 |
} finally { |
| 2607 |
RxJavaPlugins.reset(); |
| 2608 |
} |
| 2609 |
|
| 2610 |
Maybe.sequenceEqual(Maybe.just(1), Maybe.error(new TestException()), new BiPredicate<Object, Object>() { |
| 2611 |
@Override |
| 2612 |
public boolean test(Object t1, Object t2) throws Exception { |
| 2613 |
throw new TestException(); |
| 2614 |
} |
| 2615 |
}) |
| 2616 |
.test().assertFailure(TestException.class); |
| 2617 |
} |
| 2618 |
|
| 2619 |
@Test |
| 2620 |
public void timer() { |
| 2621 |
Maybe.timer(100, TimeUnit.MILLISECONDS) |
| 2622 |
.test() |
| 2623 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2624 |
.assertResult(0L); |
| 2625 |
} |
| 2626 |
|
| 2627 |
@Test |
| 2628 |
public void blockingGet() { |
| 2629 |
assertEquals(1, Maybe.just(1).blockingGet().intValue()); |
| 2630 |
|
| 2631 |
assertEquals(100, Maybe.empty().blockingGet(100)); |
| 2632 |
|
| 2633 |
try { |
| 2634 |
Maybe.error(new TestException()).blockingGet(); |
| 2635 |
fail("Should have thrown!"); |
| 2636 |
} catch (TestException ex) { |
| 2637 |
// expected |
| 2638 |
} |
| 2639 |
|
| 2640 |
try { |
| 2641 |
Maybe.error(new TestException()).blockingGet(100); |
| 2642 |
fail("Should have thrown!"); |
| 2643 |
} catch (TestException ex) { |
| 2644 |
// expected |
| 2645 |
} |
| 2646 |
} |
| 2647 |
|
| 2648 |
@Test |
| 2649 |
public void flatMapContinuation() { |
| 2650 |
Maybe.just(1).flatMapCompletable(new Function<Integer, Completable>() { |
| 2651 |
@Override |
| 2652 |
public Completable apply(Integer v) throws Exception { |
| 2653 |
return Completable.complete(); |
| 2654 |
} |
| 2655 |
}) |
| 2656 |
.test().assertResult(); |
| 2657 |
|
| 2658 |
Maybe.just(1).flatMapCompletable(new Function<Integer, Completable>() { |
| 2659 |
@Override |
| 2660 |
public Completable apply(Integer v) throws Exception { |
| 2661 |
return Completable.error(new TestException()); |
| 2662 |
} |
| 2663 |
}) |
| 2664 |
.test().assertFailure(TestException.class); |
| 2665 |
|
| 2666 |
Maybe.just(1).flatMapPublisher(new Function<Integer, Publisher<Integer>>() { |
| 2667 |
@Override |
| 2668 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 2669 |
return Flowable.range(1, 5); |
| 2670 |
} |
| 2671 |
}) |
| 2672 |
.test().assertResult(1, 2, 3, 4, 5); |
| 2673 |
|
| 2674 |
Maybe.just(1).flatMapPublisher(new Function<Integer, Publisher<Integer>>() { |
| 2675 |
@Override |
| 2676 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 2677 |
return Flowable.error(new TestException()); |
| 2678 |
} |
| 2679 |
}) |
| 2680 |
.test().assertFailure(TestException.class); |
| 2681 |
|
| 2682 |
Maybe.just(1).flatMapObservable(new Function<Integer, Observable<Integer>>() { |
| 2683 |
@Override |
| 2684 |
public Observable<Integer> apply(Integer v) throws Exception { |
| 2685 |
return Observable.range(1, 5); |
| 2686 |
} |
| 2687 |
}) |
| 2688 |
.test().assertResult(1, 2, 3, 4, 5); |
| 2689 |
|
| 2690 |
Maybe.just(1).flatMapObservable(new Function<Integer, Observable<Integer>>() { |
| 2691 |
@Override |
| 2692 |
public Observable<Integer> apply(Integer v) throws Exception { |
| 2693 |
return Observable.error(new TestException()); |
| 2694 |
} |
| 2695 |
}) |
| 2696 |
.test().assertFailure(TestException.class); |
| 2697 |
} |
| 2698 |
|
| 2699 |
@Test |
| 2700 |
public void using() { |
| 2701 |
final AtomicInteger disposeCount = new AtomicInteger(); |
| 2702 |
|
| 2703 |
Maybe.using(Functions.justSupplier(1), new Function<Integer, MaybeSource<Integer>>() { |
| 2704 |
@Override |
| 2705 |
public MaybeSource<Integer> apply(Integer v) throws Exception { |
| 2706 |
return Maybe.just(v); |
| 2707 |
} |
| 2708 |
}, new Consumer<Integer>() { |
| 2709 |
@Override |
| 2710 |
public void accept(Integer d) throws Exception { |
| 2711 |
disposeCount.set(d); |
| 2712 |
} |
| 2713 |
}) |
| 2714 |
.map(new Function<Integer, Object>() { |
| 2715 |
@Override |
| 2716 |
public String apply(Integer v) throws Exception { |
| 2717 |
return "" + disposeCount.get() + v * 10; |
| 2718 |
} |
| 2719 |
}) |
| 2720 |
.test() |
| 2721 |
.assertResult("110"); |
| 2722 |
} |
| 2723 |
|
| 2724 |
@Test |
| 2725 |
public void usingNonEager() { |
| 2726 |
final AtomicInteger disposeCount = new AtomicInteger(); |
| 2727 |
|
| 2728 |
Maybe.using(Functions.justSupplier(1), new Function<Integer, MaybeSource<Integer>>() { |
| 2729 |
@Override |
| 2730 |
public MaybeSource<Integer> apply(Integer v) throws Exception { |
| 2731 |
return Maybe.just(v); |
| 2732 |
} |
| 2733 |
}, new Consumer<Integer>() { |
| 2734 |
@Override |
| 2735 |
public void accept(Integer d) throws Exception { |
| 2736 |
disposeCount.set(d); |
| 2737 |
} |
| 2738 |
}, false) |
| 2739 |
.map(new Function<Integer, Object>() { |
| 2740 |
@Override |
| 2741 |
public String apply(Integer v) throws Exception { |
| 2742 |
return "" + disposeCount.get() + v * 10; |
| 2743 |
} |
| 2744 |
}) |
| 2745 |
.test() |
| 2746 |
.assertResult("010"); |
| 2747 |
|
| 2748 |
assertEquals(1, disposeCount.get()); |
| 2749 |
} |
| 2750 |
|
| 2751 |
Function<Object[], String> arrayToString = new Function<Object[], String>() { |
| 2752 |
@Override |
| 2753 |
public String apply(Object[] a) throws Exception { |
| 2754 |
return Arrays.toString(a); |
| 2755 |
} |
| 2756 |
}; |
| 2757 |
|
| 2758 |
@SuppressWarnings("unchecked") |
| 2759 |
@Test |
| 2760 |
public void zipArray() { |
| 2761 |
Maybe.zipArray(arrayToString, Maybe.just(1), Maybe.just(2)) |
| 2762 |
.test() |
| 2763 |
.assertResult("[1, 2]"); |
| 2764 |
|
| 2765 |
Maybe.zipArray(arrayToString, Maybe.just(1), Maybe.empty()) |
| 2766 |
.test() |
| 2767 |
.assertResult(); |
| 2768 |
|
| 2769 |
Maybe.zipArray(arrayToString, Maybe.just(1), Maybe.error(new TestException())) |
| 2770 |
.test() |
| 2771 |
.assertFailure(TestException.class); |
| 2772 |
|
| 2773 |
assertSame(Maybe.empty(), Maybe.zipArray(ArgsToString.INSTANCE)); |
| 2774 |
|
| 2775 |
Maybe.zipArray(arrayToString, Maybe.just(1)) |
| 2776 |
.test() |
| 2777 |
.assertResult("[1]"); |
| 2778 |
} |
| 2779 |
|
| 2780 |
@Test |
| 2781 |
public void zipIterable() { |
| 2782 |
Maybe.zip( |
| 2783 |
Arrays.asList(Maybe.just(1), Maybe.just(2)), |
| 2784 |
arrayToString) |
| 2785 |
.test() |
| 2786 |
.assertResult("[1, 2]"); |
| 2787 |
|
| 2788 |
Maybe.zip(Collections.<Maybe<Integer>>emptyList(), arrayToString) |
| 2789 |
.test() |
| 2790 |
.assertResult(); |
| 2791 |
|
| 2792 |
Maybe.zip(Collections.singletonList(Maybe.just(1)), arrayToString) |
| 2793 |
.test() |
| 2794 |
.assertResult("[1]"); |
| 2795 |
} |
| 2796 |
|
| 2797 |
@SuppressWarnings("unchecked") |
| 2798 |
@Test |
| 2799 |
public void zip2() { |
| 2800 |
Maybe.zip(Maybe.just(1), Maybe.just(2), ArgsToString.INSTANCE) |
| 2801 |
.test() |
| 2802 |
.assertResult("12"); |
| 2803 |
} |
| 2804 |
|
| 2805 |
@SuppressWarnings("unchecked") |
| 2806 |
@Test |
| 2807 |
public void zipWith() { |
| 2808 |
Maybe.just(1).zipWith(Maybe.just(2), ArgsToString.INSTANCE) |
| 2809 |
.test() |
| 2810 |
.assertResult("12"); |
| 2811 |
} |
| 2812 |
|
| 2813 |
@SuppressWarnings("unchecked") |
| 2814 |
@Test |
| 2815 |
public void zip3() { |
| 2816 |
Maybe.zip(Maybe.just(1), Maybe.just(2), Maybe.just(3), |
| 2817 |
ArgsToString.INSTANCE) |
| 2818 |
.test() |
| 2819 |
.assertResult("123"); |
| 2820 |
} |
| 2821 |
|
| 2822 |
@SuppressWarnings("unchecked") |
| 2823 |
@Test |
| 2824 |
public void zip4() { |
| 2825 |
Maybe.zip(Maybe.just(1), Maybe.just(2), Maybe.just(3), Maybe.just(4), |
| 2826 |
ArgsToString.INSTANCE) |
| 2827 |
.test() |
| 2828 |
.assertResult("1234"); |
| 2829 |
} |
| 2830 |
|
| 2831 |
@SuppressWarnings("unchecked") |
| 2832 |
@Test |
| 2833 |
public void zip5() { |
| 2834 |
Maybe.zip(Maybe.just(1), Maybe.just(2), Maybe.just(3), Maybe.just(4), |
| 2835 |
Maybe.just(5), |
| 2836 |
ArgsToString.INSTANCE) |
| 2837 |
.test() |
| 2838 |
.assertResult("12345"); |
| 2839 |
} |
| 2840 |
|
| 2841 |
@SuppressWarnings("unchecked") |
| 2842 |
@Test |
| 2843 |
public void zip6() { |
| 2844 |
Maybe.zip(Maybe.just(1), Maybe.just(2), Maybe.just(3), Maybe.just(4), |
| 2845 |
Maybe.just(5), Maybe.just(6), |
| 2846 |
ArgsToString.INSTANCE) |
| 2847 |
.test() |
| 2848 |
.assertResult("123456"); |
| 2849 |
} |
| 2850 |
|
| 2851 |
@SuppressWarnings("unchecked") |
| 2852 |
@Test |
| 2853 |
public void zip7() { |
| 2854 |
Maybe.zip(Maybe.just(1), Maybe.just(2), Maybe.just(3), Maybe.just(4), |
| 2855 |
Maybe.just(5), Maybe.just(6), Maybe.just(7), |
| 2856 |
ArgsToString.INSTANCE) |
| 2857 |
.test() |
| 2858 |
.assertResult("1234567"); |
| 2859 |
} |
| 2860 |
|
| 2861 |
@SuppressWarnings("unchecked") |
| 2862 |
@Test |
| 2863 |
public void zip8() { |
| 2864 |
Maybe.zip(Maybe.just(1), Maybe.just(2), Maybe.just(3), Maybe.just(4), |
| 2865 |
Maybe.just(5), Maybe.just(6), Maybe.just(7), Maybe.just(8), |
| 2866 |
ArgsToString.INSTANCE) |
| 2867 |
.test() |
| 2868 |
.assertResult("12345678"); |
| 2869 |
} |
| 2870 |
|
| 2871 |
@SuppressWarnings("unchecked") |
| 2872 |
@Test |
| 2873 |
public void zip9() { |
| 2874 |
Maybe.zip(Maybe.just(1), Maybe.just(2), Maybe.just(3), Maybe.just(4), |
| 2875 |
Maybe.just(5), Maybe.just(6), Maybe.just(7), Maybe.just(8), Maybe.just(9), |
| 2876 |
ArgsToString.INSTANCE) |
| 2877 |
.test() |
| 2878 |
.assertResult("123456789"); |
| 2879 |
} |
| 2880 |
|
| 2881 |
@Test |
| 2882 |
public void ambWith1SignalsSuccess() { |
| 2883 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 2884 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 2885 |
|
| 2886 |
TestObserver<Integer> to = pp1.singleElement().ambWith(pp2.singleElement()) |
| 2887 |
.test(); |
| 2888 |
|
| 2889 |
to.assertEmpty(); |
| 2890 |
|
| 2891 |
assertTrue(pp1.hasSubscribers()); |
| 2892 |
assertTrue(pp2.hasSubscribers()); |
| 2893 |
|
| 2894 |
pp1.onNext(1); |
| 2895 |
pp1.onComplete(); |
| 2896 |
|
| 2897 |
assertFalse(pp1.hasSubscribers()); |
| 2898 |
assertFalse(pp2.hasSubscribers()); |
| 2899 |
|
| 2900 |
to.assertResult(1); |
| 2901 |
} |
| 2902 |
|
| 2903 |
@Test |
| 2904 |
public void ambWith2SignalsSuccess() { |
| 2905 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 2906 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 2907 |
|
| 2908 |
TestObserver<Integer> to = pp1.singleElement().ambWith(pp2.singleElement()) |
| 2909 |
.test(); |
| 2910 |
|
| 2911 |
to.assertEmpty(); |
| 2912 |
|
| 2913 |
assertTrue(pp1.hasSubscribers()); |
| 2914 |
assertTrue(pp2.hasSubscribers()); |
| 2915 |
|
| 2916 |
pp2.onNext(2); |
| 2917 |
pp2.onComplete(); |
| 2918 |
|
| 2919 |
assertFalse(pp1.hasSubscribers()); |
| 2920 |
assertFalse(pp2.hasSubscribers()); |
| 2921 |
|
| 2922 |
to.assertResult(2); |
| 2923 |
} |
| 2924 |
|
| 2925 |
@Test |
| 2926 |
public void zipIterableObject() { |
| 2927 |
final List<Maybe<Integer>> maybes = Arrays.asList(Maybe.just(1), Maybe.just(4)); |
| 2928 |
Maybe.zip(maybes, new Function<Object[], Object>() { |
| 2929 |
@Override |
| 2930 |
public Object apply(final Object[] o) throws Exception { |
| 2931 |
int sum = 0; |
| 2932 |
for (Object i : o) { |
| 2933 |
sum += (Integer) i; |
| 2934 |
} |
| 2935 |
return sum; |
| 2936 |
} |
| 2937 |
}).test().assertResult(5); |
| 2938 |
} |
| 2939 |
|
| 2940 |
static long usedMemoryNow() { |
| 2941 |
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); |
| 2942 |
|
| 2943 |
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage(); |
| 2944 |
|
| 2945 |
return heapMemoryUsage.getUsed(); |
| 2946 |
} |
| 2947 |
|
| 2948 |
@Test |
| 2949 |
public void onTerminateDetach() throws Exception { |
| 2950 |
System.gc(); |
| 2951 |
|
| 2952 |
Thread.sleep(150); |
| 2953 |
|
| 2954 |
long before = usedMemoryNow(); |
| 2955 |
|
| 2956 |
Maybe<Object> source = Flowable.just((Object)new Object[10000000]).singleElement(); |
| 2957 |
|
| 2958 |
long middle = usedMemoryNow(); |
| 2959 |
|
| 2960 |
MaybeObserver<Object> observer = new MaybeObserver<Object>() { |
| 2961 |
@SuppressWarnings("unused") |
| 2962 |
Disposable u; |
| 2963 |
|
| 2964 |
@Override |
| 2965 |
public void onSubscribe(Disposable d) { |
| 2966 |
this.u = d; |
| 2967 |
} |
| 2968 |
|
| 2969 |
@Override |
| 2970 |
public void onSuccess(Object value) { |
| 2971 |
|
| 2972 |
} |
| 2973 |
|
| 2974 |
@Override |
| 2975 |
public void onError(Throwable e) { |
| 2976 |
|
| 2977 |
} |
| 2978 |
|
| 2979 |
@Override |
| 2980 |
public void onComplete() { |
| 2981 |
|
| 2982 |
} |
| 2983 |
|
| 2984 |
}; |
| 2985 |
source.onTerminateDetach().subscribe(observer); |
| 2986 |
|
| 2987 |
source = null; |
| 2988 |
|
| 2989 |
System.gc(); |
| 2990 |
|
| 2991 |
Thread.sleep(250); |
| 2992 |
|
| 2993 |
long after = usedMemoryNow(); |
| 2994 |
|
| 2995 |
String log = String.format("%.2f MB -> %.2f MB -> %.2f MB%n", |
| 2996 |
before / 1024.0 / 1024.0, |
| 2997 |
middle / 1024.0 / 1024.0, |
| 2998 |
after / 1024.0 / 1024.0); |
| 2999 |
|
| 3000 |
System.out.printf(log); |
| 3001 |
|
| 3002 |
if (before * 1.3 < after) { |
| 3003 |
fail("There seems to be a memory leak: " + log); |
| 3004 |
} |
| 3005 |
|
| 3006 |
assertNotNull(observer); // hold onto the reference to prevent premature GC |
| 3007 |
} |
| 3008 |
|
| 3009 |
@Test |
| 3010 |
public void repeat() { |
| 3011 |
Maybe.just(1).repeat().take(5).test().assertResult(1, 1, 1, 1, 1); |
| 3012 |
|
| 3013 |
Maybe.just(1).repeat(5).test().assertResult(1, 1, 1, 1, 1); |
| 3014 |
|
| 3015 |
Maybe.just(1).repeatUntil(new BooleanSupplier() { |
| 3016 |
@Override |
| 3017 |
public boolean getAsBoolean() throws Exception { |
| 3018 |
return false; |
| 3019 |
} |
| 3020 |
}).take(5).test().assertResult(1, 1, 1, 1, 1); |
| 3021 |
|
| 3022 |
Maybe.just(1).repeatWhen(new Function<Flowable<Object>, Publisher<Object>>() { |
| 3023 |
@Override |
| 3024 |
public Publisher<Object> apply(Flowable<Object> v) throws Exception { |
| 3025 |
return v; |
| 3026 |
} |
| 3027 |
}).take(5).test().assertResult(1, 1, 1, 1, 1); |
| 3028 |
} |
| 3029 |
|
| 3030 |
@Test |
| 3031 |
public void retry() { |
| 3032 |
Maybe.just(1).retry().test().assertResult(1); |
| 3033 |
|
| 3034 |
Maybe.just(1).retry(5).test().assertResult(1); |
| 3035 |
|
| 3036 |
Maybe.just(1).retry(Functions.alwaysTrue()).test().assertResult(1); |
| 3037 |
|
| 3038 |
Maybe.just(1).retry(5, Functions.alwaysTrue()).test().assertResult(1); |
| 3039 |
|
| 3040 |
Maybe.just(1).retry(new BiPredicate<Integer, Throwable>() { |
| 3041 |
@Override |
| 3042 |
public boolean test(Integer a, Throwable e) throws Exception { |
| 3043 |
return true; |
| 3044 |
} |
| 3045 |
}).test().assertResult(1); |
| 3046 |
|
| 3047 |
Maybe.just(1).retryUntil(new BooleanSupplier() { |
| 3048 |
@Override |
| 3049 |
public boolean getAsBoolean() throws Exception { |
| 3050 |
return false; |
| 3051 |
} |
| 3052 |
}).test().assertResult(1); |
| 3053 |
|
| 3054 |
Maybe.just(1).retryWhen(new Function<Flowable<? extends Throwable>, Publisher<Object>>() { |
| 3055 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 3056 |
@Override |
| 3057 |
public Publisher<Object> apply(Flowable<? extends Throwable> v) throws Exception { |
| 3058 |
return (Publisher)v; |
| 3059 |
} |
| 3060 |
}).test().assertResult(1); |
| 3061 |
|
| 3062 |
final AtomicInteger calls = new AtomicInteger(); |
| 3063 |
try { |
| 3064 |
Maybe.error(new Supplier<Throwable>() { |
| 3065 |
@Override |
| 3066 |
public Throwable get() { |
| 3067 |
calls.incrementAndGet(); |
| 3068 |
return new TestException(); |
| 3069 |
} |
| 3070 |
}).retry(5).test(); |
| 3071 |
} finally { |
| 3072 |
assertEquals(6, calls.get()); |
| 3073 |
} |
| 3074 |
} |
| 3075 |
|
| 3076 |
@Test |
| 3077 |
public void onErrorResumeWithEmpty() { |
| 3078 |
Maybe.empty() |
| 3079 |
.onErrorResumeWith(Maybe.just(1)) |
| 3080 |
.test() |
| 3081 |
.assertNoValues() |
| 3082 |
.assertNoErrors() |
| 3083 |
.assertComplete(); |
| 3084 |
} |
| 3085 |
|
| 3086 |
@Test |
| 3087 |
public void onErrorResumeWithValue() { |
| 3088 |
Maybe.just(1) |
| 3089 |
.onErrorResumeWith(Maybe.<Integer>empty()) |
| 3090 |
.test() |
| 3091 |
.assertNoErrors() |
| 3092 |
.assertValue(1); |
| 3093 |
} |
| 3094 |
|
| 3095 |
@Test |
| 3096 |
public void onErrorResumeWithError() { |
| 3097 |
Maybe.error(new RuntimeException("some error")) |
| 3098 |
.onErrorResumeWith(Maybe.empty()) |
| 3099 |
.test() |
| 3100 |
.assertNoValues() |
| 3101 |
.assertNoErrors() |
| 3102 |
.assertComplete(); |
| 3103 |
} |
| 3104 |
|
| 3105 |
@Test |
| 3106 |
public void valueConcatWithValue() { |
| 3107 |
Maybe.just(1) |
| 3108 |
.concatWith(Maybe.just(2)) |
| 3109 |
.test() |
| 3110 |
.assertNoErrors() |
| 3111 |
.assertComplete() |
| 3112 |
.assertValues(1, 2); |
| 3113 |
} |
| 3114 |
|
| 3115 |
@Test |
| 3116 |
public void errorConcatWithValue() { |
| 3117 |
Maybe.<Integer>error(new RuntimeException("error")) |
| 3118 |
.concatWith(Maybe.just(2)) |
| 3119 |
.to(TestHelper.<Integer>testConsumer()) |
| 3120 |
.assertError(RuntimeException.class) |
| 3121 |
.assertErrorMessage("error") |
| 3122 |
.assertNoValues(); |
| 3123 |
} |
| 3124 |
|
| 3125 |
@Test |
| 3126 |
public void valueConcatWithError() { |
| 3127 |
Maybe.just(1) |
| 3128 |
.concatWith(Maybe.<Integer>error(new RuntimeException("error"))) |
| 3129 |
.to(TestHelper.<Integer>testConsumer()) |
| 3130 |
.assertValue(1) |
| 3131 |
.assertError(RuntimeException.class) |
| 3132 |
.assertErrorMessage("error"); |
| 3133 |
} |
| 3134 |
|
| 3135 |
@Test |
| 3136 |
public void emptyConcatWithValue() { |
| 3137 |
Maybe.<Integer>empty() |
| 3138 |
.concatWith(Maybe.just(2)) |
| 3139 |
.test() |
| 3140 |
.assertNoErrors() |
| 3141 |
.assertComplete() |
| 3142 |
.assertValues(2); |
| 3143 |
} |
| 3144 |
|
| 3145 |
@Test |
| 3146 |
public void emptyConcatWithError() { |
| 3147 |
Maybe.<Integer>empty() |
| 3148 |
.concatWith(Maybe.<Integer>error(new RuntimeException("error"))) |
| 3149 |
.to(TestHelper.<Integer>testConsumer()) |
| 3150 |
.assertNoValues() |
| 3151 |
.assertError(RuntimeException.class) |
| 3152 |
.assertErrorMessage("error"); |
| 3153 |
} |
| 3154 |
} |