| 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.completable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.ArgumentMatchers.*; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.*; |
| 22 |
import java.util.concurrent.atomic.*; |
| 23 |
|
| 24 |
import org.junit.*; |
| 25 |
import org.reactivestreams.*; |
| 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.disposables.*; |
| 33 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 34 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 35 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 36 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 37 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 38 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 39 |
import io.reactivex.rxjava3.schedulers.*; |
| 40 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 41 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 42 |
import io.reactivex.rxjava3.testsupport.*; |
| 43 |
|
| 44 |
/** |
| 45 |
* Test Completable methods and operators. |
| 46 |
*/ |
| 47 |
public class CompletableTest extends RxJavaTest { |
| 48 |
/** |
| 49 |
* Iterable that returns an Iterator that throws in its hasNext method. |
| 50 |
*/ |
| 51 |
static final class IterableIteratorNextThrows implements Iterable<Completable> { |
| 52 |
@Override |
| 53 |
public Iterator<Completable> iterator() { |
| 54 |
return new Iterator<Completable>() { |
| 55 |
@Override |
| 56 |
public boolean hasNext() { |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public Completable next() { |
| 62 |
throw new TestException(); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void remove() { |
| 67 |
} |
| 68 |
}; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Iterable that returns an Iterator that throws in its next method. |
| 74 |
*/ |
| 75 |
static final class IterableIteratorHasNextThrows implements Iterable<Completable> { |
| 76 |
@Override |
| 77 |
public Iterator<Completable> iterator() { |
| 78 |
return new Iterator<Completable>() { |
| 79 |
@Override |
| 80 |
public boolean hasNext() { |
| 81 |
throw new TestException(); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public Completable next() { |
| 86 |
return null; |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void remove() { |
| 91 |
} |
| 92 |
}; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* A class containing a completable instance and counts the number of subscribers. |
| 98 |
*/ |
| 99 |
static final class NormalCompletable extends AtomicInteger { |
| 100 |
|
| 101 |
private static final long serialVersionUID = 7192337844700923752L; |
| 102 |
|
| 103 |
public final Completable completable = Completable.unsafeCreate(new CompletableSource() { |
| 104 |
@Override |
| 105 |
public void subscribe(CompletableObserver observer) { |
| 106 |
getAndIncrement(); |
| 107 |
EmptyDisposable.complete(observer); |
| 108 |
} |
| 109 |
}); |
| 110 |
|
| 111 |
/** |
| 112 |
* Asserts the given number of subscriptions happened. |
| 113 |
* @param n the expected number of subscriptions |
| 114 |
*/ |
| 115 |
public void assertSubscriptions(int n) { |
| 116 |
Assert.assertEquals(n, get()); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* A class containing a completable instance that emits a TestException and counts |
| 122 |
* the number of subscribers. |
| 123 |
*/ |
| 124 |
static final class ErrorCompletable extends AtomicInteger { |
| 125 |
|
| 126 |
private static final long serialVersionUID = 7192337844700923752L; |
| 127 |
|
| 128 |
public final Completable completable = Completable.unsafeCreate(new CompletableSource() { |
| 129 |
@Override |
| 130 |
public void subscribe(CompletableObserver observer) { |
| 131 |
getAndIncrement(); |
| 132 |
EmptyDisposable.error(new TestException(), observer); |
| 133 |
} |
| 134 |
}); |
| 135 |
|
| 136 |
/** |
| 137 |
* Asserts the given number of subscriptions happened. |
| 138 |
* @param n the expected number of subscriptions |
| 139 |
*/ |
| 140 |
public void assertSubscriptions(int n) { |
| 141 |
Assert.assertEquals(n, get()); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** A normal Completable object. */ |
| 146 |
final NormalCompletable normal = new NormalCompletable(); |
| 147 |
|
| 148 |
/** An error Completable object. */ |
| 149 |
final ErrorCompletable error = new ErrorCompletable(); |
| 150 |
|
| 151 |
@Test |
| 152 |
public void complete() { |
| 153 |
Completable c = Completable.complete(); |
| 154 |
|
| 155 |
c.blockingAwait(); |
| 156 |
} |
| 157 |
|
| 158 |
@Test |
| 159 |
public void concatEmpty() { |
| 160 |
Completable c = Completable.concatArray(); |
| 161 |
|
| 162 |
c.blockingAwait(); |
| 163 |
} |
| 164 |
|
| 165 |
@Test |
| 166 |
public void concatSingleSource() { |
| 167 |
Completable c = Completable.concatArray(normal.completable); |
| 168 |
|
| 169 |
c.blockingAwait(); |
| 170 |
|
| 171 |
normal.assertSubscriptions(1); |
| 172 |
} |
| 173 |
|
| 174 |
@Test(expected = TestException.class) |
| 175 |
public void concatSingleSourceThrows() { |
| 176 |
Completable c = Completable.concatArray(error.completable); |
| 177 |
|
| 178 |
c.blockingAwait(); |
| 179 |
} |
| 180 |
|
| 181 |
@Test |
| 182 |
public void concatMultipleSources() { |
| 183 |
Completable c = Completable.concatArray(normal.completable, normal.completable, normal.completable); |
| 184 |
|
| 185 |
c.blockingAwait(); |
| 186 |
|
| 187 |
normal.assertSubscriptions(3); |
| 188 |
} |
| 189 |
|
| 190 |
@Test(expected = TestException.class) |
| 191 |
public void concatMultipleOneThrows() { |
| 192 |
Completable c = Completable.concatArray(normal.completable, error.completable, normal.completable); |
| 193 |
|
| 194 |
c.blockingAwait(); |
| 195 |
} |
| 196 |
|
| 197 |
@Test(expected = NullPointerException.class) |
| 198 |
public void concatMultipleOneIsNull() { |
| 199 |
Completable c = Completable.concatArray(normal.completable, null); |
| 200 |
|
| 201 |
c.blockingAwait(); |
| 202 |
} |
| 203 |
|
| 204 |
@Test |
| 205 |
public void concatIterableEmpty() { |
| 206 |
Completable c = Completable.concat(Collections.<Completable>emptyList()); |
| 207 |
|
| 208 |
c.blockingAwait(); |
| 209 |
} |
| 210 |
|
| 211 |
@Test(expected = NullPointerException.class) |
| 212 |
public void concatIterableIteratorNull() { |
| 213 |
Completable c = Completable.concat(new Iterable<Completable>() { |
| 214 |
@Override |
| 215 |
public Iterator<Completable> iterator() { |
| 216 |
return null; |
| 217 |
} |
| 218 |
}); |
| 219 |
|
| 220 |
c.blockingAwait(); |
| 221 |
} |
| 222 |
|
| 223 |
@Test |
| 224 |
public void concatIterableSingle() { |
| 225 |
Completable c = Completable.concat(Collections.singleton(normal.completable)); |
| 226 |
|
| 227 |
c.blockingAwait(); |
| 228 |
|
| 229 |
normal.assertSubscriptions(1); |
| 230 |
} |
| 231 |
|
| 232 |
@Test |
| 233 |
public void concatIterableMany() { |
| 234 |
Completable c = Completable.concat(Arrays.asList(normal.completable, normal.completable, normal.completable)); |
| 235 |
|
| 236 |
c.blockingAwait(); |
| 237 |
|
| 238 |
normal.assertSubscriptions(3); |
| 239 |
} |
| 240 |
|
| 241 |
@Test(expected = TestException.class) |
| 242 |
public void concatIterableOneThrows() { |
| 243 |
Completable c = Completable.concat(Collections.singleton(error.completable)); |
| 244 |
|
| 245 |
c.blockingAwait(); |
| 246 |
} |
| 247 |
|
| 248 |
@Test(expected = TestException.class) |
| 249 |
public void concatIterableManyOneThrows() { |
| 250 |
Completable c = Completable.concat(Arrays.asList(normal.completable, error.completable)); |
| 251 |
|
| 252 |
c.blockingAwait(); |
| 253 |
} |
| 254 |
|
| 255 |
@Test(expected = TestException.class) |
| 256 |
public void concatIterableIterableThrows() { |
| 257 |
Completable c = Completable.concat(new Iterable<Completable>() { |
| 258 |
@Override |
| 259 |
public Iterator<Completable> iterator() { |
| 260 |
throw new TestException(); |
| 261 |
} |
| 262 |
}); |
| 263 |
|
| 264 |
c.blockingAwait(); |
| 265 |
} |
| 266 |
|
| 267 |
@Test(expected = TestException.class) |
| 268 |
public void concatIterableIteratorHasNextThrows() { |
| 269 |
Completable c = Completable.concat(new IterableIteratorHasNextThrows()); |
| 270 |
|
| 271 |
c.blockingAwait(); |
| 272 |
} |
| 273 |
|
| 274 |
@Test(expected = TestException.class) |
| 275 |
public void concatIterableIteratorNextThrows() { |
| 276 |
Completable c = Completable.concat(new IterableIteratorNextThrows()); |
| 277 |
|
| 278 |
c.blockingAwait(); |
| 279 |
} |
| 280 |
|
| 281 |
@Test |
| 282 |
public void concatObservableEmpty() { |
| 283 |
Completable c = Completable.concat(Flowable.<Completable>empty()); |
| 284 |
|
| 285 |
c.blockingAwait(); |
| 286 |
} |
| 287 |
|
| 288 |
@Test(expected = TestException.class) |
| 289 |
public void concatObservableError() { |
| 290 |
Completable c = Completable.concat(Flowable.<Completable>error(new Supplier<Throwable>() { |
| 291 |
@Override |
| 292 |
public Throwable get() { |
| 293 |
return new TestException(); |
| 294 |
} |
| 295 |
})); |
| 296 |
|
| 297 |
c.blockingAwait(); |
| 298 |
} |
| 299 |
|
| 300 |
@Test |
| 301 |
public void concatObservableSingle() { |
| 302 |
Completable c = Completable.concat(Flowable.just(normal.completable)); |
| 303 |
|
| 304 |
c.blockingAwait(); |
| 305 |
|
| 306 |
normal.assertSubscriptions(1); |
| 307 |
} |
| 308 |
|
| 309 |
@Test(expected = TestException.class) |
| 310 |
public void concatObservableSingleThrows() { |
| 311 |
Completable c = Completable.concat(Flowable.just(error.completable)); |
| 312 |
|
| 313 |
c.blockingAwait(); |
| 314 |
} |
| 315 |
|
| 316 |
@Test |
| 317 |
public void concatObservableMany() { |
| 318 |
Completable c = Completable.concat(Flowable.just(normal.completable).repeat(3)); |
| 319 |
|
| 320 |
c.blockingAwait(); |
| 321 |
|
| 322 |
normal.assertSubscriptions(3); |
| 323 |
} |
| 324 |
|
| 325 |
@Test(expected = TestException.class) |
| 326 |
public void concatObservableManyOneThrows() { |
| 327 |
Completable c = Completable.concat(Flowable.just(normal.completable, error.completable)); |
| 328 |
|
| 329 |
c.blockingAwait(); |
| 330 |
} |
| 331 |
|
| 332 |
@Test |
| 333 |
public void concatObservablePrefetch() { |
| 334 |
final List<Long> requested = new ArrayList<>(); |
| 335 |
Flowable<Completable> cs = Flowable |
| 336 |
.just(normal.completable) |
| 337 |
.repeat(10) |
| 338 |
.doOnRequest(new LongConsumer() { |
| 339 |
@Override |
| 340 |
public void accept(long v) { |
| 341 |
requested.add(v); |
| 342 |
} |
| 343 |
}); |
| 344 |
|
| 345 |
Completable c = Completable.concat(cs, 5); |
| 346 |
|
| 347 |
c.blockingAwait(); |
| 348 |
|
| 349 |
Assert.assertEquals(Arrays.asList(5L, 4L, 4L), requested); |
| 350 |
} |
| 351 |
|
| 352 |
@Test(expected = NullPointerException.class) |
| 353 |
public void createOnSubscribeThrowsNPE() { |
| 354 |
Completable c = Completable.unsafeCreate(new CompletableSource() { |
| 355 |
@Override |
| 356 |
public void subscribe(CompletableObserver observer) { throw new NullPointerException(); } |
| 357 |
}); |
| 358 |
|
| 359 |
c.blockingAwait(); |
| 360 |
} |
| 361 |
|
| 362 |
@Test |
| 363 |
public void createOnSubscribeThrowsRuntimeException() { |
| 364 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 365 |
try { |
| 366 |
Completable c = Completable.unsafeCreate(new CompletableSource() { |
| 367 |
@Override |
| 368 |
public void subscribe(CompletableObserver observer) { |
| 369 |
throw new TestException(); |
| 370 |
} |
| 371 |
}); |
| 372 |
|
| 373 |
c.blockingAwait(); |
| 374 |
|
| 375 |
Assert.fail("Did not throw exception"); |
| 376 |
} catch (NullPointerException ex) { |
| 377 |
if (!(ex.getCause() instanceof TestException)) { |
| 378 |
ex.printStackTrace(); |
| 379 |
Assert.fail("Did not wrap the TestException but it returned: " + ex); |
| 380 |
} |
| 381 |
|
| 382 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 383 |
} finally { |
| 384 |
RxJavaPlugins.reset(); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
@Test |
| 389 |
public void defer() { |
| 390 |
Completable c = Completable.defer(new Supplier<Completable>() { |
| 391 |
@Override |
| 392 |
public Completable get() { |
| 393 |
return normal.completable; |
| 394 |
} |
| 395 |
}); |
| 396 |
|
| 397 |
normal.assertSubscriptions(0); |
| 398 |
|
| 399 |
c.blockingAwait(); |
| 400 |
|
| 401 |
normal.assertSubscriptions(1); |
| 402 |
} |
| 403 |
|
| 404 |
@Test(expected = NullPointerException.class) |
| 405 |
public void deferReturnsNull() { |
| 406 |
Completable c = Completable.defer(new Supplier<Completable>() { |
| 407 |
@Override |
| 408 |
public Completable get() { |
| 409 |
return null; |
| 410 |
} |
| 411 |
}); |
| 412 |
|
| 413 |
c.blockingAwait(); |
| 414 |
} |
| 415 |
|
| 416 |
@Test(expected = TestException.class) |
| 417 |
public void deferFunctionThrows() { |
| 418 |
Completable c = Completable.defer(new Supplier<Completable>() { |
| 419 |
@Override |
| 420 |
public Completable get() { throw new TestException(); } |
| 421 |
}); |
| 422 |
|
| 423 |
c.blockingAwait(); |
| 424 |
} |
| 425 |
|
| 426 |
@Test(expected = TestException.class) |
| 427 |
public void deferErrorSource() { |
| 428 |
Completable c = Completable.defer(new Supplier<Completable>() { |
| 429 |
@Override |
| 430 |
public Completable get() { |
| 431 |
return error.completable; |
| 432 |
} |
| 433 |
}); |
| 434 |
|
| 435 |
c.blockingAwait(); |
| 436 |
} |
| 437 |
|
| 438 |
@Test(expected = TestException.class) |
| 439 |
public void errorSupplierNormal() { |
| 440 |
Completable c = Completable.error(new Supplier<Throwable>() { |
| 441 |
@Override |
| 442 |
public Throwable get() { |
| 443 |
return new TestException(); |
| 444 |
} |
| 445 |
}); |
| 446 |
|
| 447 |
c.blockingAwait(); |
| 448 |
} |
| 449 |
|
| 450 |
@Test(expected = NullPointerException.class) |
| 451 |
public void errorSupplierReturnsNull() { |
| 452 |
Completable c = Completable.error(new Supplier<Throwable>() { |
| 453 |
@Override |
| 454 |
public Throwable get() { |
| 455 |
return null; |
| 456 |
} |
| 457 |
}); |
| 458 |
|
| 459 |
c.blockingAwait(); |
| 460 |
} |
| 461 |
|
| 462 |
@Test(expected = TestException.class) |
| 463 |
public void errorSupplierThrows() { |
| 464 |
Completable c = Completable.error(new Supplier<Throwable>() { |
| 465 |
@Override |
| 466 |
public Throwable get() { throw new TestException(); } |
| 467 |
}); |
| 468 |
|
| 469 |
c.blockingAwait(); |
| 470 |
} |
| 471 |
|
| 472 |
@Test(expected = TestException.class) |
| 473 |
public void errorNormal() { |
| 474 |
Completable c = Completable.error(new TestException()); |
| 475 |
|
| 476 |
c.blockingAwait(); |
| 477 |
} |
| 478 |
|
| 479 |
@Test |
| 480 |
public void fromCallableNormal() { |
| 481 |
final AtomicInteger calls = new AtomicInteger(); |
| 482 |
|
| 483 |
Completable c = Completable.fromCallable(new Callable<Object>() { |
| 484 |
@Override |
| 485 |
public Object call() throws Exception { |
| 486 |
return calls.getAndIncrement(); |
| 487 |
} |
| 488 |
}); |
| 489 |
|
| 490 |
c.blockingAwait(); |
| 491 |
|
| 492 |
Assert.assertEquals(1, calls.get()); |
| 493 |
} |
| 494 |
|
| 495 |
@Test(expected = TestException.class) |
| 496 |
public void fromCallableThrows() { |
| 497 |
Completable c = Completable.fromCallable(new Callable<Object>() { |
| 498 |
@Override |
| 499 |
public Object call() throws Exception { throw new TestException(); } |
| 500 |
}); |
| 501 |
|
| 502 |
c.blockingAwait(); |
| 503 |
} |
| 504 |
|
| 505 |
@Test |
| 506 |
public void fromFlowableEmpty() { |
| 507 |
Completable c = Completable.fromPublisher(Flowable.empty()); |
| 508 |
|
| 509 |
c.blockingAwait(); |
| 510 |
} |
| 511 |
|
| 512 |
@Test |
| 513 |
public void fromFlowableSome() { |
| 514 |
for (int n = 1; n < 10000; n *= 10) { |
| 515 |
Completable c = Completable.fromPublisher(Flowable.range(1, n)); |
| 516 |
|
| 517 |
c.blockingAwait(); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
@Test(expected = TestException.class) |
| 522 |
public void fromFlowableError() { |
| 523 |
Completable c = Completable.fromPublisher(Flowable.error(new Supplier<Throwable>() { |
| 524 |
@Override |
| 525 |
public Throwable get() { |
| 526 |
return new TestException(); |
| 527 |
} |
| 528 |
})); |
| 529 |
|
| 530 |
c.blockingAwait(); |
| 531 |
} |
| 532 |
|
| 533 |
@Test |
| 534 |
public void fromObservableEmpty() { |
| 535 |
Completable c = Completable.fromObservable(Observable.empty()); |
| 536 |
|
| 537 |
c.blockingAwait(); |
| 538 |
} |
| 539 |
|
| 540 |
@Test |
| 541 |
public void fromObservableSome() { |
| 542 |
for (int n = 1; n < 10000; n *= 10) { |
| 543 |
Completable c = Completable.fromObservable(Observable.range(1, n)); |
| 544 |
|
| 545 |
c.blockingAwait(); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
@Test(expected = TestException.class) |
| 550 |
public void fromObservableError() { |
| 551 |
Completable c = Completable.fromObservable(Observable.error(new Supplier<Throwable>() { |
| 552 |
@Override |
| 553 |
public Throwable get() { |
| 554 |
return new TestException(); |
| 555 |
} |
| 556 |
})); |
| 557 |
|
| 558 |
c.blockingAwait(); |
| 559 |
} |
| 560 |
|
| 561 |
@Test |
| 562 |
public void fromActionNormal() { |
| 563 |
final AtomicInteger calls = new AtomicInteger(); |
| 564 |
|
| 565 |
Completable c = Completable.fromAction(new Action() { |
| 566 |
@Override |
| 567 |
public void run() { |
| 568 |
calls.getAndIncrement(); |
| 569 |
} |
| 570 |
}); |
| 571 |
|
| 572 |
c.blockingAwait(); |
| 573 |
|
| 574 |
Assert.assertEquals(1, calls.get()); |
| 575 |
} |
| 576 |
|
| 577 |
@Test(expected = TestException.class) |
| 578 |
public void fromActionThrows() { |
| 579 |
Completable c = Completable.fromAction(new Action() { |
| 580 |
@Override |
| 581 |
public void run() { throw new TestException(); } |
| 582 |
}); |
| 583 |
|
| 584 |
c.blockingAwait(); |
| 585 |
} |
| 586 |
|
| 587 |
@Test |
| 588 |
public void fromSingleNormal() { |
| 589 |
Completable c = Completable.fromSingle(Single.just(1)); |
| 590 |
|
| 591 |
c.blockingAwait(); |
| 592 |
} |
| 593 |
|
| 594 |
@Test(expected = TestException.class) |
| 595 |
public void fromSingleThrows() { |
| 596 |
Completable c = Completable.fromSingle(Single.error(new Supplier<Throwable>() { |
| 597 |
@Override |
| 598 |
public Throwable get() { |
| 599 |
return new TestException(); |
| 600 |
} |
| 601 |
})); |
| 602 |
|
| 603 |
c.blockingAwait(); |
| 604 |
} |
| 605 |
|
| 606 |
@Test |
| 607 |
public void mergeEmpty() { |
| 608 |
Completable c = Completable.mergeArray(); |
| 609 |
|
| 610 |
c.blockingAwait(); |
| 611 |
} |
| 612 |
|
| 613 |
@Test |
| 614 |
public void mergeSingleSource() { |
| 615 |
Completable c = Completable.mergeArray(normal.completable); |
| 616 |
|
| 617 |
c.blockingAwait(); |
| 618 |
|
| 619 |
normal.assertSubscriptions(1); |
| 620 |
} |
| 621 |
|
| 622 |
@Test(expected = TestException.class) |
| 623 |
public void mergeSingleSourceThrows() { |
| 624 |
Completable c = Completable.mergeArray(error.completable); |
| 625 |
|
| 626 |
c.blockingAwait(); |
| 627 |
} |
| 628 |
|
| 629 |
@Test |
| 630 |
public void mergeMultipleSources() { |
| 631 |
Completable c = Completable.mergeArray(normal.completable, normal.completable, normal.completable); |
| 632 |
|
| 633 |
c.blockingAwait(); |
| 634 |
|
| 635 |
normal.assertSubscriptions(3); |
| 636 |
} |
| 637 |
|
| 638 |
@Test(expected = TestException.class) |
| 639 |
public void mergeMultipleOneThrows() { |
| 640 |
Completable c = Completable.mergeArray(normal.completable, error.completable, normal.completable); |
| 641 |
|
| 642 |
c.blockingAwait(); |
| 643 |
} |
| 644 |
|
| 645 |
@Test(expected = NullPointerException.class) |
| 646 |
public void mergeMultipleOneIsNull() { |
| 647 |
Completable c = Completable.mergeArray(normal.completable, null); |
| 648 |
|
| 649 |
c.blockingAwait(); |
| 650 |
} |
| 651 |
|
| 652 |
@Test |
| 653 |
public void mergeIterableEmpty() { |
| 654 |
Completable c = Completable.merge(Collections.<Completable>emptyList()); |
| 655 |
|
| 656 |
c.blockingAwait(); |
| 657 |
} |
| 658 |
|
| 659 |
@Test(expected = NullPointerException.class) |
| 660 |
public void mergeIterableIteratorNull() { |
| 661 |
Completable c = Completable.merge(new Iterable<Completable>() { |
| 662 |
@Override |
| 663 |
public Iterator<Completable> iterator() { |
| 664 |
return null; |
| 665 |
} |
| 666 |
}); |
| 667 |
|
| 668 |
c.blockingAwait(); |
| 669 |
} |
| 670 |
|
| 671 |
@Test |
| 672 |
public void mergeIterableSingle() { |
| 673 |
Completable c = Completable.merge(Collections.singleton(normal.completable)); |
| 674 |
|
| 675 |
c.blockingAwait(); |
| 676 |
|
| 677 |
normal.assertSubscriptions(1); |
| 678 |
} |
| 679 |
|
| 680 |
@Test |
| 681 |
public void mergeIterableMany() { |
| 682 |
Completable c = Completable.merge(Arrays.asList(normal.completable, normal.completable, normal.completable)); |
| 683 |
|
| 684 |
c.blockingAwait(); |
| 685 |
|
| 686 |
normal.assertSubscriptions(3); |
| 687 |
} |
| 688 |
|
| 689 |
@Test(expected = TestException.class) |
| 690 |
public void mergeIterableOneThrows() { |
| 691 |
Completable c = Completable.merge(Collections.singleton(error.completable)); |
| 692 |
|
| 693 |
c.blockingAwait(); |
| 694 |
} |
| 695 |
|
| 696 |
@Test(expected = TestException.class) |
| 697 |
public void mergeIterableManyOneThrows() { |
| 698 |
Completable c = Completable.merge(Arrays.asList(normal.completable, error.completable)); |
| 699 |
|
| 700 |
c.blockingAwait(); |
| 701 |
} |
| 702 |
|
| 703 |
@Test(expected = TestException.class) |
| 704 |
public void mergeIterableIterableThrows() { |
| 705 |
Completable c = Completable.merge(new Iterable<Completable>() { |
| 706 |
@Override |
| 707 |
public Iterator<Completable> iterator() { |
| 708 |
throw new TestException(); |
| 709 |
} |
| 710 |
}); |
| 711 |
|
| 712 |
c.blockingAwait(); |
| 713 |
} |
| 714 |
|
| 715 |
@Test(expected = TestException.class) |
| 716 |
public void mergeIterableIteratorHasNextThrows() { |
| 717 |
Completable c = Completable.merge(new IterableIteratorHasNextThrows()); |
| 718 |
|
| 719 |
c.blockingAwait(); |
| 720 |
} |
| 721 |
|
| 722 |
@Test(expected = TestException.class) |
| 723 |
public void mergeIterableIteratorNextThrows() { |
| 724 |
Completable c = Completable.merge(new IterableIteratorNextThrows()); |
| 725 |
|
| 726 |
c.blockingAwait(); |
| 727 |
} |
| 728 |
|
| 729 |
@Test |
| 730 |
public void mergeObservableEmpty() { |
| 731 |
Completable c = Completable.merge(Flowable.<Completable>empty()); |
| 732 |
|
| 733 |
c.blockingAwait(); |
| 734 |
} |
| 735 |
|
| 736 |
@Test(expected = TestException.class) |
| 737 |
public void mergeObservableError() { |
| 738 |
Completable c = Completable.merge(Flowable.<Completable>error(new Supplier<Throwable>() { |
| 739 |
@Override |
| 740 |
public Throwable get() { |
| 741 |
return new TestException(); |
| 742 |
} |
| 743 |
})); |
| 744 |
|
| 745 |
c.blockingAwait(); |
| 746 |
} |
| 747 |
|
| 748 |
@Test |
| 749 |
public void mergeObservableSingle() { |
| 750 |
Completable c = Completable.merge(Flowable.just(normal.completable)); |
| 751 |
|
| 752 |
c.blockingAwait(); |
| 753 |
|
| 754 |
normal.assertSubscriptions(1); |
| 755 |
} |
| 756 |
|
| 757 |
@Test(expected = TestException.class) |
| 758 |
public void mergeObservableSingleThrows() { |
| 759 |
Completable c = Completable.merge(Flowable.just(error.completable)); |
| 760 |
|
| 761 |
c.blockingAwait(); |
| 762 |
} |
| 763 |
|
| 764 |
@Test |
| 765 |
public void mergeObservableMany() { |
| 766 |
Completable c = Completable.merge(Flowable.just(normal.completable).repeat(3)); |
| 767 |
|
| 768 |
c.blockingAwait(); |
| 769 |
|
| 770 |
normal.assertSubscriptions(3); |
| 771 |
} |
| 772 |
|
| 773 |
@Test(expected = TestException.class) |
| 774 |
public void mergeObservableManyOneThrows() { |
| 775 |
Completable c = Completable.merge(Flowable.just(normal.completable, error.completable)); |
| 776 |
|
| 777 |
c.blockingAwait(); |
| 778 |
} |
| 779 |
|
| 780 |
@Test |
| 781 |
public void mergeObservableMaxConcurrent() { |
| 782 |
final List<Long> requested = new ArrayList<>(); |
| 783 |
Flowable<Completable> cs = Flowable |
| 784 |
.just(normal.completable) |
| 785 |
.repeat(10) |
| 786 |
.doOnRequest(new LongConsumer() { |
| 787 |
@Override |
| 788 |
public void accept(long v) { |
| 789 |
requested.add(v); |
| 790 |
} |
| 791 |
}); |
| 792 |
|
| 793 |
Completable c = Completable.merge(cs, 5); |
| 794 |
|
| 795 |
c.blockingAwait(); |
| 796 |
|
| 797 |
// FIXME this request pattern looks odd because all 10 completions trigger 1 requests |
| 798 |
Assert.assertEquals(Arrays.asList(5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), requested); |
| 799 |
} |
| 800 |
|
| 801 |
@Test |
| 802 |
public void mergeDelayErrorEmpty() { |
| 803 |
Completable c = Completable.mergeArrayDelayError(); |
| 804 |
|
| 805 |
c.blockingAwait(); |
| 806 |
} |
| 807 |
|
| 808 |
@Test |
| 809 |
public void mergeDelayErrorSingleSource() { |
| 810 |
Completable c = Completable.mergeArrayDelayError(normal.completable); |
| 811 |
|
| 812 |
c.blockingAwait(); |
| 813 |
|
| 814 |
normal.assertSubscriptions(1); |
| 815 |
} |
| 816 |
|
| 817 |
@Test(expected = TestException.class) |
| 818 |
public void mergeDelayErrorSingleSourceThrows() { |
| 819 |
Completable c = Completable.mergeArrayDelayError(error.completable); |
| 820 |
|
| 821 |
c.blockingAwait(); |
| 822 |
} |
| 823 |
|
| 824 |
@Test |
| 825 |
public void mergeDelayErrorMultipleSources() { |
| 826 |
Completable c = Completable.mergeArrayDelayError(normal.completable, normal.completable, normal.completable); |
| 827 |
|
| 828 |
c.blockingAwait(); |
| 829 |
|
| 830 |
normal.assertSubscriptions(3); |
| 831 |
} |
| 832 |
|
| 833 |
@Test |
| 834 |
public void mergeDelayErrorMultipleOneThrows() { |
| 835 |
Completable c = Completable.mergeArrayDelayError(normal.completable, error.completable, normal.completable); |
| 836 |
|
| 837 |
try { |
| 838 |
c.blockingAwait(); |
| 839 |
} catch (TestException ex) { |
| 840 |
normal.assertSubscriptions(2); |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
@Test(expected = NullPointerException.class) |
| 845 |
public void mergeDelayErrorMultipleOneIsNull() { |
| 846 |
Completable c = Completable.mergeArrayDelayError(normal.completable, null); |
| 847 |
|
| 848 |
c.blockingAwait(); |
| 849 |
} |
| 850 |
|
| 851 |
@Test |
| 852 |
public void mergeDelayErrorIterableEmpty() { |
| 853 |
Completable c = Completable.mergeDelayError(Collections.<Completable>emptyList()); |
| 854 |
|
| 855 |
c.blockingAwait(); |
| 856 |
} |
| 857 |
|
| 858 |
@Test(expected = NullPointerException.class) |
| 859 |
public void mergeDelayErrorIterableIteratorNull() { |
| 860 |
Completable c = Completable.mergeDelayError(new Iterable<Completable>() { |
| 861 |
@Override |
| 862 |
public Iterator<Completable> iterator() { |
| 863 |
return null; |
| 864 |
} |
| 865 |
}); |
| 866 |
|
| 867 |
c.blockingAwait(); |
| 868 |
} |
| 869 |
|
| 870 |
@Test |
| 871 |
public void mergeDelayErrorIterableSingle() { |
| 872 |
Completable c = Completable.mergeDelayError(Collections.singleton(normal.completable)); |
| 873 |
|
| 874 |
c.blockingAwait(); |
| 875 |
|
| 876 |
normal.assertSubscriptions(1); |
| 877 |
} |
| 878 |
|
| 879 |
@Test |
| 880 |
public void mergeDelayErrorIterableMany() { |
| 881 |
Completable c = Completable.mergeDelayError(Arrays.asList(normal.completable, normal.completable, normal.completable)); |
| 882 |
|
| 883 |
c.blockingAwait(); |
| 884 |
|
| 885 |
normal.assertSubscriptions(3); |
| 886 |
} |
| 887 |
|
| 888 |
@Test(expected = TestException.class) |
| 889 |
public void mergeDelayErrorIterableOneThrows() { |
| 890 |
Completable c = Completable.mergeDelayError(Collections.singleton(error.completable)); |
| 891 |
|
| 892 |
c.blockingAwait(); |
| 893 |
} |
| 894 |
|
| 895 |
@Test |
| 896 |
public void mergeDelayErrorIterableManyOneThrows() { |
| 897 |
Completable c = Completable.mergeDelayError(Arrays.asList(normal.completable, error.completable, normal.completable)); |
| 898 |
|
| 899 |
try { |
| 900 |
c.blockingAwait(); |
| 901 |
} catch (TestException ex) { |
| 902 |
normal.assertSubscriptions(2); |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
@Test(expected = TestException.class) |
| 907 |
public void mergeDelayErrorIterableIterableThrows() { |
| 908 |
Completable c = Completable.mergeDelayError(new Iterable<Completable>() { |
| 909 |
@Override |
| 910 |
public Iterator<Completable> iterator() { |
| 911 |
throw new TestException(); |
| 912 |
} |
| 913 |
}); |
| 914 |
|
| 915 |
c.blockingAwait(); |
| 916 |
} |
| 917 |
|
| 918 |
@Test(expected = TestException.class) |
| 919 |
public void mergeDelayErrorIterableIteratorHasNextThrows() { |
| 920 |
Completable c = Completable.mergeDelayError(new IterableIteratorHasNextThrows()); |
| 921 |
|
| 922 |
c.blockingAwait(); |
| 923 |
} |
| 924 |
|
| 925 |
@Test(expected = TestException.class) |
| 926 |
public void mergeDelayErrorIterableIteratorNextThrows() { |
| 927 |
Completable c = Completable.mergeDelayError(new IterableIteratorNextThrows()); |
| 928 |
|
| 929 |
c.blockingAwait(); |
| 930 |
} |
| 931 |
|
| 932 |
@Test |
| 933 |
public void mergeDelayErrorObservableEmpty() { |
| 934 |
Completable c = Completable.mergeDelayError(Flowable.<Completable>empty()); |
| 935 |
|
| 936 |
c.blockingAwait(); |
| 937 |
} |
| 938 |
|
| 939 |
@Test(expected = TestException.class) |
| 940 |
public void mergeDelayErrorObservableError() { |
| 941 |
Completable c = Completable.mergeDelayError(Flowable.<Completable>error(new Supplier<Throwable>() { |
| 942 |
@Override |
| 943 |
public Throwable get() { |
| 944 |
return new TestException(); |
| 945 |
} |
| 946 |
})); |
| 947 |
|
| 948 |
c.blockingAwait(); |
| 949 |
} |
| 950 |
|
| 951 |
@Test |
| 952 |
public void mergeDelayErrorObservableSingle() { |
| 953 |
Completable c = Completable.mergeDelayError(Flowable.just(normal.completable)); |
| 954 |
|
| 955 |
c.blockingAwait(); |
| 956 |
|
| 957 |
normal.assertSubscriptions(1); |
| 958 |
} |
| 959 |
|
| 960 |
@Test(expected = TestException.class) |
| 961 |
public void mergeDelayErrorObservableSingleThrows() { |
| 962 |
Completable c = Completable.mergeDelayError(Flowable.just(error.completable)); |
| 963 |
|
| 964 |
c.blockingAwait(); |
| 965 |
} |
| 966 |
|
| 967 |
@Test |
| 968 |
public void mergeDelayErrorObservableMany() { |
| 969 |
Completable c = Completable.mergeDelayError(Flowable.just(normal.completable).repeat(3)); |
| 970 |
|
| 971 |
c.blockingAwait(); |
| 972 |
|
| 973 |
normal.assertSubscriptions(3); |
| 974 |
} |
| 975 |
|
| 976 |
@Test(expected = TestException.class) |
| 977 |
public void mergeDelayErrorObservableManyOneThrows() { |
| 978 |
Completable c = Completable.mergeDelayError(Flowable.just(normal.completable, error.completable)); |
| 979 |
|
| 980 |
c.blockingAwait(); |
| 981 |
} |
| 982 |
|
| 983 |
@Test |
| 984 |
public void mergeDelayErrorObservableMaxConcurrent() { |
| 985 |
final List<Long> requested = new ArrayList<>(); |
| 986 |
Flowable<Completable> cs = Flowable |
| 987 |
.just(normal.completable) |
| 988 |
.repeat(10) |
| 989 |
.doOnRequest(new LongConsumer() { |
| 990 |
@Override |
| 991 |
public void accept(long v) { |
| 992 |
requested.add(v); |
| 993 |
} |
| 994 |
}); |
| 995 |
|
| 996 |
Completable c = Completable.mergeDelayError(cs, 5); |
| 997 |
|
| 998 |
c.blockingAwait(); |
| 999 |
|
| 1000 |
// FIXME this request pattern looks odd because all 10 completions trigger 1 requests |
| 1001 |
Assert.assertEquals(Arrays.asList(5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), requested); |
| 1002 |
} |
| 1003 |
|
| 1004 |
@Test |
| 1005 |
public void never() { |
| 1006 |
final AtomicBoolean onSubscribeCalled = new AtomicBoolean(); |
| 1007 |
final AtomicInteger calls = new AtomicInteger(); |
| 1008 |
Completable.never().subscribe(new CompletableObserver() { |
| 1009 |
@Override |
| 1010 |
public void onSubscribe(Disposable d) { |
| 1011 |
onSubscribeCalled.set(true); |
| 1012 |
} |
| 1013 |
|
| 1014 |
@Override |
| 1015 |
public void onError(Throwable e) { |
| 1016 |
calls.getAndIncrement(); |
| 1017 |
} |
| 1018 |
|
| 1019 |
@Override |
| 1020 |
public void onComplete() { |
| 1021 |
calls.getAndIncrement(); |
| 1022 |
} |
| 1023 |
}); |
| 1024 |
|
| 1025 |
Assert.assertTrue("onSubscribe not called", onSubscribeCalled.get()); |
| 1026 |
Assert.assertEquals("There were calls to onXXX methods", 0, calls.get()); |
| 1027 |
} |
| 1028 |
|
| 1029 |
@Test |
| 1030 |
public void timer() { |
| 1031 |
Completable c = Completable.timer(500, TimeUnit.MILLISECONDS); |
| 1032 |
|
| 1033 |
c.blockingAwait(); |
| 1034 |
} |
| 1035 |
|
| 1036 |
@Test |
| 1037 |
public void timerNewThread() { |
| 1038 |
Completable c = Completable.timer(500, TimeUnit.MILLISECONDS, Schedulers.newThread()); |
| 1039 |
|
| 1040 |
c.blockingAwait(); |
| 1041 |
} |
| 1042 |
|
| 1043 |
@Test |
| 1044 |
public void timerTestScheduler() { |
| 1045 |
TestScheduler scheduler = new TestScheduler(); |
| 1046 |
|
| 1047 |
Completable c = Completable.timer(250, TimeUnit.MILLISECONDS, scheduler); |
| 1048 |
|
| 1049 |
final AtomicInteger calls = new AtomicInteger(); |
| 1050 |
|
| 1051 |
c.subscribe(new CompletableObserver() { |
| 1052 |
@Override |
| 1053 |
public void onSubscribe(Disposable d) { |
| 1054 |
|
| 1055 |
} |
| 1056 |
|
| 1057 |
@Override |
| 1058 |
public void onComplete() { |
| 1059 |
calls.getAndIncrement(); |
| 1060 |
} |
| 1061 |
|
| 1062 |
@Override |
| 1063 |
public void onError(Throwable e) { |
| 1064 |
RxJavaPlugins.onError(e); |
| 1065 |
} |
| 1066 |
}); |
| 1067 |
|
| 1068 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 1069 |
|
| 1070 |
Assert.assertEquals(0, calls.get()); |
| 1071 |
|
| 1072 |
scheduler.advanceTimeBy(200, TimeUnit.MILLISECONDS); |
| 1073 |
|
| 1074 |
Assert.assertEquals(1, calls.get()); |
| 1075 |
} |
| 1076 |
|
| 1077 |
@Test |
| 1078 |
public void timerCancel() throws InterruptedException { |
| 1079 |
Completable c = Completable.timer(250, TimeUnit.MILLISECONDS); |
| 1080 |
|
| 1081 |
final SequentialDisposable sd = new SequentialDisposable(); |
| 1082 |
final AtomicInteger calls = new AtomicInteger(); |
| 1083 |
|
| 1084 |
c.subscribe(new CompletableObserver() { |
| 1085 |
@Override |
| 1086 |
public void onSubscribe(Disposable d) { |
| 1087 |
sd.replace(d); |
| 1088 |
} |
| 1089 |
|
| 1090 |
@Override |
| 1091 |
public void onError(Throwable e) { |
| 1092 |
calls.getAndIncrement(); |
| 1093 |
} |
| 1094 |
|
| 1095 |
@Override |
| 1096 |
public void onComplete() { |
| 1097 |
calls.getAndIncrement(); |
| 1098 |
} |
| 1099 |
}); |
| 1100 |
|
| 1101 |
Thread.sleep(100); |
| 1102 |
|
| 1103 |
sd.dispose(); |
| 1104 |
|
| 1105 |
Thread.sleep(200); |
| 1106 |
|
| 1107 |
Assert.assertEquals(0, calls.get()); |
| 1108 |
} |
| 1109 |
|
| 1110 |
@Test |
| 1111 |
public void usingNormalEager() { |
| 1112 |
final AtomicInteger dispose = new AtomicInteger(); |
| 1113 |
|
| 1114 |
Completable c = Completable.using(new Supplier<Integer>() { |
| 1115 |
@Override |
| 1116 |
public Integer get() { |
| 1117 |
return 1; |
| 1118 |
} |
| 1119 |
}, new Function<Object, Completable>() { |
| 1120 |
@Override |
| 1121 |
public Completable apply(Object v) { |
| 1122 |
return normal.completable; |
| 1123 |
} |
| 1124 |
}, new Consumer<Integer>() { |
| 1125 |
@Override |
| 1126 |
public void accept(Integer d) { |
| 1127 |
dispose.set(d); |
| 1128 |
} |
| 1129 |
}); |
| 1130 |
|
| 1131 |
final AtomicBoolean disposedFirst = new AtomicBoolean(); |
| 1132 |
final AtomicReference<Throwable> error = new AtomicReference<>(); |
| 1133 |
|
| 1134 |
c.subscribe(new CompletableObserver() { |
| 1135 |
@Override |
| 1136 |
public void onSubscribe(Disposable d) { |
| 1137 |
|
| 1138 |
} |
| 1139 |
|
| 1140 |
@Override |
| 1141 |
public void onError(Throwable e) { |
| 1142 |
error.lazySet(e); |
| 1143 |
} |
| 1144 |
|
| 1145 |
@Override |
| 1146 |
public void onComplete() { |
| 1147 |
disposedFirst.set(dispose.get() != 0); |
| 1148 |
} |
| 1149 |
}); |
| 1150 |
|
| 1151 |
Assert.assertEquals(1, dispose.get()); |
| 1152 |
Assert.assertTrue("Not disposed first", disposedFirst.get()); |
| 1153 |
Assert.assertNull(error.get()); |
| 1154 |
} |
| 1155 |
|
| 1156 |
@Test |
| 1157 |
public void usingNormalLazy() { |
| 1158 |
final AtomicInteger dispose = new AtomicInteger(); |
| 1159 |
|
| 1160 |
Completable c = Completable.using(new Supplier<Integer>() { |
| 1161 |
@Override |
| 1162 |
public Integer get() { |
| 1163 |
return 1; |
| 1164 |
} |
| 1165 |
}, new Function<Integer, Completable>() { |
| 1166 |
@Override |
| 1167 |
public Completable apply(Integer v) { |
| 1168 |
return normal.completable; |
| 1169 |
} |
| 1170 |
}, new Consumer<Integer>() { |
| 1171 |
@Override |
| 1172 |
public void accept(Integer d) { |
| 1173 |
dispose.set(d); |
| 1174 |
} |
| 1175 |
}, false); |
| 1176 |
|
| 1177 |
final AtomicBoolean disposedFirst = new AtomicBoolean(); |
| 1178 |
final AtomicReference<Throwable> error = new AtomicReference<>(); |
| 1179 |
|
| 1180 |
c.subscribe(new CompletableObserver() { |
| 1181 |
@Override |
| 1182 |
public void onSubscribe(Disposable d) { |
| 1183 |
|
| 1184 |
} |
| 1185 |
|
| 1186 |
@Override |
| 1187 |
public void onError(Throwable e) { |
| 1188 |
error.lazySet(e); |
| 1189 |
} |
| 1190 |
|
| 1191 |
@Override |
| 1192 |
public void onComplete() { |
| 1193 |
disposedFirst.set(dispose.get() != 0); |
| 1194 |
} |
| 1195 |
}); |
| 1196 |
|
| 1197 |
Assert.assertEquals(1, dispose.get()); |
| 1198 |
Assert.assertFalse("Disposed first", disposedFirst.get()); |
| 1199 |
Assert.assertNull(error.get()); |
| 1200 |
} |
| 1201 |
|
| 1202 |
@Test |
| 1203 |
public void usingErrorEager() { |
| 1204 |
final AtomicInteger dispose = new AtomicInteger(); |
| 1205 |
|
| 1206 |
Completable c = Completable.using(new Supplier<Integer>() { |
| 1207 |
@Override |
| 1208 |
public Integer get() { |
| 1209 |
return 1; |
| 1210 |
} |
| 1211 |
}, new Function<Integer, Completable>() { |
| 1212 |
@Override |
| 1213 |
public Completable apply(Integer v) { |
| 1214 |
return error.completable; |
| 1215 |
} |
| 1216 |
}, new Consumer<Integer>() { |
| 1217 |
@Override |
| 1218 |
public void accept(Integer d) { |
| 1219 |
dispose.set(d); |
| 1220 |
} |
| 1221 |
}); |
| 1222 |
|
| 1223 |
final AtomicBoolean disposedFirst = new AtomicBoolean(); |
| 1224 |
final AtomicBoolean complete = new AtomicBoolean(); |
| 1225 |
|
| 1226 |
c.subscribe(new CompletableObserver() { |
| 1227 |
@Override |
| 1228 |
public void onSubscribe(Disposable d) { |
| 1229 |
|
| 1230 |
} |
| 1231 |
|
| 1232 |
@Override |
| 1233 |
public void onError(Throwable e) { |
| 1234 |
disposedFirst.set(dispose.get() != 0); |
| 1235 |
} |
| 1236 |
|
| 1237 |
@Override |
| 1238 |
public void onComplete() { |
| 1239 |
complete.set(true); |
| 1240 |
} |
| 1241 |
}); |
| 1242 |
|
| 1243 |
Assert.assertEquals(1, dispose.get()); |
| 1244 |
Assert.assertTrue("Not disposed first", disposedFirst.get()); |
| 1245 |
Assert.assertFalse(complete.get()); |
| 1246 |
} |
| 1247 |
|
| 1248 |
@Test |
| 1249 |
public void usingErrorLazy() { |
| 1250 |
final AtomicInteger dispose = new AtomicInteger(); |
| 1251 |
|
| 1252 |
Completable c = Completable.using(new Supplier<Integer>() { |
| 1253 |
@Override |
| 1254 |
public Integer get() { |
| 1255 |
return 1; |
| 1256 |
} |
| 1257 |
}, new Function<Integer, Completable>() { |
| 1258 |
@Override |
| 1259 |
public Completable apply(Integer v) { |
| 1260 |
return error.completable; |
| 1261 |
} |
| 1262 |
}, new Consumer<Integer>() { |
| 1263 |
@Override |
| 1264 |
public void accept(Integer d) { |
| 1265 |
dispose.set(d); |
| 1266 |
} |
| 1267 |
}, false); |
| 1268 |
|
| 1269 |
final AtomicBoolean disposedFirst = new AtomicBoolean(); |
| 1270 |
final AtomicBoolean complete = new AtomicBoolean(); |
| 1271 |
|
| 1272 |
c.subscribe(new CompletableObserver() { |
| 1273 |
@Override |
| 1274 |
public void onSubscribe(Disposable d) { |
| 1275 |
|
| 1276 |
} |
| 1277 |
|
| 1278 |
@Override |
| 1279 |
public void onError(Throwable e) { |
| 1280 |
disposedFirst.set(dispose.get() != 0); |
| 1281 |
} |
| 1282 |
|
| 1283 |
@Override |
| 1284 |
public void onComplete() { |
| 1285 |
complete.set(true); |
| 1286 |
} |
| 1287 |
}); |
| 1288 |
|
| 1289 |
Assert.assertEquals(1, dispose.get()); |
| 1290 |
Assert.assertFalse("Disposed first", disposedFirst.get()); |
| 1291 |
Assert.assertFalse(complete.get()); |
| 1292 |
} |
| 1293 |
|
| 1294 |
@Test(expected = NullPointerException.class) |
| 1295 |
public void usingMapperReturnsNull() { |
| 1296 |
Completable c = Completable.using(new Supplier<Object>() { |
| 1297 |
@Override |
| 1298 |
public Object get() { |
| 1299 |
return 1; |
| 1300 |
} |
| 1301 |
}, new Function<Object, Completable>() { |
| 1302 |
@Override |
| 1303 |
public Completable apply(Object v) { |
| 1304 |
return null; |
| 1305 |
} |
| 1306 |
}, new Consumer<Object>() { |
| 1307 |
@Override |
| 1308 |
public void accept(Object v) { } |
| 1309 |
}); |
| 1310 |
|
| 1311 |
c.blockingAwait(); |
| 1312 |
} |
| 1313 |
|
| 1314 |
@Test(expected = TestException.class) |
| 1315 |
public void usingResourceThrows() { |
| 1316 |
Completable c = Completable.using(new Supplier<Object>() { |
| 1317 |
@Override |
| 1318 |
public Object get() { throw new TestException(); } |
| 1319 |
}, |
| 1320 |
new Function<Object, Completable>() { |
| 1321 |
@Override |
| 1322 |
public Completable apply(Object v) { |
| 1323 |
return normal.completable; |
| 1324 |
} |
| 1325 |
}, new Consumer<Object>() { |
| 1326 |
@Override |
| 1327 |
public void accept(Object v) { } |
| 1328 |
}); |
| 1329 |
|
| 1330 |
c.blockingAwait(); |
| 1331 |
} |
| 1332 |
|
| 1333 |
@Test(expected = TestException.class) |
| 1334 |
public void usingMapperThrows() { |
| 1335 |
Completable c = Completable.using(new Supplier<Object>() { |
| 1336 |
@Override |
| 1337 |
public Object get() { |
| 1338 |
return 1; |
| 1339 |
} |
| 1340 |
}, |
| 1341 |
new Function<Object, Completable>() { |
| 1342 |
@Override |
| 1343 |
public Completable apply(Object v) { throw new TestException(); } |
| 1344 |
}, new Consumer<Object>() { |
| 1345 |
@Override |
| 1346 |
public void accept(Object v) { } |
| 1347 |
}); |
| 1348 |
|
| 1349 |
c.blockingAwait(); |
| 1350 |
} |
| 1351 |
|
| 1352 |
@Test(expected = TestException.class) |
| 1353 |
public void usingDisposerThrows() { |
| 1354 |
Completable c = Completable.using(new Supplier<Object>() { |
| 1355 |
@Override |
| 1356 |
public Object get() { |
| 1357 |
return 1; |
| 1358 |
} |
| 1359 |
}, |
| 1360 |
new Function<Object, Completable>() { |
| 1361 |
@Override |
| 1362 |
public Completable apply(Object v) { |
| 1363 |
return normal.completable; |
| 1364 |
} |
| 1365 |
}, new Consumer<Object>() { |
| 1366 |
@Override |
| 1367 |
public void accept(Object v) { throw new TestException(); } |
| 1368 |
}); |
| 1369 |
|
| 1370 |
c.blockingAwait(); |
| 1371 |
} |
| 1372 |
|
| 1373 |
@Test |
| 1374 |
public void composeNormal() { |
| 1375 |
Completable c = error.completable.compose(new CompletableTransformer() { |
| 1376 |
@Override |
| 1377 |
public Completable apply(Completable n) { |
| 1378 |
return n.onErrorComplete(); |
| 1379 |
} |
| 1380 |
}); |
| 1381 |
|
| 1382 |
c.blockingAwait(); |
| 1383 |
} |
| 1384 |
|
| 1385 |
@Test |
| 1386 |
public void concatWithNormal() { |
| 1387 |
Completable c = normal.completable.concatWith(normal.completable); |
| 1388 |
|
| 1389 |
c.blockingAwait(); |
| 1390 |
|
| 1391 |
normal.assertSubscriptions(2); |
| 1392 |
} |
| 1393 |
|
| 1394 |
@Test(expected = TestException.class) |
| 1395 |
public void concatWithError() { |
| 1396 |
Completable c = normal.completable.concatWith(error.completable); |
| 1397 |
|
| 1398 |
c.blockingAwait(); |
| 1399 |
} |
| 1400 |
|
| 1401 |
@Test |
| 1402 |
public void delayNormal() throws InterruptedException { |
| 1403 |
Completable c = normal.completable.delay(250, TimeUnit.MILLISECONDS); |
| 1404 |
|
| 1405 |
final AtomicBoolean done = new AtomicBoolean(); |
| 1406 |
final AtomicReference<Throwable> error = new AtomicReference<>(); |
| 1407 |
|
| 1408 |
c.subscribe(new CompletableObserver() { |
| 1409 |
@Override |
| 1410 |
public void onSubscribe(Disposable d) { |
| 1411 |
|
| 1412 |
} |
| 1413 |
|
| 1414 |
@Override |
| 1415 |
public void onError(Throwable e) { |
| 1416 |
error.set(e); |
| 1417 |
} |
| 1418 |
|
| 1419 |
@Override |
| 1420 |
public void onComplete() { |
| 1421 |
done.set(true); |
| 1422 |
} |
| 1423 |
}); |
| 1424 |
|
| 1425 |
Thread.sleep(100); |
| 1426 |
|
| 1427 |
Assert.assertFalse("Already done", done.get()); |
| 1428 |
|
| 1429 |
int timeout = 10; |
| 1430 |
|
| 1431 |
while (timeout-- > 0 && !done.get()) { |
| 1432 |
Thread.sleep(100); |
| 1433 |
} |
| 1434 |
|
| 1435 |
Assert.assertTrue("Not done", done.get()); |
| 1436 |
|
| 1437 |
Assert.assertNull(error.get()); |
| 1438 |
} |
| 1439 |
|
| 1440 |
@Test |
| 1441 |
public void delayErrorImmediately() throws InterruptedException { |
| 1442 |
final TestScheduler scheduler = new TestScheduler(); |
| 1443 |
final Completable c = error.completable.delay(250, TimeUnit.MILLISECONDS, scheduler); |
| 1444 |
|
| 1445 |
final AtomicBoolean done = new AtomicBoolean(); |
| 1446 |
final AtomicReference<Throwable> error = new AtomicReference<>(); |
| 1447 |
|
| 1448 |
c.subscribe(new CompletableObserver() { |
| 1449 |
@Override |
| 1450 |
public void onSubscribe(Disposable d) { |
| 1451 |
|
| 1452 |
} |
| 1453 |
|
| 1454 |
@Override |
| 1455 |
public void onError(Throwable e) { |
| 1456 |
error.set(e); |
| 1457 |
} |
| 1458 |
|
| 1459 |
@Override |
| 1460 |
public void onComplete() { |
| 1461 |
done.set(true); |
| 1462 |
} |
| 1463 |
}); |
| 1464 |
|
| 1465 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 1466 |
|
| 1467 |
Assert.assertTrue(error.get().toString(), error.get() instanceof TestException); |
| 1468 |
Assert.assertFalse("Already done", done.get()); |
| 1469 |
|
| 1470 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 1471 |
|
| 1472 |
Assert.assertFalse("Already done", done.get()); |
| 1473 |
} |
| 1474 |
|
| 1475 |
@Test |
| 1476 |
public void delayErrorToo() throws InterruptedException { |
| 1477 |
Completable c = error.completable.delay(250, TimeUnit.MILLISECONDS, Schedulers.computation(), true); |
| 1478 |
|
| 1479 |
final AtomicBoolean done = new AtomicBoolean(); |
| 1480 |
final AtomicReference<Throwable> error = new AtomicReference<>(); |
| 1481 |
|
| 1482 |
c.subscribe(new CompletableObserver() { |
| 1483 |
@Override |
| 1484 |
public void onSubscribe(Disposable d) { |
| 1485 |
|
| 1486 |
} |
| 1487 |
|
| 1488 |
@Override |
| 1489 |
public void onError(Throwable e) { |
| 1490 |
error.set(e); |
| 1491 |
} |
| 1492 |
|
| 1493 |
@Override |
| 1494 |
public void onComplete() { |
| 1495 |
done.set(true); |
| 1496 |
} |
| 1497 |
}); |
| 1498 |
|
| 1499 |
Thread.sleep(100); |
| 1500 |
|
| 1501 |
Assert.assertFalse("Already done", done.get()); |
| 1502 |
Assert.assertNull(error.get()); |
| 1503 |
|
| 1504 |
Thread.sleep(200); |
| 1505 |
|
| 1506 |
Assert.assertFalse("Already done", done.get()); |
| 1507 |
Assert.assertTrue(error.get() instanceof TestException); |
| 1508 |
} |
| 1509 |
|
| 1510 |
@Test |
| 1511 |
public void doOnCompleteNormal() { |
| 1512 |
final AtomicInteger calls = new AtomicInteger(); |
| 1513 |
|
| 1514 |
Completable c = normal.completable.doOnComplete(new Action() { |
| 1515 |
@Override |
| 1516 |
public void run() { |
| 1517 |
calls.getAndIncrement(); |
| 1518 |
} |
| 1519 |
}); |
| 1520 |
|
| 1521 |
c.blockingAwait(); |
| 1522 |
|
| 1523 |
Assert.assertEquals(1, calls.get()); |
| 1524 |
} |
| 1525 |
|
| 1526 |
@Test |
| 1527 |
public void doOnCompleteError() { |
| 1528 |
final AtomicInteger calls = new AtomicInteger(); |
| 1529 |
|
| 1530 |
Completable c = error.completable.doOnComplete(new Action() { |
| 1531 |
@Override |
| 1532 |
public void run() { |
| 1533 |
calls.getAndIncrement(); |
| 1534 |
} |
| 1535 |
}); |
| 1536 |
|
| 1537 |
try { |
| 1538 |
c.blockingAwait(); |
| 1539 |
Assert.fail("Failed to throw TestException"); |
| 1540 |
} catch (TestException ex) { |
| 1541 |
// expected |
| 1542 |
} |
| 1543 |
|
| 1544 |
Assert.assertEquals(0, calls.get()); |
| 1545 |
} |
| 1546 |
|
| 1547 |
@Test(expected = TestException.class) |
| 1548 |
public void doOnCompleteThrows() { |
| 1549 |
Completable c = normal.completable.doOnComplete(new Action() { |
| 1550 |
@Override |
| 1551 |
public void run() { throw new TestException(); } |
| 1552 |
}); |
| 1553 |
|
| 1554 |
c.blockingAwait(); |
| 1555 |
} |
| 1556 |
|
| 1557 |
@Test |
| 1558 |
public void doOnDisposeNormalDoesntCall() { |
| 1559 |
final AtomicInteger calls = new AtomicInteger(); |
| 1560 |
|
| 1561 |
Completable c = normal.completable.doOnDispose(new Action() { |
| 1562 |
@Override |
| 1563 |
public void run() { |
| 1564 |
calls.getAndIncrement(); |
| 1565 |
} |
| 1566 |
}); |
| 1567 |
|
| 1568 |
c.blockingAwait(); |
| 1569 |
|
| 1570 |
Assert.assertEquals(0, calls.get()); |
| 1571 |
} |
| 1572 |
|
| 1573 |
@Test |
| 1574 |
public void doOnDisposeErrorDoesntCall() { |
| 1575 |
final AtomicInteger calls = new AtomicInteger(); |
| 1576 |
|
| 1577 |
Completable c = error.completable.doOnDispose(new Action() { |
| 1578 |
@Override |
| 1579 |
public void run() { |
| 1580 |
calls.getAndIncrement(); |
| 1581 |
} |
| 1582 |
}); |
| 1583 |
|
| 1584 |
try { |
| 1585 |
c.blockingAwait(); |
| 1586 |
Assert.fail("No exception thrown"); |
| 1587 |
} catch (TestException ex) { |
| 1588 |
// expected |
| 1589 |
} |
| 1590 |
Assert.assertEquals(0, calls.get()); |
| 1591 |
} |
| 1592 |
|
| 1593 |
@Test |
| 1594 |
public void doOnDisposeChildCancels() { |
| 1595 |
final AtomicInteger calls = new AtomicInteger(); |
| 1596 |
|
| 1597 |
Completable c = normal.completable.doOnDispose(new Action() { |
| 1598 |
@Override |
| 1599 |
public void run() { |
| 1600 |
calls.getAndIncrement(); |
| 1601 |
} |
| 1602 |
}); |
| 1603 |
|
| 1604 |
c.subscribe(new CompletableObserver() { |
| 1605 |
@Override |
| 1606 |
public void onSubscribe(Disposable d) { |
| 1607 |
d.dispose(); |
| 1608 |
} |
| 1609 |
|
| 1610 |
@Override |
| 1611 |
public void onError(Throwable e) { |
| 1612 |
// ignored |
| 1613 |
} |
| 1614 |
|
| 1615 |
@Override |
| 1616 |
public void onComplete() { |
| 1617 |
// ignored |
| 1618 |
} |
| 1619 |
}); |
| 1620 |
|
| 1621 |
Assert.assertEquals(1, calls.get()); |
| 1622 |
} |
| 1623 |
|
| 1624 |
@Test |
| 1625 |
public void doOnDisposeThrows() { |
| 1626 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1627 |
try { |
| 1628 |
Completable c = normal.completable.doOnDispose(new Action() { |
| 1629 |
@Override |
| 1630 |
public void run() { throw new TestException(); } |
| 1631 |
}); |
| 1632 |
|
| 1633 |
c.subscribe(new CompletableObserver() { |
| 1634 |
@Override |
| 1635 |
public void onSubscribe(Disposable d) { |
| 1636 |
d.dispose(); |
| 1637 |
} |
| 1638 |
|
| 1639 |
@Override |
| 1640 |
public void onError(Throwable e) { |
| 1641 |
// ignored |
| 1642 |
} |
| 1643 |
|
| 1644 |
@Override |
| 1645 |
public void onComplete() { |
| 1646 |
// ignored |
| 1647 |
} |
| 1648 |
}); |
| 1649 |
|
| 1650 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 1651 |
} finally { |
| 1652 |
RxJavaPlugins.reset(); |
| 1653 |
} |
| 1654 |
} |
| 1655 |
|
| 1656 |
@Test |
| 1657 |
public void doOnErrorNoError() { |
| 1658 |
final AtomicReference<Throwable> error = new AtomicReference<>(); |
| 1659 |
|
| 1660 |
Completable c = normal.completable.doOnError(new Consumer<Throwable>() { |
| 1661 |
@Override |
| 1662 |
public void accept(Throwable e) { |
| 1663 |
error.set(e); |
| 1664 |
} |
| 1665 |
}); |
| 1666 |
|
| 1667 |
c.blockingAwait(); |
| 1668 |
|
| 1669 |
Assert.assertNull(error.get()); |
| 1670 |
} |
| 1671 |
|
| 1672 |
@Test |
| 1673 |
public void doOnErrorHasError() { |
| 1674 |
final AtomicReference<Throwable> err = new AtomicReference<>(); |
| 1675 |
|
| 1676 |
Completable c = error.completable.doOnError(new Consumer<Throwable>() { |
| 1677 |
@Override |
| 1678 |
public void accept(Throwable e) { |
| 1679 |
err.set(e); |
| 1680 |
} |
| 1681 |
}); |
| 1682 |
|
| 1683 |
try { |
| 1684 |
c.blockingAwait(); |
| 1685 |
Assert.fail("Did not throw exception"); |
| 1686 |
} catch (Throwable e) { |
| 1687 |
// expected |
| 1688 |
} |
| 1689 |
|
| 1690 |
Assert.assertTrue(err.get() instanceof TestException); |
| 1691 |
} |
| 1692 |
|
| 1693 |
@Test |
| 1694 |
public void doOnErrorThrows() { |
| 1695 |
Completable c = error.completable.doOnError(new Consumer<Throwable>() { |
| 1696 |
@Override |
| 1697 |
public void accept(Throwable e) { throw new IllegalStateException(); } |
| 1698 |
}); |
| 1699 |
|
| 1700 |
try { |
| 1701 |
c.blockingAwait(); |
| 1702 |
} catch (CompositeException ex) { |
| 1703 |
List<Throwable> a = ex.getExceptions(); |
| 1704 |
Assert.assertEquals(2, a.size()); |
| 1705 |
Assert.assertTrue(a.get(0) instanceof TestException); |
| 1706 |
Assert.assertTrue(a.get(1) instanceof IllegalStateException); |
| 1707 |
} |
| 1708 |
} |
| 1709 |
|
| 1710 |
@Test |
| 1711 |
public void doOnSubscribeNormal() { |
| 1712 |
final AtomicInteger calls = new AtomicInteger(); |
| 1713 |
|
| 1714 |
Completable c = normal.completable.doOnSubscribe(new Consumer<Disposable>() { |
| 1715 |
@Override |
| 1716 |
public void accept(Disposable d) { |
| 1717 |
calls.getAndIncrement(); |
| 1718 |
} |
| 1719 |
}); |
| 1720 |
|
| 1721 |
for (int i = 0; i < 10; i++) { |
| 1722 |
c.blockingAwait(); |
| 1723 |
} |
| 1724 |
|
| 1725 |
Assert.assertEquals(10, calls.get()); |
| 1726 |
} |
| 1727 |
|
| 1728 |
@Test(expected = TestException.class) |
| 1729 |
public void doOnSubscribeThrows() { |
| 1730 |
Completable c = normal.completable.doOnSubscribe(new Consumer<Disposable>() { |
| 1731 |
@Override |
| 1732 |
public void accept(Disposable d) { throw new TestException(); } |
| 1733 |
}); |
| 1734 |
|
| 1735 |
c.blockingAwait(); |
| 1736 |
} |
| 1737 |
|
| 1738 |
@Test |
| 1739 |
public void doOnTerminateNormal() { |
| 1740 |
final AtomicInteger calls = new AtomicInteger(); |
| 1741 |
|
| 1742 |
Completable c = normal.completable.doOnTerminate(new Action() { |
| 1743 |
@Override |
| 1744 |
public void run() { |
| 1745 |
calls.getAndIncrement(); |
| 1746 |
} |
| 1747 |
}); |
| 1748 |
|
| 1749 |
c.blockingAwait(); |
| 1750 |
|
| 1751 |
Assert.assertEquals(1, calls.get()); |
| 1752 |
} |
| 1753 |
|
| 1754 |
@Test |
| 1755 |
public void doOnTerminateError() { |
| 1756 |
final AtomicInteger calls = new AtomicInteger(); |
| 1757 |
|
| 1758 |
Completable c = error.completable.doOnTerminate(new Action() { |
| 1759 |
@Override |
| 1760 |
public void run() { |
| 1761 |
calls.getAndIncrement(); |
| 1762 |
} |
| 1763 |
}); |
| 1764 |
|
| 1765 |
try { |
| 1766 |
c.blockingAwait(); |
| 1767 |
Assert.fail("Did dot throw exception"); |
| 1768 |
} catch (TestException ex) { |
| 1769 |
// expected |
| 1770 |
} |
| 1771 |
|
| 1772 |
Assert.assertEquals(1, calls.get()); |
| 1773 |
} |
| 1774 |
|
| 1775 |
@Test(expected = NullPointerException.class) |
| 1776 |
public void liftReturnsNull() { |
| 1777 |
Completable c = normal.completable.lift(new CompletableOperator() { |
| 1778 |
@Override |
| 1779 |
public CompletableObserver apply(CompletableObserver v) { |
| 1780 |
return null; |
| 1781 |
} |
| 1782 |
}); |
| 1783 |
|
| 1784 |
c.blockingAwait(); |
| 1785 |
} |
| 1786 |
|
| 1787 |
static final class CompletableOperatorSwap implements CompletableOperator { |
| 1788 |
@Override |
| 1789 |
public CompletableObserver apply(final CompletableObserver v) { |
| 1790 |
return new CompletableObserver() { |
| 1791 |
|
| 1792 |
@Override |
| 1793 |
public void onComplete() { |
| 1794 |
v.onError(new TestException()); |
| 1795 |
} |
| 1796 |
|
| 1797 |
@Override |
| 1798 |
public void onError(Throwable e) { |
| 1799 |
v.onComplete(); |
| 1800 |
} |
| 1801 |
|
| 1802 |
@Override |
| 1803 |
public void onSubscribe(Disposable d) { |
| 1804 |
v.onSubscribe(d); |
| 1805 |
} |
| 1806 |
|
| 1807 |
}; |
| 1808 |
} |
| 1809 |
} |
| 1810 |
|
| 1811 |
@Test(expected = TestException.class) |
| 1812 |
public void liftOnCompleteError() { |
| 1813 |
Completable c = normal.completable.lift(new CompletableOperatorSwap()); |
| 1814 |
|
| 1815 |
c.blockingAwait(); |
| 1816 |
} |
| 1817 |
|
| 1818 |
@Test |
| 1819 |
public void liftOnErrorComplete() { |
| 1820 |
Completable c = error.completable.lift(new CompletableOperatorSwap()); |
| 1821 |
|
| 1822 |
c.blockingAwait(); |
| 1823 |
} |
| 1824 |
|
| 1825 |
@Test |
| 1826 |
public void mergeWithNormal() { |
| 1827 |
Completable c = normal.completable.mergeWith(normal.completable); |
| 1828 |
|
| 1829 |
c.blockingAwait(); |
| 1830 |
|
| 1831 |
normal.assertSubscriptions(2); |
| 1832 |
} |
| 1833 |
|
| 1834 |
@Test |
| 1835 |
public void observeOnNormal() throws InterruptedException { |
| 1836 |
final AtomicReference<String> name = new AtomicReference<>(); |
| 1837 |
final AtomicReference<Throwable> err = new AtomicReference<>(); |
| 1838 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 1839 |
|
| 1840 |
Completable c = normal.completable.observeOn(Schedulers.computation()); |
| 1841 |
|
| 1842 |
c.subscribe(new CompletableObserver() { |
| 1843 |
@Override |
| 1844 |
public void onSubscribe(Disposable d) { |
| 1845 |
|
| 1846 |
} |
| 1847 |
|
| 1848 |
@Override |
| 1849 |
public void onComplete() { |
| 1850 |
name.set(Thread.currentThread().getName()); |
| 1851 |
cdl.countDown(); |
| 1852 |
} |
| 1853 |
|
| 1854 |
@Override |
| 1855 |
public void onError(Throwable e) { |
| 1856 |
err.set(e); |
| 1857 |
cdl.countDown(); |
| 1858 |
} |
| 1859 |
}); |
| 1860 |
|
| 1861 |
cdl.await(); |
| 1862 |
|
| 1863 |
Assert.assertNull(err.get()); |
| 1864 |
Assert.assertTrue(name.get().startsWith("RxComputation")); |
| 1865 |
} |
| 1866 |
|
| 1867 |
@Test |
| 1868 |
public void observeOnError() throws InterruptedException { |
| 1869 |
final AtomicReference<String> name = new AtomicReference<>(); |
| 1870 |
final AtomicReference<Throwable> err = new AtomicReference<>(); |
| 1871 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 1872 |
|
| 1873 |
Completable c = error.completable.observeOn(Schedulers.computation()); |
| 1874 |
|
| 1875 |
c.subscribe(new CompletableObserver() { |
| 1876 |
@Override |
| 1877 |
public void onSubscribe(Disposable d) { |
| 1878 |
|
| 1879 |
} |
| 1880 |
|
| 1881 |
@Override |
| 1882 |
public void onComplete() { |
| 1883 |
name.set(Thread.currentThread().getName()); |
| 1884 |
cdl.countDown(); |
| 1885 |
} |
| 1886 |
|
| 1887 |
@Override |
| 1888 |
public void onError(Throwable e) { |
| 1889 |
name.set(Thread.currentThread().getName()); |
| 1890 |
err.set(e); |
| 1891 |
cdl.countDown(); |
| 1892 |
} |
| 1893 |
}); |
| 1894 |
|
| 1895 |
cdl.await(); |
| 1896 |
|
| 1897 |
Assert.assertTrue(err.get() instanceof TestException); |
| 1898 |
Assert.assertTrue(name.get().startsWith("RxComputation")); |
| 1899 |
} |
| 1900 |
|
| 1901 |
@Test |
| 1902 |
public void onErrorComplete() { |
| 1903 |
Completable c = error.completable.onErrorComplete(); |
| 1904 |
|
| 1905 |
c.blockingAwait(); |
| 1906 |
} |
| 1907 |
|
| 1908 |
@Test(expected = TestException.class) |
| 1909 |
public void onErrorCompleteFalse() { |
| 1910 |
Completable c = error.completable.onErrorComplete(new Predicate<Throwable>() { |
| 1911 |
@Override |
| 1912 |
public boolean test(Throwable e) { |
| 1913 |
return e instanceof IllegalStateException; |
| 1914 |
} |
| 1915 |
}); |
| 1916 |
|
| 1917 |
c.blockingAwait(); |
| 1918 |
} |
| 1919 |
|
| 1920 |
@Test |
| 1921 |
public void onErrorResumeNextFunctionReturnsNull() { |
| 1922 |
Completable c = error.completable.onErrorResumeNext(new Function<Throwable, Completable>() { |
| 1923 |
@Override |
| 1924 |
public Completable apply(Throwable e) { |
| 1925 |
return null; |
| 1926 |
} |
| 1927 |
}); |
| 1928 |
|
| 1929 |
try { |
| 1930 |
c.blockingAwait(); |
| 1931 |
Assert.fail("Did not throw an exception"); |
| 1932 |
} catch (CompositeException ex) { |
| 1933 |
List<Throwable> errors = ex.getExceptions(); |
| 1934 |
TestHelper.assertError(errors, 0, TestException.class); |
| 1935 |
TestHelper.assertError(errors, 1, NullPointerException.class); |
| 1936 |
assertEquals(2, errors.size()); |
| 1937 |
} |
| 1938 |
} |
| 1939 |
|
| 1940 |
@Test |
| 1941 |
public void onErrorResumeNextFunctionThrows() { |
| 1942 |
Completable c = error.completable.onErrorResumeNext(new Function<Throwable, Completable>() { |
| 1943 |
@Override |
| 1944 |
public Completable apply(Throwable e) { throw new TestException(); } |
| 1945 |
}); |
| 1946 |
|
| 1947 |
try { |
| 1948 |
c.blockingAwait(); |
| 1949 |
Assert.fail("Did not throw an exception"); |
| 1950 |
} catch (CompositeException ex) { |
| 1951 |
List<Throwable> a = ex.getExceptions(); |
| 1952 |
|
| 1953 |
Assert.assertEquals(2, a.size()); |
| 1954 |
Assert.assertTrue(a.get(0) instanceof TestException); |
| 1955 |
Assert.assertTrue(a.get(1) instanceof TestException); |
| 1956 |
} |
| 1957 |
} |
| 1958 |
|
| 1959 |
@Test |
| 1960 |
public void onErrorResumeNextNormal() { |
| 1961 |
Completable c = error.completable.onErrorResumeNext(new Function<Throwable, Completable>() { |
| 1962 |
@Override |
| 1963 |
public Completable apply(Throwable v) { |
| 1964 |
return normal.completable; |
| 1965 |
} |
| 1966 |
}); |
| 1967 |
|
| 1968 |
c.blockingAwait(); |
| 1969 |
} |
| 1970 |
|
| 1971 |
@Test(expected = TestException.class) |
| 1972 |
public void onErrorResumeNextError() { |
| 1973 |
Completable c = error.completable.onErrorResumeNext(new Function<Throwable, Completable>() { |
| 1974 |
@Override |
| 1975 |
public Completable apply(Throwable v) { |
| 1976 |
return error.completable; |
| 1977 |
} |
| 1978 |
}); |
| 1979 |
|
| 1980 |
c.blockingAwait(); |
| 1981 |
} |
| 1982 |
|
| 1983 |
@Test |
| 1984 |
public void repeatNormal() { |
| 1985 |
final AtomicReference<Throwable> err = new AtomicReference<>(); |
| 1986 |
final AtomicInteger calls = new AtomicInteger(); |
| 1987 |
|
| 1988 |
Completable c = Completable.fromCallable(new Callable<Object>() { |
| 1989 |
@Override |
| 1990 |
public Object call() throws Exception { |
| 1991 |
calls.getAndIncrement(); |
| 1992 |
Thread.sleep(100); |
| 1993 |
return null; |
| 1994 |
} |
| 1995 |
}).repeat(); |
| 1996 |
|
| 1997 |
c.subscribe(new CompletableObserver() { |
| 1998 |
@Override |
| 1999 |
public void onSubscribe(final Disposable d) { |
| 2000 |
Schedulers.single().scheduleDirect(new Runnable() { |
| 2001 |
@Override |
| 2002 |
public void run() { |
| 2003 |
d.dispose(); |
| 2004 |
} |
| 2005 |
}, 550, TimeUnit.MILLISECONDS); |
| 2006 |
} |
| 2007 |
|
| 2008 |
@Override |
| 2009 |
public void onError(Throwable e) { |
| 2010 |
err.set(e); |
| 2011 |
} |
| 2012 |
|
| 2013 |
@Override |
| 2014 |
public void onComplete() { |
| 2015 |
|
| 2016 |
} |
| 2017 |
}); |
| 2018 |
|
| 2019 |
Assert.assertEquals(6, calls.get()); |
| 2020 |
Assert.assertNull(err.get()); |
| 2021 |
} |
| 2022 |
|
| 2023 |
@Test(expected = TestException.class) |
| 2024 |
public void repeatError() { |
| 2025 |
Completable c = error.completable.repeat(); |
| 2026 |
|
| 2027 |
c.blockingAwait(); |
| 2028 |
} |
| 2029 |
|
| 2030 |
@Test |
| 2031 |
public void repeat5Times() { |
| 2032 |
final AtomicInteger calls = new AtomicInteger(); |
| 2033 |
|
| 2034 |
Completable c = Completable.fromCallable(new Callable<Object>() { |
| 2035 |
@Override |
| 2036 |
public Object call() throws Exception { |
| 2037 |
calls.getAndIncrement(); |
| 2038 |
return null; |
| 2039 |
} |
| 2040 |
}).repeat(5); |
| 2041 |
|
| 2042 |
c.blockingAwait(); |
| 2043 |
|
| 2044 |
Assert.assertEquals(5, calls.get()); |
| 2045 |
} |
| 2046 |
|
| 2047 |
@Test |
| 2048 |
public void repeat1Time() { |
| 2049 |
final AtomicInteger calls = new AtomicInteger(); |
| 2050 |
|
| 2051 |
Completable c = Completable.fromCallable(new Callable<Object>() { |
| 2052 |
@Override |
| 2053 |
public Object call() throws Exception { |
| 2054 |
calls.getAndIncrement(); |
| 2055 |
return null; |
| 2056 |
} |
| 2057 |
}).repeat(1); |
| 2058 |
|
| 2059 |
c.blockingAwait(); |
| 2060 |
|
| 2061 |
Assert.assertEquals(1, calls.get()); |
| 2062 |
} |
| 2063 |
|
| 2064 |
@Test |
| 2065 |
public void repeat0Time() { |
| 2066 |
final AtomicInteger calls = new AtomicInteger(); |
| 2067 |
|
| 2068 |
Completable c = Completable.fromCallable(new Callable<Object>() { |
| 2069 |
@Override |
| 2070 |
public Object call() throws Exception { |
| 2071 |
calls.getAndIncrement(); |
| 2072 |
return null; |
| 2073 |
} |
| 2074 |
}).repeat(0); |
| 2075 |
|
| 2076 |
c.blockingAwait(); |
| 2077 |
|
| 2078 |
Assert.assertEquals(0, calls.get()); |
| 2079 |
} |
| 2080 |
|
| 2081 |
@Test |
| 2082 |
public void repeatUntilNormal() { |
| 2083 |
final AtomicInteger calls = new AtomicInteger(); |
| 2084 |
final AtomicInteger times = new AtomicInteger(5); |
| 2085 |
|
| 2086 |
Completable c = Completable.fromCallable(new Callable<Object>() { |
| 2087 |
@Override |
| 2088 |
public Object call() throws Exception { |
| 2089 |
calls.getAndIncrement(); |
| 2090 |
return null; |
| 2091 |
} |
| 2092 |
}).repeatUntil(new BooleanSupplier() { |
| 2093 |
@Override |
| 2094 |
public boolean getAsBoolean() { |
| 2095 |
return times.decrementAndGet() == 0; |
| 2096 |
} |
| 2097 |
}); |
| 2098 |
|
| 2099 |
c.blockingAwait(); |
| 2100 |
|
| 2101 |
Assert.assertEquals(5, calls.get()); |
| 2102 |
} |
| 2103 |
|
| 2104 |
@Test |
| 2105 |
public void retryNormal() { |
| 2106 |
Completable c = normal.completable.retry(); |
| 2107 |
|
| 2108 |
c.blockingAwait(); |
| 2109 |
|
| 2110 |
normal.assertSubscriptions(1); |
| 2111 |
} |
| 2112 |
|
| 2113 |
@Test |
| 2114 |
public void retry5Times() { |
| 2115 |
final AtomicInteger calls = new AtomicInteger(5); |
| 2116 |
Completable c = Completable.fromAction(new Action() { |
| 2117 |
@Override |
| 2118 |
public void run() { |
| 2119 |
if (calls.decrementAndGet() != 0) { |
| 2120 |
throw new TestException(); |
| 2121 |
} |
| 2122 |
} |
| 2123 |
}).retry(); |
| 2124 |
|
| 2125 |
c.blockingAwait(); |
| 2126 |
} |
| 2127 |
|
| 2128 |
@Test(expected = TestException.class) |
| 2129 |
public void retryBiPredicate5Times() { |
| 2130 |
Completable c = error.completable.retry(new BiPredicate<Integer, Throwable>() { |
| 2131 |
@Override |
| 2132 |
public boolean test(Integer n, Throwable e) { |
| 2133 |
return n < 5; |
| 2134 |
} |
| 2135 |
}); |
| 2136 |
|
| 2137 |
c.blockingAwait(); |
| 2138 |
} |
| 2139 |
|
| 2140 |
@Test(expected = TestException.class) |
| 2141 |
public void retryTimes5Error() { |
| 2142 |
Completable c = error.completable.retry(5); |
| 2143 |
|
| 2144 |
c.blockingAwait(); |
| 2145 |
} |
| 2146 |
|
| 2147 |
@Test |
| 2148 |
public void retryTimes5Normal() { |
| 2149 |
final AtomicInteger calls = new AtomicInteger(); |
| 2150 |
|
| 2151 |
Completable c = Completable.fromAction(new Action() { |
| 2152 |
@Override |
| 2153 |
public void run() { |
| 2154 |
if (calls.incrementAndGet() != 6) { |
| 2155 |
throw new TestException(); |
| 2156 |
} |
| 2157 |
} |
| 2158 |
}).retry(5); |
| 2159 |
|
| 2160 |
c.blockingAwait(); |
| 2161 |
|
| 2162 |
assertEquals(6, calls.get()); |
| 2163 |
} |
| 2164 |
|
| 2165 |
@Test(expected = IllegalArgumentException.class) |
| 2166 |
public void retryNegativeTimes() { |
| 2167 |
normal.completable.retry(-1); |
| 2168 |
} |
| 2169 |
|
| 2170 |
@Test(expected = TestException.class) |
| 2171 |
public void retryPredicateError() { |
| 2172 |
Completable c = error.completable.retry(new Predicate<Throwable>() { |
| 2173 |
@Override |
| 2174 |
public boolean test(Throwable e) { |
| 2175 |
return false; |
| 2176 |
} |
| 2177 |
}); |
| 2178 |
|
| 2179 |
c.blockingAwait(); |
| 2180 |
} |
| 2181 |
|
| 2182 |
@Test |
| 2183 |
public void retryPredicate5Times() { |
| 2184 |
final AtomicInteger calls = new AtomicInteger(5); |
| 2185 |
|
| 2186 |
Completable c = Completable.fromAction(new Action() { |
| 2187 |
@Override |
| 2188 |
public void run() { |
| 2189 |
if (calls.decrementAndGet() != 0) { |
| 2190 |
throw new TestException(); |
| 2191 |
} |
| 2192 |
} |
| 2193 |
}).retry(new Predicate<Throwable>() { |
| 2194 |
@Override |
| 2195 |
public boolean test(Throwable e) { |
| 2196 |
return true; |
| 2197 |
} |
| 2198 |
}); |
| 2199 |
|
| 2200 |
c.blockingAwait(); |
| 2201 |
} |
| 2202 |
|
| 2203 |
@Test |
| 2204 |
public void retryWhen5Times() { |
| 2205 |
final AtomicInteger calls = new AtomicInteger(5); |
| 2206 |
|
| 2207 |
Completable c = Completable.fromAction(new Action() { |
| 2208 |
@Override |
| 2209 |
public void run() { |
| 2210 |
if (calls.decrementAndGet() != 0) { |
| 2211 |
throw new TestException(); |
| 2212 |
} |
| 2213 |
} |
| 2214 |
}).retryWhen(new Function<Flowable<? extends Throwable>, Publisher<Object>>() { |
| 2215 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 2216 |
@Override |
| 2217 |
public Publisher<Object> apply(Flowable<? extends Throwable> f) { |
| 2218 |
return (Publisher)f; |
| 2219 |
} |
| 2220 |
}); |
| 2221 |
|
| 2222 |
c.blockingAwait(); |
| 2223 |
} |
| 2224 |
|
| 2225 |
@Test |
| 2226 |
public void subscribe() throws InterruptedException { |
| 2227 |
final AtomicBoolean complete = new AtomicBoolean(); |
| 2228 |
|
| 2229 |
Completable c = normal.completable |
| 2230 |
.delay(100, TimeUnit.MILLISECONDS) |
| 2231 |
.doOnComplete(new Action() { |
| 2232 |
@Override |
| 2233 |
public void run() { |
| 2234 |
complete.set(true); |
| 2235 |
} |
| 2236 |
}); |
| 2237 |
|
| 2238 |
Disposable d = c.subscribe(); |
| 2239 |
|
| 2240 |
assertFalse(d.isDisposed()); |
| 2241 |
|
| 2242 |
Thread.sleep(150); |
| 2243 |
|
| 2244 |
Assert.assertTrue("Not completed", complete.get()); |
| 2245 |
|
| 2246 |
assertTrue(d.isDisposed()); |
| 2247 |
} |
| 2248 |
|
| 2249 |
@Test |
| 2250 |
public void subscribeDispose() throws InterruptedException { |
| 2251 |
final AtomicBoolean complete = new AtomicBoolean(); |
| 2252 |
|
| 2253 |
Completable c = normal.completable |
| 2254 |
.delay(200, TimeUnit.MILLISECONDS) |
| 2255 |
.doOnComplete(new Action() { |
| 2256 |
@Override |
| 2257 |
public void run() { |
| 2258 |
complete.set(true); |
| 2259 |
} |
| 2260 |
}); |
| 2261 |
|
| 2262 |
Disposable d = c.subscribe(); |
| 2263 |
|
| 2264 |
Thread.sleep(100); |
| 2265 |
|
| 2266 |
d.dispose(); |
| 2267 |
|
| 2268 |
Thread.sleep(150); |
| 2269 |
|
| 2270 |
Assert.assertFalse("Completed", complete.get()); |
| 2271 |
} |
| 2272 |
|
| 2273 |
@Test |
| 2274 |
public void subscribeTwoCallbacksNormal() { |
| 2275 |
final AtomicReference<Throwable> err = new AtomicReference<>(); |
| 2276 |
final AtomicBoolean complete = new AtomicBoolean(); |
| 2277 |
normal.completable.subscribe(new Action() { |
| 2278 |
@Override |
| 2279 |
public void run() { |
| 2280 |
complete.set(true); |
| 2281 |
} |
| 2282 |
}, new Consumer<Throwable>() { |
| 2283 |
@Override |
| 2284 |
public void accept(Throwable e) { |
| 2285 |
err.set(e); |
| 2286 |
} |
| 2287 |
}); |
| 2288 |
|
| 2289 |
Assert.assertNull(err.get()); |
| 2290 |
Assert.assertTrue("Not completed", complete.get()); |
| 2291 |
} |
| 2292 |
|
| 2293 |
@Test |
| 2294 |
public void subscribeTwoCallbacksError() { |
| 2295 |
final AtomicReference<Throwable> err = new AtomicReference<>(); |
| 2296 |
final AtomicBoolean complete = new AtomicBoolean(); |
| 2297 |
error.completable.subscribe(new Action() { |
| 2298 |
@Override |
| 2299 |
public void run() { |
| 2300 |
complete.set(true); |
| 2301 |
} |
| 2302 |
}, new Consumer<Throwable>() { |
| 2303 |
@Override |
| 2304 |
public void accept(Throwable e) { |
| 2305 |
err.set(e); |
| 2306 |
} |
| 2307 |
}); |
| 2308 |
|
| 2309 |
Assert.assertTrue(err.get() instanceof TestException); |
| 2310 |
Assert.assertFalse("Not completed", complete.get()); |
| 2311 |
} |
| 2312 |
|
| 2313 |
@Test |
| 2314 |
public void subscribeTwoCallbacksCompleteThrows() { |
| 2315 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 2316 |
try { |
| 2317 |
final AtomicReference<Throwable> err = new AtomicReference<>(); |
| 2318 |
normal.completable.subscribe(new Action() { |
| 2319 |
@Override |
| 2320 |
public void run() { throw new TestException(); } |
| 2321 |
}, new Consumer<Throwable>() { |
| 2322 |
@Override |
| 2323 |
public void accept(Throwable e) { |
| 2324 |
err.set(e); |
| 2325 |
} |
| 2326 |
}); |
| 2327 |
|
| 2328 |
Assert.assertNull(err.get()); |
| 2329 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 2330 |
} finally { |
| 2331 |
RxJavaPlugins.reset(); |
| 2332 |
} |
| 2333 |
} |
| 2334 |
|
| 2335 |
@Test |
| 2336 |
public void subscribeTwoCallbacksOnErrorThrows() { |
| 2337 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 2338 |
try { |
| 2339 |
error.completable.subscribe(new Action() { |
| 2340 |
@Override |
| 2341 |
public void run() { } |
| 2342 |
}, new Consumer<Throwable>() { |
| 2343 |
@Override |
| 2344 |
public void accept(Throwable e) { throw new TestException(); } |
| 2345 |
}); |
| 2346 |
|
| 2347 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 2348 |
} finally { |
| 2349 |
RxJavaPlugins.reset(); |
| 2350 |
} |
| 2351 |
} |
| 2352 |
|
| 2353 |
@Test |
| 2354 |
public void subscribeObserverNormal() { |
| 2355 |
TestObserver<Object> to = new TestObserver<>(); |
| 2356 |
|
| 2357 |
normal.completable.toObservable().subscribe(to); |
| 2358 |
|
| 2359 |
to.assertComplete(); |
| 2360 |
to.assertNoValues(); |
| 2361 |
to.assertNoErrors(); |
| 2362 |
} |
| 2363 |
|
| 2364 |
@Test |
| 2365 |
public void subscribeObserverError() { |
| 2366 |
TestObserver<Object> to = new TestObserver<>(); |
| 2367 |
|
| 2368 |
error.completable.toObservable().subscribe(to); |
| 2369 |
|
| 2370 |
to.assertNotComplete(); |
| 2371 |
to.assertNoValues(); |
| 2372 |
to.assertError(TestException.class); |
| 2373 |
} |
| 2374 |
|
| 2375 |
@Test |
| 2376 |
public void subscribeActionNormal() { |
| 2377 |
final AtomicBoolean run = new AtomicBoolean(); |
| 2378 |
|
| 2379 |
normal.completable.subscribe(new Action() { |
| 2380 |
@Override |
| 2381 |
public void run() { |
| 2382 |
run.set(true); |
| 2383 |
} |
| 2384 |
}); |
| 2385 |
|
| 2386 |
Assert.assertTrue("Not completed", run.get()); |
| 2387 |
} |
| 2388 |
|
| 2389 |
@Test |
| 2390 |
public void subscribeActionError() { |
| 2391 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 2392 |
try { |
| 2393 |
final AtomicBoolean run = new AtomicBoolean(); |
| 2394 |
|
| 2395 |
error.completable.subscribe(new Action() { |
| 2396 |
@Override |
| 2397 |
public void run() { |
| 2398 |
run.set(true); |
| 2399 |
} |
| 2400 |
}); |
| 2401 |
|
| 2402 |
Assert.assertFalse("Completed", run.get()); |
| 2403 |
|
| 2404 |
TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class); |
| 2405 |
} finally { |
| 2406 |
RxJavaPlugins.reset(); |
| 2407 |
} |
| 2408 |
} |
| 2409 |
|
| 2410 |
@Test |
| 2411 |
public void subscribeSubscriberNormal() { |
| 2412 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 2413 |
|
| 2414 |
normal.completable.toFlowable().subscribe(ts); |
| 2415 |
|
| 2416 |
ts.assertComplete(); |
| 2417 |
ts.assertNoValues(); |
| 2418 |
ts.assertNoErrors(); |
| 2419 |
} |
| 2420 |
|
| 2421 |
@Test |
| 2422 |
public void subscribeSubscriberError() { |
| 2423 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 2424 |
|
| 2425 |
error.completable.toFlowable().subscribe(ts); |
| 2426 |
|
| 2427 |
ts.assertNotComplete(); |
| 2428 |
ts.assertNoValues(); |
| 2429 |
ts.assertError(TestException.class); |
| 2430 |
} |
| 2431 |
|
| 2432 |
@Test |
| 2433 |
public void subscribeOnNormal() { |
| 2434 |
final AtomicReference<String> name = new AtomicReference<>(); |
| 2435 |
|
| 2436 |
Completable c = Completable.unsafeCreate(new CompletableSource() { |
| 2437 |
@Override |
| 2438 |
public void subscribe(CompletableObserver observer) { |
| 2439 |
name.set(Thread.currentThread().getName()); |
| 2440 |
EmptyDisposable.complete(observer); |
| 2441 |
} |
| 2442 |
}).subscribeOn(Schedulers.computation()); |
| 2443 |
|
| 2444 |
c.blockingAwait(); |
| 2445 |
|
| 2446 |
Assert.assertTrue(name.get().startsWith("RxComputation")); |
| 2447 |
} |
| 2448 |
|
| 2449 |
@Test |
| 2450 |
public void subscribeOnError() { |
| 2451 |
final AtomicReference<String> name = new AtomicReference<>(); |
| 2452 |
|
| 2453 |
Completable c = Completable.unsafeCreate(new CompletableSource() { |
| 2454 |
@Override |
| 2455 |
public void subscribe(CompletableObserver observer) { |
| 2456 |
name.set(Thread.currentThread().getName()); |
| 2457 |
EmptyDisposable.error(new TestException(), observer); |
| 2458 |
} |
| 2459 |
}).subscribeOn(Schedulers.computation()); |
| 2460 |
|
| 2461 |
try { |
| 2462 |
c.blockingAwait(); |
| 2463 |
Assert.fail("No exception thrown"); |
| 2464 |
} catch (TestException ex) { |
| 2465 |
// expected |
| 2466 |
} |
| 2467 |
|
| 2468 |
Assert.assertTrue(name.get().startsWith("RxComputation")); |
| 2469 |
} |
| 2470 |
|
| 2471 |
@Test |
| 2472 |
public void timeoutSwitchNormal() { |
| 2473 |
Completable c = Completable.never().timeout(100, TimeUnit.MILLISECONDS, normal.completable); |
| 2474 |
|
| 2475 |
c.blockingAwait(); |
| 2476 |
|
| 2477 |
normal.assertSubscriptions(1); |
| 2478 |
} |
| 2479 |
|
| 2480 |
@Test |
| 2481 |
public void timeoutTimerCancelled() throws InterruptedException { |
| 2482 |
Completable c = Completable.fromCallable(new Callable<Object>() { |
| 2483 |
@Override |
| 2484 |
public Object call() throws Exception { |
| 2485 |
Thread.sleep(50); |
| 2486 |
return null; |
| 2487 |
} |
| 2488 |
}).timeout(100, TimeUnit.MILLISECONDS, normal.completable); |
| 2489 |
|
| 2490 |
c.blockingAwait(); |
| 2491 |
|
| 2492 |
Thread.sleep(100); |
| 2493 |
|
| 2494 |
normal.assertSubscriptions(0); |
| 2495 |
} |
| 2496 |
|
| 2497 |
@Test |
| 2498 |
public void toNormal() { |
| 2499 |
normal.completable |
| 2500 |
.to(new CompletableConverter<Flowable<Object>>() { |
| 2501 |
@Override |
| 2502 |
public Flowable<Object> apply(Completable c) { |
| 2503 |
return c.toFlowable(); |
| 2504 |
} |
| 2505 |
}) |
| 2506 |
.test() |
| 2507 |
.assertComplete() |
| 2508 |
.assertNoValues(); |
| 2509 |
} |
| 2510 |
|
| 2511 |
@Test |
| 2512 |
public void asNormal() { |
| 2513 |
normal.completable |
| 2514 |
.to(new CompletableConverter<Flowable<Object>>() { |
| 2515 |
@Override |
| 2516 |
public Flowable<Object> apply(Completable c) { |
| 2517 |
return c.toFlowable(); |
| 2518 |
} |
| 2519 |
}) |
| 2520 |
.test() |
| 2521 |
.assertComplete() |
| 2522 |
.assertNoValues(); |
| 2523 |
} |
| 2524 |
|
| 2525 |
@Test |
| 2526 |
public void as() { |
| 2527 |
Completable.complete().to(new CompletableConverter<Flowable<Integer>>() { |
| 2528 |
@Override |
| 2529 |
public Flowable<Integer> apply(Completable v) { |
| 2530 |
return v.toFlowable(); |
| 2531 |
} |
| 2532 |
}) |
| 2533 |
.test() |
| 2534 |
.assertComplete(); |
| 2535 |
} |
| 2536 |
|
| 2537 |
@Test |
| 2538 |
public void toFlowableNormal() { |
| 2539 |
normal.completable.toFlowable().blockingForEach(Functions.emptyConsumer()); |
| 2540 |
} |
| 2541 |
|
| 2542 |
@Test(expected = TestException.class) |
| 2543 |
public void toFlowableError() { |
| 2544 |
error.completable.toFlowable().blockingForEach(Functions.emptyConsumer()); |
| 2545 |
} |
| 2546 |
|
| 2547 |
@Test |
| 2548 |
public void toObservableNormal() { |
| 2549 |
normal.completable.toObservable().blockingForEach(Functions.emptyConsumer()); |
| 2550 |
} |
| 2551 |
|
| 2552 |
@Test(expected = TestException.class) |
| 2553 |
public void toObservableError() { |
| 2554 |
error.completable.toObservable().blockingForEach(Functions.emptyConsumer()); |
| 2555 |
} |
| 2556 |
|
| 2557 |
@Test |
| 2558 |
public void toSingleSupplierNormal() { |
| 2559 |
Assert.assertEquals(1, normal.completable.toSingle(new Supplier<Object>() { |
| 2560 |
@Override |
| 2561 |
public Object get() { |
| 2562 |
return 1; |
| 2563 |
} |
| 2564 |
}).blockingGet()); |
| 2565 |
} |
| 2566 |
|
| 2567 |
@Test(expected = TestException.class) |
| 2568 |
public void toSingleSupplierError() { |
| 2569 |
error.completable.toSingle(new Supplier<Object>() { |
| 2570 |
@Override |
| 2571 |
public Object get() { |
| 2572 |
return 1; |
| 2573 |
} |
| 2574 |
}).blockingGet(); |
| 2575 |
} |
| 2576 |
|
| 2577 |
@Test(expected = NullPointerException.class) |
| 2578 |
public void toSingleSupplierReturnsNull() { |
| 2579 |
normal.completable.toSingle(new Supplier<Object>() { |
| 2580 |
@Override |
| 2581 |
public Object get() { |
| 2582 |
return null; |
| 2583 |
} |
| 2584 |
}).blockingGet(); |
| 2585 |
} |
| 2586 |
|
| 2587 |
@Test(expected = TestException.class) |
| 2588 |
public void toSingleSupplierThrows() { |
| 2589 |
normal.completable.toSingle(new Supplier<Object>() { |
| 2590 |
@Override |
| 2591 |
public Object get() { throw new TestException(); } |
| 2592 |
}).blockingGet(); |
| 2593 |
} |
| 2594 |
|
| 2595 |
@Test(expected = TestException.class) |
| 2596 |
public void toSingleDefaultError() { |
| 2597 |
error.completable.toSingleDefault(1).blockingGet(); |
| 2598 |
} |
| 2599 |
|
| 2600 |
@Test |
| 2601 |
public void toSingleDefaultNormal() { |
| 2602 |
Assert.assertEquals((Integer)1, normal.completable.toSingleDefault(1).blockingGet()); |
| 2603 |
} |
| 2604 |
|
| 2605 |
@Test |
| 2606 |
public void unsubscribeOnNormal() throws InterruptedException { |
| 2607 |
final AtomicReference<String> name = new AtomicReference<>(); |
| 2608 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 2609 |
|
| 2610 |
normal.completable.delay(1, TimeUnit.SECONDS) |
| 2611 |
.doOnDispose(new Action() { |
| 2612 |
@Override |
| 2613 |
public void run() { |
| 2614 |
name.set(Thread.currentThread().getName()); |
| 2615 |
cdl.countDown(); |
| 2616 |
} |
| 2617 |
}) |
| 2618 |
.unsubscribeOn(Schedulers.computation()) |
| 2619 |
.subscribe(new CompletableObserver() { |
| 2620 |
@Override |
| 2621 |
public void onSubscribe(final Disposable d) { |
| 2622 |
Schedulers.single().scheduleDirect(new Runnable() { |
| 2623 |
@Override |
| 2624 |
public void run() { |
| 2625 |
d.dispose(); |
| 2626 |
} |
| 2627 |
}, 100, TimeUnit.MILLISECONDS); |
| 2628 |
} |
| 2629 |
|
| 2630 |
@Override |
| 2631 |
public void onError(Throwable e) { |
| 2632 |
|
| 2633 |
} |
| 2634 |
|
| 2635 |
@Override |
| 2636 |
public void onComplete() { |
| 2637 |
|
| 2638 |
} |
| 2639 |
}); |
| 2640 |
|
| 2641 |
cdl.await(); |
| 2642 |
|
| 2643 |
Assert.assertTrue(name.get().startsWith("RxComputation")); |
| 2644 |
} |
| 2645 |
|
| 2646 |
@Test |
| 2647 |
public void ambArrayEmpty() { |
| 2648 |
Completable c = Completable.ambArray(); |
| 2649 |
|
| 2650 |
c.blockingAwait(); |
| 2651 |
} |
| 2652 |
|
| 2653 |
@Test |
| 2654 |
public void ambArraySingleNormal() { |
| 2655 |
Completable c = Completable.ambArray(normal.completable); |
| 2656 |
|
| 2657 |
c.blockingAwait(); |
| 2658 |
} |
| 2659 |
|
| 2660 |
@Test |
| 2661 |
public void ambArraySingleError() { |
| 2662 |
Completable.ambArray(error.completable) |
| 2663 |
.test() |
| 2664 |
.assertError(TestException.class); |
| 2665 |
} |
| 2666 |
|
| 2667 |
@Test |
| 2668 |
public void ambArrayOneFires() { |
| 2669 |
PublishProcessor<Object> pp1 = PublishProcessor.create(); |
| 2670 |
PublishProcessor<Object> pp2 = PublishProcessor.create(); |
| 2671 |
|
| 2672 |
Completable c1 = Completable.fromPublisher(pp1); |
| 2673 |
|
| 2674 |
Completable c2 = Completable.fromPublisher(pp2); |
| 2675 |
|
| 2676 |
Completable c = Completable.ambArray(c1, c2); |
| 2677 |
|
| 2678 |
final AtomicBoolean complete = new AtomicBoolean(); |
| 2679 |
|
| 2680 |
c.subscribe(new Action() { |
| 2681 |
@Override |
| 2682 |
public void run() { |
| 2683 |
complete.set(true); |
| 2684 |
} |
| 2685 |
}); |
| 2686 |
|
| 2687 |
Assert.assertTrue("First subject no subscribers", pp1.hasSubscribers()); |
| 2688 |
Assert.assertTrue("Second subject no subscribers", pp2.hasSubscribers()); |
| 2689 |
|
| 2690 |
pp1.onComplete(); |
| 2691 |
|
| 2692 |
Assert.assertFalse("First subject has subscribers", pp1.hasSubscribers()); |
| 2693 |
Assert.assertFalse("Second subject has subscribers", pp2.hasSubscribers()); |
| 2694 |
|
| 2695 |
Assert.assertTrue("Not completed", complete.get()); |
| 2696 |
} |
| 2697 |
|
| 2698 |
@Test |
| 2699 |
public void ambArrayOneFiresError() { |
| 2700 |
PublishProcessor<Object> pp1 = PublishProcessor.create(); |
| 2701 |
PublishProcessor<Object> pp2 = PublishProcessor.create(); |
| 2702 |
|
| 2703 |
Completable c1 = Completable.fromPublisher(pp1); |
| 2704 |
|
| 2705 |
Completable c2 = Completable.fromPublisher(pp2); |
| 2706 |
|
| 2707 |
Completable c = Completable.ambArray(c1, c2); |
| 2708 |
|
| 2709 |
final AtomicReference<Throwable> complete = new AtomicReference<>(); |
| 2710 |
|
| 2711 |
c.subscribe(Functions.EMPTY_ACTION, new Consumer<Throwable>() { |
| 2712 |
@Override |
| 2713 |
public void accept(Throwable v) { |
| 2714 |
complete.set(v); |
| 2715 |
} |
| 2716 |
}); |
| 2717 |
|
| 2718 |
Assert.assertTrue("First subject no subscribers", pp1.hasSubscribers()); |
| 2719 |
Assert.assertTrue("Second subject no subscribers", pp2.hasSubscribers()); |
| 2720 |
|
| 2721 |
pp1.onError(new TestException()); |
| 2722 |
|
| 2723 |
Assert.assertFalse("First subject has subscribers", pp1.hasSubscribers()); |
| 2724 |
Assert.assertFalse("Second subject has subscribers", pp2.hasSubscribers()); |
| 2725 |
|
| 2726 |
Assert.assertTrue("Not completed", complete.get() instanceof TestException); |
| 2727 |
} |
| 2728 |
|
| 2729 |
@Test |
| 2730 |
public void ambArraySecondFires() { |
| 2731 |
PublishProcessor<Object> pp1 = PublishProcessor.create(); |
| 2732 |
PublishProcessor<Object> pp2 = PublishProcessor.create(); |
| 2733 |
|
| 2734 |
Completable c1 = Completable.fromPublisher(pp1); |
| 2735 |
|
| 2736 |
Completable c2 = Completable.fromPublisher(pp2); |
| 2737 |
|
| 2738 |
Completable c = Completable.ambArray(c1, c2); |
| 2739 |
|
| 2740 |
final AtomicBoolean complete = new AtomicBoolean(); |
| 2741 |
|
| 2742 |
c.subscribe(new Action() { |
| 2743 |
@Override |
| 2744 |
public void run() { |
| 2745 |
complete.set(true); |
| 2746 |
} |
| 2747 |
}); |
| 2748 |
|
| 2749 |
Assert.assertTrue("First subject no subscribers", pp1.hasSubscribers()); |
| 2750 |
Assert.assertTrue("Second subject no subscribers", pp2.hasSubscribers()); |
| 2751 |
|
| 2752 |
pp2.onComplete(); |
| 2753 |
|
| 2754 |
Assert.assertFalse("First subject has subscribers", pp1.hasSubscribers()); |
| 2755 |
Assert.assertFalse("Second subject has subscribers", pp2.hasSubscribers()); |
| 2756 |
|
| 2757 |
Assert.assertTrue("Not completed", complete.get()); |
| 2758 |
} |
| 2759 |
|
| 2760 |
@Test |
| 2761 |
public void ambArraySecondFiresError() { |
| 2762 |
PublishProcessor<Object> pp1 = PublishProcessor.create(); |
| 2763 |
PublishProcessor<Object> pp2 = PublishProcessor.create(); |
| 2764 |
|
| 2765 |
Completable c1 = Completable.fromPublisher(pp1); |
| 2766 |
|
| 2767 |
Completable c2 = Completable.fromPublisher(pp2); |
| 2768 |
|
| 2769 |
Completable c = Completable.ambArray(c1, c2); |
| 2770 |
|
| 2771 |
final AtomicReference<Throwable> complete = new AtomicReference<>(); |
| 2772 |
|
| 2773 |
c.subscribe(Functions.EMPTY_ACTION, new Consumer<Throwable>() { |
| 2774 |
@Override |
| 2775 |
public void accept(Throwable v) { |
| 2776 |
complete.set(v); |
| 2777 |
} |
| 2778 |
}); |
| 2779 |
|
| 2780 |
Assert.assertTrue("First subject no subscribers", pp1.hasSubscribers()); |
| 2781 |
Assert.assertTrue("Second subject no subscribers", pp2.hasSubscribers()); |
| 2782 |
|
| 2783 |
pp2.onError(new TestException()); |
| 2784 |
|
| 2785 |
Assert.assertFalse("First subject has subscribers", pp1.hasSubscribers()); |
| 2786 |
Assert.assertFalse("Second subject has subscribers", pp2.hasSubscribers()); |
| 2787 |
|
| 2788 |
Assert.assertTrue("Not completed", complete.get() instanceof TestException); |
| 2789 |
} |
| 2790 |
|
| 2791 |
@Test |
| 2792 |
public void ambMultipleOneIsNull() { |
| 2793 |
Completable.ambArray(null, normal.completable) |
| 2794 |
.test() |
| 2795 |
.assertError(NullPointerException.class); |
| 2796 |
} |
| 2797 |
|
| 2798 |
@Test |
| 2799 |
public void ambIterableEmpty() { |
| 2800 |
Completable c = Completable.amb(Collections.<Completable>emptyList()); |
| 2801 |
|
| 2802 |
c.blockingAwait(); |
| 2803 |
} |
| 2804 |
|
| 2805 |
@Test |
| 2806 |
public void ambIterableIteratorNull() { |
| 2807 |
Completable.amb(new Iterable<Completable>() { |
| 2808 |
@Override |
| 2809 |
public Iterator<Completable> iterator() { |
| 2810 |
return null; |
| 2811 |
} |
| 2812 |
}).test().assertError(NullPointerException.class); |
| 2813 |
} |
| 2814 |
|
| 2815 |
@Test |
| 2816 |
public void ambIterableWithNull() { |
| 2817 |
Completable.amb(Arrays.asList(null, normal.completable)) |
| 2818 |
.test() |
| 2819 |
.assertError(NullPointerException.class); |
| 2820 |
} |
| 2821 |
|
| 2822 |
@Test |
| 2823 |
public void ambIterableSingle() { |
| 2824 |
Completable c = Completable.amb(Collections.singleton(normal.completable)); |
| 2825 |
|
| 2826 |
c.blockingAwait(); |
| 2827 |
|
| 2828 |
normal.assertSubscriptions(1); |
| 2829 |
} |
| 2830 |
|
| 2831 |
@Test |
| 2832 |
public void ambIterableMany() { |
| 2833 |
Completable c = Completable.amb(Arrays.asList(normal.completable, normal.completable, normal.completable)); |
| 2834 |
|
| 2835 |
c.blockingAwait(); |
| 2836 |
|
| 2837 |
normal.assertSubscriptions(1); |
| 2838 |
} |
| 2839 |
|
| 2840 |
@Test |
| 2841 |
public void ambIterableOneThrows() { |
| 2842 |
Completable.amb(Collections.singleton(error.completable)) |
| 2843 |
.test() |
| 2844 |
.assertError(TestException.class); |
| 2845 |
} |
| 2846 |
|
| 2847 |
@Test |
| 2848 |
public void ambIterableManyOneThrows() { |
| 2849 |
Completable.amb(Arrays.asList(error.completable, normal.completable)) |
| 2850 |
.test() |
| 2851 |
.assertError(TestException.class); |
| 2852 |
} |
| 2853 |
|
| 2854 |
@Test |
| 2855 |
public void ambIterableIterableThrows() { |
| 2856 |
Completable.amb(new Iterable<Completable>() { |
| 2857 |
@Override |
| 2858 |
public Iterator<Completable> iterator() { |
| 2859 |
throw new TestException(); |
| 2860 |
} |
| 2861 |
}).test().assertError(TestException.class); |
| 2862 |
} |
| 2863 |
|
| 2864 |
@Test |
| 2865 |
public void ambIterableIteratorHasNextThrows() { |
| 2866 |
Completable.amb(new IterableIteratorHasNextThrows()) |
| 2867 |
.test() |
| 2868 |
.assertError(TestException.class); |
| 2869 |
} |
| 2870 |
|
| 2871 |
@Test |
| 2872 |
public void ambIterableIteratorNextThrows() { |
| 2873 |
Completable.amb(new IterableIteratorNextThrows()) |
| 2874 |
.test() |
| 2875 |
.assertError(TestException.class); |
| 2876 |
} |
| 2877 |
|
| 2878 |
@Test |
| 2879 |
public void ambWithArrayOneFires() { |
| 2880 |
PublishProcessor<Object> pp1 = PublishProcessor.create(); |
| 2881 |
PublishProcessor<Object> pp2 = PublishProcessor.create(); |
| 2882 |
|
| 2883 |
Completable c1 = Completable.fromPublisher(pp1); |
| 2884 |
|
| 2885 |
Completable c2 = Completable.fromPublisher(pp2); |
| 2886 |
|
| 2887 |
Completable c = c1.ambWith(c2); |
| 2888 |
|
| 2889 |
final AtomicBoolean complete = new AtomicBoolean(); |
| 2890 |
|
| 2891 |
c.subscribe(new Action() { |
| 2892 |
@Override |
| 2893 |
public void run() { |
| 2894 |
complete.set(true); |
| 2895 |
} |
| 2896 |
}); |
| 2897 |
|
| 2898 |
Assert.assertTrue("First subject no subscribers", pp1.hasSubscribers()); |
| 2899 |
Assert.assertTrue("Second subject no subscribers", pp2.hasSubscribers()); |
| 2900 |
|
| 2901 |
pp1.onComplete(); |
| 2902 |
|
| 2903 |
Assert.assertFalse("First subject has subscribers", pp1.hasSubscribers()); |
| 2904 |
Assert.assertFalse("Second subject has subscribers", pp2.hasSubscribers()); |
| 2905 |
|
| 2906 |
Assert.assertTrue("Not completed", complete.get()); |
| 2907 |
} |
| 2908 |
|
| 2909 |
@Test |
| 2910 |
public void ambWithArrayOneFiresError() { |
| 2911 |
PublishProcessor<Object> pp1 = PublishProcessor.create(); |
| 2912 |
PublishProcessor<Object> pp2 = PublishProcessor.create(); |
| 2913 |
|
| 2914 |
Completable c1 = Completable.fromPublisher(pp1); |
| 2915 |
|
| 2916 |
Completable c2 = Completable.fromPublisher(pp2); |
| 2917 |
|
| 2918 |
Completable c = c1.ambWith(c2); |
| 2919 |
|
| 2920 |
final AtomicReference<Throwable> complete = new AtomicReference<>(); |
| 2921 |
|
| 2922 |
c.subscribe(Functions.EMPTY_ACTION, new Consumer<Throwable>() { |
| 2923 |
@Override |
| 2924 |
public void accept(Throwable v) { |
| 2925 |
complete.set(v); |
| 2926 |
} |
| 2927 |
}); |
| 2928 |
|
| 2929 |
Assert.assertTrue("First subject no subscribers", pp1.hasSubscribers()); |
| 2930 |
Assert.assertTrue("Second subject no subscribers", pp2.hasSubscribers()); |
| 2931 |
|
| 2932 |
pp1.onError(new TestException()); |
| 2933 |
|
| 2934 |
Assert.assertFalse("First subject has subscribers", pp1.hasSubscribers()); |
| 2935 |
Assert.assertFalse("Second subject has subscribers", pp2.hasSubscribers()); |
| 2936 |
|
| 2937 |
Assert.assertTrue("Not completed", complete.get() instanceof TestException); |
| 2938 |
} |
| 2939 |
|
| 2940 |
@Test |
| 2941 |
public void ambWithArraySecondFires() { |
| 2942 |
PublishProcessor<Object> pp1 = PublishProcessor.create(); |
| 2943 |
PublishProcessor<Object> pp2 = PublishProcessor.create(); |
| 2944 |
|
| 2945 |
Completable c1 = Completable.fromPublisher(pp1); |
| 2946 |
|
| 2947 |
Completable c2 = Completable.fromPublisher(pp2); |
| 2948 |
|
| 2949 |
Completable c = c1.ambWith(c2); |
| 2950 |
|
| 2951 |
final AtomicBoolean complete = new AtomicBoolean(); |
| 2952 |
|
| 2953 |
c.subscribe(new Action() { |
| 2954 |
@Override |
| 2955 |
public void run() { |
| 2956 |
complete.set(true); |
| 2957 |
} |
| 2958 |
}); |
| 2959 |
|
| 2960 |
Assert.assertTrue("First subject no subscribers", pp1.hasSubscribers()); |
| 2961 |
Assert.assertTrue("Second subject no subscribers", pp2.hasSubscribers()); |
| 2962 |
|
| 2963 |
pp2.onComplete(); |
| 2964 |
|
| 2965 |
Assert.assertFalse("First subject has subscribers", pp1.hasSubscribers()); |
| 2966 |
Assert.assertFalse("Second subject has subscribers", pp2.hasSubscribers()); |
| 2967 |
|
| 2968 |
Assert.assertTrue("Not completed", complete.get()); |
| 2969 |
} |
| 2970 |
|
| 2971 |
@Test |
| 2972 |
public void ambWithArraySecondFiresError() { |
| 2973 |
PublishProcessor<Object> pp1 = PublishProcessor.create(); |
| 2974 |
PublishProcessor<Object> pp2 = PublishProcessor.create(); |
| 2975 |
|
| 2976 |
Completable c1 = Completable.fromPublisher(pp1); |
| 2977 |
|
| 2978 |
Completable c2 = Completable.fromPublisher(pp2); |
| 2979 |
|
| 2980 |
Completable c = c1.ambWith(c2); |
| 2981 |
|
| 2982 |
final AtomicReference<Throwable> complete = new AtomicReference<>(); |
| 2983 |
|
| 2984 |
c.subscribe(Functions.EMPTY_ACTION, new Consumer<Throwable>() { |
| 2985 |
@Override |
| 2986 |
public void accept(Throwable v) { |
| 2987 |
complete.set(v); |
| 2988 |
} |
| 2989 |
}); |
| 2990 |
|
| 2991 |
Assert.assertTrue("First subject no subscribers", pp1.hasSubscribers()); |
| 2992 |
Assert.assertTrue("Second subject no subscribers", pp2.hasSubscribers()); |
| 2993 |
|
| 2994 |
pp2.onError(new TestException()); |
| 2995 |
|
| 2996 |
Assert.assertFalse("First subject has subscribers", pp1.hasSubscribers()); |
| 2997 |
Assert.assertFalse("Second subject has subscribers", pp2.hasSubscribers()); |
| 2998 |
|
| 2999 |
Assert.assertTrue("Not completed", complete.get() instanceof TestException); |
| 3000 |
} |
| 3001 |
|
| 3002 |
@Test |
| 3003 |
public void startWithCompletableNormal() { |
| 3004 |
final AtomicBoolean run = new AtomicBoolean(); |
| 3005 |
Completable c = normal.completable |
| 3006 |
.startWith(Completable.fromCallable(new Callable<Object>() { |
| 3007 |
@Override |
| 3008 |
public Object call() throws Exception { |
| 3009 |
run.set(normal.get() == 0); |
| 3010 |
return null; |
| 3011 |
} |
| 3012 |
})); |
| 3013 |
|
| 3014 |
c.blockingAwait(); |
| 3015 |
|
| 3016 |
Assert.assertTrue("Did not start with other", run.get()); |
| 3017 |
normal.assertSubscriptions(1); |
| 3018 |
} |
| 3019 |
|
| 3020 |
@Test |
| 3021 |
public void startWithCompletableError() { |
| 3022 |
Completable c = normal.completable.startWith(error.completable); |
| 3023 |
|
| 3024 |
try { |
| 3025 |
c.blockingAwait(); |
| 3026 |
Assert.fail("Did not throw TestException"); |
| 3027 |
} catch (TestException ex) { |
| 3028 |
normal.assertSubscriptions(0); |
| 3029 |
error.assertSubscriptions(1); |
| 3030 |
} |
| 3031 |
} |
| 3032 |
|
| 3033 |
@Test |
| 3034 |
public void startWithFlowableNormal() { |
| 3035 |
final AtomicBoolean run = new AtomicBoolean(); |
| 3036 |
Flowable<Object> c = normal.completable |
| 3037 |
.startWith(Flowable.fromCallable(new Callable<Object>() { |
| 3038 |
@Override |
| 3039 |
public Object call() throws Exception { |
| 3040 |
run.set(normal.get() == 0); |
| 3041 |
return 1; |
| 3042 |
} |
| 3043 |
})); |
| 3044 |
|
| 3045 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 3046 |
|
| 3047 |
c.subscribe(ts); |
| 3048 |
|
| 3049 |
Assert.assertTrue("Did not start with other", run.get()); |
| 3050 |
normal.assertSubscriptions(1); |
| 3051 |
|
| 3052 |
ts.assertValue(1); |
| 3053 |
ts.assertComplete(); |
| 3054 |
ts.assertNoErrors(); |
| 3055 |
} |
| 3056 |
|
| 3057 |
@Test |
| 3058 |
public void startWithFlowableError() { |
| 3059 |
Flowable<Object> c = normal.completable |
| 3060 |
.startWith(Flowable.error(new TestException())); |
| 3061 |
|
| 3062 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 3063 |
|
| 3064 |
c.subscribe(ts); |
| 3065 |
|
| 3066 |
normal.assertSubscriptions(0); |
| 3067 |
|
| 3068 |
ts.assertNoValues(); |
| 3069 |
ts.assertError(TestException.class); |
| 3070 |
ts.assertNotComplete(); |
| 3071 |
} |
| 3072 |
|
| 3073 |
@Test |
| 3074 |
public void startWithObservableNormal() { |
| 3075 |
final AtomicBoolean run = new AtomicBoolean(); |
| 3076 |
Observable<Object> o = normal.completable |
| 3077 |
.startWith(Observable.fromCallable(new Callable<Object>() { |
| 3078 |
@Override |
| 3079 |
public Object call() throws Exception { |
| 3080 |
run.set(normal.get() == 0); |
| 3081 |
return 1; |
| 3082 |
} |
| 3083 |
})); |
| 3084 |
|
| 3085 |
TestObserver<Object> to = new TestObserver<>(); |
| 3086 |
|
| 3087 |
o.subscribe(to); |
| 3088 |
|
| 3089 |
Assert.assertTrue("Did not start with other", run.get()); |
| 3090 |
normal.assertSubscriptions(1); |
| 3091 |
|
| 3092 |
to.assertValue(1); |
| 3093 |
to.assertComplete(); |
| 3094 |
to.assertNoErrors(); |
| 3095 |
} |
| 3096 |
|
| 3097 |
@Test |
| 3098 |
public void startWithObservableError() { |
| 3099 |
Observable<Object> o = normal.completable |
| 3100 |
.startWith(Observable.error(new TestException())); |
| 3101 |
|
| 3102 |
TestObserver<Object> to = new TestObserver<>(); |
| 3103 |
|
| 3104 |
o.subscribe(to); |
| 3105 |
|
| 3106 |
normal.assertSubscriptions(0); |
| 3107 |
|
| 3108 |
to.assertNoValues(); |
| 3109 |
to.assertError(TestException.class); |
| 3110 |
to.assertNotComplete(); |
| 3111 |
} |
| 3112 |
|
| 3113 |
@Test |
| 3114 |
public void andThen() { |
| 3115 |
TestSubscriber<String> ts = new TestSubscriber<>(0); |
| 3116 |
Completable.complete().andThen(Flowable.just("foo")).subscribe(ts); |
| 3117 |
ts.request(1); |
| 3118 |
ts.assertValue("foo"); |
| 3119 |
ts.assertComplete(); |
| 3120 |
ts.assertNoErrors(); |
| 3121 |
|
| 3122 |
TestObserver<String> to = new TestObserver<>(); |
| 3123 |
Completable.complete().andThen(Observable.just("foo")).subscribe(to); |
| 3124 |
to.assertValue("foo"); |
| 3125 |
to.assertComplete(); |
| 3126 |
to.assertNoErrors(); |
| 3127 |
} |
| 3128 |
|
| 3129 |
private static void expectUncaughtTestException(Action action) { |
| 3130 |
Thread.UncaughtExceptionHandler originalHandler = Thread.getDefaultUncaughtExceptionHandler(); |
| 3131 |
CapturingUncaughtExceptionHandler handler = new CapturingUncaughtExceptionHandler(); |
| 3132 |
Thread.setDefaultUncaughtExceptionHandler(handler); |
| 3133 |
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { |
| 3134 |
@Override |
| 3135 |
public void accept(Throwable error) throws Exception { |
| 3136 |
Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), error); |
| 3137 |
} |
| 3138 |
}); |
| 3139 |
try { |
| 3140 |
action.run(); |
| 3141 |
assertEquals("Should have received exactly 1 exception", 1, handler.count); |
| 3142 |
Throwable caught = handler.caught; |
| 3143 |
while (caught != null) { |
| 3144 |
if (caught instanceof TestException) { break; } |
| 3145 |
if (caught == caught.getCause()) { break; } |
| 3146 |
caught = caught.getCause(); |
| 3147 |
} |
| 3148 |
assertTrue("A TestException should have been delivered to the handler", |
| 3149 |
caught instanceof TestException); |
| 3150 |
} catch (Throwable ex) { |
| 3151 |
throw ExceptionHelper.wrapOrThrow(ex); |
| 3152 |
} finally { |
| 3153 |
Thread.setDefaultUncaughtExceptionHandler(originalHandler); |
| 3154 |
RxJavaPlugins.setErrorHandler(null); |
| 3155 |
} |
| 3156 |
} |
| 3157 |
|
| 3158 |
@Test |
| 3159 |
public void subscribeOneActionThrowFromOnCompleted() { |
| 3160 |
expectUncaughtTestException(new Action() { |
| 3161 |
@Override |
| 3162 |
public void run() { |
| 3163 |
normal.completable.subscribe(new Action() { |
| 3164 |
@Override |
| 3165 |
public void run() { |
| 3166 |
throw new TestException(); |
| 3167 |
} |
| 3168 |
}); |
| 3169 |
} |
| 3170 |
}); |
| 3171 |
} |
| 3172 |
|
| 3173 |
@Test |
| 3174 |
public void subscribeTwoActionsThrowFromOnError() { |
| 3175 |
expectUncaughtTestException(new Action() { |
| 3176 |
@Override |
| 3177 |
public void run() { |
| 3178 |
error.completable.subscribe( |
| 3179 |
new Action() { |
| 3180 |
@Override |
| 3181 |
public void run() { |
| 3182 |
} |
| 3183 |
}, |
| 3184 |
new Consumer<Throwable>() { |
| 3185 |
@Override |
| 3186 |
public void accept(Throwable throwable) { |
| 3187 |
throw new TestException(); |
| 3188 |
} |
| 3189 |
}); |
| 3190 |
} |
| 3191 |
}); |
| 3192 |
} |
| 3193 |
|
| 3194 |
@Test |
| 3195 |
public void propagateExceptionSubscribeOneAction() { |
| 3196 |
expectUncaughtTestException(new Action() { |
| 3197 |
@Override |
| 3198 |
public void run() { |
| 3199 |
error.completable.toSingleDefault(1).subscribe(new Consumer<Integer>() { |
| 3200 |
@Override |
| 3201 |
public void accept(Integer integer) { |
| 3202 |
} |
| 3203 |
}); |
| 3204 |
} |
| 3205 |
}); |
| 3206 |
} |
| 3207 |
|
| 3208 |
@Test |
| 3209 |
public void usingFactoryReturnsNullAndDisposerThrows() { |
| 3210 |
Consumer<Integer> onDispose = new Consumer<Integer>() { |
| 3211 |
@Override |
| 3212 |
public void accept(Integer t) { |
| 3213 |
throw new TestException(); |
| 3214 |
} |
| 3215 |
}; |
| 3216 |
|
| 3217 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 3218 |
|
| 3219 |
Completable.using(new Supplier<Integer>() { |
| 3220 |
@Override |
| 3221 |
public Integer get() { |
| 3222 |
return 1; |
| 3223 |
} |
| 3224 |
}, |
| 3225 |
new Function<Integer, Completable>() { |
| 3226 |
@Override |
| 3227 |
public Completable apply(Integer t) { |
| 3228 |
return null; |
| 3229 |
} |
| 3230 |
}, onDispose).<Integer>toFlowable().subscribe(ts); |
| 3231 |
|
| 3232 |
ts.assertNoValues(); |
| 3233 |
ts.assertNotComplete(); |
| 3234 |
ts.assertError(CompositeException.class); |
| 3235 |
|
| 3236 |
CompositeException ex = (CompositeException)ts.errors().get(0); |
| 3237 |
|
| 3238 |
List<Throwable> listEx = ex.getExceptions(); |
| 3239 |
|
| 3240 |
assertEquals(2, listEx.size()); |
| 3241 |
|
| 3242 |
assertTrue(listEx.get(0).toString(), listEx.get(0) instanceof NullPointerException); |
| 3243 |
assertTrue(listEx.get(1).toString(), listEx.get(1) instanceof TestException); |
| 3244 |
} |
| 3245 |
|
| 3246 |
@Test |
| 3247 |
public void subscribeReportsUnsubscribedOnError() { |
| 3248 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 3249 |
try { |
| 3250 |
PublishSubject<String> stringSubject = PublishSubject.create(); |
| 3251 |
Completable completable = stringSubject.ignoreElements(); |
| 3252 |
|
| 3253 |
Disposable completableSubscription = completable.subscribe(); |
| 3254 |
|
| 3255 |
stringSubject.onError(new TestException()); |
| 3256 |
|
| 3257 |
assertTrue("Not unsubscribed?", completableSubscription.isDisposed()); |
| 3258 |
|
| 3259 |
TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class); |
| 3260 |
} finally { |
| 3261 |
RxJavaPlugins.reset(); |
| 3262 |
} |
| 3263 |
} |
| 3264 |
|
| 3265 |
@Test |
| 3266 |
public void subscribeActionReportsUnsubscribed() { |
| 3267 |
PublishSubject<String> stringSubject = PublishSubject.create(); |
| 3268 |
Completable completable = stringSubject.ignoreElements(); |
| 3269 |
|
| 3270 |
Disposable completableSubscription = completable.subscribe(new Action() { |
| 3271 |
@Override |
| 3272 |
public void run() { |
| 3273 |
|
| 3274 |
} |
| 3275 |
}); |
| 3276 |
|
| 3277 |
stringSubject.onComplete(); |
| 3278 |
|
| 3279 |
assertTrue("Not unsubscribed?", completableSubscription.isDisposed()); |
| 3280 |
} |
| 3281 |
|
| 3282 |
@Test |
| 3283 |
public void subscribeActionReportsUnsubscribedAfter() { |
| 3284 |
PublishSubject<String> stringSubject = PublishSubject.create(); |
| 3285 |
Completable completable = stringSubject.ignoreElements(); |
| 3286 |
|
| 3287 |
final AtomicReference<Disposable> disposableRef = new AtomicReference<>(); |
| 3288 |
Disposable completableSubscription = completable.subscribe(new Action() { |
| 3289 |
@Override |
| 3290 |
public void run() { |
| 3291 |
if (disposableRef.get().isDisposed()) { |
| 3292 |
disposableRef.set(null); |
| 3293 |
} |
| 3294 |
} |
| 3295 |
}); |
| 3296 |
disposableRef.set(completableSubscription); |
| 3297 |
|
| 3298 |
stringSubject.onComplete(); |
| 3299 |
|
| 3300 |
assertTrue("Not unsubscribed?", completableSubscription.isDisposed()); |
| 3301 |
assertNotNull("Unsubscribed before the call to onComplete", disposableRef.get()); |
| 3302 |
} |
| 3303 |
|
| 3304 |
@Test |
| 3305 |
public void subscribeActionReportsUnsubscribedOnError() { |
| 3306 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 3307 |
try { |
| 3308 |
PublishSubject<String> stringSubject = PublishSubject.create(); |
| 3309 |
Completable completable = stringSubject.ignoreElements(); |
| 3310 |
|
| 3311 |
Disposable completableSubscription = completable.subscribe(new Action() { |
| 3312 |
@Override |
| 3313 |
public void run() { |
| 3314 |
} |
| 3315 |
}); |
| 3316 |
|
| 3317 |
stringSubject.onError(new TestException()); |
| 3318 |
|
| 3319 |
assertTrue("Not unsubscribed?", completableSubscription.isDisposed()); |
| 3320 |
|
| 3321 |
TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class); |
| 3322 |
} finally { |
| 3323 |
RxJavaPlugins.reset(); |
| 3324 |
} |
| 3325 |
} |
| 3326 |
|
| 3327 |
@Test |
| 3328 |
public void subscribeAction2ReportsUnsubscribed() { |
| 3329 |
PublishSubject<String> stringSubject = PublishSubject.create(); |
| 3330 |
Completable completable = stringSubject.ignoreElements(); |
| 3331 |
|
| 3332 |
Disposable completableSubscription = completable.subscribe(new Action() { |
| 3333 |
@Override |
| 3334 |
public void run() { |
| 3335 |
|
| 3336 |
} |
| 3337 |
}, new Consumer<Throwable>() { |
| 3338 |
@Override |
| 3339 |
public void accept(Throwable t) { |
| 3340 |
|
| 3341 |
} |
| 3342 |
}); |
| 3343 |
|
| 3344 |
stringSubject.onComplete(); |
| 3345 |
|
| 3346 |
assertTrue("Not unsubscribed?", completableSubscription.isDisposed()); |
| 3347 |
} |
| 3348 |
|
| 3349 |
@Test |
| 3350 |
public void subscribeAction2ReportsUnsubscribedOnError() { |
| 3351 |
PublishSubject<String> stringSubject = PublishSubject.create(); |
| 3352 |
Completable completable = stringSubject.ignoreElements(); |
| 3353 |
|
| 3354 |
Disposable completableSubscription = completable.subscribe(new Action() { |
| 3355 |
@Override |
| 3356 |
public void run() { } |
| 3357 |
}, new Consumer<Throwable>() { |
| 3358 |
@Override |
| 3359 |
public void accept(Throwable e) { } |
| 3360 |
}); |
| 3361 |
|
| 3362 |
stringSubject.onError(new TestException()); |
| 3363 |
|
| 3364 |
assertTrue("Not unsubscribed?", completableSubscription.isDisposed()); |
| 3365 |
} |
| 3366 |
|
| 3367 |
@Test |
| 3368 |
public void andThenSubscribeOn() { |
| 3369 |
TestSubscriberEx<String> ts = new TestSubscriberEx<>(0); |
| 3370 |
TestScheduler scheduler = new TestScheduler(); |
| 3371 |
Completable.complete().andThen(Flowable.just("foo").delay(1, TimeUnit.SECONDS, scheduler)).subscribe(ts); |
| 3372 |
|
| 3373 |
ts.request(1); |
| 3374 |
ts.assertNoValues(); |
| 3375 |
ts.assertNotTerminated(); |
| 3376 |
|
| 3377 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 3378 |
|
| 3379 |
ts.assertValue("foo"); |
| 3380 |
ts.assertComplete(); |
| 3381 |
ts.assertNoErrors(); |
| 3382 |
} |
| 3383 |
|
| 3384 |
@Test |
| 3385 |
public void andThenSingleNever() { |
| 3386 |
TestSubscriberEx<String> ts = new TestSubscriberEx<>(0); |
| 3387 |
Completable.never().andThen(Single.just("foo")).toFlowable().subscribe(ts); |
| 3388 |
ts.request(1); |
| 3389 |
ts.assertNoValues(); |
| 3390 |
ts.assertNotTerminated(); |
| 3391 |
} |
| 3392 |
|
| 3393 |
@Test |
| 3394 |
public void andThenSingleError() { |
| 3395 |
TestSubscriber<String> ts = new TestSubscriber<>(0); |
| 3396 |
final AtomicBoolean hasRun = new AtomicBoolean(false); |
| 3397 |
final Exception e = new Exception(); |
| 3398 |
Completable.error(e) |
| 3399 |
.andThen(new Single<String>() { |
| 3400 |
@Override |
| 3401 |
public void subscribeActual(SingleObserver<? super String> observer) { |
| 3402 |
hasRun.set(true); |
| 3403 |
observer.onSuccess("foo"); |
| 3404 |
} |
| 3405 |
}) |
| 3406 |
.toFlowable().subscribe(ts); |
| 3407 |
ts.assertNoValues(); |
| 3408 |
ts.assertError(e); |
| 3409 |
Assert.assertFalse("Should not have subscribed to single when completable errors", hasRun.get()); |
| 3410 |
} |
| 3411 |
|
| 3412 |
@Test |
| 3413 |
public void andThenSingleSubscribeOn() { |
| 3414 |
TestSubscriberEx<String> ts = new TestSubscriberEx<>(0); |
| 3415 |
TestScheduler scheduler = new TestScheduler(); |
| 3416 |
Completable.complete().andThen(Single.just("foo").delay(1, TimeUnit.SECONDS, scheduler)).toFlowable().subscribe(ts); |
| 3417 |
|
| 3418 |
ts.request(1); |
| 3419 |
ts.assertNoValues(); |
| 3420 |
ts.assertNotTerminated(); |
| 3421 |
|
| 3422 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 3423 |
|
| 3424 |
ts.assertValue("foo"); |
| 3425 |
ts.assertComplete(); |
| 3426 |
ts.assertNoErrors(); |
| 3427 |
} |
| 3428 |
|
| 3429 |
private Function<Completable, Completable> onCreate; |
| 3430 |
|
| 3431 |
private BiFunction<Completable, CompletableObserver, CompletableObserver> onStart; |
| 3432 |
|
| 3433 |
@Before |
| 3434 |
public void setUp() throws Exception { |
| 3435 |
onCreate = spy(new Function<Completable, Completable>() { |
| 3436 |
@Override |
| 3437 |
public Completable apply(Completable t) { |
| 3438 |
return t; |
| 3439 |
} |
| 3440 |
}); |
| 3441 |
|
| 3442 |
RxJavaPlugins.setOnCompletableAssembly(onCreate); |
| 3443 |
|
| 3444 |
onStart = spy(new BiFunction<Completable, CompletableObserver, CompletableObserver>() { |
| 3445 |
@Override |
| 3446 |
public CompletableObserver apply(Completable t1, CompletableObserver t2) { |
| 3447 |
return t2; |
| 3448 |
} |
| 3449 |
}); |
| 3450 |
|
| 3451 |
RxJavaPlugins.setOnCompletableSubscribe(onStart); |
| 3452 |
} |
| 3453 |
|
| 3454 |
@After |
| 3455 |
public void after() { |
| 3456 |
RxJavaPlugins.reset(); |
| 3457 |
} |
| 3458 |
|
| 3459 |
@Test |
| 3460 |
public void hookCreate() throws Throwable { |
| 3461 |
CompletableSource subscriber = mock(CompletableSource.class); |
| 3462 |
Completable create = Completable.unsafeCreate(subscriber); |
| 3463 |
|
| 3464 |
verify(onCreate, times(1)).apply(create); |
| 3465 |
} |
| 3466 |
|
| 3467 |
@Test |
| 3468 |
public void doOnCompletedNormal() { |
| 3469 |
final AtomicInteger calls = new AtomicInteger(); |
| 3470 |
|
| 3471 |
Completable c = normal.completable.doOnComplete(new Action() { |
| 3472 |
@Override |
| 3473 |
public void run() { |
| 3474 |
calls.getAndIncrement(); |
| 3475 |
} |
| 3476 |
}); |
| 3477 |
|
| 3478 |
c.blockingAwait(); |
| 3479 |
|
| 3480 |
Assert.assertEquals(1, calls.get()); |
| 3481 |
} |
| 3482 |
|
| 3483 |
@Test |
| 3484 |
public void doOnCompletedError() { |
| 3485 |
final AtomicInteger calls = new AtomicInteger(); |
| 3486 |
|
| 3487 |
Completable c = error.completable.doOnComplete(new Action() { |
| 3488 |
@Override |
| 3489 |
public void run() { |
| 3490 |
calls.getAndIncrement(); |
| 3491 |
} |
| 3492 |
}); |
| 3493 |
|
| 3494 |
try { |
| 3495 |
c.blockingAwait(); |
| 3496 |
Assert.fail("Failed to throw TestException"); |
| 3497 |
} catch (TestException ex) { |
| 3498 |
// expected |
| 3499 |
} |
| 3500 |
|
| 3501 |
Assert.assertEquals(0, calls.get()); |
| 3502 |
} |
| 3503 |
|
| 3504 |
@Test(expected = TestException.class) |
| 3505 |
public void doOnCompletedThrows() { |
| 3506 |
Completable c = normal.completable.doOnComplete(new Action() { |
| 3507 |
@Override |
| 3508 |
public void run() { throw new TestException(); } |
| 3509 |
}); |
| 3510 |
|
| 3511 |
c.blockingAwait(); |
| 3512 |
} |
| 3513 |
|
| 3514 |
@Test |
| 3515 |
public void doAfterTerminateNormal() { |
| 3516 |
final AtomicBoolean doneAfter = new AtomicBoolean(); |
| 3517 |
final AtomicBoolean complete = new AtomicBoolean(); |
| 3518 |
|
| 3519 |
Completable c = normal.completable.doAfterTerminate(new Action() { |
| 3520 |
@Override |
| 3521 |
public void run() { |
| 3522 |
doneAfter.set(complete.get()); |
| 3523 |
} |
| 3524 |
}); |
| 3525 |
|
| 3526 |
c.subscribe(new CompletableObserver() { |
| 3527 |
@Override |
| 3528 |
public void onSubscribe(Disposable d) { |
| 3529 |
|
| 3530 |
} |
| 3531 |
|
| 3532 |
@Override |
| 3533 |
public void onError(Throwable e) { |
| 3534 |
|
| 3535 |
} |
| 3536 |
|
| 3537 |
@Override |
| 3538 |
public void onComplete() { |
| 3539 |
complete.set(true); |
| 3540 |
} |
| 3541 |
}); |
| 3542 |
|
| 3543 |
c.blockingAwait(); |
| 3544 |
|
| 3545 |
Assert.assertTrue("Not completed", complete.get()); |
| 3546 |
Assert.assertTrue("Closure called before onComplete", doneAfter.get()); |
| 3547 |
} |
| 3548 |
|
| 3549 |
@Test |
| 3550 |
public void doAfterTerminateWithError() { |
| 3551 |
final AtomicBoolean doneAfter = new AtomicBoolean(); |
| 3552 |
|
| 3553 |
Completable c = error.completable.doAfterTerminate(new Action() { |
| 3554 |
@Override |
| 3555 |
public void run() { |
| 3556 |
doneAfter.set(true); |
| 3557 |
} |
| 3558 |
}); |
| 3559 |
|
| 3560 |
try { |
| 3561 |
c.blockingAwait(5, TimeUnit.SECONDS); |
| 3562 |
Assert.fail("Did not throw TestException"); |
| 3563 |
} catch (TestException ex) { |
| 3564 |
// expected |
| 3565 |
} |
| 3566 |
|
| 3567 |
Assert.assertTrue("Closure not called", doneAfter.get()); |
| 3568 |
} |
| 3569 |
|
| 3570 |
@Test |
| 3571 |
public void subscribeEmptyOnError() { |
| 3572 |
expectUncaughtTestException(new Action() { |
| 3573 |
@Override public void run() { |
| 3574 |
error.completable.subscribe(); |
| 3575 |
} |
| 3576 |
}); |
| 3577 |
} |
| 3578 |
|
| 3579 |
@Test |
| 3580 |
public void subscribeOneActionOnError() { |
| 3581 |
expectUncaughtTestException(new Action() { |
| 3582 |
@Override |
| 3583 |
public void run() { |
| 3584 |
error.completable.subscribe(new Action() { |
| 3585 |
@Override |
| 3586 |
public void run() { |
| 3587 |
} |
| 3588 |
}); |
| 3589 |
} |
| 3590 |
}); |
| 3591 |
} |
| 3592 |
|
| 3593 |
@Test |
| 3594 |
public void propagateExceptionSubscribeEmpty() { |
| 3595 |
expectUncaughtTestException(new Action() { |
| 3596 |
@Override |
| 3597 |
public void run() { |
| 3598 |
error.completable.toSingleDefault(0).subscribe(); |
| 3599 |
} |
| 3600 |
}); |
| 3601 |
} |
| 3602 |
|
| 3603 |
@Test |
| 3604 |
public void andThenCompletableNormal() { |
| 3605 |
final AtomicBoolean run = new AtomicBoolean(); |
| 3606 |
Completable c = normal.completable |
| 3607 |
.andThen(Completable.fromCallable(new Callable<Object>() { |
| 3608 |
@Override |
| 3609 |
public Object call() throws Exception { |
| 3610 |
run.set(normal.get() == 0); |
| 3611 |
return null; |
| 3612 |
} |
| 3613 |
})); |
| 3614 |
|
| 3615 |
c.blockingAwait(); |
| 3616 |
|
| 3617 |
Assert.assertFalse("Start with other", run.get()); |
| 3618 |
normal.assertSubscriptions(1); |
| 3619 |
} |
| 3620 |
|
| 3621 |
@Test |
| 3622 |
public void andThenCompletableError() { |
| 3623 |
Completable c = normal.completable.andThen(error.completable); |
| 3624 |
|
| 3625 |
try { |
| 3626 |
c.blockingAwait(); |
| 3627 |
Assert.fail("Did not throw TestException"); |
| 3628 |
} catch (TestException ex) { |
| 3629 |
normal.assertSubscriptions(1); |
| 3630 |
error.assertSubscriptions(1); |
| 3631 |
} |
| 3632 |
} |
| 3633 |
|
| 3634 |
@Test |
| 3635 |
public void andThenFlowableNormal() { |
| 3636 |
final AtomicBoolean run = new AtomicBoolean(); |
| 3637 |
Flowable<Object> c = normal.completable |
| 3638 |
.andThen(Flowable.fromCallable(new Callable<Object>() { |
| 3639 |
@Override |
| 3640 |
public Object call() throws Exception { |
| 3641 |
run.set(normal.get() == 0); |
| 3642 |
return 1; |
| 3643 |
} |
| 3644 |
})); |
| 3645 |
|
| 3646 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 3647 |
|
| 3648 |
c.subscribe(ts); |
| 3649 |
|
| 3650 |
Assert.assertFalse("Start with other", run.get()); |
| 3651 |
normal.assertSubscriptions(1); |
| 3652 |
|
| 3653 |
ts.assertValue(1); |
| 3654 |
ts.assertComplete(); |
| 3655 |
ts.assertNoErrors(); |
| 3656 |
} |
| 3657 |
|
| 3658 |
@Test |
| 3659 |
public void andThenFlowableError() { |
| 3660 |
Flowable<Object> c = normal.completable |
| 3661 |
.andThen(Flowable.error(new TestException())); |
| 3662 |
|
| 3663 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 3664 |
|
| 3665 |
c.subscribe(ts); |
| 3666 |
|
| 3667 |
normal.assertSubscriptions(1); |
| 3668 |
|
| 3669 |
ts.assertNoValues(); |
| 3670 |
ts.assertError(TestException.class); |
| 3671 |
ts.assertNotComplete(); |
| 3672 |
} |
| 3673 |
|
| 3674 |
@Test |
| 3675 |
public void usingFactoryThrows() throws Throwable { |
| 3676 |
@SuppressWarnings("unchecked") |
| 3677 |
Consumer<Integer> onDispose = mock(Consumer.class); |
| 3678 |
|
| 3679 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 3680 |
|
| 3681 |
Completable.using(new Supplier<Integer>() { |
| 3682 |
@Override |
| 3683 |
public Integer get() { |
| 3684 |
return 1; |
| 3685 |
} |
| 3686 |
}, |
| 3687 |
new Function<Integer, Completable>() { |
| 3688 |
@Override |
| 3689 |
public Completable apply(Integer t) { |
| 3690 |
throw new TestException(); |
| 3691 |
} |
| 3692 |
}, onDispose).<Integer>toFlowable().subscribe(ts); |
| 3693 |
|
| 3694 |
verify(onDispose).accept(1); |
| 3695 |
|
| 3696 |
ts.assertNoValues(); |
| 3697 |
ts.assertNotComplete(); |
| 3698 |
ts.assertError(TestException.class); |
| 3699 |
} |
| 3700 |
|
| 3701 |
@Test |
| 3702 |
public void usingFactoryAndDisposerThrow() { |
| 3703 |
Consumer<Integer> onDispose = new Consumer<Integer>() { |
| 3704 |
@Override |
| 3705 |
public void accept(Integer t) { |
| 3706 |
throw new TestException(); |
| 3707 |
} |
| 3708 |
}; |
| 3709 |
|
| 3710 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 3711 |
|
| 3712 |
Completable.using(new Supplier<Integer>() { |
| 3713 |
@Override |
| 3714 |
public Integer get() { |
| 3715 |
return 1; |
| 3716 |
} |
| 3717 |
}, |
| 3718 |
new Function<Integer, Completable>() { |
| 3719 |
@Override |
| 3720 |
public Completable apply(Integer t) { |
| 3721 |
throw new TestException(); |
| 3722 |
} |
| 3723 |
}, onDispose).<Integer>toFlowable().subscribe(ts); |
| 3724 |
|
| 3725 |
ts.assertNoValues(); |
| 3726 |
ts.assertNotComplete(); |
| 3727 |
ts.assertError(CompositeException.class); |
| 3728 |
|
| 3729 |
CompositeException ex = (CompositeException)ts.errors().get(0); |
| 3730 |
|
| 3731 |
List<Throwable> listEx = ex.getExceptions(); |
| 3732 |
|
| 3733 |
assertEquals(2, listEx.size()); |
| 3734 |
|
| 3735 |
assertTrue(listEx.get(0).toString(), listEx.get(0) instanceof TestException); |
| 3736 |
assertTrue(listEx.get(1).toString(), listEx.get(1) instanceof TestException); |
| 3737 |
} |
| 3738 |
|
| 3739 |
@Test |
| 3740 |
public void usingFactoryReturnsNull() throws Throwable { |
| 3741 |
@SuppressWarnings("unchecked") |
| 3742 |
Consumer<Integer> onDispose = mock(Consumer.class); |
| 3743 |
|
| 3744 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 3745 |
|
| 3746 |
Completable.using(new Supplier<Integer>() { |
| 3747 |
@Override |
| 3748 |
public Integer get() { |
| 3749 |
return 1; |
| 3750 |
} |
| 3751 |
}, |
| 3752 |
new Function<Integer, Completable>() { |
| 3753 |
@Override |
| 3754 |
public Completable apply(Integer t) { |
| 3755 |
return null; |
| 3756 |
} |
| 3757 |
}, onDispose).<Integer>toFlowable().subscribe(ts); |
| 3758 |
|
| 3759 |
verify(onDispose).accept(1); |
| 3760 |
|
| 3761 |
ts.assertNoValues(); |
| 3762 |
ts.assertNotComplete(); |
| 3763 |
ts.assertError(NullPointerException.class); |
| 3764 |
} |
| 3765 |
|
| 3766 |
@Test |
| 3767 |
public void subscribeReportsUnsubscribed() { |
| 3768 |
PublishSubject<String> stringSubject = PublishSubject.create(); |
| 3769 |
Completable completable = stringSubject.ignoreElements(); |
| 3770 |
|
| 3771 |
Disposable completableSubscription = completable.subscribe(); |
| 3772 |
|
| 3773 |
stringSubject.onComplete(); |
| 3774 |
|
| 3775 |
assertTrue("Not unsubscribed?", completableSubscription.isDisposed()); |
| 3776 |
} |
| 3777 |
|
| 3778 |
@Test |
| 3779 |
public void hookSubscribeStart() throws Throwable { |
| 3780 |
TestSubscriber<String> ts = new TestSubscriber<>(); |
| 3781 |
|
| 3782 |
Completable completable = Completable.unsafeCreate(new CompletableSource() { |
| 3783 |
@Override public void subscribe(CompletableObserver observer) { |
| 3784 |
observer.onComplete(); |
| 3785 |
} |
| 3786 |
}); |
| 3787 |
completable.<String>toFlowable().subscribe(ts); |
| 3788 |
|
| 3789 |
verify(onStart, times(1)).apply(eq(completable), any(CompletableObserver.class)); |
| 3790 |
} |
| 3791 |
|
| 3792 |
@Test |
| 3793 |
public void onStartCalledSafe() { |
| 3794 |
TestSubscriber<Object> ts = new TestSubscriber<Object>() { |
| 3795 |
@Override |
| 3796 |
public void onStart() { |
| 3797 |
onNext(1); |
| 3798 |
} |
| 3799 |
}; |
| 3800 |
|
| 3801 |
normal.completable.<Object>toFlowable().subscribe(ts); |
| 3802 |
|
| 3803 |
ts.assertValue(1); |
| 3804 |
ts.assertNoErrors(); |
| 3805 |
ts.assertComplete(); |
| 3806 |
} |
| 3807 |
|
| 3808 |
@Test |
| 3809 |
public void onErrorCompleteFunctionThrows() { |
| 3810 |
TestSubscriberEx<String> ts = new TestSubscriberEx<>(); |
| 3811 |
|
| 3812 |
error.completable.onErrorComplete(new Predicate<Throwable>() { |
| 3813 |
@Override |
| 3814 |
public boolean test(Throwable t) { |
| 3815 |
throw new TestException("Forced inner failure"); |
| 3816 |
} |
| 3817 |
}).<String>toFlowable().subscribe(ts); |
| 3818 |
|
| 3819 |
ts.assertNoValues(); |
| 3820 |
ts.assertNotComplete(); |
| 3821 |
ts.assertError(CompositeException.class); |
| 3822 |
|
| 3823 |
CompositeException composite = (CompositeException)ts.errors().get(0); |
| 3824 |
|
| 3825 |
List<Throwable> errors = composite.getExceptions(); |
| 3826 |
Assert.assertEquals(2, errors.size()); |
| 3827 |
|
| 3828 |
Assert.assertTrue(errors.get(0).toString(), errors.get(0) instanceof TestException); |
| 3829 |
Assert.assertNull(errors.get(0).toString(), errors.get(0).getMessage()); |
| 3830 |
Assert.assertTrue(errors.get(1).toString(), errors.get(1) instanceof TestException); |
| 3831 |
Assert.assertEquals(errors.get(1).toString(), "Forced inner failure", errors.get(1).getMessage()); |
| 3832 |
} |
| 3833 |
|
| 3834 |
@Test |
| 3835 |
public void subscribeAction2ReportsUnsubscribedAfter() { |
| 3836 |
PublishSubject<String> stringSubject = PublishSubject.create(); |
| 3837 |
Completable completable = stringSubject.ignoreElements(); |
| 3838 |
|
| 3839 |
final AtomicReference<Disposable> disposableRef = new AtomicReference<>(); |
| 3840 |
Disposable completableSubscription = completable.subscribe(new Action() { |
| 3841 |
@Override |
| 3842 |
public void run() { |
| 3843 |
if (disposableRef.get().isDisposed()) { |
| 3844 |
disposableRef.set(null); |
| 3845 |
} |
| 3846 |
} |
| 3847 |
}, Functions.emptyConsumer()); |
| 3848 |
disposableRef.set(completableSubscription); |
| 3849 |
|
| 3850 |
stringSubject.onComplete(); |
| 3851 |
|
| 3852 |
assertTrue("Not unsubscribed?", completableSubscription.isDisposed()); |
| 3853 |
assertNotNull("Unsubscribed before the call to onComplete", disposableRef.get()); |
| 3854 |
} |
| 3855 |
|
| 3856 |
@Test |
| 3857 |
public void subscribeAction2ReportsUnsubscribedOnErrorAfter() { |
| 3858 |
PublishSubject<String> stringSubject = PublishSubject.create(); |
| 3859 |
Completable completable = stringSubject.ignoreElements(); |
| 3860 |
|
| 3861 |
final AtomicReference<Disposable> disposableRef = new AtomicReference<>(); |
| 3862 |
Disposable completableSubscription = completable.subscribe(Functions.EMPTY_ACTION, |
| 3863 |
new Consumer<Throwable>() { |
| 3864 |
@Override |
| 3865 |
public void accept(Throwable e) { |
| 3866 |
if (disposableRef.get().isDisposed()) { |
| 3867 |
disposableRef.set(null); |
| 3868 |
} |
| 3869 |
} |
| 3870 |
}); |
| 3871 |
disposableRef.set(completableSubscription); |
| 3872 |
|
| 3873 |
stringSubject.onError(new TestException()); |
| 3874 |
|
| 3875 |
assertTrue("Not unsubscribed?", completableSubscription.isDisposed()); |
| 3876 |
assertNotNull("Unsubscribed before the call to onError", disposableRef.get()); |
| 3877 |
} |
| 3878 |
|
| 3879 |
@Test |
| 3880 |
public void propagateExceptionSubscribeOneActionThrowFromOnSuccess() { |
| 3881 |
expectUncaughtTestException(new Action() { |
| 3882 |
@Override |
| 3883 |
public void run() { |
| 3884 |
normal.completable.toSingleDefault(1).subscribe(new Consumer<Integer>() { |
| 3885 |
@Override |
| 3886 |
public void accept(Integer integer) { |
| 3887 |
throw new TestException(); |
| 3888 |
} |
| 3889 |
}); |
| 3890 |
} |
| 3891 |
}); |
| 3892 |
} |
| 3893 |
|
| 3894 |
@Test |
| 3895 |
public void andThenNever() { |
| 3896 |
TestSubscriberEx<String> ts = new TestSubscriberEx<>(0); |
| 3897 |
Completable.never().andThen(Flowable.just("foo")).subscribe(ts); |
| 3898 |
ts.request(1); |
| 3899 |
ts.assertNoValues(); |
| 3900 |
ts.assertNotTerminated(); |
| 3901 |
} |
| 3902 |
|
| 3903 |
@Test |
| 3904 |
public void andThenError() { |
| 3905 |
TestSubscriber<String> ts = new TestSubscriber<>(0); |
| 3906 |
final AtomicBoolean hasRun = new AtomicBoolean(false); |
| 3907 |
final Exception e = new Exception(); |
| 3908 |
Completable.unsafeCreate(new CompletableSource() { |
| 3909 |
@Override |
| 3910 |
public void subscribe(CompletableObserver co) { |
| 3911 |
co.onSubscribe(Disposable.empty()); |
| 3912 |
co.onError(e); |
| 3913 |
} |
| 3914 |
}) |
| 3915 |
.andThen(Flowable.<String>unsafeCreate(new Publisher<String>() { |
| 3916 |
@Override |
| 3917 |
public void subscribe(Subscriber<? super String> s) { |
| 3918 |
hasRun.set(true); |
| 3919 |
s.onSubscribe(new BooleanSubscription()); |
| 3920 |
s.onNext("foo"); |
| 3921 |
s.onComplete(); |
| 3922 |
} |
| 3923 |
})) |
| 3924 |
.subscribe(ts); |
| 3925 |
ts.assertNoValues(); |
| 3926 |
ts.assertError(e); |
| 3927 |
Assert.assertFalse("Should not have subscribed to observable when completable errors", hasRun.get()); |
| 3928 |
} |
| 3929 |
|
| 3930 |
@Test |
| 3931 |
public void andThenSingle() { |
| 3932 |
TestSubscriber<String> ts = new TestSubscriber<>(0); |
| 3933 |
Completable.complete().andThen(Single.just("foo")).toFlowable().subscribe(ts); |
| 3934 |
ts.request(1); |
| 3935 |
ts.assertValue("foo"); |
| 3936 |
ts.assertComplete(); |
| 3937 |
ts.assertNoErrors(); |
| 3938 |
} |
| 3939 |
|
| 3940 |
@Test |
| 3941 |
public void fromFutureNormal() { |
| 3942 |
ExecutorService exec = Executors.newSingleThreadExecutor(); |
| 3943 |
|
| 3944 |
try { |
| 3945 |
Completable c = Completable.fromFuture(exec.submit(new Runnable() { |
| 3946 |
@Override |
| 3947 |
public void run() { |
| 3948 |
// no action |
| 3949 |
} |
| 3950 |
})); |
| 3951 |
|
| 3952 |
c.blockingAwait(); |
| 3953 |
} finally { |
| 3954 |
exec.shutdown(); |
| 3955 |
} |
| 3956 |
} |
| 3957 |
|
| 3958 |
@Test |
| 3959 |
public void fromFutureThrows() { |
| 3960 |
ExecutorService exec = Executors.newSingleThreadExecutor(); |
| 3961 |
|
| 3962 |
Completable c = Completable.fromFuture(exec.submit(new Runnable() { |
| 3963 |
@Override |
| 3964 |
public void run() { |
| 3965 |
throw new TestException(); |
| 3966 |
} |
| 3967 |
})); |
| 3968 |
|
| 3969 |
try { |
| 3970 |
c.blockingAwait(); |
| 3971 |
Assert.fail("Failed to throw Exception"); |
| 3972 |
} catch (RuntimeException ex) { |
| 3973 |
if (!((ex.getCause() instanceof ExecutionException) && (ex.getCause().getCause() instanceof TestException))) { |
| 3974 |
ex.printStackTrace(); |
| 3975 |
Assert.fail("Wrong exception received"); |
| 3976 |
} |
| 3977 |
} finally { |
| 3978 |
exec.shutdown(); |
| 3979 |
} |
| 3980 |
} |
| 3981 |
|
| 3982 |
@Test |
| 3983 |
public void fromRunnableNormal() { |
| 3984 |
final AtomicInteger calls = new AtomicInteger(); |
| 3985 |
|
| 3986 |
Completable c = Completable.fromRunnable(new Runnable() { |
| 3987 |
@Override |
| 3988 |
public void run() { |
| 3989 |
calls.getAndIncrement(); |
| 3990 |
} |
| 3991 |
}); |
| 3992 |
|
| 3993 |
c.blockingAwait(); |
| 3994 |
|
| 3995 |
Assert.assertEquals(1, calls.get()); |
| 3996 |
} |
| 3997 |
|
| 3998 |
@Test(expected = TestException.class) |
| 3999 |
public void fromRunnableThrows() { |
| 4000 |
Completable c = Completable.fromRunnable(new Runnable() { |
| 4001 |
@Override |
| 4002 |
public void run() { throw new TestException(); } |
| 4003 |
}); |
| 4004 |
|
| 4005 |
c.blockingAwait(); |
| 4006 |
} |
| 4007 |
|
| 4008 |
@Test |
| 4009 |
public void doOnEventComplete() { |
| 4010 |
final AtomicInteger atomicInteger = new AtomicInteger(0); |
| 4011 |
|
| 4012 |
Completable.complete().doOnEvent(new Consumer<Throwable>() { |
| 4013 |
@Override |
| 4014 |
public void accept(final Throwable throwable) throws Exception { |
| 4015 |
if (throwable == null) { |
| 4016 |
atomicInteger.incrementAndGet(); |
| 4017 |
} |
| 4018 |
} |
| 4019 |
}).subscribe(); |
| 4020 |
|
| 4021 |
assertEquals(1, atomicInteger.get()); |
| 4022 |
} |
| 4023 |
|
| 4024 |
@Test |
| 4025 |
public void doOnEventError() { |
| 4026 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 4027 |
try { |
| 4028 |
final AtomicInteger atomicInteger = new AtomicInteger(0); |
| 4029 |
|
| 4030 |
Completable.error(new RuntimeException()).doOnEvent(new Consumer<Throwable>() { |
| 4031 |
@Override |
| 4032 |
public void accept(final Throwable throwable) throws Exception { |
| 4033 |
if (throwable != null) { |
| 4034 |
atomicInteger.incrementAndGet(); |
| 4035 |
} |
| 4036 |
} |
| 4037 |
}).subscribe(); |
| 4038 |
|
| 4039 |
assertEquals(1, atomicInteger.get()); |
| 4040 |
|
| 4041 |
TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class); |
| 4042 |
} finally { |
| 4043 |
RxJavaPlugins.reset(); |
| 4044 |
} |
| 4045 |
} |
| 4046 |
|
| 4047 |
@Test |
| 4048 |
public void subscribeTwoCallbacksDispose() { |
| 4049 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 4050 |
Disposable d = pp.ignoreElements().subscribe(Functions.EMPTY_ACTION, Functions.emptyConsumer()); |
| 4051 |
|
| 4052 |
assertFalse(d.isDisposed()); |
| 4053 |
assertTrue(pp.hasSubscribers()); |
| 4054 |
|
| 4055 |
d.dispose(); |
| 4056 |
|
| 4057 |
assertTrue(d.isDisposed()); |
| 4058 |
assertFalse(pp.hasSubscribers()); |
| 4059 |
} |
| 4060 |
|
| 4061 |
} |