Search in sources :

Example 1 with ProtonBuffer

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);
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) Tracker(org.apache.qpid.protonj2.client.Tracker) ClientResourceRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientResourceRemotelyClosedException) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) ClientConnectionRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException) ClientSendTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientSendTimedOutException)

Example 2 with ProtonBuffer

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();
    }
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 3 with ProtonBuffer

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());
}
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 4 with ProtonBuffer

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

Example 5 with ProtonBuffer

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;
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) Encoder(org.apache.qpid.protonj2.codec.Encoder) ProtonByteBufferAllocator(org.apache.qpid.protonj2.buffer.ProtonByteBufferAllocator)

Aggregations

ProtonBuffer (org.apache.qpid.protonj2.buffer.ProtonBuffer)775 InputStream (java.io.InputStream)572 ProtonBufferInputStream (org.apache.qpid.protonj2.buffer.ProtonBufferInputStream)572 DecodeException (org.apache.qpid.protonj2.codec.DecodeException)212 Test (org.junit.jupiter.api.Test)203 Binary (org.apache.qpid.protonj2.types.Binary)57 Symbol (org.apache.qpid.protonj2.types.Symbol)53 UUID (java.util.UUID)39 Engine (org.apache.qpid.protonj2.engine.Engine)33 Connection (org.apache.qpid.protonj2.engine.Connection)28 Session (org.apache.qpid.protonj2.engine.Session)28 ProtonTestConnector (org.apache.qpid.protonj2.test.driver.ProtonTestConnector)28 Modified (org.apache.qpid.protonj2.types.messaging.Modified)28 LinkedHashMap (java.util.LinkedHashMap)26 Random (java.util.Random)23 ErrorCondition (org.apache.qpid.protonj2.types.transport.ErrorCondition)23 Sender (org.apache.qpid.protonj2.engine.Sender)21 HashMap (java.util.HashMap)20 PrimitiveTypeDecoder (org.apache.qpid.protonj2.codec.decoders.PrimitiveTypeDecoder)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19