Search in sources :

Example 6 with ReadableBuffer

use of org.apache.qpid.proton.codec.ReadableBuffer in project vertx-proton by vert-x3.

the class ProtonReceiverImplTest method testMultiTransferMessage.

/**
 * Verifies that the receiver accepts a multi-transfer message that does
 * not exceed the link's max-message-size and delivers it to the application layer.
 */
@SuppressWarnings("unchecked")
@Test
public void testMultiTransferMessage() {
    int maxFrameSize = 512;
    int maxMessageSize = 800;
    Transport transport = mock(Transport.class);
    when(transport.getMaxFrameSize()).thenReturn(maxFrameSize);
    Connection con = mock(Connection.class);
    ProtonConnectionImpl conImpl = new ProtonConnectionImpl(mock(Vertx.class), "hostname", mock(ContextInternal.class));
    when(con.getTransport()).thenReturn(transport);
    when(con.getContext()).thenReturn(conImpl);
    Session session = mock(Session.class);
    ProtonSessionImpl sessionImpl = new ProtonSessionImpl(session);
    when(session.getConnection()).thenReturn(con);
    when(session.getContext()).thenReturn(sessionImpl);
    when(session.getIncomingCapacity()).thenReturn(10_000);
    when(session.getIncomingBytes()).thenReturn(10_000);
    Receiver r = mock(Receiver.class);
    when(r.getLocalState()).thenReturn(EndpointState.ACTIVE);
    when(r.getSession()).thenReturn(session);
    when(r.getMaxMessageSize()).thenReturn(new UnsignedLong(maxMessageSize));
    ProtonReceiverImpl recImpl = new ProtonReceiverImpl(r);
    when(r.getContext()).thenReturn(recImpl);
    ProtonMessageHandler messageHandler = mock(ProtonMessageHandler.class);
    Handler<ProtonReceiver> maxMessageSizeExceededHandler = mock(Handler.class);
    ProtonReceiverImpl receiver = new ProtonReceiverImpl(r);
    receiver.handler(messageHandler);
    receiver.maxMessageSizeExceededHandler(maxMessageSizeExceededHandler);
    byte[] encodedMessage = createEncodedMessage(maxMessageSize);
    assertTrue(encodedMessage.length > maxFrameSize);
    assertTrue(encodedMessage.length <= 2 * maxFrameSize);
    assertTrue(encodedMessage.length <= maxMessageSize);
    byte[] chunk0 = new byte[maxFrameSize];
    System.arraycopy(encodedMessage, 0, chunk0, 0, chunk0.length);
    ReadableBuffer frame0 = ReadableBuffer.ByteBufferReader.wrap(chunk0);
    Delivery delivery0 = mock(Delivery.class);
    when(delivery0.getLink()).thenReturn(r);
    when(delivery0.isPartial()).thenReturn(true);
    when(delivery0.available()).thenReturn(chunk0.length);
    when(delivery0.isSettled()).thenReturn(false);
    byte[] chunk1 = new byte[encodedMessage.length - maxFrameSize];
    System.arraycopy(encodedMessage, maxFrameSize, chunk1, 0, chunk1.length);
    ReadableBuffer frame1 = ReadableBuffer.ByteBufferReader.wrap(chunk1);
    Delivery delivery1 = mock(Delivery.class);
    when(delivery1.getLink()).thenReturn(r);
    when(delivery1.isPartial()).thenReturn(false);
    when(delivery1.available()).thenReturn(chunk1.length);
    when(delivery1.isSettled()).thenReturn(false);
    when(r.current()).thenReturn(delivery0, delivery1);
    when(r.recv()).thenReturn(frame0, frame1);
    // WHEN delivering the message in two consecutive transfer frames
    receiver.onDelivery();
    receiver.onDelivery();
    // THEN the disposition in reply to the second transfer has state accepted
    verify(delivery1).disposition(any(Accepted.class));
    // and the receiver has been advanced to the next delivery
    verify(r).advance();
    // and the sender has been issued a new credit
    verify(r).flow(1);
    // and the message has been delivered to the application layer
    verify(messageHandler).handle(any(ProtonDelivery.class), any(Message.class));
    // and the link is not being closed
    verify(r, never()).close();
    verify(maxMessageSizeExceededHandler, never()).handle(any(ProtonReceiver.class));
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) Connection(org.apache.qpid.proton.engine.Connection) ProtonReceiver(io.vertx.proton.ProtonReceiver) Receiver(org.apache.qpid.proton.engine.Receiver) ContextInternal(io.vertx.core.impl.ContextInternal) Vertx(io.vertx.core.Vertx) ReadableBuffer(org.apache.qpid.proton.codec.ReadableBuffer) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Delivery(org.apache.qpid.proton.engine.Delivery) ProtonDelivery(io.vertx.proton.ProtonDelivery) Transport(org.apache.qpid.proton.engine.Transport) Session(org.apache.qpid.proton.engine.Session) Test(org.junit.Test)

