Example #1
0
  @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);
  }
Example #2
0
 @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);
 }
Example #3
0
 @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(
         "-------------------------------------------------------------------------------------------");
   }
 }