Esempio n. 1
0
  @Test
  public void testInterruptTerminalEventAwaitTimed() {
    TestSubscriber<Integer> ts = TestSubscriber.create();

    final Thread t0 = Thread.currentThread();
    Worker w = Schedulers.computation().createWorker();
    try {
      w.schedule(
          new Action0() {
            @Override
            public void call() {
              t0.interrupt();
            }
          },
          200,
          TimeUnit.MILLISECONDS);

      try {
        ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
        fail("Did not interrupt wait!");
      } catch (RuntimeException ex) {
        if (!(ex.getCause() instanceof InterruptedException)) {
          fail("The cause is not InterruptedException! " + ex.getCause());
        }
      }
    } finally {
      w.unsubscribe();
    }
  }
Esempio n. 2
0
 @Test
 public void testAwaitTerminalEventWithDuration() {
   TestSubscriber<Object> ts = new TestSubscriber<Object>();
   Observable.just(1).subscribe(ts);
   ts.awaitTerminalEvent(1, TimeUnit.SECONDS);
   ts.assertTerminalEvent();
 }
Esempio n. 3
0
  @Test
  public void testInterruptTerminalEventAwaitAndUnsubscribe() {
    TestSubscriber<Integer> ts = TestSubscriber.create();

    final Thread t0 = Thread.currentThread();
    Worker w = Schedulers.computation().createWorker();
    try {
      w.schedule(
          new Action0() {
            @Override
            public void call() {
              t0.interrupt();
            }
          },
          200,
          TimeUnit.MILLISECONDS);

      ts.awaitTerminalEventAndUnsubscribeOnTimeout(5, TimeUnit.SECONDS);
      if (!ts.isUnsubscribed()) {
        fail("Did not unsubscribe!");
      }
    } finally {
      w.unsubscribe();
    }
  }
Esempio n. 4
0
 @Test
 public void testAssertError() {
   RuntimeException e = new RuntimeException("Oops");
   TestSubscriber<Object> subscriber = new TestSubscriber<Object>();
   Observable.error(e).subscribe(subscriber);
   subscriber.assertError(e);
 }
Esempio n. 5
0
  @Test
  public void testDelegate1() {
    TestObserver<Integer> to = new TestObserver<Integer>();
    TestSubscriber<Integer> ts = TestSubscriber.create(to);
    ts.onCompleted();

    to.assertTerminalEvent();
  }
Esempio n. 6
0
  @Test
  public void testAssert() {
    Observable<Integer> oi = Observable.from(Arrays.asList(1, 2));
    TestSubscriber<Integer> o = new TestSubscriber<Integer>();
    oi.subscribe(o);

    o.assertReceivedOnNext(Arrays.asList(1, 2));
    assertEquals(2, o.getOnNextEvents().size());
    o.assertTerminalEvent();
  }
Esempio n. 7
0
 @Test
 public void testNotCompleted() {
   TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
   try {
     ts.assertCompleted();
   } catch (AssertionError ex) {
     // expected
     return;
   }
   fail("Not completed and no assertion error!");
 }
Esempio n. 8
0
 @Test
 public void testNoError2() {
   TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
   try {
     ts.assertError(new TestException());
   } catch (AssertionError ex) {
     // expected
     return;
   }
   fail("No present but no assertion error!");
 }
Esempio n. 9
0
 @Test
 public void testUnsubscribed() {
   TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
   try {
     ts.assertUnsubscribed();
   } catch (AssertionError ex) {
     // expected
     return;
   }
   fail("Not unsubscribed but not reported!");
 }
Esempio n. 10
0
  @Test
  public void testNoValues() {
    TestSubscriber<Integer> ts = TestSubscriber.create();
    ts.onNext(1);

    try {
      ts.assertNoValues();
      fail("Failed to report there were values!");
    } catch (AssertionError ex) {
      // expected
    }
  }
Esempio n. 11
0
 @Test
 public void testDifferentError3() {
   TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
   ts.onError(new RuntimeException());
   try {
     ts.assertError(TestException.class);
   } catch (AssertionError ex) {
     // expected
     return;
   }
   fail("Different Error present but no assertion error!");
 }
Esempio n. 12
0
  @Test
  public void testAssertNotMatchValue() {
    Observable<Integer> oi = Observable.from(Arrays.asList(1, 2));
    TestSubscriber<Integer> o = new TestSubscriber<Integer>();
    oi.subscribe(o);

    thrown.expect(AssertionError.class);
    thrown.expectMessage("Value at index: 1 expected to be [3] (Integer) but was: [2] (Integer)");

    o.assertReceivedOnNext(Arrays.asList(1, 3));
    assertEquals(2, o.getOnNextEvents().size());
    o.assertTerminalEvent();
  }
