Ejemplo 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();
    }
  }
Ejemplo 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();
 }
Ejemplo 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();
    }
  }
Ejemplo 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);
 }
Ejemplo n.º 5
0
  @Test
  public void testDelegate1() {
    TestObserver<Integer> to = new TestObserver<Integer>();
    TestSubscriber<Integer> ts = TestSubscriber.create(to);
    ts.onCompleted();

    to.assertTerminalEvent();
  }
Ejemplo 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();
  }
Ejemplo 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!");
 }
Ejemplo 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!");
 }
Ejemplo 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!");
 }
Ejemplo 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
    }
  }
Ejemplo 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!");
 }
Ejemplo 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();
  }
Ejemplo 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
    }
  }
Ejemplo 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();
  }
Ejemplo 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!");
 }
Ejemplo 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();
  }
Ejemplo 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!");
 }
Ejemplo 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());
      }
    }
  }
Ejemplo 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();
  }
Ejemplo 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());
 }
Ejemplo n.º 21
0
 @Test
 public void testDelegate3() {
   TestSubscriber<Integer> ts1 = TestSubscriber.create();
   TestSubscriber<Integer> ts2 = TestSubscriber.create(ts1, 0);
   ts2.onCompleted();
   ts1.assertCompleted();
 }
Ejemplo n.º 22
0
 @Test(expected = NullPointerException.class)
 public void testNullDelegate3() {
   TestSubscriber<Integer> ts = new TestSubscriber<Integer>((Subscriber<Integer>) null, 0);
   ts.onCompleted();
 }