@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);
 }
  @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);
  }
  @Test(timeout = 2000)
  public void testFirehoseFailsAsExpected() {
    AtomicInteger c = new AtomicInteger();
    TestSubscriber<Integer> ts = new TestSubscriber<>();

    firehose(c)
        .observeOn(Schedulers.computation())
        .map(
            v -> {
              try {
                Thread.sleep(10);
              } catch (Exception e) {
                e.printStackTrace();
              }
              return v;
            })
        .subscribe(ts);

    ts.awaitTerminalEvent();
    System.out.println(
        "testFirehoseFailsAsExpected => Received: " + ts.valueCount() + "  Emitted: " + c.get());

    // FIXME it is possible slow is not slow enough or the main gets delayed and thus more than one
    // source value is emitted.
    int vc = ts.valueCount();
    assertTrue("10 < " + vc, vc <= 10);

    ts.assertError(MissingBackpressureException.class);
  }
  @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);
  }
  @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);
  }
  @Test
  public void testMergeAsyncThenObserveOn() {
    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.observeOn(Schedulers.newThread()).take(NUM).subscribe(ts);
    ts.awaitTerminalEvent();
    ts.assertNoErrors();
    System.out.println(
        "testMergeAsyncThenObserveOn => 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
    // 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() < Observable.bufferSize() * 7);
    assertTrue(c2.get() < Observable.bufferSize() * 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) (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.observeOn(Schedulers.io()).take(NUM).subscribe(ts);

      ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
      ts.assertComplete();
      ts.assertNoErrors();
      System.out.println(
          "testMergeAsyncThenObserveOn => Received: "
              + ts.valueCount()
              + "  Emitted: "
              + c1.get()
              + " / "
              + c2.get());
      assertEquals(NUM, ts.valueCount());
    }
  }
  @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);
  }
 @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);
 }
  @Test
  public void testFlatMapSync() {
    int NUM = (int) (Observable.bufferSize() * 2.1);
    AtomicInteger c = new AtomicInteger();
    TestSubscriber<Integer> ts = new TestSubscriber<>();

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

    ts.awaitTerminalEvent();
    ts.assertNoErrors();
    System.out.println("testFlatMapSync => Received: " + ts.valueCount() + "  Emitted: " + c.get());
    assertEquals(NUM, ts.valueCount());
    // expect less than 1 buffer since the flatMap is emitting 10 each time, so it is NUM/10 that
    // will be taken.
    assertTrue(c.get() < Observable.bufferSize());
  }
 @Test
 public void testSubscribeOnScheduling() {
   // in a loop for repeating the concurrency in this to increase chance of failure
   for (int i = 0; i < 100; i++) {
     int NUM = (int) (Observable.bufferSize() * 2.1);
     AtomicInteger c = new AtomicInteger();
     ConcurrentLinkedQueue<Thread> threads = new ConcurrentLinkedQueue<>();
     TestSubscriber<Integer> ts = new TestSubscriber<>();
     // observeOn is there to make it async and need backpressure
     incrementingIntegers(c, threads)
         .subscribeOn(Schedulers.computation())
         .observeOn(Schedulers.computation())
         .take(NUM)
         .subscribe(ts);
     ts.awaitTerminalEvent();
     ts.assertNoErrors();
     System.out.println(
         "testSubscribeOnScheduling => Received: " + ts.valueCount() + "  Emitted: " + c.get());
     assertEquals(NUM, ts.valueCount());
     assertTrue(c.get() < Observable.bufferSize() * 4);
     Thread first = null;
     for (Thread t : threads) {
       System.out.println("testSubscribeOnScheduling => thread: " + t);
       if (first == null) {
         first = t;
       } else {
         if (!first.equals(t)) {
           fail("Expected to see the same thread");
         }
       }
     }
     System.out.println(
         "testSubscribeOnScheduling => Number of batch requests seen: " + threads.size());
     assertTrue(threads.size() > 1);
     System.out.println(
         "-------------------------------------------------------------------------------------------");
   }
 }
  @Test(timeout = 2000)
  public void testOnBackpressureBuffer() {
    int NUM =
        (int) (Observable.bufferSize() * 1.1); // > 1 so that take doesn't prevent buffer overflow
    AtomicInteger c = new AtomicInteger();
    TestSubscriber<Integer> ts = new TestSubscriber<>();

    firehose(c)
        .takeWhile(t1 -> t1 < 100000)
        .onBackpressureBuffer()
        .observeOn(Schedulers.computation())
        .map(SLOW_PASS_THRU)
        .take(NUM)
        .subscribe(ts);

    ts.awaitTerminalEvent();
    ts.assertNoErrors();
    System.out.println(
        "testOnBackpressureBuffer => Received: " + ts.valueCount() + "  Emitted: " + c.get());
    assertEquals(NUM, ts.valueCount());
    // it buffers, so we should get the right value sequentially
    assertEquals(NUM - 1, ts.values().get(NUM - 1).intValue());
  }