Example usage for io.netty.channel.embedded EmbeddedChannel flush

List of usage examples for io.netty.channel.embedded EmbeddedChannel flush

Introduction

In this page you can find the example usage for io.netty.channel.embedded EmbeddedChannel flush.

Prototype

@Override
    public Channel flush() 

Source Link

Usage

From source file:com.cloudera.livy.client.local.rpc.TestRpc.java

License:Apache License

private void transfer(Rpc serverRpc, Rpc clientRpc) {
    EmbeddedChannel client = (EmbeddedChannel) clientRpc.getChannel();
    EmbeddedChannel server = (EmbeddedChannel) serverRpc.getChannel();

    int count = 0;
    while (!client.outboundMessages().isEmpty()) {
        server.writeInbound(client.readOutbound());
        count++;// w ww.j av a2s .  co m
    }
    server.flush();
    LOG.debug("Transferred {} outbound client messages.", count);

    count = 0;
    while (!server.outboundMessages().isEmpty()) {
        client.writeInbound(server.readOutbound());
        count++;
    }
    client.flush();
    LOG.debug("Transferred {} outbound server messages.", count);
}

From source file:io.crate.protocols.postgres.MessagesTest.java

License:Apache License

@Test
public void testNullValuesAddToLength() throws Exception {
    EmbeddedChannel channel = new EmbeddedChannel();
    Messages.sendDataRow(channel, new RowN($(10, null)), Arrays.asList(DataTypes.INTEGER, DataTypes.STRING),
            null);//from   w w  w.j a v  a2  s . c o  m
    channel.flush();
    ByteBuf buffer = channel.readOutbound();

    // message type
    assertThat((char) buffer.readByte(), is('D'));

    // size of the message
    assertThat(buffer.readInt(), is(16));
    assertThat(buffer.readableBytes(), is(12)); // 16 - INT4 because the size was already read
}

From source file:io.moquette.spi.impl.NettyChannelAssertions.java

License:Open Source License

static void assertConnAckAccepted(EmbeddedChannel channel) {
    channel.flush();
    assertEqualsConnAck(CONNECTION_ACCEPTED, channel.readOutbound());
}

From source file:io.moquette.spi.impl.ProtocolProcessorTest.java

License:Open Source License

