@Override
  public CompositeAction<Integer, Integer> createProcessor(int bufferSize) {

    Stream<String> otherStream = Streams.just("test", "test2", "test3");
    System.out.println("Providing new processor");

    ProcessorGroup<Integer> asyncGroup =
        Processors.asyncGroup("stream-tck", bufferSize, 4, Throwable::printStackTrace);

    return Broadcaster.<Integer>passthrough()
        .dispatchOn(asyncGroup)
        .partition(2)
        .flatMap(
            stream ->
                stream
                    .dispatchOn(asyncGroup)
                    .observe(this::monitorThreadUse)
                    .scan((prev, next) -> next)
                    .map(integer -> -integer)
                    .filter(integer -> integer <= 0)
                    .sample(1)
                    .map(integer -> -integer)
                    .buffer(batch, 50, TimeUnit.MILLISECONDS)
                    .<Integer>split()
                    .flatMap(i -> Streams.zip(Streams.just(i), otherStream, Tuple1::getT1)))
        .dispatchOn(asyncGroup)
        .when(Throwable.class, Throwable::printStackTrace)
        .combine();
  }
 @Test
 public void decode() throws InterruptedException {
   Stream<ByteBuffer> source =
       Streams.just(Buffer.wrap("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}").byteBuffer());
   List<Object> results =
       Streams.wrap(decoder.decode(source, ResolvableType.forClass(Pojo.class), null))
           .toList()
           .await();
   assertEquals(1, results.size());
   assertEquals("foofoo", ((Pojo) results.get(0)).getFoo());
 }
 @Override
 public Publisher<HandlerResult> handle(
     ServerHttpRequest request, ServerHttpResponse response, Object handler) {
   HttpHandler httpHandler = (HttpHandler) handler;
   Publisher<Void> publisher = httpHandler.handle(request, response);
   return Streams.wrap(publisher).map(aVoid -> null);
 }
  @Test
  public void list() {
    ListOrganizationsResponse page1 =
        new ListOrganizationsResponse()
            .withResource(
                new ListOrganizationsResponseResource()
                    .withMetadata(new Metadata().withId("test-id-1"))
                    .withEntity(new ListOrganizationsResponseEntity().withName("test-name-1")))
            .withTotalPages(2);
    when(this.cloudFoundryClient.organizations().list(new ListOrganizationsRequest().withPage(1)))
        .thenReturn(Publishers.just(page1));

    ListOrganizationsResponse page2 =
        new ListOrganizationsResponse()
            .withResource(
                new ListOrganizationsResponseResource()
                    .withMetadata(new Metadata().withId("test-id-2"))
                    .withEntity(new ListOrganizationsResponseEntity().withName("test-name-2")))
            .withTotalPages(2);
    when(this.cloudFoundryClient.organizations().list(new ListOrganizationsRequest().withPage(2)))
        .thenReturn(Publishers.just(page2));

    List<Organization> expected =
        Arrays.asList(
            new Organization("test-id-1", "test-name-1"),
            new Organization("test-id-2", "test-name-2"));

    List<Organization> actual = Streams.wrap(this.organizations.list()).toList().poll();

    assertEquals(expected, actual);
  }
 @Test
 public void decodeMultipleChunksToArray() throws InterruptedException {
   JsonObjectDecoder decoder = new JsonObjectDecoder(true);
   Stream<ByteBuffer> source =
       Streams.just(
           Buffer.wrap("[{\"foo\": \"foofoo\", \"bar\"").byteBuffer(),
           Buffer.wrap(": \"barbar\"},{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}]")
               .byteBuffer());
   List<String> results =
       Streams.wrap(decoder.decode(source, null, null))
           .map(
               chunk -> {
                 byte[] b = new byte[chunk.remaining()];
                 chunk.get(b);
                 return new String(b, StandardCharsets.UTF_8);
               })
           .toList()
           .await();
   assertEquals(2, results.size());
   assertEquals("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}", results.get(0));
   assertEquals("{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}", results.get(1));
 }
  public static void main(String[] args) throws IOException {

    Environment.initialize();

    Stream<Long> stream = Streams.period(1);

    stream.consume(n -> logger.debug("\t A[{}]", n));

    // 2nd subscriber starts 5 seconds later to emphasize independent streams
    sleep(5);

    stream.consume(n -> logger.debug("\t\t\t B[{}]", n));

    System.in.read();
  }
Exemplo n.º 7
0
 /**
  * Flush the headers if not sent. Might be useful for the case
  *
  * @return Stream to signal error or successful write to the client
  */
 public Stream<Void> writeHeaders() {
   if (statusAndHeadersSent == 0) {
     return new Stream<Void>() {
       @Override
       public void subscribe(Subscriber<? super Void> s) {
         if (markHeadersAsFlushed()) {
           doSubscribeHeaders(s);
         } else {
           Publishers.<Void>error(new IllegalStateException("Status and headers already sent"))
               .subscribe(s);
         }
       }
     };
   } else {
     return Streams.empty();
   }
 }