Example 7 with ReadableBuffer

use of org.apache.qpid.proton.codec.ReadableBuffer in project vertx-proton by vert-x3.

the class ProtonReceiverImplTest method testSingleTransferExceedingMaxMessageSizeResultsInLinkBeingDetached.

/**
 * Verifies that the receiver detaches the link with an
 * amqp:link:message-size-exceeded error code when receiving a single-transfer message that
 * exceeds the link's max-message-size.
 */
@Test
public void testSingleTransferExceedingMaxMessageSizeResultsInLinkBeingDetached() {
    int maxFrameSize = 800;
    long maxMessageSize = 500;
    Transport transport = mock(Transport.class);
    when(transport.getMaxFrameSize()).thenReturn(maxFrameSize);
    Connection con = mock(Connection.class);
    ProtonConnectionImpl conImpl = new ProtonConnectionImpl(mock(Vertx.class), "hostname", mock(ContextInternal.class));
    when(con.getTransport()).thenReturn(transport);
    when(con.getContext()).thenReturn(conImpl);
    Session session = mock(Session.class);
    ProtonSessionImpl sessionImpl = new ProtonSessionImpl(session);
    when(session.getConnection()).thenReturn(con);
    when(session.getContext()).thenReturn(sessionImpl);
    when(session.getIncomingCapacity()).thenReturn(10_000);
    when(session.getIncomingBytes()).thenReturn(10_000);
    Receiver r = mock(Receiver.class);
    when(r.getLocalState()).thenReturn(EndpointState.ACTIVE);
    when(r.getSession()).thenReturn(session);
    when(r.getMaxMessageSize()).thenReturn(new UnsignedLong(maxMessageSize));
    ProtonReceiverImpl recImpl = new ProtonReceiverImpl(r);
    when(r.getContext()).thenReturn(recImpl);
    ProtonMessageHandler messageHandler = mock(ProtonMessageHandler.class);
    ProtonReceiverImpl receiver = new ProtonReceiverImpl(r);
    receiver.handler(messageHandler);
    byte[] encodedMessage = createEncodedMessage(700);
    assertTrue(encodedMessage.length <= maxFrameSize);
    assertTrue(encodedMessage.length > maxMessageSize);
    ReadableBuffer frame0 = ReadableBuffer.ByteBufferReader.wrap(encodedMessage);
    Delivery delivery0 = mock(Delivery.class);
    when(delivery0.getLink()).thenReturn(r);
    when(delivery0.isPartial()).thenReturn(false);
    when(delivery0.available()).thenReturn(encodedMessage.length);
    when(delivery0.isSettled()).thenReturn(false);
    when(r.current()).thenReturn(delivery0);
    when(r.recv()).thenReturn(frame0);
    // WHEN delivering the message in a single transfer frame
    receiver.onDelivery();
    // THEN the receiver is being detached with the expected error condition
    verify(r).detach();
    ArgumentCaptor<ErrorCondition> conditionCapture = ArgumentCaptor.forClass(ErrorCondition.class);
    verify(r).setCondition(conditionCapture.capture());
    ErrorCondition errorCondition = conditionCapture.getValue();
    assertNotNull(errorCondition);
    assertEquals("Unxpected error condition", LinkError.MESSAGE_SIZE_EXCEEDED, errorCondition.getCondition());
    // and the message is not being delivered to the application layer
    verify(messageHandler, never()).handle(any(ProtonDelivery.class), any(Message.class));
}
Also used : UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Connection(org.apache.qpid.proton.engine.Connection) ProtonReceiver(io.vertx.proton.ProtonReceiver) Receiver(org.apache.qpid.proton.engine.Receiver) ContextInternal(io.vertx.core.impl.ContextInternal) Vertx(io.vertx.core.Vertx) ReadableBuffer(org.apache.qpid.proton.codec.ReadableBuffer) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Delivery(org.apache.qpid.proton.engine.Delivery) ProtonDelivery(io.vertx.proton.ProtonDelivery) Transport(org.apache.qpid.proton.engine.Transport) Session(org.apache.qpid.proton.engine.Session) Test(org.junit.Test)

