io.vertx.rx.java.RxHelper Java Examples

The following examples show how to use io.vertx.rx.java.RxHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CoreApiTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testScheduledTimer() {
  vertx.runOnContext(v -> {
    long startTime = System.currentTimeMillis();
    Context initCtx = Vertx.currentContext();
    Observable.timer(100, 100, TimeUnit.MILLISECONDS, io.vertx.rxjava.core.RxHelper.scheduler(vertx)).take(10).subscribe(new Observer<Long>() {
      public void onNext(Long value) {
        assertEquals(initCtx.getDelegate(), Vertx.currentContext().getDelegate());
      }

      public void onError(Throwable e) {
        fail("unexpected failure");
      }

      public void onCompleted() {
        long timeTaken = System.currentTimeMillis() - startTime;
        assertTrue("Was expecting to have time taken | " + timeTaken + " -  1000 | < 200", Math.abs(timeTaken - 1000) < 1000);
        testComplete();
      }
    });
  });
  await();
}
 
Example #2
Source File: CoreApiTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testObserverToHandler() throws Exception {
  AtomicInteger count = new AtomicInteger();
  Observer<Long> observer = new Observer<Long>() {
    @Override
    public void onCompleted() {
      assertEquals(1, count.get());
      testComplete();
    }

    @Override
    public void onError(Throwable e) {
      fail(e.getMessage());
    }

    @Override
    public void onNext(Long l) {
      count.incrementAndGet();
    }
  };
  vertx.setTimer(1, RxHelper.toHandler(observer));
  await();
}
 
Example #3
Source File: WriteStreamSubscriberTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteStreamError() throws Exception {
  waitFor(2);
  RuntimeException expected = new RuntimeException();
  FakeWriteStream writeStream = new FakeWriteStream(vertx).failAfterWrite(expected);
  Subscriber<Integer> subscriber = RxHelper.toSubscriber(writeStream).onWriteStreamError(throwable -> {
    assertThat(throwable, is(sameInstance(expected)));
    complete();
  });
  Observable.<Integer>create(s -> s.onNext(0)).observeOn(RxHelper.scheduler(vertx))
    .doOnUnsubscribe(this::complete)
    .subscribeOn(RxHelper.scheduler(vertx))
    .subscribe(subscriber);
  await();
  assertFalse("Did not expect writeStream end method to be invoked", writeStream.endInvoked());
}
 
Example #4
Source File: SchedulerTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnsubscribePeriodicInTask() throws Exception {
  ContextScheduler scheduler = (ContextScheduler) RxHelper.blockingScheduler(vertx);
  ContextScheduler.ContextWorker worker = scheduler.createWorker();
  CountDownLatch latch = new CountDownLatch(1);
  AtomicReference<Subscription> ref = new AtomicReference<>();
  ref.set(worker.schedulePeriodically(() -> {
    Subscription sub;
    while ((sub = ref.get()) == null) {
      Thread.yield();
    }
    sub.unsubscribe();
    latch.countDown();
  }, 10, 10, MILLISECONDS));
  awaitLatch(latch);
  waitUntil(() -> worker.countActions() == 0);
}
 
Example #5
Source File: SchedulerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemovedFromContextAfterRun() throws Exception {
  ContextScheduler scheduler = (ContextScheduler) RxHelper.blockingScheduler(vertx);
  ContextScheduler.ContextWorker worker = scheduler.createWorker();
  CountDownLatch latch = new CountDownLatch(1);
  worker.schedule(latch::countDown);
  awaitLatch(latch);
  waitUntil(() -> worker.countActions() == 0);
}
 
Example #6
Source File: ObservableHandlerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleNotifyTwice() {
  ObservableHandler<String> o = RxHelper.observableHandler();
  TestSubscriber<String> subscriber = new TestSubscriber<>();
  TestUtils.subscribe(o, subscriber);
  o.toHandler().handle("abc");
  o.toHandler().handle("def");
  subscriber.assertItem("abc").assertCompleted().assertEmpty();
}
 
Example #7
Source File: ObservableHandlerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiNotifyTwice() {
  ObservableHandler<String> o = RxHelper.observableHandler(true);
  TestSubscriber<String> subscriber = new TestSubscriber<>();
  TestUtils.subscribe(o, subscriber);
  o.toHandler().handle("abc");
  o.toHandler().handle("def");
  subscriber.assertItem("abc").assertItem("def").assertEmpty();
}
 