Esempio n. 13
0
  @Test
  public void testNoTerminalEventBut1Error() {
    TestSubscriber<Integer> ts = TestSubscriber.create();

    ts.onError(new TestException());

    try {
      ts.assertNoTerminalEvent();
      fail("Failed to report there were terminal event(s)!");
    } catch (AssertionError ex) {
      // expected
    }
  }
Esempio n. 14
0
  @Test
  public void testAssertNotMatchCount() {
    Observable<Integer> oi = Observable.from(Arrays.asList(1, 2));
    TestSubscriber<Integer> o = new TestSubscriber<Integer>();
    oi.subscribe(o);

    thrown.expect(AssertionError.class);
    thrown.expectMessage("Number of items does not match. Provided: 1  Actual: 2");

    o.assertReceivedOnNext(Arrays.asList(1));
    assertEquals(2, o.getOnNextEvents().size());
    o.assertTerminalEvent();
  }
Esempio n. 15
0
 @Test
 public void testMultipleCompletions2() {
   TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
   ts.onCompleted();
   ts.onCompleted();
   try {
     ts.assertNotCompleted();
   } catch (AssertionError ex) {
     // expected
     return;
   }
   fail("Multiple completions and no assertion error!");
 }
Esempio n. 16
0
  @Test
  public void testAssertTerminalEventNotReceived() {
    PublishSubject<Integer> p = PublishSubject.create();
    TestSubscriber<Integer> o = new TestSubscriber<Integer>();
    p.subscribe(o);

    p.onNext(1);
    p.onNext(2);

    thrown.expect(AssertionError.class);
    thrown.expectMessage("No terminal events received.");

    o.assertReceivedOnNext(Arrays.asList(1, 2));
    assertEquals(2, o.getOnNextEvents().size());
    o.assertTerminalEvent();
  }
Esempio n. 17
0
 @Test
 public void testMultipleErrors3() {
   TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
   ts.onError(new TestException());
   ts.onError(new TestException());
   try {
     ts.assertError(new TestException());
   } catch (AssertionError ex) {
     if (!(ex.getCause() instanceof CompositeException)) {
       fail("Multiple Error present but the reported error doesn't have a composite cause!");
     }
     // expected
     return;
   }
   fail("Multiple Error present but no assertion error!");
 }
Esempio n. 18
0
  @Test
  public void testNoTerminalEventBut2Errors() {
    TestSubscriber<Integer> ts = TestSubscriber.create();

    ts.onError(new TestException());
    ts.onError(new TestException());

    try {
      ts.assertNoTerminalEvent();
      fail("Failed to report there were terminal event(s)!");
    } catch (AssertionError ex) {
      // expected
      if (!(ex.getCause() instanceof CompositeException)) {
        fail("Did not report a composite exception cause: " + ex.getCause());
      }
    }
  }
Esempio n. 19
0
  @Test(timeout = 1000)
  public void testOnErrorCrashCountsDownLatch() {
    TestObserver<Integer> to =
        new TestObserver<Integer>() {
          @Override
          public void onError(Throwable e) {
            throw new TestException();
          }
        };
    TestSubscriber<Integer> ts = TestSubscriber.create(to);

    try {
      ts.onError(new RuntimeException());
    } catch (TestException ex) {
      // expected
    }

    ts.awaitTerminalEvent();
  }
Esempio n. 20
0
 @Test
 public void testAwaitTerminalEventWithDurationAndUnsubscribeOnTimeout() {
   TestSubscriber<Object> ts = new TestSubscriber<Object>();
   final AtomicBoolean unsub = new AtomicBoolean(false);
   Observable.just(1)
       //
       .doOnUnsubscribe(
           new Action0() {
             @Override
             public void call() {
               unsub.set(true);
             }
           })
       //
       .delay(1000, TimeUnit.MILLISECONDS)
       .subscribe(ts);
   ts.awaitTerminalEventAndUnsubscribeOnTimeout(100, TimeUnit.MILLISECONDS);
   assertTrue(unsub.get());
 }
Esempio n. 21
0
 @Test
 public void testDelegate3() {
   TestSubscriber<Integer> ts1 = TestSubscriber.create();
   TestSubscriber<Integer> ts2 = TestSubscriber.create(ts1, 0);
   ts2.onCompleted();
   ts1.assertCompleted();
 }
Esempio n. 22
0
 @Test(expected = NullPointerException.class)
 public void testNullDelegate3() {
   TestSubscriber<Integer> ts = new TestSubscriber<Integer>((Subscriber<Integer>) null, 0);
   ts.onCompleted();
 }