Exemple #1
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);
  }
  @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);
  }
  @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);
  }
  @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());
    }
  }
  @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);
  }
Exemple #6
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();
  }
Exemple #7
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);
  }
Exemple #8
0
 /** This won't compile if super/extends isn't done correctly on generics */
 @Test
 public void testCovarianceOfMerge() {
   Observable<HorrorMovie> horrors = Observable.from(new HorrorMovie());
   Observable<Observable<HorrorMovie>> metaHorrors = Observable.just(horrors);
   Observable.<Media>merge(metaHorrors);
 }