示例#1
0
  @Test
  public void testPublishLast() throws InterruptedException {
    final AtomicInteger count = new AtomicInteger();
    ConnectableObservable<String> connectable =
        Observable.<String>create(
                observer -> {
                  observer.onSubscribe(EmptySubscription.INSTANCE);
                  count.incrementAndGet();
                  new Thread(
                          () -> {
                            observer.onNext("first");
                            observer.onNext("last");
                            observer.onComplete();
                          })
                      .start();
                })
            .takeLast(1)
            .publish();

    // subscribe once
    final CountDownLatch latch = new CountDownLatch(1);
    connectable.subscribe(
        value -> {
          assertEquals("last", value);
          latch.countDown();
        });

    // subscribe twice
    connectable.subscribe();

    Disposable subscription = connectable.connect();
    assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
    assertEquals(1, count.get());
    subscription.dispose();
  }
示例#2
0
  @Test(timeout = 20000)
  public void testOnBackpressureDropWithAction() {
    for (int i = 0; i < 100; i++) {
      final AtomicInteger emitCount = new AtomicInteger();
      final AtomicInteger dropCount = new AtomicInteger();
      final AtomicInteger passCount = new AtomicInteger();
      final int NUM =
          Observable.bufferSize() * 3; // > 1 so that take doesn't prevent buffer overflow
      TestSubscriber<Integer> ts = new TestSubscriber<>();

      firehose(emitCount)
          .onBackpressureDrop(v -> dropCount.incrementAndGet())
          .doOnNext(v -> passCount.incrementAndGet())
          .observeOn(Schedulers.computation())
          .map(SLOW_PASS_THRU)
          .take(NUM)
          .subscribe(ts);

      ts.awaitTerminalEvent();
      ts.assertNoErrors();

      List<Integer> onNextEvents = ts.values();
      Integer lastEvent = onNextEvents.get(NUM - 1);
      System.out.println(
          testName.getMethodName()
              + " => Received: "
              + onNextEvents.size()
              + " Passed: "
              + passCount.get()
              + " Dropped: "
              + dropCount.get()
              + "  Emitted: "
              + emitCount.get()
              + " Last value: "
              + lastEvent);
      assertEquals(NUM, onNextEvents.size());
      // in reality, NUM < passCount
      assertTrue(NUM <= passCount.get());
      // it drop, so we should get some number far higher than what would have sequentially
      // incremented
      assertTrue(NUM - 1 <= lastEvent.intValue());
      assertTrue(0 < dropCount.get());
      assertEquals(emitCount.get(), passCount.get() + dropCount.get());
    }
  }
示例#3
0
 @Test
 public void testTakeWhileToList() {
   final int expectedCount = 3;
   final AtomicInteger count = new AtomicInteger();
   for (int i = 0; i < expectedCount; i++) {
     Observable.just(Boolean.TRUE, Boolean.FALSE)
         .takeWhile(v -> v)
         .toList()
         .doOnNext(booleans -> count.incrementAndGet())
         .subscribe();
   }
   assertEquals(expectedCount, count.get());
 }
示例#4
0
    @Override
    public void request(long n) {
      if (SubscriptionHelper.validateRequest(n)) {
        return;
      }
      if (compareAndSet(false, true)) {
        int i = 0;
        final Subscriber<? super Integer> a = s;
        final AtomicInteger c = counter;

        while (!cancelled) {
          a.onNext(i++);
          c.incrementAndGet();
        }
        System.out.println("unsubscribed after: " + i);
      }
    }
示例#5
0
  @Test
  public void testCacheWithCapacity() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    Observable<String> o =
        Observable.<String>create(
                observer -> {
                  observer.onSubscribe(EmptySubscription.INSTANCE);
                  new Thread(
                          () -> {
                            counter.incrementAndGet();
                            observer.onNext("one");
                            observer.onComplete();
                          })
                      .start();
                })
            .cache(1);

    // we then expect the following 2 subscriptions to get that same value
    final CountDownLatch latch = new CountDownLatch(2);

    // subscribe once
    o.subscribe(
        v -> {
          assertEquals("one", v);
          latch.countDown();
        });

    // subscribe again
    o.subscribe(
        v -> {
          assertEquals("one", v);
          latch.countDown();
        });

    if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
      fail("subscriptions did not receive values");
    }
    assertEquals(1, counter.get());
  }
示例#6
0
  @Test(timeout = 10000)
  public void testOnBackpressureDropSynchronousWithAction() {
    for (int i = 0; i < 100; i++) {
      final AtomicInteger dropCount = new AtomicInteger();
      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)
          .onBackpressureDrop(j -> dropCount.incrementAndGet())
          .map(SLOW_PASS_THRU)
          .take(NUM)
          .subscribe(ts);
      ts.awaitTerminalEvent();
      ts.assertNoErrors();

      List<Integer> onNextEvents = ts.values();
      assertEquals(NUM, onNextEvents.size());

      Integer lastEvent = onNextEvents.get(NUM - 1);

      System.out.println(
          "testOnBackpressureDrop => Received: "
              + onNextEvents.size()
              + " Dropped: "
              + dropCount.get()
              + "  Emitted: "
              + c.get()
              + " Last value: "
              + lastEvent);
      // it drop, so we should get some number far higher than what would have sequentially
      // incremented
      assertTrue(NUM - 1 <= lastEvent.intValue());
      // no drop in synchronous mode
      assertEquals(0, dropCount.get());
      assertEquals(c.get(), onNextEvents.size());
    }
  }