Example #1
0
  @Test
  public void testZip() {
    Observable<String> os =
        OBSERVABLE_OF_5_INTEGERS.zip(
            OBSERVABLE_OF_5_INTEGERS,
            new Func2<Integer, Integer, String>() {

              @Override
              public String call(Integer a, Integer b) {
                return a + "-" + b;
              }
            });

    final ArrayList<String> list = new ArrayList<String>();
    os.subscribe(
        new Action1<String>() {

          @Override
          public void call(String s) {
            System.out.println(s);
            list.add(s);
          }
        });

    assertEquals(5, list.size());
    assertEquals("1-1", list.get(0));
    assertEquals("2-2", list.get(1));
    assertEquals("5-5", list.get(4));
  }
Example #2
0
  @Test
  public void testZipInfiniteAndFinite() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch infiniteObservable = new CountDownLatch(1);
    Observable<String> os =
        OBSERVABLE_OF_5_INTEGERS.zip(
            ASYNC_OBSERVABLE_OF_INFINITE_INTEGERS(infiniteObservable),
            new Func2<Integer, Integer, String>() {

              @Override
              public String call(Integer a, Integer b) {
                return a + "-" + b;
              }
            });

    final ArrayList<String> list = new ArrayList<String>();
    os.subscribe(
        new Observer<String>() {

          @Override
          public void onCompleted() {
            latch.countDown();
          }

          @Override
          public void onError(Throwable e) {
            e.printStackTrace();
            latch.countDown();
          }

          @Override
          public void onNext(String s) {
            System.out.println(s);
            list.add(s);
          }
        });

    latch.await(1000, TimeUnit.MILLISECONDS);
    if (!infiniteObservable.await(2000, TimeUnit.MILLISECONDS)) {
      throw new RuntimeException("didn't unsubscribe");
    }

    assertEquals(5, list.size());
    assertEquals("1-1", list.get(0));
    assertEquals("2-2", list.get(1));
    assertEquals("5-5", list.get(4));
  }