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());
}
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());
}
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) {
}
}
}
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());
}
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());
}
Aggregations