@Test
public void testPublishToMultipleSubscribers() throws InterruptedException {
    final Subscription subscription = new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC,
            AbstractMessage.QOSType.MOST_ONE);
    final Subscription subscriptionClient2 = new Subscription(FAKE_CLIENT_ID2, FAKE_TOPIC,
            AbstractMessage.QOSType.MOST_ONE);

    //subscriptions.matches(topic) redefine the method to return true
    SubscriptionsStore subs = new SubscriptionsStore() {
        @Override/* w  w w  .j  av a  2  s .  c  o m*/
        public List<Subscription> matches(String topic) {
            if (topic.equals(FAKE_TOPIC)) {
                return Arrays.asList(subscription, subscriptionClient2);
            } else {
                throw new IllegalArgumentException("Expected " + FAKE_TOPIC + " buf found " + topic);
            }
        }
    };

    //simulate a connect that register a clientID to an IoSession
    MemoryStorageService storageService = new MemoryStorageService();
    storageService.initStore();
    subs.init(storageService.sessionsStore());
    m_processor.init(subs, m_messagesStore, m_sessionStore, null, true, new PermitAllAuthorizator(),
            NO_OBSERVERS_INTERCEPTOR);

    EmbeddedChannel firstReceiverChannel = new EmbeddedChannel();
    ConnectMessage connectMessage = new ConnectMessage();
    connectMessage.setProtocolVersion((byte) 3);
    connectMessage.setClientID(FAKE_CLIENT_ID);
    connectMessage.setCleanSession(true);
    m_processor.processConnect(firstReceiverChannel, connectMessage);
    assertConnAckAccepted(firstReceiverChannel);

    //connect the second fake subscriber
    EmbeddedChannel secondReceiverChannel = new EmbeddedChannel();
    ConnectMessage connectMessage2 = new ConnectMessage();
    connectMessage2.setProtocolVersion((byte) 3);
    connectMessage2.setClientID(FAKE_CLIENT_ID2);
    connectMessage2.setCleanSession(true);
    connectMessage2.setCleanSession(true);
    m_processor.processConnect(secondReceiverChannel, connectMessage2);
    assertConnAckAccepted(secondReceiverChannel);

    //Exercise
    ByteBuffer buffer = ByteBuffer.allocate(5).put("Hello".getBytes());
    buffer.rewind();
    PublishMessage msg = new PublishMessage();
    msg.setTopicName(FAKE_TOPIC);
    msg.setQos(QOSType.MOST_ONE);
    msg.setPayload(buffer);
    msg.setRetainFlag(false);
    NettyUtils.userName(m_channel, "FakeCLI");
    m_processor.processPublish(m_channel, msg);

    //Verify
    firstReceiverChannel.flush();
    PublishMessage pub2FirstSubscriber = (PublishMessage) firstReceiverChannel.readOutbound();
    assertNotNull(pub2FirstSubscriber);
    String firstMessageContent = DebugUtils.payload2Str(pub2FirstSubscriber.getPayload());
    assertEquals("Hello", firstMessageContent);

    secondReceiverChannel.flush();
    PublishMessage pub2SecondSubscriber = (PublishMessage) secondReceiverChannel.readOutbound();
    assertNotNull(pub2SecondSubscriber);
    String secondMessageContent = DebugUtils.payload2Str(pub2SecondSubscriber.getPayload());
    assertEquals("Hello", secondMessageContent);
}

From source file:nats.codec.ClientFrameRecodeTest.java

License:Open Source License

private <T extends NatsFrame> T recode(T frame) {
    final EmbeddedChannel channel = new EmbeddedChannel(new ClientFrameEncoder(), new ClientFrameDecoder());

    // Encode/*from   w  w  w  .j  av  a  2 s.  c om*/
    channel.write(frame);
    channel.flush();
    channel.checkException();
    final ByteBuf data = (ByteBuf) channel.readOutbound();

    // Decode
    channel.writeInbound(data);
    channel.checkException();
    final T recodedFrame = (T) channel.readInbound();

    // Ensure we got a frame
    assertNotNull(recodedFrame);

    return recodedFrame;
}

From source file:nats.codec.ServerFrameRecodeTest.java

License:Open Source License

protected <T extends NatsFrame> T recode(T frame) {
    final EmbeddedChannel channel = new EmbeddedChannel(new ServerFrameEncoder(), new ServerFrameDecoder());

    // Encode/*w  w  w  . j a v a  2  s  . co  m*/
    channel.write(frame);
    channel.flush();
    channel.checkException();
    final ByteBuf data = (ByteBuf) channel.readOutbound();

    // Decode
    channel.writeInbound(data);
    channel.checkException();
    final T recodedFrame = (T) channel.readInbound();

    // Ensure we got a frame
    assertNotNull(recodedFrame);

    return recodedFrame;
}

From source file:org.elasticsearch.http.netty4.Netty4HttpPipeliningHandlerTests.java

License:Apache License

public void testThatPipeliningWorksWithFastSerializedRequests() throws InterruptedException {
    final int numberOfRequests = randomIntBetween(2, 128);
    final EmbeddedChannel embeddedChannel = new EmbeddedChannel(
            new Netty4HttpPipeliningHandler(logger, numberOfRequests), new WorkEmulatorHandler());

    for (int i = 0; i < numberOfRequests; i++) {
        embeddedChannel.writeInbound(createHttpRequest("/" + String.valueOf(i)));
    }/*from ww w.j a va2  s  . c  o  m*/

    final List<CountDownLatch> latches = new ArrayList<>();
    for (final String url : waitingRequests.keySet()) {
        latches.add(finishRequest(url));
    }

    for (final CountDownLatch latch : latches) {
        latch.await();
    }

    embeddedChannel.flush();

    for (int i = 0; i < numberOfRequests; i++) {
        assertReadHttpMessageHasContent(embeddedChannel, String.valueOf(i));
    }

    assertTrue(embeddedChannel.isOpen());
}

