예제 #1
0
  @Test
  public void testZipAsync() {
    int NUM = (int) (RxRingBuffer.SIZE * 2.1);
    AtomicInteger c1 = new AtomicInteger();
    AtomicInteger c2 = new AtomicInteger();
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    Observable<Integer> zipped =
        Observable.zip(
            incrementingIntegers(c1).subscribeOn(Schedulers.computation()),
            incrementingIntegers(c2).subscribeOn(Schedulers.computation()),
            new Func2<Integer, Integer, Integer>() {

              @Override
              public Integer call(Integer t1, Integer t2) {
                return t1 + t2;
              }
            });

    zipped.take(NUM).subscribe(ts);
    ts.awaitTerminalEvent();
    ts.assertNoErrors();
    System.out.println(
        "testZipAsync => Received: "
            + ts.getOnNextEvents().size()
            + "  Emitted: "
            + c1.get()
            + " / "
            + c2.get());
    assertEquals(NUM, ts.getOnNextEvents().size());
    assertTrue(c1.get() < RxRingBuffer.SIZE * 3);
    assertTrue(c2.get() < RxRingBuffer.SIZE * 3);
  }
예제 #2
0
  @Test
  public void testMergeAsyncThenObserveOn() {
    int NUM = (int) (RxRingBuffer.SIZE * 4.1);
    AtomicInteger c1 = new AtomicInteger();
    AtomicInteger c2 = new AtomicInteger();
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    Observable<Integer> merged =
        Observable.merge(
            incrementingIntegers(c1).subscribeOn(Schedulers.computation()),
            incrementingIntegers(c2).subscribeOn(Schedulers.computation()));

    merged.observeOn(Schedulers.newThread()).take(NUM).subscribe(ts);
    ts.awaitTerminalEvent();
    ts.assertNoErrors();
    System.out.println(
        "testMergeAsyncThenObserveOn => Received: "
            + ts.getOnNextEvents().size()
            + "  Emitted: "
            + c1.get()
            + " / "
            + c2.get());
    assertEquals(NUM, ts.getOnNextEvents().size());
    // either one can starve the other, but neither should be capable of doing more than 5 batches
    // (taking 4.1)
    // TODO is it possible to make this deterministic rather than one possibly starving the other?
    // benjchristensen => In general I'd say it's not worth trying to make it so, as "fair"
    // algoritms generally take a performance hit
    // akarnokd => run this in a loop over 10k times and never saw values get as high as 7*SIZE, but
    // since observeOn delays the unsubscription non-deterministically, the test will remain
    // unreliable
    assertTrue(c1.get() < RxRingBuffer.SIZE * 7);
    assertTrue(c2.get() < RxRingBuffer.SIZE * 7);
  }
예제 #3
0
  @Test
  public void testMergeSync() {
    int NUM = (int) (RxRingBuffer.SIZE * 4.1);
    AtomicInteger c1 = new AtomicInteger();
    AtomicInteger c2 = new AtomicInteger();
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    Observable<Integer> merged =
        Observable.merge(incrementingIntegers(c1), incrementingIntegers(c2));

    merged.take(NUM).subscribe(ts);
    ts.awaitTerminalEvent();
    ts.assertNoErrors();
    System.out.println("Expected: " + NUM + " got: " + ts.getOnNextEvents().size());
    System.out.println(
        "testMergeSync => Received: "
            + ts.getOnNextEvents().size()
            + "  Emitted: "
            + c1.get()
            + " / "
            + c2.get());
    assertEquals(NUM, ts.getOnNextEvents().size());
    // either one can starve the other, but neither should be capable of doing more than 5 batches
    // (taking 4.1)
    // TODO is it possible to make this deterministic rather than one possibly starving the other?
    // benjchristensen => In general I'd say it's not worth trying to make it so, as "fair"
    // algoritms generally take a performance hit
    assertTrue(c1.get() < RxRingBuffer.SIZE * 5);
    assertTrue(c2.get() < RxRingBuffer.SIZE * 5);
  }