Example 8 with ReadableBuffer

use of org.apache.qpid.proton.codec.ReadableBuffer in project vertx-proton by vert-x3.

the class ProtonReceiverImplTest method testMultiTransferExceedingMaxMessageSizeWithMaxMessageSizeExceededHandler.

/**
 * Verifies that the receivers maxMessageSizeExceeded closes the link with an
 * when receiving a multi-transfer message that exceeds the link's max-message-size.
 */
@SuppressWarnings("unchecked")
@Test
public void testMultiTransferExceedingMaxMessageSizeWithMaxMessageSizeExceededHandler() {
    int maxFrameSize = 512;
    long maxMessageSize = 650;
    Transport transport = mock(Transport.class);
    when(transport.getMaxFrameSize()).thenReturn(maxFrameSize);
    Connection con = mock(Connection.class);
    ProtonConnectionImpl conImpl = new ProtonConnectionImpl(mock(Vertx.class), "hostname", mock(ContextInternal.class));
    when(con.getTransport()).thenReturn(transport);
    when(con.getContext()).thenReturn(conImpl);
    Session session = mock(Session.class);
    ProtonSessionImpl sessionImpl = new ProtonSessionImpl(session);
    when(session.getConnection()).thenReturn(con);
    when(session.getContext()).thenReturn(sessionImpl);
    when(session.getIncomingCapacity()).thenReturn(10_000);
    when(session.getIncomingBytes()).thenReturn(10_000);
    Receiver r = mock(Receiver.class);
    when(r.getLocalState()).thenReturn(EndpointState.ACTIVE);
    when(r.getSession()).thenReturn(session);
    when(r.getMaxMessageSize()).thenReturn(new UnsignedLong(maxMessageSize));
    ProtonReceiverImpl recImpl = new ProtonReceiverImpl(r);
    when(r.getContext()).thenReturn(recImpl);
    ProtonMessageHandler messageHandler = mock(ProtonMessageHandler.class);
    Handler<ProtonReceiver> maxMessageSizeExceededHandler = mock(Handler.class);
    doAnswer(invocation -> {
        // Contrary to the default behaviour of detaching, the handler will close the link manually.
        ProtonReceiver receiver = invocation.getArgument(0);
        receiver.close();
        when(r.getLocalState()).thenReturn(EndpointState.CLOSED);
        return null;
    }).when(maxMessageSizeExceededHandler).handle(any(ProtonReceiver.class));
    ProtonReceiverImpl receiver = new ProtonReceiverImpl(r);
    receiver.handler(messageHandler);
    receiver.maxMessageSizeExceededHandler(maxMessageSizeExceededHandler);
    byte[] encodedMessage = createEncodedMessage(700);
    assertTrue(encodedMessage.length > maxFrameSize);
    assertTrue(encodedMessage.length <= 2 * maxFrameSize);
    assertTrue(encodedMessage.length > maxMessageSize);
    byte[] chunk0 = new byte[maxFrameSize];
    System.arraycopy(encodedMessage, 0, chunk0, 0, chunk0.length);
    ReadableBuffer frame0 = ReadableBuffer.ByteBufferReader.wrap(chunk0);
    Delivery delivery0 = mock(Delivery.class);
    when(delivery0.getLink()).thenReturn(r);
    when(delivery0.isPartial()).thenReturn(true);
    when(delivery0.available()).thenReturn(chunk0.length);
    when(delivery0.isSettled()).thenReturn(false);
    byte[] chunk1 = new byte[encodedMessage.length - maxFrameSize];
    System.arraycopy(encodedMessage, maxFrameSize, chunk1, 0, chunk1.length);
    ReadableBuffer frame1 = ReadableBuffer.ByteBufferReader.wrap(chunk1);
    Delivery delivery1 = mock(Delivery.class);
    when(delivery1.getLink()).thenReturn(r);
    when(delivery1.isPartial()).thenReturn(false);
    when(delivery1.available()).thenReturn(chunk1.length);
    when(delivery1.isSettled()).thenReturn(false);
    when(r.current()).thenReturn(delivery0, delivery1);
    when(r.recv()).thenReturn(frame0, frame1);
    // WHEN delivering the message in two consecutive transfer frames
    receiver.onDelivery();
    receiver.onDelivery();
    // THEN the maxMessageSizeExceeded handler is being invoked
    verify(maxMessageSizeExceededHandler).handle(receiver);
    // and the receiver has been closed by the maxMessageSizeExceeded handler
    verify(r).close();
    verify(r, never()).detach();
    // and the message is not being delivered to the application layer
    verify(messageHandler, never()).handle(any(ProtonDelivery.class), any(Message.class));
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) Connection(org.apache.qpid.proton.engine.Connection) ProtonReceiver(io.vertx.proton.ProtonReceiver) Receiver(org.apache.qpid.proton.engine.Receiver) ContextInternal(io.vertx.core.impl.ContextInternal) Vertx(io.vertx.core.Vertx) ReadableBuffer(org.apache.qpid.proton.codec.ReadableBuffer) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Delivery(org.apache.qpid.proton.engine.Delivery) ProtonDelivery(io.vertx.proton.ProtonDelivery) Transport(org.apache.qpid.proton.engine.Transport) Session(org.apache.qpid.proton.engine.Session) Test(org.junit.Test)

