| 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.observers; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.TimeUnit; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
import org.junit.function.ThrowingRunnable; |
| 25 |
import org.mockito.InOrder; |
| 26 |
import org.reactivestreams.Subscriber; |
| 27 |
|
| 28 |
import io.reactivex.rxjava3.core.*; |
| 29 |
import io.reactivex.rxjava3.core.Observable; |
| 30 |
import io.reactivex.rxjava3.core.Observer; |
| 31 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 32 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 33 |
import io.reactivex.rxjava3.functions.Predicate; |
| 34 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 35 |
import io.reactivex.rxjava3.internal.operators.observable.ObservableScalarXMap.ScalarDisposable; |
| 36 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 37 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 38 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 39 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 40 |
|
| 41 |
public class TestObserverTest extends RxJavaTest { |
| 42 |
|
| 43 |
static void assertThrowsWithMessage(String message, Class<? extends Throwable> clazz, ThrowingRunnable run) { |
| 44 |
assertEquals(message, assertThrows(clazz, run).getMessage()); |
| 45 |
} |
| 46 |
|
| 47 |
static void assertThrowsWithMessageMatchRegex(String regex, Class<? extends Throwable> clazz, ThrowingRunnable run) { |
| 48 |
assertTrue(assertThrows(clazz, run).getMessage().matches(regex)); |
| 49 |
} |
| 50 |
|
| 51 |
private static final String ASSERT_MESSAGE_REGEX = "\nexpected: (.*)\n\\s*got: (.*)"; |
| 52 |
|
| 53 |
@Test |
| 54 |
public void assertTestObserver() { |
| 55 |
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2)); |
| 56 |
TestSubscriber<Integer> subscriber = new TestSubscriber<>(); |
| 57 |
oi.subscribe(subscriber); |
| 58 |
|
| 59 |
subscriber.assertValues(1, 2); |
| 60 |
subscriber.assertValueCount(2); |
| 61 |
subscriber.assertComplete().assertNoErrors(); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void assertNotMatchCount() { |
| 66 |
assertThrows(AssertionError.class, () -> { |
| 67 |
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2)); |
| 68 |
TestSubscriber<Integer> subscriber = new TestSubscriber<>(); |
| 69 |
oi.subscribe(subscriber); |
| 70 |
|
| 71 |
subscriber.assertValue(1); |
| 72 |
subscriber.assertValueCount(2); |
| 73 |
subscriber.assertComplete().assertNoErrors(); |
| 74 |
}); |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void assertNotMatchValue() { |
| 79 |
assertThrows(AssertionError.class, () -> { |
| 80 |
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2)); |
| 81 |
TestSubscriber<Integer> subscriber = new TestSubscriber<>(); |
| 82 |
oi.subscribe(subscriber); |
| 83 |
|
| 84 |
subscriber.assertValues(1, 3); |
| 85 |
subscriber.assertValueCount(2); |
| 86 |
subscriber.assertComplete().assertNoErrors(); |
| 87 |
}); |
| 88 |
} |
| 89 |
|
| 90 |
@Test |
| 91 |
public void assertTerminalEventNotReceived() { |
| 92 |
assertThrows(AssertionError.class, () -> { |
| 93 |
PublishProcessor<Integer> p = PublishProcessor.create(); |
| 94 |
TestSubscriber<Integer> subscriber = new TestSubscriber<>(); |
| 95 |
p.subscribe(subscriber); |
| 96 |
|
| 97 |
p.onNext(1); |
| 98 |
p.onNext(2); |
| 99 |
|
| 100 |
subscriber.assertValues(1, 2); |
| 101 |
subscriber.assertValueCount(2); |
| 102 |
subscriber.assertComplete().assertNoErrors(); |
| 103 |
}); |
| 104 |
} |
| 105 |
|
| 106 |
@Test |
| 107 |
public void wrappingMock() { |
| 108 |
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2)); |
| 109 |
|
| 110 |
Subscriber<Integer> mockSubscriber = TestHelper.mockSubscriber(); |
| 111 |
|
| 112 |
oi.subscribe(new TestSubscriber<>(mockSubscriber)); |
| 113 |
|
| 114 |
InOrder inOrder = inOrder(mockSubscriber); |
| 115 |
inOrder.verify(mockSubscriber, times(1)).onNext(1); |
| 116 |
inOrder.verify(mockSubscriber, times(1)).onNext(2); |
| 117 |
inOrder.verify(mockSubscriber, times(1)).onComplete(); |
| 118 |
inOrder.verifyNoMoreInteractions(); |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void wrappingMockWhenUnsubscribeInvolved() { |
| 123 |
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9)).take(2); |
| 124 |
Subscriber<Integer> mockSubscriber = TestHelper.mockSubscriber(); |
| 125 |
oi.subscribe(new TestSubscriber<>(mockSubscriber)); |
| 126 |
|
| 127 |
InOrder inOrder = inOrder(mockSubscriber); |
| 128 |
inOrder.verify(mockSubscriber, times(1)).onNext(1); |
| 129 |
inOrder.verify(mockSubscriber, times(1)).onNext(2); |
| 130 |
inOrder.verify(mockSubscriber, times(1)).onComplete(); |
| 131 |
inOrder.verifyNoMoreInteractions(); |
| 132 |
} |
| 133 |
|
| 134 |
@Test |
| 135 |
public void errorSwallowed() { |
| 136 |
Flowable.error(new RuntimeException()).subscribe(new TestSubscriber<>()); |
| 137 |
} |
| 138 |
|
| 139 |
@Test |
| 140 |
public void nullExpected() { |
| 141 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 142 |
ts.onNext(1); |
| 143 |
|
| 144 |
try { |
| 145 |
ts.assertValue((Integer) null); |
| 146 |
} catch (AssertionError ex) { |
| 147 |
// this is expected |
| 148 |
return; |
| 149 |
} |
| 150 |
fail("Null element check assertion didn't happen!"); |
| 151 |
} |
| 152 |
|
| 153 |
@Test |
| 154 |
public void nullActual() { |
| 155 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 156 |
ts.onNext(null); |
| 157 |
|
| 158 |
try { |
| 159 |
ts.assertValue(1); |
| 160 |
} catch (AssertionError ex) { |
| 161 |
// this is expected |
| 162 |
return; |
| 163 |
} |
| 164 |
fail("Null element check assertion didn't happen!"); |
| 165 |
} |
| 166 |
|
| 167 |
@Test |
| 168 |
public void createDelegate() { |
| 169 |
TestObserver<Integer> to1 = TestObserver.create(); |
| 170 |
|
| 171 |
TestObserver<Integer> to = TestObserver.create(to1); |
| 172 |
|
| 173 |
assertFalse(to.hasSubscription()); |
| 174 |
|
| 175 |
to.onSubscribe(Disposable.empty()); |
| 176 |
|
| 177 |
assertTrue(to.hasSubscription()); |
| 178 |
|
| 179 |
assertFalse(to.isDisposed()); |
| 180 |
|
| 181 |
to.onNext(1); |
| 182 |
to.onError(new TestException()); |
| 183 |
to.onComplete(); |
| 184 |
|
| 185 |
to1.assertValue(1).assertError(TestException.class).assertComplete(); |
| 186 |
|
| 187 |
to.dispose(); |
| 188 |
|
| 189 |
assertTrue(to.isDisposed()); |
| 190 |
|
| 191 |
try { |
| 192 |
to.assertNoValues(); |
| 193 |
throw new RuntimeException("Should have thrown"); |
| 194 |
} catch (AssertionError exc) { |
| 195 |
// expected |
| 196 |
} |
| 197 |
|
| 198 |
try { |
| 199 |
to.assertValueCount(0); |
| 200 |
throw new RuntimeException("Should have thrown"); |
| 201 |
} catch (AssertionError exc) { |
| 202 |
// expected |
| 203 |
} |
| 204 |
|
| 205 |
to.assertValueSequence(Collections.singletonList(1)); |
| 206 |
|
| 207 |
try { |
| 208 |
to.assertValueSequence(Collections.singletonList(2)); |
| 209 |
throw new RuntimeException("Should have thrown"); |
| 210 |
} catch (AssertionError exc) { |
| 211 |
// expected |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
@Test |
| 216 |
public void assertError() { |
| 217 |
TestObserver<Integer> to = TestObserver.create(); |
| 218 |
|
| 219 |
try { |
| 220 |
to.assertError(TestException.class); |
| 221 |
throw new RuntimeException("Should have thrown"); |
| 222 |
} catch (AssertionError ex) { |
| 223 |
// expected |
| 224 |
} |
| 225 |
|
| 226 |
try { |
| 227 |
to.assertError(new TestException()); |
| 228 |
throw new RuntimeException("Should have thrown"); |
| 229 |
} catch (AssertionError ex) { |
| 230 |
// expected |
| 231 |
} |
| 232 |
|
| 233 |
try { |
| 234 |
to.assertError(Functions.<Throwable>alwaysTrue()); |
| 235 |
throw new RuntimeException("Should have thrown"); |
| 236 |
} catch (AssertionError ex) { |
| 237 |
// expected |
| 238 |
} |
| 239 |
|
| 240 |
try { |
| 241 |
to.assertSubscribed(); |
| 242 |
throw new RuntimeException("Should have thrown"); |
| 243 |
} catch (AssertionError exc) { |
| 244 |
// expected |
| 245 |
} |
| 246 |
|
| 247 |
to.onSubscribe(Disposable.empty()); |
| 248 |
|
| 249 |
to.assertSubscribed(); |
| 250 |
|
| 251 |
to.assertNoErrors(); |
| 252 |
|
| 253 |
TestException ex = new TestException("Forced failure"); |
| 254 |
|
| 255 |
to.onError(ex); |
| 256 |
|
| 257 |
to.assertError(ex); |
| 258 |
|
| 259 |
to.assertError(TestException.class); |
| 260 |
|
| 261 |
to.assertError(Functions.<Throwable>alwaysTrue()); |
| 262 |
|
| 263 |
to.assertError(new Predicate<Throwable>() { |
| 264 |
@Override |
| 265 |
public boolean test(Throwable t) throws Exception { |
| 266 |
return t.getMessage() != null && t.getMessage().contains("Forced"); |
| 267 |
} |
| 268 |
}); |
| 269 |
|
| 270 |
try { |
| 271 |
to.assertError(new RuntimeException()); |
| 272 |
throw new RuntimeException("Should have thrown"); |
| 273 |
} catch (AssertionError exc) { |
| 274 |
// expected |
| 275 |
} |
| 276 |
|
| 277 |
try { |
| 278 |
to.assertError(IOException.class); |
| 279 |
throw new RuntimeException("Should have thrown"); |
| 280 |
} catch (AssertionError exc) { |
| 281 |
// expected |
| 282 |
} |
| 283 |
|
| 284 |
try { |
| 285 |
to.assertError(Functions.<Throwable>alwaysFalse()); |
| 286 |
throw new RuntimeException("Should have thrown"); |
| 287 |
} catch (AssertionError exc) { |
| 288 |
// expected |
| 289 |
} |
| 290 |
|
| 291 |
try { |
| 292 |
to.assertNoErrors(); |
| 293 |
throw new RuntimeException("Should have thrown"); |
| 294 |
} catch (AssertionError exc) { |
| 295 |
// expected |
| 296 |
} |
| 297 |
|
| 298 |
to.assertValueCount(0); |
| 299 |
|
| 300 |
to.assertNoValues(); |
| 301 |
} |
| 302 |
|
| 303 |
@Test |
| 304 |
public void emptyObserverEnum() { |
| 305 |
assertEquals(1, TestObserver.EmptyObserver.values().length); |
| 306 |
assertNotNull(TestObserver.EmptyObserver.valueOf("INSTANCE")); |
| 307 |
} |
| 308 |
|
| 309 |
@Test |
| 310 |
public void valueAndClass() { |
| 311 |
assertEquals("null", TestObserver.valueAndClass(null)); |
| 312 |
assertEquals("1 (class: Integer)", TestObserver.valueAndClass(1)); |
| 313 |
} |
| 314 |
|
| 315 |
@Test |
| 316 |
public void assertFailure() { |
| 317 |
TestObserver<Integer> to = TestObserver.create(); |
| 318 |
|
| 319 |
to.onSubscribe(Disposable.empty()); |
| 320 |
|
| 321 |
to.onError(new TestException("Forced failure")); |
| 322 |
|
| 323 |
to.assertFailure(TestException.class); |
| 324 |
|
| 325 |
to.onNext(1); |
| 326 |
|
| 327 |
to.assertFailure(TestException.class, 1); |
| 328 |
} |
| 329 |
|
| 330 |
@Test |
| 331 |
public void assertFuseable() { |
| 332 |
TestObserver<Integer> to = TestObserver.create(); |
| 333 |
|
| 334 |
to.onSubscribe(Disposable.empty()); |
| 335 |
|
| 336 |
to = TestObserver.create(); |
| 337 |
|
| 338 |
to.onSubscribe(new ScalarDisposable<>(to, 1)); |
| 339 |
} |
| 340 |
|
| 341 |
@Test |
| 342 |
public void assertResult() { |
| 343 |
TestObserver<Integer> to = TestObserver.create(); |
| 344 |
|
| 345 |
to.onSubscribe(Disposable.empty()); |
| 346 |
|
| 347 |
to.onComplete(); |
| 348 |
|
| 349 |
to.assertResult(); |
| 350 |
|
| 351 |
try { |
| 352 |
to.assertResult(1); |
| 353 |
throw new RuntimeException("Should have thrown"); |
| 354 |
} catch (AssertionError ex) { |
| 355 |
// expected |
| 356 |
} |
| 357 |
|
| 358 |
to.onNext(1); |
| 359 |
|
| 360 |
to.assertResult(1); |
| 361 |
|
| 362 |
try { |
| 363 |
to.assertResult(2); |
| 364 |
throw new RuntimeException("Should have thrown"); |
| 365 |
} catch (AssertionError ex) { |
| 366 |
// expected |
| 367 |
} |
| 368 |
|
| 369 |
try { |
| 370 |
to.assertResult(); |
| 371 |
throw new RuntimeException("Should have thrown"); |
| 372 |
} catch (AssertionError ex) { |
| 373 |
// expected |
| 374 |
} |
| 375 |
|
| 376 |
} |
| 377 |
|
| 378 |
@Test |
| 379 |
public void await() throws Exception { |
| 380 |
TestObserver<Integer> to = TestObserver.create(); |
| 381 |
|
| 382 |
to.onSubscribe(Disposable.empty()); |
| 383 |
|
| 384 |
assertFalse(to.await(100, TimeUnit.MILLISECONDS)); |
| 385 |
|
| 386 |
to.awaitDone(100, TimeUnit.MILLISECONDS); |
| 387 |
|
| 388 |
assertTrue(to.isDisposed()); |
| 389 |
|
| 390 |
assertFalse(to.await(100, TimeUnit.MILLISECONDS)); |
| 391 |
|
| 392 |
to.assertNotComplete(); |
| 393 |
to.assertNoErrors(); |
| 394 |
|
| 395 |
to.onComplete(); |
| 396 |
|
| 397 |
assertTrue(to.await(100, TimeUnit.MILLISECONDS)); |
| 398 |
|
| 399 |
to.await(); |
| 400 |
|
| 401 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 402 |
|
| 403 |
to.assertComplete(); |
| 404 |
to.assertNoErrors(); |
| 405 |
|
| 406 |
assertTrue(to.await(5, TimeUnit.SECONDS)); |
| 407 |
|
| 408 |
final TestObserver<Integer> to1 = TestObserver.create(); |
| 409 |
|
| 410 |
to1.onSubscribe(Disposable.empty()); |
| 411 |
|
| 412 |
Schedulers.single().scheduleDirect(new Runnable() { |
| 413 |
@Override |
| 414 |
public void run() { |
| 415 |
to1.onComplete(); |
| 416 |
} |
| 417 |
}, 200, TimeUnit.MILLISECONDS); |
| 418 |
|
| 419 |
to1.await(); |
| 420 |
} |
| 421 |
|
| 422 |
@Test |
| 423 |
public void onNext() { |
| 424 |
TestObserver<Integer> to = TestObserver.create(); |
| 425 |
|
| 426 |
to.onSubscribe(Disposable.empty()); |
| 427 |
|
| 428 |
assertEquals(0, to.values().size()); |
| 429 |
|
| 430 |
assertEquals(Collections.emptyList(), to.values()); |
| 431 |
|
| 432 |
to.onNext(1); |
| 433 |
|
| 434 |
assertEquals(Collections.singletonList(1), to.values()); |
| 435 |
|
| 436 |
to.dispose(); |
| 437 |
|
| 438 |
assertTrue(to.isDisposed()); |
| 439 |
|
| 440 |
to.assertValue(1); |
| 441 |
|
| 442 |
to.onComplete(); |
| 443 |
} |
| 444 |
|
| 445 |
@Test |
| 446 |
public void multipleTerminals() { |
| 447 |
TestObserver<Integer> to = TestObserver.create(); |
| 448 |
|
| 449 |
to.onSubscribe(Disposable.empty()); |
| 450 |
|
| 451 |
to.assertNotComplete(); |
| 452 |
|
| 453 |
to.onComplete(); |
| 454 |
|
| 455 |
try { |
| 456 |
to.assertNotComplete(); |
| 457 |
throw new RuntimeException("Should have thrown"); |
| 458 |
} catch (Throwable ex) { |
| 459 |
// expected |
| 460 |
} |
| 461 |
|
| 462 |
to.onComplete(); |
| 463 |
|
| 464 |
try { |
| 465 |
to.assertComplete(); |
| 466 |
throw new RuntimeException("Should have thrown"); |
| 467 |
} catch (Throwable ex) { |
| 468 |
// expected |
| 469 |
} |
| 470 |
|
| 471 |
try { |
| 472 |
to.assertNotComplete(); |
| 473 |
throw new RuntimeException("Should have thrown"); |
| 474 |
} catch (Throwable ex) { |
| 475 |
// expected |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
@Test |
| 480 |
public void assertValue() { |
| 481 |
TestObserver<Integer> to = TestObserver.create(); |
| 482 |
|
| 483 |
to.onSubscribe(Disposable.empty()); |
| 484 |
|
| 485 |
try { |
| 486 |
to.assertValue(1); |
| 487 |
throw new RuntimeException("Should have thrown"); |
| 488 |
} catch (Throwable ex) { |
| 489 |
// expected |
| 490 |
} |
| 491 |
|
| 492 |
to.onNext(1); |
| 493 |
|
| 494 |
to.assertValue(1); |
| 495 |
|
| 496 |
try { |
| 497 |
to.assertValue(2); |
| 498 |
throw new RuntimeException("Should have thrown"); |
| 499 |
} catch (Throwable ex) { |
| 500 |
// expected |
| 501 |
} |
| 502 |
|
| 503 |
to.onNext(2); |
| 504 |
|
| 505 |
try { |
| 506 |
to.assertValue(1); |
| 507 |
throw new RuntimeException("Should have thrown"); |
| 508 |
} catch (Throwable ex) { |
| 509 |
// expected |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
@Test |
| 514 |
public void onNextMisbehave() { |
| 515 |
TestObserver<Integer> to = TestObserver.create(); |
| 516 |
|
| 517 |
to.onNext(1); |
| 518 |
|
| 519 |
to.assertError(IllegalStateException.class); |
| 520 |
|
| 521 |
to = TestObserver.create(); |
| 522 |
|
| 523 |
to.onSubscribe(Disposable.empty()); |
| 524 |
|
| 525 |
to.onNext(null); |
| 526 |
|
| 527 |
to.assertFailure(NullPointerException.class, (Integer)null); |
| 528 |
} |
| 529 |
|
| 530 |
@Test |
| 531 |
public void awaitTerminalEventInterrupt() { |
| 532 |
final TestObserver<Integer> to = TestObserver.create(); |
| 533 |
|
| 534 |
to.onSubscribe(Disposable.empty()); |
| 535 |
|
| 536 |
Thread.currentThread().interrupt(); |
| 537 |
|
| 538 |
try { |
| 539 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 540 |
} catch (RuntimeException allowed) { |
| 541 |
assertTrue(allowed.toString(), allowed.getCause() instanceof InterruptedException); |
| 542 |
} |
| 543 |
|
| 544 |
// FIXME ? catch consumes this flag |
| 545 |
// assertTrue(Thread.interrupted()); |
| 546 |
|
| 547 |
Thread.currentThread().interrupt(); |
| 548 |
|
| 549 |
try { |
| 550 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 551 |
} catch (RuntimeException allowed) { |
| 552 |
assertTrue(allowed.toString(), allowed.getCause() instanceof InterruptedException); |
| 553 |
} |
| 554 |
|
| 555 |
// FIXME ? catch consumes this flag |
| 556 |
// assertTrue(Thread.interrupted()); |
| 557 |
} |
| 558 |
|
| 559 |
@Test |
| 560 |
public void assertTerminated2() { |
| 561 |
TestObserver<Integer> to = TestObserver.create(); |
| 562 |
|
| 563 |
to.onSubscribe(Disposable.empty()); |
| 564 |
|
| 565 |
to.onError(new TestException()); |
| 566 |
to.onError(new IOException()); |
| 567 |
|
| 568 |
try { |
| 569 |
to.assertError(TestException.class); |
| 570 |
throw new RuntimeException("Should have thrown"); |
| 571 |
} catch (AssertionError ex) { |
| 572 |
// expected |
| 573 |
} |
| 574 |
|
| 575 |
to = TestObserver.create(); |
| 576 |
|
| 577 |
to.onSubscribe(Disposable.empty()); |
| 578 |
|
| 579 |
to.onError(new TestException()); |
| 580 |
to.onComplete(); |
| 581 |
} |
| 582 |
|
| 583 |
@Test |
| 584 |
public void onSubscribe() { |
| 585 |
TestObserver<Integer> to = TestObserver.create(); |
| 586 |
|
| 587 |
to.onSubscribe(null); |
| 588 |
|
| 589 |
to.assertError(NullPointerException.class); |
| 590 |
|
| 591 |
to = TestObserver.create(); |
| 592 |
|
| 593 |
to.onSubscribe(Disposable.empty()); |
| 594 |
|
| 595 |
Disposable d1 = Disposable.empty(); |
| 596 |
|
| 597 |
to.onSubscribe(d1); |
| 598 |
|
| 599 |
assertTrue(d1.isDisposed()); |
| 600 |
|
| 601 |
to.assertError(IllegalStateException.class); |
| 602 |
|
| 603 |
to = TestObserver.create(); |
| 604 |
to.dispose(); |
| 605 |
|
| 606 |
d1 = Disposable.empty(); |
| 607 |
|
| 608 |
to.onSubscribe(d1); |
| 609 |
|
| 610 |
assertTrue(d1.isDisposed()); |
| 611 |
|
| 612 |
} |
| 613 |
|
| 614 |
@Test |
| 615 |
public void assertValueSequence() { |
| 616 |
TestObserver<Integer> to = TestObserver.create(); |
| 617 |
|
| 618 |
to.onSubscribe(Disposable.empty()); |
| 619 |
|
| 620 |
to.onNext(1); |
| 621 |
to.onNext(2); |
| 622 |
|
| 623 |
try { |
| 624 |
to.assertValueSequence(Collections.<Integer>emptyList()); |
| 625 |
throw new RuntimeException("Should have thrown"); |
| 626 |
} catch (AssertionError expected) { |
| 627 |
assertTrue(expected.getMessage(), expected.getMessage().startsWith("More values received than expected (0)")); |
| 628 |
} |
| 629 |
|
| 630 |
try { |
| 631 |
to.assertValueSequence(Collections.singletonList(1)); |
| 632 |
throw new RuntimeException("Should have thrown"); |
| 633 |
} catch (AssertionError expected) { |
| 634 |
assertTrue(expected.getMessage(), expected.getMessage().startsWith("More values received than expected (1)")); |
| 635 |
} |
| 636 |
|
| 637 |
to.assertValueSequence(Arrays.asList(1, 2)); |
| 638 |
|
| 639 |
try { |
| 640 |
to.assertValueSequence(Arrays.asList(1, 2, 3)); |
| 641 |
throw new RuntimeException("Should have thrown"); |
| 642 |
} catch (AssertionError expected) { |
| 643 |
assertTrue(expected.getMessage(), expected.getMessage().startsWith("Fewer values received than expected (2)")); |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
@Test |
| 648 |
public void assertEmpty() { |
| 649 |
TestObserver<Integer> to = new TestObserver<>(); |
| 650 |
|
| 651 |
try { |
| 652 |
to.assertEmpty(); |
| 653 |
throw new RuntimeException("Should have thrown!"); |
| 654 |
} catch (AssertionError ex) { |
| 655 |
// expected |
| 656 |
} |
| 657 |
|
| 658 |
to.onSubscribe(Disposable.empty()); |
| 659 |
|
| 660 |
to.assertEmpty(); |
| 661 |
|
| 662 |
to.onNext(1); |
| 663 |
|
| 664 |
try { |
| 665 |
to.assertEmpty(); |
| 666 |
throw new RuntimeException("Should have thrown!"); |
| 667 |
} catch (AssertionError ex) { |
| 668 |
// expected |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
@Test |
| 673 |
public void awaitDoneTimed() { |
| 674 |
TestObserver<Integer> to = new TestObserver<>(); |
| 675 |
|
| 676 |
Thread.currentThread().interrupt(); |
| 677 |
|
| 678 |
try { |
| 679 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 680 |
} catch (RuntimeException ex) { |
| 681 |
assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException); |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
@Test |
| 686 |
public void assertErrorMultiple() { |
| 687 |
TestObserver<Integer> to = new TestObserver<>(); |
| 688 |
|
| 689 |
TestException e = new TestException(); |
| 690 |
to.onError(e); |
| 691 |
to.onError(new TestException()); |
| 692 |
|
| 693 |
try { |
| 694 |
to.assertError(TestException.class); |
| 695 |
throw new RuntimeException("Should have thrown!"); |
| 696 |
} catch (AssertionError ex) { |
| 697 |
// expected |
| 698 |
} |
| 699 |
try { |
| 700 |
to.assertError(e); |
| 701 |
throw new RuntimeException("Should have thrown!"); |
| 702 |
} catch (AssertionError ex) { |
| 703 |
// expected |
| 704 |
} |
| 705 |
try { |
| 706 |
to.assertError(Functions.<Throwable>alwaysTrue()); |
| 707 |
throw new RuntimeException("Should have thrown!"); |
| 708 |
} catch (AssertionError ex) { |
| 709 |
// expected |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
@Test |
| 714 |
public void errorInPredicate() { |
| 715 |
TestObserver<Object> to = new TestObserver<>(); |
| 716 |
to.onError(new RuntimeException()); |
| 717 |
try { |
| 718 |
to.assertError(new Predicate<Throwable>() { |
| 719 |
@Override |
| 720 |
public boolean test(Throwable throwable) throws Exception { |
| 721 |
throw new TestException(); |
| 722 |
} |
| 723 |
}); |
| 724 |
} catch (TestException ex) { |
| 725 |
// expected |
| 726 |
return; |
| 727 |
} |
| 728 |
fail("Error in predicate but not thrown!"); |
| 729 |
} |
| 730 |
|
| 731 |
@Test |
| 732 |
public void assertComplete() { |
| 733 |
TestObserver<Integer> to = new TestObserver<>(); |
| 734 |
|
| 735 |
to.onSubscribe(Disposable.empty()); |
| 736 |
|
| 737 |
try { |
| 738 |
to.assertComplete(); |
| 739 |
throw new RuntimeException("Should have thrown!"); |
| 740 |
} catch (AssertionError ex) { |
| 741 |
// expected |
| 742 |
} |
| 743 |
|
| 744 |
to.onComplete(); |
| 745 |
|
| 746 |
to.assertComplete(); |
| 747 |
|
| 748 |
to.onComplete(); |
| 749 |
|
| 750 |
try { |
| 751 |
to.assertComplete(); |
| 752 |
throw new RuntimeException("Should have thrown!"); |
| 753 |
} catch (AssertionError ex) { |
| 754 |
// expected |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
@Test |
| 759 |
public void completeWithoutOnSubscribe() { |
| 760 |
TestObserver<Integer> to = new TestObserver<>(); |
| 761 |
|
| 762 |
to.onComplete(); |
| 763 |
|
| 764 |
to.assertError(IllegalStateException.class); |
| 765 |
} |
| 766 |
|
| 767 |
@Test |
| 768 |
public void completeDelegateThrows() { |
| 769 |
TestObserver<Integer> to = new TestObserver<>(new Observer<Integer>() { |
| 770 |
|
| 771 |
@Override |
| 772 |
public void onSubscribe(Disposable d) { |
| 773 |
|
| 774 |
} |
| 775 |
|
| 776 |
@Override |
| 777 |
public void onNext(Integer value) { |
| 778 |
|
| 779 |
} |
| 780 |
|
| 781 |
@Override |
| 782 |
public void onError(Throwable e) { |
| 783 |
throw new TestException(); |
| 784 |
} |
| 785 |
|
| 786 |
@Override |
| 787 |
public void onComplete() { |
| 788 |
throw new TestException(); |
| 789 |
} |
| 790 |
|
| 791 |
}); |
| 792 |
|
| 793 |
to.onSubscribe(Disposable.empty()); |
| 794 |
|
| 795 |
try { |
| 796 |
to.onComplete(); |
| 797 |
throw new RuntimeException("Should have thrown!"); |
| 798 |
} catch (TestException ex) { |
| 799 |
to.assertComplete().assertNoErrors(); |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
@Test |
| 804 |
public void errorDelegateThrows() { |
| 805 |
TestObserver<Integer> to = new TestObserver<>(new Observer<Integer>() { |
| 806 |
|
| 807 |
@Override |
| 808 |
public void onSubscribe(Disposable d) { |
| 809 |
|
| 810 |
} |
| 811 |
|
| 812 |
@Override |
| 813 |
public void onNext(Integer value) { |
| 814 |
|
| 815 |
} |
| 816 |
|
| 817 |
@Override |
| 818 |
public void onError(Throwable e) { |
| 819 |
throw new TestException(); |
| 820 |
} |
| 821 |
|
| 822 |
@Override |
| 823 |
public void onComplete() { |
| 824 |
throw new TestException(); |
| 825 |
} |
| 826 |
|
| 827 |
}); |
| 828 |
|
| 829 |
to.onSubscribe(Disposable.empty()); |
| 830 |
|
| 831 |
try { |
| 832 |
to.onError(new IOException()); |
| 833 |
throw new RuntimeException("Should have thrown!"); |
| 834 |
} catch (TestException ex) { |
| 835 |
to.assertNotComplete().assertError(Throwable.class); |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
@Test |
| 840 |
public void completedMeansDisposed() { |
| 841 |
// 2.0.2 - a terminated TestObserver no longer reports isDisposed |
| 842 |
assertFalse(Observable.just(1) |
| 843 |
.test() |
| 844 |
.assertResult(1).isDisposed()); |
| 845 |
} |
| 846 |
|
| 847 |
@Test |
| 848 |
public void errorMeansDisposed() { |
| 849 |
// 2.0.2 - a terminated TestObserver no longer reports isDisposed |
| 850 |
assertFalse(Observable.error(new TestException()) |
| 851 |
.test() |
| 852 |
.assertFailure(TestException.class).isDisposed()); |
| 853 |
} |
| 854 |
|
| 855 |
@Test |
| 856 |
public void assertValuePredicateEmpty() { |
| 857 |
assertThrowsWithMessage("No values (latch = 0, values = 0, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 858 |
TestObserver<Object> to = new TestObserver<>(); |
| 859 |
|
| 860 |
Observable.empty().subscribe(to); |
| 861 |
|
| 862 |
to.assertValue(new Predicate<Object>() { |
| 863 |
@Override public boolean test(final Object o) throws Exception { |
| 864 |
return false; |
| 865 |
} |
| 866 |
}); |
| 867 |
}); |
| 868 |
} |
| 869 |
|
| 870 |
@Test |
| 871 |
public void assertValuePredicateMatch() { |
| 872 |
TestObserver<Integer> to = new TestObserver<>(); |
| 873 |
|
| 874 |
Observable.just(1).subscribe(to); |
| 875 |
|
| 876 |
to.assertValue(new Predicate<Integer>() { |
| 877 |
@Override public boolean test(final Integer o) throws Exception { |
| 878 |
return o == 1; |
| 879 |
} |
| 880 |
}); |
| 881 |
} |
| 882 |
|
| 883 |
@Test |
| 884 |
public void assertValuePredicateNoMatch() { |
| 885 |
assertThrowsWithMessage("Value 1 (class: Integer) at position 0 did not pass the predicate (latch = 0, values = 1, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 886 |
TestObserver<Integer> to = new TestObserver<>(); |
| 887 |
|
| 888 |
Observable.just(1).subscribe(to); |
| 889 |
|
| 890 |
to.assertValue(new Predicate<Integer>() { |
| 891 |
@Override public boolean test(final Integer o) throws Exception { |
| 892 |
return o != 1; |
| 893 |
} |
| 894 |
}); |
| 895 |
}); |
| 896 |
} |
| 897 |
|
| 898 |
@Test |
| 899 |
public void assertValuePredicateMatchButMore() { |
| 900 |
assertThrowsWithMessage("The first value passed the predicate but this consumer received more than one value (latch = 0, values = 2, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 901 |
TestObserver<Integer> to = new TestObserver<>(); |
| 902 |
|
| 903 |
Observable.just(1, 2).subscribe(to); |
| 904 |
|
| 905 |
to.assertValue(new Predicate<Integer>() { |
| 906 |
@Override public boolean test(final Integer o) throws Exception { |
| 907 |
return o == 1; |
| 908 |
} |
| 909 |
}); |
| 910 |
}); |
| 911 |
} |
| 912 |
|
| 913 |
@Test |
| 914 |
public void assertValueAtPredicateEmpty() { |
| 915 |
assertThrowsWithMessage("No values (latch = 0, values = 0, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 916 |
TestObserver<Object> to = new TestObserver<>(); |
| 917 |
|
| 918 |
Observable.empty().subscribe(to); |
| 919 |
|
| 920 |
to.assertValueAt(0, new Predicate<Object>() { |
| 921 |
@Override public boolean test(final Object o) throws Exception { |
| 922 |
return false; |
| 923 |
} |
| 924 |
}); |
| 925 |
}); |
| 926 |
} |
| 927 |
|
| 928 |
@Test |
| 929 |
public void assertValueAtPredicateMatch() { |
| 930 |
TestObserver<Integer> to = new TestObserver<>(); |
| 931 |
|
| 932 |
Observable.just(1, 2).subscribe(to); |
| 933 |
|
| 934 |
to.assertValueAt(1, new Predicate<Integer>() { |
| 935 |
@Override public boolean test(final Integer o) throws Exception { |
| 936 |
return o == 2; |
| 937 |
} |
| 938 |
}); |
| 939 |
} |
| 940 |
|
| 941 |
@Test |
| 942 |
public void assertValueAtPredicateNoMatch() { |
| 943 |
assertThrowsWithMessage("Value 3 (class: Integer) at position 2 did not pass the predicate (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 944 |
TestObserver<Integer> to = new TestObserver<>(); |
| 945 |
|
| 946 |
Observable.just(1, 2, 3).subscribe(to); |
| 947 |
|
| 948 |
to.assertValueAt(2, new Predicate<Integer>() { |
| 949 |
@Override public boolean test(final Integer o) throws Exception { |
| 950 |
return o != 3; |
| 951 |
} |
| 952 |
}); |
| 953 |
}); |
| 954 |
} |
| 955 |
|
| 956 |
@Test |
| 957 |
public void assertValueAtInvalidIndex() { |
| 958 |
assertThrowsWithMessage("Index 2 is out of range [0, 2) (latch = 0, values = 2, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 959 |
TestObserver<Integer> to = new TestObserver<>(); |
| 960 |
|
| 961 |
Observable.just(1, 2).subscribe(to); |
| 962 |
|
| 963 |
to.assertValueAt(2, new Predicate<Integer>() { |
| 964 |
@Override public boolean test(final Integer o) throws Exception { |
| 965 |
return o == 1; |
| 966 |
} |
| 967 |
}); |
| 968 |
}); |
| 969 |
} |
| 970 |
|
| 971 |
@Test |
| 972 |
public void assertValueAtInvalidIndexNegative() { |
| 973 |
assertThrowsWithMessage("Index -2 is out of range [0, 2) (latch = 0, values = 2, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 974 |
TestObserver<Integer> to = new TestObserver<>(); |
| 975 |
|
| 976 |
Observable.just(1, 2).subscribe(to); |
| 977 |
|
| 978 |
to.assertValueAt(-2, new Predicate<Integer>() { |
| 979 |
@Override public boolean test(final Integer o) throws Exception { |
| 980 |
return o == 1; |
| 981 |
} |
| 982 |
}); |
| 983 |
}); |
| 984 |
} |
| 985 |
|
| 986 |
@Test |
| 987 |
public void assertValueAtIndexEmpty() { |
| 988 |
assertThrowsWithMessage("No values (latch = 0, values = 0, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 989 |
TestObserver<Object> to = new TestObserver<>(); |
| 990 |
|
| 991 |
Observable.empty().subscribe(to); |
| 992 |
|
| 993 |
to.assertValueAt(0, "a"); |
| 994 |
}); |
| 995 |
} |
| 996 |
|
| 997 |
@Test |
| 998 |
public void assertValueAtIndexMatch() { |
| 999 |
TestObserver<String> to = new TestObserver<>(); |
| 1000 |
|
| 1001 |
Observable.just("a", "b").subscribe(to); |
| 1002 |
|
| 1003 |
to.assertValueAt(1, "b"); |
| 1004 |
} |
| 1005 |
|
| 1006 |
@Test |
| 1007 |
public void assertValueAtIndexNoMatch() { |
| 1008 |
assertThrowsWithMessage("\nexpected: b (class: String)\ngot: c (class: String); Value at position 2 differ (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 1009 |
TestObserver<String> to = new TestObserver<>(); |
| 1010 |
|
| 1011 |
Observable.just("a", "b", "c").subscribe(to); |
| 1012 |
|
| 1013 |
to.assertValueAt(2, "b"); |
| 1014 |
}); |
| 1015 |
} |
| 1016 |
|
| 1017 |
@Test |
| 1018 |
public void assertValueAtIndexThrowsMessageMatchRegex() { |
| 1019 |
assertThrowsWithMessageMatchRegex(ASSERT_MESSAGE_REGEX, AssertionError.class, () -> { |
| 1020 |
TestObserver<String> to = new TestObserver<>(); |
| 1021 |
|
| 1022 |
Observable.just("a", "b", "c").subscribe(to); |
| 1023 |
|
| 1024 |
to.assertValueAt(2, "b"); |
| 1025 |
}); |
| 1026 |
} |
| 1027 |
|
| 1028 |
@Test |
| 1029 |
public void assertValuesCountNoMatch() { |
| 1030 |
assertThrowsWithMessage("\nexpected: 2 [a, b]\ngot: 3 [a, b, c]; Value count differs (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 1031 |
TestObserver<String> to = new TestObserver<>(); |
| 1032 |
|
| 1033 |
Observable.just("a", "b", "c").subscribe(to); |
| 1034 |
|
| 1035 |
to.assertValues("a", "b"); |
| 1036 |
}); |
| 1037 |
} |
| 1038 |
|
| 1039 |
@Test |
| 1040 |
public void assertValuesCountThrowsMessageMatchRegex() { |
| 1041 |
assertThrowsWithMessageMatchRegex(ASSERT_MESSAGE_REGEX, AssertionError.class, () -> { |
| 1042 |
TestObserver<String> to = new TestObserver<>(); |
| 1043 |
|
| 1044 |
Observable.just("a", "b", "c").subscribe(to); |
| 1045 |
|
| 1046 |
to.assertValues("a", "b"); |
| 1047 |
}); |
| 1048 |
} |
| 1049 |
|
| 1050 |
@Test |
| 1051 |
public void assertValuesNoMatch() { |
| 1052 |
assertThrowsWithMessage("\nexpected: d (class: String)\ngot: c (class: String); Value at position 2 differ (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 1053 |
TestObserver<String> to = new TestObserver<>(); |
| 1054 |
|
| 1055 |
Observable.just("a", "b", "c").subscribe(to); |
| 1056 |
|
| 1057 |
to.assertValues("a", "b", "d"); |
| 1058 |
}); |
| 1059 |
} |
| 1060 |
|
| 1061 |
@Test |
| 1062 |
public void assertValuesThrowsMessageMatchRegex() { |
| 1063 |
assertThrowsWithMessageMatchRegex(ASSERT_MESSAGE_REGEX, AssertionError.class, () -> { |
| 1064 |
TestObserver<String> to = new TestObserver<>(); |
| 1065 |
|
| 1066 |
Observable.just("a", "b", "c").subscribe(to); |
| 1067 |
|
| 1068 |
to.assertValues("a", "b", "d"); |
| 1069 |
}); |
| 1070 |
} |
| 1071 |
|
| 1072 |
@Test |
| 1073 |
public void assertValueCountNoMatch() { |
| 1074 |
assertThrowsWithMessage("\nexpected: 2\ngot: 3; Value counts differ (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 1075 |
TestObserver<String> to = new TestObserver<>(); |
| 1076 |
|
| 1077 |
Observable.just("a", "b", "c").subscribe(to); |
| 1078 |
|
| 1079 |
to.assertValueCount(2); |
| 1080 |
}); |
| 1081 |
} |
| 1082 |
|
| 1083 |
@Test |
| 1084 |
public void assertValueCountThrowsMessageMatchRegex() { |
| 1085 |
assertThrowsWithMessageMatchRegex(ASSERT_MESSAGE_REGEX, AssertionError.class, () -> { |
| 1086 |
TestObserver<String> to = new TestObserver<>(); |
| 1087 |
|
| 1088 |
Observable.just("a", "b", "c").subscribe(to); |
| 1089 |
|
| 1090 |
to.assertValueCount(2); |
| 1091 |
}); |
| 1092 |
} |
| 1093 |
|
| 1094 |
@Test |
| 1095 |
public void assertValueSequenceNoMatch() { |
| 1096 |
assertThrowsWithMessage("\nexpected: d (class: String)\ngot: c (class: String); Value at position 2 differ (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 1097 |
TestObserver<String> to = new TestObserver<>(); |
| 1098 |
|
| 1099 |
Observable.just("a", "b", "c").subscribe(to); |
| 1100 |
|
| 1101 |
to.assertValueSequence(Arrays.asList("a", "b", "d")); |
| 1102 |
}); |
| 1103 |
} |
| 1104 |
|
| 1105 |
@Test |
| 1106 |
public void assertValueSequenceThrowsMessageMatchRegex() { |
| 1107 |
assertThrowsWithMessageMatchRegex(ASSERT_MESSAGE_REGEX, AssertionError.class, () -> { |
| 1108 |
TestObserver<String> to = new TestObserver<>(); |
| 1109 |
|
| 1110 |
Observable.just("a", "b", "c").subscribe(to); |
| 1111 |
|
| 1112 |
to.assertValueSequence(Arrays.asList("a", "b", "d")); |
| 1113 |
}); |
| 1114 |
} |
| 1115 |
|
| 1116 |
@Test |
| 1117 |
public void assertValueAtIndexInvalidIndex() { |
| 1118 |
assertThrowsWithMessage("Index 2 is out of range [0, 2) (latch = 0, values = 2, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 1119 |
TestObserver<String> to = new TestObserver<>(); |
| 1120 |
|
| 1121 |
Observable.just("a", "b").subscribe(to); |
| 1122 |
|
| 1123 |
to.assertValueAt(2, "c"); |
| 1124 |
}); |
| 1125 |
} |
| 1126 |
|
| 1127 |
@Test |
| 1128 |
public void assertValueAtIndexInvalidIndexNegative() { |
| 1129 |
assertThrowsWithMessage("Index -2 is out of range [0, 2) (latch = 0, values = 2, errors = 0, completions = 1)", AssertionError.class, () -> { |
| 1130 |
TestObserver<String> to = new TestObserver<>(); |
| 1131 |
|
| 1132 |
Observable.just("a", "b").subscribe(to); |
| 1133 |
|
| 1134 |
to.assertValueAt(-2, "c"); |
| 1135 |
}); |
| 1136 |
} |
| 1137 |
|
| 1138 |
@Test |
| 1139 |
public void withTag() { |
| 1140 |
try { |
| 1141 |
for (int i = 1; i < 3; i++) { |
| 1142 |
Observable.just(i) |
| 1143 |
.test() |
| 1144 |
.withTag("testing with item=" + i) |
| 1145 |
.assertResult(1) |
| 1146 |
; |
| 1147 |
} |
| 1148 |
throw new RuntimeException("Should have thrown!"); |
| 1149 |
} catch (AssertionError ex) { |
| 1150 |
assertTrue(ex.toString(), ex.toString().contains("testing with item=2")); |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
@Test |
| 1155 |
public void assertValuesOnly() { |
| 1156 |
TestObserver<Integer> to = TestObserver.create(); |
| 1157 |
to.onSubscribe(Disposable.empty()); |
| 1158 |
to.assertValuesOnly(); |
| 1159 |
|
| 1160 |
to.onNext(5); |
| 1161 |
to.assertValuesOnly(5); |
| 1162 |
|
| 1163 |
to.onNext(-1); |
| 1164 |
to.assertValuesOnly(5, -1); |
| 1165 |
} |
| 1166 |
|
| 1167 |
@Test |
| 1168 |
public void assertValuesOnlyThrowsOnUnexpectedValue() { |
| 1169 |
TestObserver<Integer> to = TestObserver.create(); |
| 1170 |
to.onSubscribe(Disposable.empty()); |
| 1171 |
to.assertValuesOnly(); |
| 1172 |
|
| 1173 |
to.onNext(5); |
| 1174 |
to.assertValuesOnly(5); |
| 1175 |
|
| 1176 |
to.onNext(-1); |
| 1177 |
|
| 1178 |
try { |
| 1179 |
to.assertValuesOnly(5); |
| 1180 |
throw new RuntimeException(); |
| 1181 |
} catch (AssertionError ex) { |
| 1182 |
// expected |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
@Test |
| 1187 |
public void assertValuesOnlyThrowsWhenCompleted() { |
| 1188 |
TestObserver<Integer> to = TestObserver.create(); |
| 1189 |
to.onSubscribe(Disposable.empty()); |
| 1190 |
|
| 1191 |
to.onComplete(); |
| 1192 |
|
| 1193 |
try { |
| 1194 |
to.assertValuesOnly(); |
| 1195 |
throw new RuntimeException(); |
| 1196 |
} catch (AssertionError ex) { |
| 1197 |
// expected |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
@Test |
| 1202 |
public void assertValuesOnlyThrowsWhenErrored() { |
| 1203 |
TestObserver<Integer> to = TestObserver.create(); |
| 1204 |
to.onSubscribe(Disposable.empty()); |
| 1205 |
|
| 1206 |
to.onError(new TestException()); |
| 1207 |
|
| 1208 |
try { |
| 1209 |
to.assertValuesOnly(); |
| 1210 |
throw new RuntimeException(); |
| 1211 |
} catch (AssertionError ex) { |
| 1212 |
// expected |
| 1213 |
} |
| 1214 |
} |
| 1215 |
|
| 1216 |
@Test |
| 1217 |
public void onErrorIsNull() { |
| 1218 |
TestObserver<Integer> to = TestObserver.create(); |
| 1219 |
to.onSubscribe(Disposable.empty()); |
| 1220 |
|
| 1221 |
to.onError(null); |
| 1222 |
|
| 1223 |
to.assertFailure(NullPointerException.class); |
| 1224 |
} |
| 1225 |
|
| 1226 |
@Test |
| 1227 |
public void awaitCountTimeout() { |
| 1228 |
TestObserver<Integer> to = TestObserver.create(); |
| 1229 |
to.onSubscribe(Disposable.empty()); |
| 1230 |
to.awaitCount(1); |
| 1231 |
assertTrue(to.timeout); |
| 1232 |
} |
| 1233 |
|
| 1234 |
@Test(expected = RuntimeException.class) |
| 1235 |
public void awaitCountInterrupted() { |
| 1236 |
try { |
| 1237 |
TestObserver<Integer> to = TestObserver.create(); |
| 1238 |
to.onSubscribe(Disposable.empty()); |
| 1239 |
Thread.currentThread().interrupt(); |
| 1240 |
to.awaitCount(1); |
| 1241 |
} finally { |
| 1242 |
Thread.interrupted(); |
| 1243 |
} |
| 1244 |
} |
| 1245 |
} |