use of org.apache.qpid.protonj2.buffer.ProtonBuffer in project qpid-protonj2 by apache.
the class ClientSender method sendMessage.
private Tracker sendMessage(AdvancedMessage<?> message, Map<String, Object> deliveryAnnotations, boolean waitForCredit) throws ClientException {
final ClientFuture<Tracker> operation = session.getFutureFactory().createFuture();
final ProtonBuffer buffer = message.encode(deliveryAnnotations);
executor.execute(() -> {
if (notClosedOrFailed(operation)) {
try {
final ClientOutgoingEnvelope envelope = new ClientOutgoingEnvelope(this, message.messageFormat(), buffer, operation);
if (protonSender.isSendable() && protonSender.current() == null) {
session.getTransactionContext().send(envelope, null, protonSender.getSenderSettleMode() == SenderSettleMode.SETTLED);
} else if (waitForCredit) {
addToTailOfBlockedQueue(envelope);
} else {
operation.complete(null);
}
} catch (Exception error) {
operation.failed(ClientExceptionSupport.createNonFatalOrPassthrough(error));
}
}
});
return session.request(this, operation);
}
use of org.apache.qpid.protonj2.buffer.ProtonBuffer in project qpid-protonj2 by apache.
the class TcpTransportTest method testSendToClosedTransportFailsButDoesNotLeak.
@Disabled("Used for checking for transport level leaks, my be unstable on CI.")
@Test
public void testSendToClosedTransportFailsButDoesNotLeak() throws Exception {
Transport transport = null;
ResourceLeakDetector.setLevel(Level.PARANOID);
try (NettyEchoServer server = createEchoServer()) {
server.start();
int port = server.getServerPort();
for (int i = 0; i < 256; ++i) {
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());
ProtonBuffer sendBuffer = transport.getBufferAllocator().outputBuffer(10 * 1024 * 1024);
sendBuffer.writeBytes(new byte[] { 0, 1, 2, 3, 4 });
transport.close();
try {
transport.writeAndFlush(sendBuffer);
fail("Should throw on send of closed transport");
} catch (IOException ex) {
}
}
System.gc();
}
}
use of org.apache.qpid.protonj2.buffer.ProtonBuffer 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.ProtonBuffer 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());
}
use of org.apache.qpid.protonj2.buffer.ProtonBuffer in project qpid-protonj2 by apache.
the class ImperativeClientTestCase method createEncodedMessage.
protected byte[] createEncodedMessage(Section<?>... body) {
Encoder encoder = CodecFactory.getEncoder();
ProtonBuffer buffer = new ProtonByteBufferAllocator().allocate();
for (Section<?> section : body) {
encoder.writeObject(buffer, encoder.newEncoderState(), section);
}
byte[] result = new byte[buffer.getReadableBytes()];
buffer.readBytes(result);
return result;
}
Aggregations