Java Code Examples for reactor.core.publisher.UnicastProcessor#onNext()

The following examples show how to use reactor.core.publisher.UnicastProcessor#onNext() . 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: RSocketRequesterTest.java    From rsocket-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testChannelRequestServerSideCancellation() {
  MonoProcessor<Payload> cancelled = MonoProcessor.create();
  UnicastProcessor<Payload> request = UnicastProcessor.create();
  request.onNext(EmptyPayload.INSTANCE);
  rule.socket.requestChannel(request).subscribe(cancelled);
  int streamId = rule.getStreamIdForRequestType(REQUEST_CHANNEL);
  rule.connection.addToReceivedBuffer(CancelFrameCodec.encode(rule.alloc(), streamId));
  rule.connection.addToReceivedBuffer(PayloadFrameCodec.encodeComplete(rule.alloc(), streamId));
  Flux.first(
          cancelled,
          Flux.error(new IllegalStateException("Channel request not cancelled"))
              .delaySubscription(Duration.ofSeconds(1)))
      .blockFirst();

  Assertions.assertThat(request.isDisposed()).isTrue();
  Assertions.assertThat(rule.connection.getSent())
      .hasSize(1)
      .first()
      .matches(bb -> frameType(bb) == REQUEST_CHANNEL)
      .matches(ReferenceCounted::release);
  rule.assertHasNoLeaks();
}
 
Example 2
Source File: SimpleReactorExample.java    From Learning-Path-Spring-5-End-to-End-Programming with MIT License 5 votes vote down vote up
@Test
public void testHotPublisher(){
  UnicastProcessor<String> hotSource = UnicastProcessor.create();
  Flux<Category> hotPublisher = hotSource.publish()
      .autoConnect().map((String t) -> Category.builder().name(t).build());
  hotPublisher.subscribe(category -> System.out.println("Subscriber 1: "+ category.getName()));
  hotSource.onNext("sports");
  hotSource.onNext("cars");
  hotPublisher.subscribe(category -> System.out.println("Subscriber 2: "+category.getName()));
  hotSource.onNext("games");
  hotSource.onNext("electronics");
  hotSource.onComplete();
}
 
Example 3
Source File: SimpleReactorExample.java    From Spring-5.0-By-Example with MIT License 5 votes vote down vote up
@Test
public void testHotPublisher(){
  UnicastProcessor<String> hotSource = UnicastProcessor.create();
  Flux<Category> hotPublisher = hotSource.publish()
      .autoConnect().map((String t) -> Category.builder().name(t).build());
  hotPublisher.subscribe(category -> System.out.println("Subscriber 1: "+ category.getName()));
  hotSource.onNext("sports");
  hotSource.onNext("cars");
  hotPublisher.subscribe(category -> System.out.println("Subscriber 2: "+category.getName()));
  hotSource.onNext("games");
  hotSource.onNext("electronics");
  hotSource.onComplete();
}
 
Example 4
Source File: TcpIntegrationTest.java    From rsocket-java with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 15_000L)
public void testTwoConcurrentStreams() throws InterruptedException {
  ConcurrentHashMap<String, UnicastProcessor<Payload>> map = new ConcurrentHashMap<>();
  UnicastProcessor<Payload> processor1 = UnicastProcessor.create();
  map.put("REQUEST1", processor1);
  UnicastProcessor<Payload> processor2 = UnicastProcessor.create();
  map.put("REQUEST2", processor2);

  handler =
      new RSocket() {
        @Override
        public Flux<Payload> requestStream(Payload payload) {
          return map.get(payload.getDataUtf8());
        }
      };

  RSocket client = buildClient();

  Flux<Payload> response1 = client.requestStream(DefaultPayload.create("REQUEST1"));
  Flux<Payload> response2 = client.requestStream(DefaultPayload.create("REQUEST2"));

  CountDownLatch nextCountdown = new CountDownLatch(2);
  CountDownLatch completeCountdown = new CountDownLatch(2);

  response1
      .subscribeOn(Schedulers.newSingle("1"))
      .subscribe(c -> nextCountdown.countDown(), t -> {}, completeCountdown::countDown);

  response2
      .subscribeOn(Schedulers.newSingle("2"))
      .subscribe(c -> nextCountdown.countDown(), t -> {}, completeCountdown::countDown);

  processor1.onNext(DefaultPayload.create("RESPONSE1A"));
  processor2.onNext(DefaultPayload.create("RESPONSE2A"));

  nextCountdown.await();

  processor1.onComplete();
  processor2.onComplete();

  completeCountdown.await();
}