Example 9 with ReadableBuffer

use of org.apache.qpid.proton.codec.ReadableBuffer in project vertx-proton by vert-x3.

the class ProtonReadableBufferImplTest method testSlice.

@Test
public void testSlice() {
    byte[] data = new byte[] { 0, 1, 2, 3, 4 };
    ByteBuf byteBuffer = Unpooled.wrappedBuffer(data);
    ProtonReadableBufferImpl buffer = new ProtonReadableBufferImpl(byteBuffer);
    ReadableBuffer slice = buffer.slice();
    for (int i = 0; i < data.length; i++) {
        assertEquals(data[i], slice.get());
    }
    assertFalse(slice.hasRemaining());
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) ReadableBuffer(org.apache.qpid.proton.codec.ReadableBuffer) Test(org.junit.Test)

Example 10 with ReadableBuffer

use of org.apache.qpid.proton.codec.ReadableBuffer in project vertx-proton by vert-x3.

the class ProtonWritableBufferImplTest method doPutReadableBufferTestImpl.

private void doPutReadableBufferTestImpl(boolean readOnly) {
    ByteBuffer buf = ByteBuffer.allocate(1024);
    buf.put((byte) 1);
    buf.flip();
    if (readOnly) {
        buf = buf.asReadOnlyBuffer();
    }
    ReadableBuffer input = new ReadableBuffer.ByteBufferReader(buf);
    if (readOnly) {
        assertFalse("Expected buffer not to hasArray()", input.hasArray());
    } else {
        assertTrue("Expected buffer to hasArray()", input.hasArray());
    }
    ByteBuf buffer = Unpooled.buffer(1024);
    ProtonWritableBufferImpl writable = new ProtonWritableBufferImpl(buffer);
    assertEquals(0, writable.position());
    writable.put(input);
    assertEquals(1, writable.position());
}
Also used : ReadableBuffer(org.apache.qpid.proton.codec.ReadableBuffer) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ReadableBuffer (org.apache.qpid.proton.codec.ReadableBuffer)14 UnsignedLong (org.apache.qpid.proton.amqp.UnsignedLong)7 Delivery (org.apache.qpid.proton.engine.Delivery)5 Message (org.apache.qpid.proton.message.Message)5 Test (org.junit.Test)5 ProtonDelivery (io.vertx.proton.ProtonDelivery)4 ProtonReceiver (io.vertx.proton.ProtonReceiver)4 Symbol (org.apache.qpid.proton.amqp.Symbol)4 Target (org.apache.qpid.proton.amqp.messaging.Target)4 Receiver (org.apache.qpid.proton.engine.Receiver)4 ByteBuf (io.netty.buffer.ByteBuf)3 Vertx (io.vertx.core.Vertx)3 ContextInternal (io.vertx.core.impl.ContextInternal)3 ProtonMessageHandler (io.vertx.proton.ProtonMessageHandler)3 ErrorCondition (org.apache.qpid.proton.amqp.transport.ErrorCondition)3 CompositeReadableBuffer (org.apache.qpid.proton.codec.CompositeReadableBuffer)3 WritableBuffer (org.apache.qpid.proton.codec.WritableBuffer)3 Connection (org.apache.qpid.proton.engine.Connection)3 Session (org.apache.qpid.proton.engine.Session)3 Transport (org.apache.qpid.proton.engine.Transport)3