예제 #4
0
 @Test
 public void testObserveOnWithSlowConsumer() {
   int NUM = (int) (Observable.bufferSize() * 0.2);
   AtomicInteger c = new AtomicInteger();
   TestSubscriber<Integer> ts = new TestSubscriber<>();
   incrementingIntegers(c)
       .observeOn(Schedulers.computation())
       .map(
           i -> {
             try {
               Thread.sleep(1);
             } catch (InterruptedException e) {
               e.printStackTrace();
             }
             return i;
           })
       .take(NUM)
       .subscribe(ts);
   ts.awaitTerminalEvent();
   ts.assertNoErrors();
   System.out.println(
       "testObserveOnWithSlowConsumer => Received: " + ts.valueCount() + "  Emitted: " + c.get());
   assertEquals(NUM, ts.valueCount());
   assertTrue(c.get() < Observable.bufferSize() * 2);
 }
예제 #5
0
  @Test
  public void testTakeFilterSkipChainAsync() {
    int NUM = (int) (Observable.bufferSize() * 2.1);
    AtomicInteger c = new AtomicInteger();
    TestSubscriber<Integer> ts = new TestSubscriber<>();
    incrementingIntegers(c)
        .observeOn(Schedulers.computation())
        .skip(10000)
        .filter(i -> i > 11000)
        .take(NUM)
        .subscribe(ts);

    ts.awaitTerminalEvent();
    ts.assertNoErrors();

    // emit 10000 that are skipped
    // emit next 1000 that are filtered out
    // take NUM
    // so emitted is at least 10000+1000+NUM + extra for buffer size/threshold
    int expected = 10000 + 1000 + Observable.bufferSize() * 3 + Observable.bufferSize() / 2;

    System.out.println(
        "testTakeFilterSkipChain => Received: "
            + ts.valueCount()
            + "  Emitted: "
            + c.get()
            + " Expected: "
            + expected);
    assertEquals(NUM, ts.valueCount());
    assertTrue(c.get() < expected);
  }
예제 #6
0
  @Test
  public void testZipAsync() {
    int NUM = (int) (Observable.bufferSize() * 2.1);
    AtomicInteger c1 = new AtomicInteger();
    AtomicInteger c2 = new AtomicInteger();
    TestSubscriber<Integer> ts = new TestSubscriber<>();
    Observable<Integer> zipped =
        Observable.zip(
            incrementingIntegers(c1).subscribeOn(Schedulers.computation()),
            incrementingIntegers(c2).subscribeOn(Schedulers.computation()),
            (t1, t2) -> t1 + t2);

    zipped.take(NUM).subscribe(ts);
    ts.awaitTerminalEvent();
    ts.assertNoErrors();
    System.out.println(
        "testZipAsync => Received: "
            + ts.valueCount()
            + "  Emitted: "
            + c1.get()
            + " / "
            + c2.get());
    assertEquals(NUM, ts.valueCount());
    int max = Observable.bufferSize() * 5;
    assertTrue("" + c1.get() + " >= " + max, c1.get() < max);
    assertTrue("" + c2.get() + " >= " + max, c2.get() < max);
  }
예제 #7
0
  @Test
  @Ignore // the test is non-deterministic and can't be made deterministic
  public void testFlatMapAsync() {
    int NUM = (int) (Observable.bufferSize() * 2.1);
    AtomicInteger c = new AtomicInteger();
    TestSubscriber<Integer> ts = new TestSubscriber<>();

    incrementingIntegers(c)
        .subscribeOn(Schedulers.computation())
        .flatMap(
            i ->
                incrementingIntegers(new AtomicInteger())
                    .take(10)
                    .subscribeOn(Schedulers.computation()))
        .take(NUM)
        .subscribe(ts);

    ts.awaitTerminalEvent();
    ts.assertNoErrors();
    System.out.println(
        "testFlatMapAsync => Received: "
            + ts.valueCount()
            + "  Emitted: "
            + c.get()
            + " Size: "
            + Observable.bufferSize());
    assertEquals(NUM, ts.valueCount());
    // even though we only need 10, it will request at least Observable.bufferSize(), and then as it
    // drains keep requesting more
    // and then it will be non-deterministic when the take() causes the unsubscribe as it is
    // scheduled on 10 different schedulers (threads)
    // normally this number is ~250 but can get up to ~1200 when Observable.bufferSize() == 1024
    assertTrue(c.get() <= Observable.bufferSize() * 2);
  }