From source file:org.elasticsearch.http.netty4.Netty4HttpPipeliningHandlerTests.java

License:Apache License

public void testThatPipeliningWorksWhenSlowRequestsInDifferentOrder() throws InterruptedException {
    final int numberOfRequests = randomIntBetween(2, 128);
    final EmbeddedChannel embeddedChannel = new EmbeddedChannel(
            new Netty4HttpPipeliningHandler(logger, numberOfRequests), new WorkEmulatorHandler());

    for (int i = 0; i < numberOfRequests; i++) {
        embeddedChannel.writeInbound(createHttpRequest("/" + String.valueOf(i)));
    }/* w w  w .  jav  a  2 s  .com*/

    // random order execution
    final List<String> urls = new ArrayList<>(waitingRequests.keySet());
    Randomness.shuffle(urls);
    final List<CountDownLatch> latches = new ArrayList<>();
    for (final String url : urls) {
        latches.add(finishRequest(url));
    }

    for (final CountDownLatch latch : latches) {
        latch.await();
    }

    embeddedChannel.flush();

    for (int i = 0; i < numberOfRequests; i++) {
        assertReadHttpMessageHasContent(embeddedChannel, String.valueOf(i));
    }

    assertTrue(embeddedChannel.isOpen());
}

From source file:org.elasticsearch.http.netty4.Netty4HttpPipeliningHandlerTests.java

License:Apache License

public void testThatPipeliningClosesConnectionWithTooManyEvents() throws InterruptedException {
    final int numberOfRequests = randomIntBetween(2, 128);
    final EmbeddedChannel embeddedChannel = new EmbeddedChannel(
            new Netty4HttpPipeliningHandler(logger, numberOfRequests), new WorkEmulatorHandler());

    for (int i = 0; i < 1 + numberOfRequests + 1; i++) {
        embeddedChannel.writeInbound(createHttpRequest("/" + Integer.toString(i)));
    }//from   w  w w .j a v  a  2  s  . c  om

    final List<CountDownLatch> latches = new ArrayList<>();
    final List<Integer> requests = IntStream.range(1, numberOfRequests + 1).boxed()
            .collect(Collectors.toList());
    Randomness.shuffle(requests);

    for (final Integer request : requests) {
        latches.add(finishRequest(request.toString()));
    }

    for (final CountDownLatch latch : latches) {
        latch.await();
    }

    finishRequest(Integer.toString(numberOfRequests + 1)).await();

    embeddedChannel.flush();

    assertFalse(embeddedChannel.isOpen());
}

From source file:org.elasticsearch.http.netty4.pipelining.Netty4HttpPipeliningHandlerTests.java

License:Apache License

public void testThatPipeliningWorksWithFastSerializedRequests() throws InterruptedException {
    final int numberOfRequests = randomIntBetween(2, 128);
    final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new HttpPipeliningHandler(numberOfRequests),
            new WorkEmulatorHandler());

    for (int i = 0; i < numberOfRequests; i++) {
        embeddedChannel.writeInbound(createHttpRequest("/" + String.valueOf(i)));
    }//from   www .j a va2 s  . c  om

    final List<CountDownLatch> latches = new ArrayList<>();
    for (final String url : waitingRequests.keySet()) {
        latches.add(finishRequest(url));
    }

    for (final CountDownLatch latch : latches) {
        latch.await();
    }

    embeddedChannel.flush();

    for (int i = 0; i < numberOfRequests; i++) {
        assertReadHttpMessageHasContent(embeddedChannel, String.valueOf(i));
    }

    assertTrue(embeddedChannel.isOpen());
}