Example #8
Source File: ObservableHandlerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleUnsubscribeBeforeNotify() {
  ObservableHandler<String> o = RxHelper.observableHandler();
  TestSubscriber<String> subscriber = new TestSubscriber<>();
  TestUtils.subscribe(o, subscriber);
  subscriber.unsubscribe();
  assertTrue(subscriber.isUnsubscribed());
  subscriber.assertEmpty();
  o.toHandler().handle("abc");
  subscriber.assertEmpty();
}
 
Example #9
Source File: ObservableHandlerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiUnsubscribeBeforeNotify() {
  ObservableHandler<String> o = RxHelper.observableHandler(true);
  TestSubscriber<String> subscriber = new TestSubscriber<>();
  TestUtils.subscribe(o, subscriber);
  subscriber.unsubscribe();
  assertTrue(subscriber.isUnsubscribed());
  subscriber.assertEmpty();
  o.toHandler().handle("abc");
  subscriber.assertEmpty();
}
 
Example #10
Source File: ObservableHandlerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleNotifyAfterSubscribe() {
  ObservableHandler<String> o = RxHelper.observableHandler();
  TestSubscriber<String> subscriber = new TestSubscriber<>();
  TestUtils.subscribe(o, subscriber);
  subscriber.assertEmpty();
  o.toHandler().handle("abc");
  subscriber.assertItem("abc").assertCompleted().assertEmpty();
}
 
Example #11
Source File: ObservableHandlerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiNotifyAfterSubscribe() {
  ObservableHandler<String> o = RxHelper.observableHandler(true);
  TestSubscriber<String> subscriber = new TestSubscriber<>();
  TestUtils.subscribe(o, subscriber);
  subscriber.assertEmpty();
  o.toHandler().handle("abc");
  subscriber.assertItem("abc").assertEmpty();
}
 
Example #12
Source File: SubscriberTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleFutureSubscriptionError() {
  List<Object> results = new ArrayList<>();
  Subscriber<String> sub = RxHelper.toSubscriber(handler(results));
  SingleEmitter<String> emitter = subscribeToSingle(sub);
  assertEquals(Collections.emptyList(), results);
  Throwable cause = new Throwable();
  emitter.onError(cause);
  assertEquals(Collections.singletonList(cause), results);
  emitter.onSuccess("the-value");
  assertEquals(Collections.singletonList(cause), results);
}
 
Example #13
Source File: NativeExamples.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
public void unmarshaller(FileSystem fileSystem) {
  fileSystem.open("/data.txt", new OpenOptions(), result -> {
    AsyncFile file = result.result();
    Observable<Buffer> observable = RxHelper.toObservable(file);
    observable.lift(RxHelper.unmarshaller(MyPojo.class)).subscribe(
        mypojo -> {
          // Process the object
        }
    );
  });
}
 
Example #14
Source File: SubscriberTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservableFutureSubscriptionCompletedWithItems() {
  List<Object> results = new ArrayList<>();
  Subscriber<String> sub = RxHelper.toSubscriber(handler(results));
  Emitter<String> emitter = subscribe(sub);
  assertEquals(Collections.emptyList(), results);
  emitter.onNext("the-value-1");
  assertEquals(Collections.singletonList("the-value-1"), results);
  emitter.onNext("the-value-2");
  assertEquals(Collections.singletonList("the-value-1"), results);
}
 
Example #15
Source File: SubscriberTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservableFutureSubscriptionError() {
  List<Object> results = new ArrayList<>();
  Subscriber<String> sub = RxHelper.toSubscriber(handler(results));
  Emitter<String> emitter = subscribe(sub);
  assertEquals(Collections.emptyList(), results);
  Throwable cause = new Throwable();
  emitter.onError(cause);
  assertEquals(Collections.singletonList(cause), results);
  emitter.onNext("the-value");
  assertEquals(Collections.singletonList(cause), results);
}
 
