Search in sources :

Example 1 with ProtonNettyByteBuffer

use of org.apache.qpid.protonj2.buffer.ProtonNettyByteBuffer in project qpid-protonj2 by apache.

the class TcpTransportTest method testZeroSizedSentNoErrors.

private void testZeroSizedSentNoErrors(boolean writeAndFlush) throws Exception {
    try (NettyEchoServer server = createEchoServer()) {
        server.start();
        int port = server.getServerPort();
        Transport transport = createTransport(createTransportOptions(), createSSLOptions());
        try {
            transport.connect(HOSTNAME, port, testListener).awaitConnect();
            LOG.info("Connected to server:{}:{} as expected.", HOSTNAME, port);
        } catch (Exception e) {
            fail("Should not have failed to connect to the server at " + HOSTNAME + ":" + port + " but got exception: " + e);
        }
        assertTrue(transport.isConnected());
        if (writeAndFlush) {
            transport.writeAndFlush(new ProtonNettyByteBuffer(Unpooled.buffer(0)));
        } else {
            transport.write(new ProtonNettyByteBuffer(Unpooled.buffer(0)));
            transport.flush();
        }
        transport.close();
    }
    // Normal shutdown does not trigger the event.
    assertTrue(!transportErrored);
    assertTrue(exceptions.isEmpty());
    assertTrue(data.isEmpty());
}
Also used : ProtonNettyByteBuffer(org.apache.qpid.protonj2.buffer.ProtonNettyByteBuffer) IOException(java.io.IOException)

Example 2 with ProtonNettyByteBuffer

use of org.apache.qpid.protonj2.buffer.ProtonNettyByteBuffer in project qpid-protonj2 by apache.

the class TcpTransportTest method testMultipleConnectionsSendReceive.