예제 #8
0
  @Test
  public void fromArityArgs1() {
    Observable<String> items = Observable.just("one");

    assertEquals((Long) 1L, items.count().toBlocking().single());
    assertEquals("one", items.takeLast(1).toBlocking().single());
  }
예제 #9
0
  @Test
  public void testMergeAsyncThenObserveOnLoop() {
    for (int i = 0; i < 500; i++) {
      if (i % 10 == 0) {
        System.out.println("testMergeAsyncThenObserveOnLoop >> " + i);
      }
      // Verify there is no MissingBackpressureException
      int NUM = (int) (RxRingBuffer.SIZE * 4.1);
      AtomicInteger c1 = new AtomicInteger();
      AtomicInteger c2 = new AtomicInteger();

      TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
      Observable<Integer> merged =
          Observable.merge(
              incrementingIntegers(c1).subscribeOn(Schedulers.computation()),
              incrementingIntegers(c2).subscribeOn(Schedulers.computation()));

      merged.observeOn(Schedulers.io()).take(NUM).subscribe(ts);
      ts.awaitTerminalEvent();
      ts.assertNoErrors();
      System.out.println(
          "testMergeAsyncThenObserveOn => Received: "
              + ts.getOnNextEvents().size()
              + "  Emitted: "
              + c1.get()
              + " / "
              + c2.get());
      assertEquals(NUM, ts.getOnNextEvents().size());
    }
  }
예제 #10
0
  @Test
  public void testMergeCovariance4() {

    Observable<Movie> o1 =
        Observable.create(
            new OnSubscribeFunc<Movie>() {

              @Override
              public Subscription onSubscribe(Observer<? super Movie> o) {
                o.onNext(new HorrorMovie());
                o.onNext(new Movie());
                //                o.onNext(new Media()); // correctly doesn't compile
                o.onCompleted();
                return Subscriptions.empty();
              }
            });

    Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

    List<Media> values = Observable.merge(o1, o2).toList().toBlockingObservable().single();

    assertTrue(values.get(0) instanceof HorrorMovie);
    assertTrue(values.get(1) instanceof Movie);
    assertTrue(values.get(2) instanceof Media);
    assertTrue(values.get(3) instanceof HorrorMovie);
  }
예제 #11
0
 @Test
 public void fromArray() {
   String[] items = new String[] {"one", "two", "three"};
   assertEquals((Long) 3L, Observable.fromArray(items).count().toBlocking().single());
   assertEquals("two", Observable.fromArray(items).skip(1).take(1).toBlocking().single());
   assertEquals("three", Observable.fromArray(items).takeLast(1).toBlocking().single());
 }
예제 #12
0
  @Test
  public void testMergeAsync() {
    int NUM = (int) (Observable.bufferSize() * 4.1);
    AtomicInteger c1 = new AtomicInteger();
    AtomicInteger c2 = new AtomicInteger();
    TestSubscriber<Integer> ts = new TestSubscriber<>();
    Observable<Integer> merged =
        Observable.merge(
            incrementingIntegers(c1).subscribeOn(Schedulers.computation()),
            incrementingIntegers(c2).subscribeOn(Schedulers.computation()));

    merged.take(NUM).subscribe(ts);
    ts.awaitTerminalEvent();
    ts.assertNoErrors();
    System.out.println(
        "testMergeAsync => Received: "
            + ts.valueCount()
            + "  Emitted: "
            + c1.get()
            + " / "
            + c2.get());
    assertEquals(NUM, ts.valueCount());
    // either one can starve the other, but neither should be capable of doing more than 5 batches
    // (taking 4.1)
    // TODO is it possible to make this deterministic rather than one possibly starving the other?
    // benjchristensen => In general I'd say it's not worth trying to make it so, as "fair"
    // algoritms generally take a performance hit
    int max = Observable.bufferSize() * 7;
    assertTrue("" + c1.get() + " >= " + max, c1.get() < max);
    assertTrue("" + c2.get() + " >= " + max, c2.get() < max);
  }
