@Test
  public void exampleEquivalence() {
    TestScheduler scheduler = Schedulers.test();
    TestSubscriber<Object> testerJoin = new TestSubscriber<>();
    TestSubscriber<Object> testerGroupJoin = new TestSubscriber<>();

    Observable<Long> left = Observable.interval(100, TimeUnit.MILLISECONDS, scheduler);
    Observable<Long> right = Observable.interval(100, TimeUnit.MILLISECONDS, scheduler);

    left.join(
            right,
            i -> Observable.timer(150, TimeUnit.MILLISECONDS, scheduler),
            i -> Observable.timer(0, TimeUnit.MILLISECONDS, scheduler),
            (l, r) -> Tuple.create(l, r))
        .take(10)
        .subscribe(testerJoin);

    left.groupJoin(
            right,
            i -> Observable.timer(150, TimeUnit.MILLISECONDS, scheduler),
            i -> Observable.timer(0, TimeUnit.MILLISECONDS, scheduler),
            (l, rs) -> rs.map(r -> Tuple.create(l, r)))
        .flatMap(i -> i)
        .take(10)
        .subscribe(testerGroupJoin);

    scheduler.advanceTimeTo(600, TimeUnit.MILLISECONDS);
    testerJoin.assertReceivedOnNext(testerGroupJoin.getOnNextEvents());
  }
  @Test
  public void test() {
    TestSubscriber<Object> tester = new TestSubscriber<>();
    TestScheduler scheduler = Schedulers.test();

    Observable<Long> left = Observable.interval(100, TimeUnit.MILLISECONDS, scheduler).take(6);
    Observable<Long> right = Observable.interval(200, TimeUnit.MILLISECONDS, scheduler).take(3);

    left.groupJoin(
            right,
            i -> Observable.never(),
            i -> Observable.timer(0, TimeUnit.MILLISECONDS, scheduler),
            (l, rs) -> rs.toList().map(rl -> Tuple.create(l, rl)))
        .flatMap(i -> i)
        .subscribe(tester);

    scheduler.advanceTimeTo(600, TimeUnit.MILLISECONDS);
    tester.assertReceivedOnNext(
        Arrays.asList(
            Tuple.create(0L, Arrays.asList(0L, 1L, 2L)),
            Tuple.create(1L, Arrays.asList(0L, 1L, 2L)),
            Tuple.create(2L, Arrays.asList(1L, 2L)),
            Tuple.create(3L, Arrays.asList(1L, 2L)),
            Tuple.create(4L, Arrays.asList(2L)),
            Tuple.create(5L, Arrays.asList(2L))));
  }
  @Test
  public void testReplayWithBufferSize() {
    TestScheduler scheduler = Schedulers.test();
    TestSubscriber<Long> tester = new TestSubscriber<>();

    ConnectableObservable<Long> source =
        Observable.interval(1000, TimeUnit.MILLISECONDS, scheduler).take(5).replay(2, scheduler);

    source.connect();
    scheduler.advanceTimeBy(4500, TimeUnit.MILLISECONDS);
    source.subscribe(tester);
    scheduler.triggerActions();
    tester.assertReceivedOnNext(Arrays.asList(2L, 3L));
    scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS);
    tester.assertReceivedOnNext(Arrays.asList(2L, 3L, 4L));
  }
  @Test
  public void testCallRequest() throws Exception {

    RestfitClient client =
        new RestfitClient.Builder()
            .userAgent("RestfitTest/1.0")
            .httpStack(
                new RestfitHttpStack() {
                  @NonNull
                  @Override
                  public Single<RestfitResponse> perform(@NonNull RestfitRequest request) {
                    return Single.just(response);
                  }
                })
            .build();

    final RestfitRequest.Builder requestBuilder =
        client.requestBuilder().method("GET").url("http://example.com/");

    response = new RestfitResponse.Builder().request(requestBuilder.build()).build();

    TestSubscriber<RestfitResponse> testSubscriber = TestSubscriber.create();

    client.call(requestBuilder).subscribe(testSubscriber);

    testSubscriber.awaitTerminalEvent(10, TimeUnit.MILLISECONDS);

    testSubscriber.assertCompleted();
    testSubscriber.assertReceivedOnNext(Collections.singletonList(response));
  }
  @Test
  public void testBackpressureWithTakeBefore() {
    final AtomicInteger generated = new AtomicInteger();
    Observable<Integer> observable =
        Observable.from(
            new Iterable<Integer>() {
              @Override
              public Iterator<Integer> iterator() {
                return new Iterator<Integer>() {

                  @Override
                  public void remove() {}

                  @Override
                  public Integer next() {
                    return generated.getAndIncrement();
                  }

                  @Override
                  public boolean hasNext() {
                    return true;
                  }
                };
              }
            });

    TestSubscriber<Integer> testSubscriber = new TestSubscriber<Integer>();
    observable.take(7).observeOn(Schedulers.newThread()).subscribe(testSubscriber);

    testSubscriber.awaitTerminalEvent();
    testSubscriber.assertReceivedOnNext(Arrays.asList(0, 1, 2, 3, 4, 5, 6));
    assertEquals(7, generated.get());
  }
 /** https://github.com/ReactiveX/RxJava/issues/1147 */
 @Test
 public void testRaceForTerminalState() {
   final List<Integer> expected = Arrays.asList(1);
   for (int i = 0; i < 100000; i++) {
     TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
     Observable.just(1).subscribeOn(Schedulers.computation()).cache().subscribe(ts);
     ts.awaitTerminalEvent();
     ts.assertReceivedOnNext(expected);
     ts.assertTerminalEvent();
   }
 }
  @Test
  public void testReplayWithTime() throws InterruptedException {
    TestScheduler scheduler = Schedulers.test();
    TestSubscriber<Long> tester = new TestSubscriber<>();

    ConnectableObservable<Long> source =
        Observable.interval(1000, TimeUnit.MILLISECONDS, scheduler)
            .take(5)
            .replay(2000, TimeUnit.MILLISECONDS, scheduler);

    source.connect();
    scheduler.advanceTimeBy(4500, TimeUnit.MILLISECONDS);
    source.subscribe(tester);
    tester.assertReceivedOnNext(Arrays.asList(2L, 3L));
    scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS);
    tester.assertReceivedOnNext(Arrays.asList(2L, 3L, 4L));

    //		2
    //		3
    //		4
  }
  @Test
  public void test() {
    TestSubscriber<Long> tester = new TestSubscriber<>();
    TestScheduler scheduler = Schedulers.test();

    Observable.switchOnNext(
            Observable.interval(100, TimeUnit.MILLISECONDS, scheduler)
                .map(i -> Observable.interval(30, TimeUnit.MILLISECONDS, scheduler).map(i2 -> i)))
        .distinctUntilChanged()
        .subscribe(tester);

    scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS);
    tester.assertReceivedOnNext(Arrays.asList(0L, 1L, 2L, 3L));
    tester.assertNoErrors();
    assertEquals(tester.getOnCompletedEvents().size(), 0);
  }
  @Test
  public void testBackpressureWithTakeAfter() {
    final AtomicInteger generated = new AtomicInteger();
    Observable<Integer> observable =
        Observable.from(
            new Iterable<Integer>() {
              @Override
              public Iterator<Integer> iterator() {
                return new Iterator<Integer>() {

                  @Override
                  public void remove() {}

                  @Override
                  public Integer next() {
                    return generated.getAndIncrement();
                  }

                  @Override
                  public boolean hasNext() {
                    return true;
                  }
                };
              }
            });

    TestSubscriber<Integer> testSubscriber =
        new TestSubscriber<Integer>() {
          @Override
          public void onNext(Integer t) {
            System.err.println("c t = " + t + " thread " + Thread.currentThread());
            super.onNext(t);
            try {
              Thread.sleep(10);
            } catch (InterruptedException e) {
            }
          }
        };

    observable.observeOn(Schedulers.newThread()).take(3).subscribe(testSubscriber);
    testSubscriber.awaitTerminalEvent();
    System.err.println(testSubscriber.getOnNextEvents());
    testSubscriber.assertReceivedOnNext(Arrays.asList(0, 1, 2));
    // it should be between the take num and requested batch size across the async boundary
    System.out.println("Generated: " + generated.get());
    assertTrue(generated.get() >= 3 && generated.get() <= RxRingBuffer.SIZE);
  }
示例#10
0
  @Test
  public void testReplay() throws InterruptedException {
    TestScheduler scheduler = Schedulers.test();
    TestSubscriber<Long> tester1 = new TestSubscriber<>();
    TestSubscriber<Long> tester2 = new TestSubscriber<>();

    ConnectableObservable<Long> cold =
        Observable.interval(200, TimeUnit.MILLISECONDS, scheduler).replay();
    Subscription connection = cold.connect();

    cold.subscribe(tester1);
    scheduler.advanceTimeBy(700, TimeUnit.MILLISECONDS);
    tester1.assertReceivedOnNext(Arrays.asList(0L, 1L, 2L));
    tester2.assertReceivedOnNext(Arrays.asList());

    cold.subscribe(tester2);
    tester1.assertReceivedOnNext(Arrays.asList(0L, 1L, 2L));
    tester2.assertReceivedOnNext(Arrays.asList(0L, 1L, 2L));
    scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS);
    tester1.assertReceivedOnNext(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L));
    tester2.assertReceivedOnNext(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L));

    connection.unsubscribe();
  }