Example #16
Source File: SubscriberTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservableFutureSubscriptionCompletedWithNoItems() {
  List<Object> results = new ArrayList<>();
  Subscriber<String> sub = RxHelper.toSubscriber(handler(results));
  Emitter<String> emitter = subscribe(sub);
  assertEquals(Collections.emptyList(), results);
  emitter.onCompleted();
  assertEquals(Collections.singletonList(null), results);
  emitter.onNext("the-value-1");
  assertEquals(Collections.singletonList(null), results);
}
 
Example #17
Source File: SchedulerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemovedFromContextAfterDelay() throws Exception {
  ContextScheduler scheduler = (ContextScheduler) RxHelper.blockingScheduler(vertx);
  ContextScheduler.ContextWorker worker = scheduler.createWorker();
  CountDownLatch latch = new CountDownLatch(1);
  worker.schedule(latch::countDown, 10, MILLISECONDS);
  awaitLatch(latch);
  waitUntil(() -> worker.countActions() == 0);
}
 
Example #18
Source File: ObservableHandlerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiFulfillAdaptedSubscriber() {
  TestSubscriber<String> subscriber = new TestSubscriber<>();
  Handler<String> o = RxHelper.toHandler(TestUtils.toObserver(subscriber), true);
  o.handle("abc");
  subscriber.assertItem("abc").assertEmpty();
}
 
Example #19
Source File: ObservableHandlerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleNotifyBeforeSubscribe() {
  ObservableHandler<String> o = RxHelper.observableHandler();
  o.toHandler().handle("abc");
  TestSubscriber<String> subscriber = new TestSubscriber<>();
  TestUtils.subscribe(o, subscriber);
  subscriber.assertCompleted().assertEmpty();
}
 
Example #20
Source File: SubscriberTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservableFutureSubscriptionCompletedWithItem() {
  List<Object> results = new ArrayList<>();
  Subscriber<String> sub = RxHelper.toSubscriber(handler(results));
  Emitter<String> emitter = subscribe(sub);
  assertEquals(Collections.emptyList(), results);
  emitter.onNext("the-value-1");
  assertEquals(Collections.singletonList("the-value-1"), results);
  emitter.onCompleted();
  emitter.onNext("the-value-1");
  assertEquals(Collections.singletonList("the-value-1"), results);
}
 
Example #21
Source File: SubscriberTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleFutureSubscriptionSuccess() {
  List<Object> results = new ArrayList<>();
  Subscriber<String> sub = RxHelper.toSubscriber(handler(results));
  SingleEmitter<String> emitter = subscribeToSingle(sub);
  assertEquals(Collections.emptyList(), results);
  emitter.onSuccess("the-value-1");
  assertEquals(Collections.singletonList("the-value-1"), results);
  emitter.onSuccess("the-value-2");
  assertEquals(Collections.singletonList("the-value-1"), results);
}
 
Example #22
Source File: ObservableHandlerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleFulfillAdaptedSubscriber() {
  TestSubscriber<String> subscriber = new TestSubscriber<>();
  Handler<String> o = RxHelper.toHandler(TestUtils.toObserver(subscriber));
  o.handle("abc");
  subscriber.assertItem("abc").assertCompleted().assertEmpty();
}
 
Example #23
Source File: ObservableHandlerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testFulfillAdaptedFunctions1() {
  List<String> items = new ArrayList<>();
  Handler<String> o = RxHelper.toHandler(items::add);
  o.handle("abc");
  assertEquals(Collections.singletonList("abc"), items);
}
 
Example #24
Source File: WriteStreamSubscriberTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservableErrorReported() throws Exception {
  Exception expected = new Exception();
  FakeWriteStream writeStream = new FakeWriteStream(vertx);
  Subscriber<Integer> subscriber = RxHelper.toSubscriber(writeStream).onError(throwable -> {
    assertThat(throwable, is(sameInstance(expected)));
    complete();
  });
  Observable.<Integer>error(expected)
    .observeOn(RxHelper.scheduler(vertx))
    .subscribeOn(RxHelper.scheduler(vertx))
    .subscribe(subscriber);
  await();
  assertFalse("Did not expect writeStream end method to be invoked", writeStream.endInvoked());
}
 
Example #25
Source File: CoreApiTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeployVerticle() throws Exception {
  CountDownLatch deployLatch = new CountDownLatch(2);
  io.vertx.rxjava.core.RxHelper.deployVerticle(vertx, new AbstractVerticle() {
    @Override
    public void start() {
      deployLatch.countDown();
    }
  }).subscribe(resp -> {
    deployLatch.countDown();
  });
  awaitLatch(deployLatch);
}
 