예제 #13
0
  /**
   * The error from the user provided Observable is handled by the subscribe try/catch because this
   * is synchronous
   *
   * <p>Result: Passes
   */
  @Test
  public void testCustomObservableWithErrorInObservableSynchronous() {
    final AtomicInteger count = new AtomicInteger();
    final AtomicReference<Throwable> error = new AtomicReference<>();
    // FIXME custom built???
    Observable.just("1", "2")
        .concatWith(Observable.error(() -> new NumberFormatException()))
        .subscribe(
            new Observer<String>() {

              @Override
              public void onComplete() {
                System.out.println("completed");
              }

              @Override
              public void onError(Throwable e) {
                error.set(e);
                System.out.println("error");
                e.printStackTrace();
              }

              @Override
              public void onNext(String v) {
                System.out.println(v);
                count.incrementAndGet();
              }
            });
    assertEquals(2, count.get());
    assertNotNull(error.get());
    if (!(error.get() instanceof NumberFormatException)) {
      fail("It should be a NumberFormatException");
    }
  }
예제 #14
0
  /**
   * A reduce on an empty Observable and a seed should just pass the seed through.
   *
   * <p>This is confirmed at https://github.com/ReactiveX/RxJava/issues/423#issuecomment-27642456
   */
  @Test
  public void testReduceWithEmptyObservableAndSeed() {
    Observable<Integer> observable = Observable.range(1, 0);
    int value = observable.reduce(1, (t1, t2) -> t1 + t2).toBlocking().last();

    assertEquals(1, value);
  }
예제 #15
0
 public void testTakeFirstWithPredicateOfSome() {
   Observable<Integer> observable = Observable.just(1, 3, 5, 4, 6, 3);
   observable.takeFirst(IS_EVEN).subscribe(w);
   verify(w, times(1)).onNext(anyInt());
   verify(w).onNext(4);
   verify(w, times(1)).onComplete();
   verify(w, never()).onError(any(Throwable.class));
 }
예제 #16
0
 @Test
 public void testFirstWithPredicateOfNoneMatchingThePredicate() {
   Observable<Integer> observable = Observable.just(1, 3, 5, 7, 9, 7, 5, 3, 1);
   observable.filter(IS_EVEN).first().subscribe(w);
   verify(w, never()).onNext(anyInt());
   verify(w, never()).onComplete();
   verify(w, times(1)).onError(isA(NoSuchElementException.class));
 }
예제 #17
0
 @Test
 public void testFirstOfNone() {
   Observable<Integer> observable = Observable.empty();
   observable.first().subscribe(w);
   verify(w, never()).onNext(anyInt());
   verify(w, never()).onComplete();
   verify(w, times(1)).onError(isA(NoSuchElementException.class));
 }
예제 #18
0
 @Test
 public void testTakeFirstOfNone() {
   Observable<Integer> observable = Observable.empty();
   observable.take(1).subscribe(w);
   verify(w, never()).onNext(anyInt());
   verify(w, times(1)).onComplete();
   verify(w, never()).onError(any(Throwable.class));
 }
예제 #19
0
 @Test
 public void testTakeFirstWithPredicateOfNoneMatchingThePredicate() {
   Observable<Integer> observable = Observable.just(1, 3, 5, 7, 9, 7, 5, 3, 1);
   observable.takeFirst(IS_EVEN).subscribe(w);
   verify(w, never()).onNext(anyInt());
   verify(w, times(1)).onComplete();
   verify(w, never()).onError(any(Throwable.class));
 }
예제 #20
0
 @Test
 public void testReduceWithInitialValue() {
   Observable<Integer> observable = Observable.just(1, 2, 3, 4);
   observable.reduce(50, (t1, t2) -> t1 + t2).subscribe(w);
   // we should be called only once
   verify(w, times(1)).onNext(anyInt());
   verify(w).onNext(60);
 }
