@Test(groups = "standalone")
  public void testObserveMultiple() {
    final TestSubscriber<Response> tester = new TestSubscriber<>();

    try (AsyncHttpClient client = asyncHttpClient()) {
      Observable<Response> o1 =
          AsyncHttpObservable.observe(() -> client.prepareGet("http://gatling.io"));
      Observable<Response> o2 =
          AsyncHttpObservable.observe(
              () -> client.prepareGet("http://www.wisc.edu").setFollowRedirect(true));
      Observable<Response> o3 =
          AsyncHttpObservable.observe(
              () -> client.prepareGet("http://www.umn.edu").setFollowRedirect(true));
      Observable<Response> all = Observable.merge(o1, o2, o3);
      all.subscribe(tester);
      tester.awaitTerminalEvent();
      tester.assertTerminalEvent();
      tester.assertCompleted();
      tester.assertNoErrors();
      List<Response> responses = tester.getOnNextEvents();
      assertNotNull(responses);
      assertEquals(responses.size(), 3);
      for (Response response : responses) {
        assertEquals(response.getStatusCode(), 200);
      }
    } catch (Exception e) {
      Thread.currentThread().interrupt();
    }
  }
 /** 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();
   }
 }
Beispiel #3
0
  @Test
  public void testSimpleObject() {
    TestSubscriber<Person> subscriber = new TestSubscriber<>();
    observable.lift(new GsonConverter<Person>()).subscribe(subscriber);

    subscriber.assertNoErrors();
    subscriber.assertTerminalEvent();

    List<Person> persons = subscriber.getOnNextEvents();
    assertEquals(1, persons.size());

    Person person = persons.get(0);
    assertEquals("John", person.name);
    assertEquals(25, person.age);
    assertTrue(person.isDev);
  }
  @Test(timeout = 30000)
  public void flatMapRangeMixedAsyncLoop() {
    for (int i = 0; i < 2000; i++) {
      if (i % 10 == 0) {
        System.out.println("flatMapRangeAsyncLoop > " + i);
      }
      TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
      Observable.range(0, 1000)
          .flatMap(
              new Func1<Integer, Observable<Integer>>() {
                final Random rnd = new Random();

                @Override
                public Observable<Integer> call(Integer t) {
                  Observable<Integer> r = Observable.just(t);
                  if (rnd.nextBoolean()) {
                    r = r.asObservable();
                  }
                  return r;
                }
              })
          .observeOn(Schedulers.computation())
          .subscribe(ts);

      ts.awaitTerminalEvent(2500, TimeUnit.MILLISECONDS);
      if (ts.getOnCompletedEvents().isEmpty()) {
        System.out.println(ts.getOnNextEvents().size());
      }
      ts.assertTerminalEvent();
      ts.assertNoErrors();
      List<Integer> list = ts.getOnNextEvents();
      if (list.size() < 1000) {
        Set<Integer> set = new HashSet<Integer>(list);
        for (int j = 0; j < 1000; j++) {
          if (!set.contains(j)) {
            System.out.println(j + " missing");
          }
        }
      }
      assertEquals(1000, list.size());
    }
  }
  @Test(groups = "standalone")
  public void testObserveError() {
    final TestSubscriber<Response> tester = new TestSubscriber<>();

    try (AsyncHttpClient client = asyncHttpClient()) {
      Observable<Response> o1 =
          AsyncHttpObservable.observe(() -> client.prepareGet("http://gatling.io/ttfn"));
      o1.subscribe(tester);
      tester.awaitTerminalEvent();
      tester.assertTerminalEvent();
      tester.assertCompleted();
      tester.assertNoErrors();
      List<Response> responses = tester.getOnNextEvents();
      assertNotNull(responses);
      assertEquals(responses.size(), 1);
      assertEquals(responses.get(0).getStatusCode(), 404);
    } catch (Exception e) {
      Thread.currentThread().interrupt();
    }
  }
  @Ignore // don't care for any reordering
  @Test(timeout = 10000)
  public void flatMapRangeAsyncLoop() {
    for (int i = 0; i < 2000; i++) {
      if (i % 10 == 0) {
        System.out.println("flatMapRangeAsyncLoop > " + i);
      }
      TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
      Observable.range(0, 1000)
          .flatMap(
              new Func1<Integer, Observable<Integer>>() {
                @Override
                public Observable<Integer> call(Integer t) {
                  return Observable.just(t);
                }
              })
          .observeOn(Schedulers.computation())
          .subscribe(ts);

      ts.awaitTerminalEvent(2500, TimeUnit.MILLISECONDS);
      if (ts.getOnCompletedEvents().isEmpty()) {
        System.out.println(ts.getOnNextEvents().size());
      }
      ts.assertTerminalEvent();
      ts.assertNoErrors();
      List<Integer> list = ts.getOnNextEvents();
      assertEquals(1000, list.size());
      boolean f = false;
      for (int j = 0; j < list.size(); j++) {
        if (list.get(j) != j) {
          System.out.println(j + " " + list.get(j));
          f = true;
        }
      }
      if (f) {
        Assert.fail("Results are out of order!");
      }
    }
  }
  @Test
  public void testFlatMapTransformsMaxConcurrentNormal() {
    final int m = 2;
    final AtomicInteger subscriptionCount = new AtomicInteger();
    Observable<Integer> onNext =
        compose(
                Observable.from(Arrays.asList(1, 2, 3)).observeOn(Schedulers.computation()),
                subscriptionCount,
                m)
            .subscribeOn(Schedulers.computation());
    Observable<Integer> onCompleted =
        compose(Observable.from(Arrays.asList(4)), subscriptionCount, m)
            .subscribeOn(Schedulers.computation());
    Observable<Integer> onError = Observable.from(Arrays.asList(5));

    Observable<Integer> source = Observable.from(Arrays.asList(10, 20, 30));

    @SuppressWarnings("unchecked")
    Observer<Object> o = mock(Observer.class);
    TestSubscriber<Object> ts = new TestSubscriber<Object>(o);

    source.flatMap(just(onNext), just(onError), just0(onCompleted), m).subscribe(ts);

    ts.awaitTerminalEvent(1, TimeUnit.SECONDS);
    ts.assertNoErrors();
    ts.assertTerminalEvent();

    verify(o, times(3)).onNext(1);
    verify(o, times(3)).onNext(2);
    verify(o, times(3)).onNext(3);
    verify(o).onNext(4);
    verify(o).onCompleted();

    verify(o, never()).onNext(5);
    verify(o, never()).onError(any(Throwable.class));
  }