@Test
public void testMultipleConnectionsSendReceive() throws Exception {
    final int CONNECTION_COUNT = 10;
    final int FRAME_SIZE = 8;
    ProtonNettyByteBuffer sendBuffer = new ProtonNettyByteBuffer(Unpooled.buffer(FRAME_SIZE));
    for (int i = 0; i < 8; ++i) {
        sendBuffer.writeByte('A');
    }
    try (NettyEchoServer server = createEchoServer()) {
        server.start();
        final int port = server.getServerPort();
        List<Transport> transports = new ArrayList<>();
        NettyIOContext context = createContext(createTransportOptions(), createSSLOptions());
        for (int i = 0; i < CONNECTION_COUNT; ++i) {
            Transport transport = context.newTransport();
            try {
                transport.connect(HOSTNAME, port, testListener).awaitConnect();
                transport.writeAndFlush(sendBuffer.copy());
                transports.add(transport);
            } catch (Exception e) {
                fail("Should not have failed to connect to the server at " + HOSTNAME + ":" + port + " but got exception: " + e);
            }
        }
        assertTrue(Wait.waitFor(new Wait.Condition() {

            @Override
            public boolean isSatisfied() throws Exception {
                LOG.debug("Checking completion: read {} expecting {}", bytesRead.get(), (FRAME_SIZE * CONNECTION_COUNT));
                return bytesRead.get() == (FRAME_SIZE * CONNECTION_COUNT);
            }
        }, 10000, 50));
        for (Transport transport : transports) {
            transport.close();
        }
    }
    assertTrue(exceptions.isEmpty());
}
Also used : ProtonNettyByteBuffer(org.apache.qpid.protonj2.buffer.ProtonNettyByteBuffer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 3 with ProtonNettyByteBuffer

use of org.apache.qpid.protonj2.buffer.ProtonNettyByteBuffer in project qpid-protonj2 by apache.

the class TcpTransportTest method testSendToClosedTransportFails.

@Test
public void testSendToClosedTransportFails() throws Exception {
    Transport transport = null;
    try (NettyEchoServer server = createEchoServer()) {
        server.start();
        int port = server.getServerPort();
        transport = createTransport(createTransportOptions(), createSSLOptions());
        try {
            transport.connect(HOSTNAME, port, testListener).awaitConnect();
            LOG.info("Connected to server:{}:{} as expected.", HOSTNAME, port);
        } catch (Exception e) {
            fail("Should not have failed to connect to the server at " + HOSTNAME + ":" + port + " but got exception: " + e);
        }
        assertTrue(transport.isConnected());
        transport.close();
        ProtonNettyByteBuffer sendBuffer = new ProtonNettyByteBuffer(Unpooled.buffer(10));
        try {
            transport.writeAndFlush(sendBuffer);
            fail("Should throw on send of closed transport");
        } catch (IOException ex) {
        }
    }
}
Also used : ProtonNettyByteBuffer(org.apache.qpid.protonj2.buffer.ProtonNettyByteBuffer) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 4 with ProtonNettyByteBuffer

use of org.apache.qpid.protonj2.buffer.ProtonNettyByteBuffer in project qpid-protonj2 by apache.

the class WebSocketTransportTest method testConnectionReceivesFragmentedData.

private void testConnectionReceivesFragmentedData(boolean writeAndFlush) throws Exception {
    final int FRAME_SIZE = 5317;
    ProtonBuffer sendBuffer = new ProtonNettyByteBuffer(Unpooled.buffer(FRAME_SIZE));
    for (int i = 0; i < FRAME_SIZE; ++i) {
        sendBuffer.writeByte('A' + (i % 10));
    }
    try (NettyEchoServer server = createEchoServer()) {
        server.setMaxFrameSize(FRAME_SIZE);
        // Server should fragment the data as it goes through
        server.setFragmentWrites(true);
        server.start();
        final int port = server.getServerPort();
        List<Transport> transports = new ArrayList<>();
        TransportOptions clientOptions = createTransportOptions();
        clientOptions.traceBytes(true);
        clientOptions.webSocketMaxFrameSize(FRAME_SIZE);
        NettyTransportListener wsListener = new NettyTransportListener(true);
        Transport transport = createTransport(clientOptions, createSSLOptions());
        try {
            transport.connect(HOSTNAME, port, wsListener).awaitConnect();
            transports.add(transport);
            if (writeAndFlush) {
                transport.writeAndFlush(ProtonByteBufferAllocator.DEFAULT.allocate());
                transport.writeAndFlush(sendBuffer.copy());
            } else {
                transport.write(ProtonByteBufferAllocator.DEFAULT.allocate());
                transport.write(sendBuffer.copy());
                transport.flush();
            }
        } catch (Exception e) {
            fail("Should have connected to the server at " + HOSTNAME + ":" + port + " but got exception: " + e);
        }
        assertTrue(transport instanceof WebSocketTransport);
        assertTrue(Wait.waitFor(new Wait.Condition() {

            @Override
            public boolean isSatisfied() throws Exception {
                LOG.debug("Checking completion: read {} expecting {}", bytesRead.get(), FRAME_SIZE);
                return bytesRead.get() == FRAME_SIZE || !transport.isConnected();
            }
        }, 10000, 50));
        assertTrue(transport.isConnected(), "Connection failed while receiving.");
        transport.close();
        assertEquals(2, data.size(), "Expected 2 data packets due to separate websocket frames");
        ProtonBuffer receivedBuffer = ProtonByteBufferAllocator.DEFAULT.allocate(FRAME_SIZE);
        for (ProtonBuffer buf : data) {
            buf.readBytes(receivedBuffer, buf.getReadableBytes());
        }
        assertEquals(FRAME_SIZE, receivedBuffer.getReadableBytes(), "Unexpected data length");
        assertEquals(sendBuffer, receivedBuffer, "Unexpected data");
    } finally {
        for (ProtonBuffer buf : data) {
            ((ByteBuf) buf.unwrap()).release();
        }
    }
    assertTrue(exceptions.isEmpty());
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) ProtonNettyByteBuffer(org.apache.qpid.protonj2.buffer.ProtonNettyByteBuffer) ArrayList(java.util.ArrayList) TransportOptions(org.apache.qpid.protonj2.client.TransportOptions) ByteBuf(io.netty.buffer.ByteBuf) IOException(java.io.IOException)

Example 5 with ProtonNettyByteBuffer

use of org.apache.qpid.protonj2.buffer.ProtonNettyByteBuffer in project qpid-protonj2 by apache.

the class WebSocketTransportTest method testConnectionsSendReceiveLargeDataWhenFrameSizeAllowsIt.

@Test
public void testConnectionsSendReceiveLargeDataWhenFrameSizeAllowsIt() throws Exception {
    final int FRAME_SIZE = 8192;
    ProtonBuffer sendBuffer = new ProtonNettyByteBuffer(Unpooled.buffer(FRAME_SIZE));
    for (int i = 0; i < FRAME_SIZE; ++i) {
        sendBuffer.writeByte('A');
    }
    try (NettyEchoServer server = createEchoServer()) {
        // Server should pass the data through without issue with this size
        server.setMaxFrameSize(FRAME_SIZE);
        server.start();
        final int port = server.getServerPort();
        List<Transport> transports = new ArrayList<>();
        Transport transport = createTransport(createTransportOptions().webSocketMaxFrameSize(FRAME_SIZE), createSSLOptions());
        try {
            // The transport should allow for the size of data we sent.
            transport.connect(HOSTNAME, port, testListener).awaitConnect();
            transports.add(transport);
            transport.writeAndFlush(sendBuffer.copy());
        } catch (Exception e) {
            fail("Should have connected to the server at " + HOSTNAME + ":" + port + " but got exception: " + e);
        }
        assertTrue(transport instanceof WebSocketTransport);
        assertTrue(Wait.waitFor(new Wait.Condition() {

            @Override
            public boolean isSatisfied() throws Exception {
                LOG.debug("Checking completion: read {} expecting {}", bytesRead.get(), FRAME_SIZE);
                return bytesRead.get() == FRAME_SIZE || !transport.isConnected();
            }
        }, 10000, 50));
        assertTrue(transport.isConnected(), "Connection failed while receiving.");
        transport.close();
    }
    assertTrue(exceptions.isEmpty());
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) ProtonNettyByteBuffer(org.apache.qpid.protonj2.buffer.ProtonNettyByteBuffer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

ProtonNettyByteBuffer (org.apache.qpid.protonj2.buffer.ProtonNettyByteBuffer)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 ProtonBuffer (org.apache.qpid.protonj2.buffer.ProtonBuffer)4 ByteBuf (io.netty.buffer.ByteBuf)2 TransportOptions (org.apache.qpid.protonj2.client.TransportOptions)1