@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());
 }
 @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));
 }
Пример #4
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));
  }