Example #26
Source File: CoreApiTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimeMap() {
  vertx.runOnContext(v -> {
    Context initCtx = Vertx.currentContext();
    EventBus eb = vertx.eventBus();
    ReadStream<String> consumer = eb.<String>localConsumer("the-address").bodyStream();
    Observer<String> observer = new Subscriber<String>() {
      boolean first = true;
      @Override
      public void onNext(String s) {
        if (first) {
          first = false;
          assertEquals(initCtx.getDelegate(), Vertx.currentContext().getDelegate());
          assertEquals("msg1msg2msg3", s);
          testComplete();
        }
      }
      @Override
      public void onError(Throwable e) {
        fail(e.getMessage());
      }
      @Override
      public void onCompleted() {
      }
    };
    Observable<String> observable = consumer.toObservable();
    observable.
        buffer(500, TimeUnit.MILLISECONDS, io.vertx.rxjava.core.RxHelper.scheduler(vertx)).
        map(samples -> samples.stream().reduce("", (a, b) -> a + b)).
        subscribe(observer);
    eb.send("the-address", "msg1");
    eb.send("the-address", "msg2");
    eb.send("the-address", "msg3");
  });
  await();
}
 
Example #27
Source File: CoreApiTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testScheduledBuffer() {
  vertx.runOnContext(v -> {
    long startTime = System.currentTimeMillis();
    Context initCtx = Vertx.currentContext();
    Observable
        .timer(10, 10, TimeUnit.MILLISECONDS, io.vertx.rxjava.core.RxHelper.scheduler(vertx))
        .buffer(100, TimeUnit.MILLISECONDS, io.vertx.rxjava.core.RxHelper.scheduler(vertx))
        .take(10)
        .subscribe(new Observer<List<Long>>() {
          private int eventCount = 0;

          public void onNext(List<Long> value) {
            eventCount++;
            assertEquals(initCtx.getDelegate(), Vertx.currentContext().getDelegate());
          }

          public void onError(Throwable e) {
            fail("unexpected failure");
          }

          public void onCompleted() {
            long timeTaken = System.currentTimeMillis() - startTime;
            assertEquals(10, eventCount);
            assertTrue("Was expecting to have time taken | " + timeTaken + " -  1000 | < 200", Math.abs(timeTaken - 1000) < 1000);
            testComplete();
          }
        });
  });
  await();
}
 
Example #28
Source File: BufferTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapPojoListFromBuffer() throws Exception {
  Observable<Buffer> stream = Observable.just(Buffer.buffer("[{\"foo\":\"bar\"}]"));
  Observable<List<SimplePojo>> mapped = stream.lift(RxHelper.unmarshaller(new TypeReference<List<SimplePojo>>(){}));
  TestSubscriber<List<SimplePojo>> sub = new TestSubscriber<>();
  subscribe(mapped, sub);
  sub.assertItems(Arrays.asList(new SimplePojo("bar")));
  sub.assertCompleted();
  sub.assertEmpty();
}
 
Example #29
Source File: BufferTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapObjectNodeFromBuffer() throws Exception {
  Observable<Buffer> stream = Observable.from(Arrays.asList(Buffer.buffer("{\"foo\":\"bar\"}")));
  Observable<JsonNode> mapped = stream.lift(RxHelper.unmarshaller(JsonNode.class));
  TestSubscriber<JsonNode> sub = new TestSubscriber<>();
  subscribe(mapped, sub);
  sub.assertItem(new ObjectMapper().createObjectNode().put("foo", "bar"));
  sub.assertCompleted();
  sub.assertEmpty();
}
 
Example #30
Source File: BufferTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapPojoFromBuffer() throws Exception {
  Observable<Buffer> stream = Observable.from(Arrays.asList(Buffer.buffer("{\"foo\":\"bar\"}")));
  Observable<SimplePojo> mapped = stream.lift(RxHelper.unmarshaller(SimplePojo.class));
  TestSubscriber<SimplePojo> sub = new TestSubscriber<>();
  subscribe(mapped, sub);
  sub.assertItem(new SimplePojo("bar"));
  sub.assertCompleted();
  sub.assertEmpty();
}