Exemplo n.º 8
0
  @SuppressWarnings("unchecked")
  protected <T> void assertTcpClientServerExchangedData(
      Class<? extends reactor.io.net.tcp.TcpServer> serverType,
      Class<? extends reactor.io.net.tcp.TcpClient> clientType,
      Codec<Buffer, T, T> codec,
      T data,
      Predicate<T> replyPredicate)
      throws InterruptedException {
    final Codec<Buffer, T, T> elCodec =
        codec == null ? (Codec<Buffer, T, T>) StandardCodecs.PASS_THROUGH_CODEC : codec;

    TcpServer<T, T> server =
        NetStreams.tcpServer(
            serverType, s -> s.env(env1).listen(LOCALHOST, getPort()).codec(elCodec));

    server.start(ch -> ch.writeWith(ch.take(1))).await();

    TcpClient<T, T> client =
        NetStreams.tcpClient(
            clientType, s -> s.env(env2).connect(LOCALHOST, getPort()).codec(elCodec));

    final Promise<T> p = Promises.prepare();

    client
        .start(
            input -> {
              input.log("echo-in").subscribe(p);
              return input.writeWith(Streams.just(data).log("echo-out")).log("echo.close");
            })
        .await();

    T reply = p.await(50, TimeUnit.SECONDS);

    assertTrue("reply was correct", replyPredicate.test(reply));

    //		assertServerStopped(server);
    //		assertClientStopped(client);
  }
  @Test
  public void testPromiseGateway() throws Exception {

    final AtomicReference<List<Integer>> ref = new AtomicReference<List<Integer>>();
    final CountDownLatch consumeLatch = new CountDownLatch(1);

    Streams.just("1", "2", "3", "4", "5")
        .dispatchOn(this.environment)
        .map(Integer::parseInt)
        .flatMap(this.testGateway::multiply)
        .toList()
        .onSuccess(
            integers -> {
              ref.set(integers);
              consumeLatch.countDown();
            });

    assertTrue(consumeLatch.await(2, TimeUnit.SECONDS));

    List<Integer> integers = ref.get();
    assertEquals(5, integers.size());

    assertThat(integers, Matchers.<Integer>contains(2, 4, 6, 8, 10));
  }
  private HttpServer<Buffer, Buffer> createProtocolListener() throws Exception {

    final Stream<Buffer> stream =
        Streams.wrap(processor)
            .window(flushCount, flushTime, TimeUnit.SECONDS)
            .flatMap(
                new Function<Stream<Buffer>, Publisher<Buffer>>() {

                  @Override
                  public Publisher<Buffer> apply(Stream<Buffer> t) {

                    return t.reduce(
                        new Buffer(),
                        new BiFunction<Buffer, Buffer, Buffer>() {

                          @Override
                          public Buffer apply(Buffer prev, Buffer next) {
                            return prev.append(next);
                          }
                        });
                  }
                })
            .process(RingBufferWorkProcessor.<Buffer>create("gpfdist-sink-worker", 8192, false));

    HttpServer<Buffer, Buffer> httpServer =
        NetStreams.httpServer(
            new Function<HttpServerSpec<Buffer, Buffer>, HttpServerSpec<Buffer, Buffer>>() {

              @Override
              public HttpServerSpec<Buffer, Buffer> apply(HttpServerSpec<Buffer, Buffer> server) {
                return server.codec(new GpfdistCodec()).listen(port);
              }
            });

    httpServer.get(
        "/data",
        new ReactorChannelHandler<Buffer, Buffer, HttpChannel<Buffer, Buffer>>() {

          @Override
          public Publisher<Void> apply(HttpChannel<Buffer, Buffer> request) {
            request.responseHeaders().removeTransferEncodingChunked();
            request.addResponseHeader("Content-type", "text/plain");
            request.addResponseHeader("Expires", "0");
            request.addResponseHeader("X-GPFDIST-VERSION", "Spring XD");
            request.addResponseHeader("X-GP-PROTO", "1");
            request.addResponseHeader("Cache-Control", "no-cache");
            request.addResponseHeader("Connection", "close");

            return request
                .writeWith(
                    stream
                        .take(batchCount)
                        .timeout(batchTimeout, TimeUnit.SECONDS, Streams.<Buffer>empty())
                        .concatWith(Streams.just(Buffer.wrap(new byte[0]))))
                .capacity(1l);
          }
        });

    httpServer.start().awaitSuccess();
    log.info("Server running using address=[" + httpServer.getListenAddress() + "]");
    localPort = httpServer.getListenAddress().getPort();
    return httpServer;
  }
 private HandlerMethod toHandlerMethod(Publisher<?> handlerPublisher) throws InterruptedException {
   assertNotNull(handlerPublisher);
   List<?> handlerList = Streams.wrap(handlerPublisher).toList().await(5, TimeUnit.SECONDS);
   assertEquals(1, handlerList.size());
   return (HandlerMethod) handlerList.get(0);
 }
 public String toString() {
   return Long.toString(Streams.create(get()).next().poll());
 }