예제 #21
0
  @Test
  public void testMergeCovariance2() {
    Observable<Media> o1 = Observable.from(new HorrorMovie(), new Movie(), new Media());
    Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

    Observable<Observable<Media>> os = Observable.from(o1, o2);

    List<Media> values = Observable.merge(os).toList().toBlockingObservable().single();
  }
예제 #22
0
  @Test
  public void testCountError() {
    Observable<String> o = Observable.error(() -> new RuntimeException());

    o.count().subscribe(w);
    verify(w, never()).onNext(anyInt());
    verify(w, never()).onComplete();
    verify(w, times(1)).onError(any(RuntimeException.class));
  }
예제 #23
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();
  }
예제 #24
0
 @Test
 public void testCountZeroItems() {
   Observable<String> observable = Observable.empty();
   observable.count().subscribe(w);
   // we should be called only once
   verify(w, times(1)).onNext(anyLong());
   verify(w).onNext(0L);
   verify(w, never()).onError(any(Throwable.class));
   verify(w, times(1)).onComplete();
 }
예제 #25
0
  @Test
  public void fromIterable() {
    ArrayList<String> items = new ArrayList<>();
    items.add("one");
    items.add("two");
    items.add("three");

    assertEquals((Long) 3L, Observable.fromIterable(items).count().toBlocking().single());
    assertEquals("two", Observable.fromIterable(items).skip(1).take(1).toBlocking().single());
    assertEquals("three", Observable.fromIterable(items).takeLast(1).toBlocking().single());
  }
예제 #26
0
  @Test
  public void testMergeCovariance3() {
    Observable<Movie> o1 = Observable.from(new HorrorMovie(), new Movie());
    Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

    List<Media> values = Observable.merge(o1, o2).toList().toBlockingObservable().single();

    assertTrue(values.get(0) instanceof HorrorMovie);
    assertTrue(values.get(1) instanceof Movie);
    assertTrue(values.get(2) instanceof Media);
    assertTrue(values.get(3) instanceof HorrorMovie);
  }
예제 #27
0
 @Test
 public void testObserveOn() {
   int NUM = (int) (Observable.bufferSize() * 2.1);
   AtomicInteger c = new AtomicInteger();
   TestSubscriber<Integer> ts = new TestSubscriber<>();
   incrementingIntegers(c).observeOn(Schedulers.computation()).take(NUM).subscribe(ts);
   ts.awaitTerminalEvent();
   ts.assertNoErrors();
   System.out.println("testObserveOn => Received: " + ts.valueCount() + "  Emitted: " + c.get());
   assertEquals(NUM, ts.valueCount());
   assertTrue(c.get() < Observable.bufferSize() * 4);
 }
예제 #28
0
  @Test
  public void testCountAFewItems() {
    Observable<String> observable = Observable.just("a", "b", "c", "d");

    observable.count().subscribe(w);

    // we should be called only once
    verify(w, times(1)).onNext(anyLong());
    verify(w).onNext(4L);
    verify(w, never()).onError(any(Throwable.class));
    verify(w, times(1)).onComplete();
  }
예제 #29
0
  @Test
  public void testIgnoreElements() {
    Observable<Integer> observable = Observable.just(1, 2, 3).ignoreElements();

    Subscriber<Object> observer = TestHelper.mockSubscriber();

    observable.subscribe(observer);

    verify(observer, never()).onNext(any(Integer.class));
    verify(observer, never()).onError(any(Throwable.class));
    verify(observer, times(1)).onComplete();
  }
예제 #30
0
  @Test
  public void testContainsWithEmptyObservable() {
    Observable<Boolean> observable = Observable.<String>empty().contains("a");

    Subscriber<Object> observer = TestHelper.mockSubscriber();

    observable.subscribe(observer);

    verify(observer, times(1)).onNext(false);
    verify(observer, never()).onNext(true);
    verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class));
    verify(observer, times(1)).onComplete();
  }