org.apache.qpid.proton.framing.TransportFrame Java Examples

The following examples show how to use org.apache.qpid.proton.framing.TransportFrame. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransportImpl.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public boolean handleFrame(TransportFrame frame)
{
    if (!isHandlingFrames())
    {
        throw new IllegalStateException("Transport cannot accept frame: " + frame);
    }

    log(INCOMING, frame);

    ProtocolTracer tracer = _protocolTracer.get();
    if( tracer != null )
    {
        tracer.receivedFrame(frame);
    }

    frame.getBody().invoke(this,frame.getPayload(), frame.getChannel());
    return _closeReceived;
}
 
Example #2
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testProtocolTracingLogsFrameToSystem()
{
    Connection connection = new ConnectionImpl();
    TransportImpl spy = spy(_transport);

    assertTrue(spy.isHandlingFrames());
    spy.bind(connection);

    assertTrue(spy.isHandlingFrames());
    spy.handleFrame(TRANSPORT_FRAME_OPEN);
    assertTrue(spy.isHandlingFrames());

    ArgumentCaptor<TransportFrame> frameCatcher = ArgumentCaptor.forClass(TransportFrame.class);
    Mockito.verify(spy).log(eq(TransportImpl.INCOMING), frameCatcher.capture());

    assertEquals(TRANSPORT_FRAME_OPEN.getChannel(), frameCatcher.getValue().getChannel());
    assertTrue(frameCatcher.getValue().getBody() instanceof Open);
    assertNull(frameCatcher.getValue().getPayload());
}
 
Example #3
Source File: FrameWriterTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testFrameWriterLogsFramesToSystem() {
    transport.trace(2);
    TransportImpl spy = Mockito.spy(transport);

    Transfer transfer = createTransfer();
    FrameWriter framer = new FrameWriter(encoder, Integer.MAX_VALUE, (byte) 0, spy);

    int channel = 16;
    int payloadLength = littlePayload.capacity();

    framer.writeFrame(channel, transfer, littlePayload, new PartialTransferHandler(transfer));

    ArgumentCaptor<TransportFrame> frameCatcher = ArgumentCaptor.forClass(TransportFrame.class);
    Mockito.verify(spy).log(eq(TransportImpl.OUTGOING), frameCatcher.capture());

    assertEquals(channel, frameCatcher.getValue().getChannel());
    assertTrue(frameCatcher.getValue().getBody() instanceof Transfer);

    Binary payload = frameCatcher.getValue().getPayload();

    assertEquals(payloadLength, payload.getLength());
}
 
Example #4
Source File: FrameParserTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(TransportFrame transportFrame)
{
    if(transportFrame == null)
    {
        return false;
    }

    FrameBody actualFrame = transportFrame.getBody();

    int _expectedChannel = _expectedTransportFrame.getChannel();
    FrameBody expectedFrame = _expectedTransportFrame.getBody();

    return _expectedChannel == transportFrame.getChannel()
            && expectedFrame.getClass().equals(actualFrame.getClass());
}
 
Example #5
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
private void handlePartialTransfer(TransportImpl transport, UnsignedInteger handle, UnsignedInteger deliveryId, String deliveryTag, byte[] partialPayload, boolean more, boolean aborted, Boolean settled)
{
    byte[] tag = deliveryTag.getBytes(StandardCharsets.UTF_8);

    Transfer transfer = new Transfer();
    transfer.setHandle(handle);
    transfer.setDeliveryTag(new Binary(tag));
    transfer.setMessageFormat(UnsignedInteger.valueOf(DeliveryImpl.DEFAULT_MESSAGE_FORMAT));
    transfer.setMore(more);
    transfer.setAborted(aborted);
    if(deliveryId != null) {
        // Can be omitted in continuation frames for a given delivery.
        transfer.setDeliveryId(deliveryId);
    }
    if(settled != null) {
        transfer.setSettled(settled);
    }

    transport.handleFrame(new TransportFrame(0, transfer, new Binary(partialPayload, 0, partialPayload.length)));
}
 
Example #6
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
private void handleTransfer(TransportImpl transport, int deliveryNumber, String deliveryTag, String messageContent)
{
    byte[] tag = deliveryTag.getBytes(StandardCharsets.UTF_8);

    Message m = Message.Factory.create();
    m.setBody(new AmqpValue(messageContent));

    byte[] encoded = new byte[BUFFER_SIZE];
    int len = m.encode(encoded, 0, BUFFER_SIZE);

    assertTrue("given array was too small", len < BUFFER_SIZE);

    Transfer transfer = new Transfer();
    transfer.setDeliveryId(UnsignedInteger.valueOf(deliveryNumber));
    transfer.setHandle(UnsignedInteger.ZERO);
    transfer.setDeliveryTag(new Binary(tag));
    transfer.setMessageFormat(UnsignedInteger.valueOf(DeliveryImpl.DEFAULT_MESSAGE_FORMAT));

    transport.handleFrame(new TransportFrame(0, transfer, new Binary(encoded, 0, len)));
}
 
Example #7
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
private void prepareAndOpenConnection(MockTransportImpl transport, Connection connection) {
    transport.bind(connection);
    connection.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 1, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);

    // Give the necessary response to open
    transport.handleFrame(new TransportFrame(0, new Open(), null));
}
 
Example #8
Source File: AmqpProtocolTracer.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public void sentFrame(TransportFrame transportFrame) {
   if (connection.isTraceFrames()) {
      TRACE_FRAMES.trace("{} | SENT: {}", connection.getRemoteURI(), transportFrame.getBody());
   }

   AmqpFrameValidator inspector = connection.getSentFrameInspector();
   if (inspector != null) {
      transportFrame.getBody().invoke(this, transportFrame.getPayload(), inspector);
   }
}
 
Example #9
Source File: AmqpProtocolTracer.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public void receivedFrame(TransportFrame transportFrame) {
   if (connection.isTraceFrames()) {
      TRACE_FRAMES.trace("{} | RECV: {}", connection.getRemoteURI(), transportFrame.getBody());
   }

   AmqpFrameValidator inspector = connection.getReceivedFrameInspector();
   if (inspector != null) {
      transportFrame.getBody().invoke(this, transportFrame.getPayload(), inspector);
   }
}
 
Example #10
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testTickRemoteTimeout()
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    int timeout = 4000;
    Open open = new Open();
    open.setIdleTimeOut(new UnsignedInteger(4000));
    TransportFrame openFrame = new TransportFrame(CHANNEL_ID, open, null);
    transport.handleFrame(openFrame);
    pumpMockTransport(transport);

    long deadline = transport.tick(0);
    assertEquals("Expected to be returned a deadline of 2000",  2000, deadline);  // deadline = 4000 / 2

    deadline = transport.tick(1000);    // Wait for less than the deadline with no data - get the same value
    assertEquals("When the deadline hasn't been reached tick() should return the previous deadline",  2000, deadline);
    assertEquals("When the deadline hasn't been reached tick() shouldn't write data", 0, transport.writes.size());

    deadline = transport.tick(timeout/2); // Wait for the deadline - next deadline should be (4000/2)*2
    assertEquals("When the deadline has been reached expected a new deadline to be returned 4000",  4000, deadline);
    assertEquals("tick() should have written data", 1, transport.writes.size());
    assertEquals("tick() should have written an empty frame", null, transport.writes.get(0));

    transport.writeFrame(CHANNEL_ID, new Begin(), null, null);
    while(transport.pending() > 0) transport.pop(transport.head().remaining());
    int framesWrittenBeforeTick = transport.writes.size();
    deadline = transport.tick(3000);
    assertEquals("Writing data resets the deadline",  5000, deadline);
    assertEquals("When the deadline is reset tick() shouldn't write an empty frame", 0, transport.writes.size() - framesWrittenBeforeTick);

    transport.writeFrame(CHANNEL_ID, new Attach(), null, null);
    assertTrue(transport.pending() > 0);
    framesWrittenBeforeTick = transport.writes.size();
    deadline = transport.tick(4000);
    assertEquals("Having pending data does not reset the deadline",  5000, deadline);
    assertEquals("Having pending data prevents tick() from sending an empty frame", 0, transport.writes.size() - framesWrittenBeforeTick);
}
 
Example #11
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that no Begin frame is emitted by the Transport should a Session open
 * after the Close frame was sent.
 */
@Test
public void testSessionBeginAfterCloseSent()
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

    Session session = connection.session();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 1, transport.writes.size());

    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);

    // Send the necessary response to Open
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 1, transport.writes.size());

    // Cause a Close frame to be sent
    connection.close();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Close);

    // Open the session and verify the transport doesn't
    // send any Begin frame, as a Close frame was sent already.
    session.open();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());
}
 
Example #12
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private void doInvalidTransferProvokesDecodeErrorTestImpl(byte[] bytes, String description) {
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();

    Collector collector = Collector.Factory.create();
    connection.collect(collector);

    transport.bind(connection);
    connection.open();

    Session session = connection.session();
    session.open();

    String linkName = "myReceiver";
    Receiver receiver = session.receiver(linkName);
    receiver.flow(5);
    receiver.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);
    assertTrue("Unexpected frame type", transport.writes.get(2) instanceof Attach);
    assertTrue("Unexpected frame type", transport.writes.get(3) instanceof Flow);

    // Provide the response bytes for the header
    transport.tail().put(AmqpHeader.HEADER);
    transport.process();

    // Send the necessary response to Open/Begin/Attach
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    begin.setNextOutgoingId(UnsignedInteger.ONE);
    begin.setIncomingWindow(UnsignedInteger.valueOf(1024));
    begin.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    transport.handleFrame(new TransportFrame(0, begin, null));

    Attach attach = new Attach();
    attach.setHandle(UnsignedInteger.ZERO);
    attach.setRole(Role.SENDER);
    attach.setName(linkName);
    attach.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach, null));

    int capacity = transport.capacity();
    assertTrue("Unexpected transport capacity: " + capacity, capacity > bytes.length);

    transport.tail().put(bytes);
    transport.process();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 5, transport.writes.size());
    FrameBody frameBody = transport.writes.get(4);
    assertTrue("Unexpected frame type", frameBody instanceof Close);

    // Expect the close frame generated to contain a decode error condition referencing the missing container-id.
    ErrorCondition expectedCondition = new ErrorCondition();
    expectedCondition.setCondition(AmqpError.DECODE_ERROR);
    expectedCondition.setDescription(description);

    assertEquals("Unexpected condition", expectedCondition, ((Close) frameBody).getError());
}
 
Example #13
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
/**
 * Verify that no Disposition frame is emitted by the Transport should a Delivery
 * have disposition applied after the delivery has been settled previously.
 */
@Test
public void testNoDispositionUpdatesAfterSettlementProceessedReceiver()
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

    Session session = connection.session();
    session.open();

    String linkName = "myReceiver";
    Sender sender = session.sender(linkName);
    sender.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 3, transport.writes.size());

    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);
    assertTrue("Unexpected frame type", transport.writes.get(2) instanceof Attach);

    // Send the necessary responses to open/begin/attach as well as a transfer
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    begin.setNextOutgoingId(UnsignedInteger.ONE);
    begin.setIncomingWindow(UnsignedInteger.valueOf(1024));
    begin.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    transport.handleFrame(new TransportFrame(0, begin, null));

    Attach attach = new Attach();
    attach.setHandle(UnsignedInteger.ZERO);
    attach.setRole(Role.RECEIVER);
    attach.setName(linkName);
    attach.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach, null));

    int credit = 1;
    Flow flow = new Flow();
    flow.setHandle(UnsignedInteger.ZERO);
    flow.setDeliveryCount(UnsignedInteger.ZERO);
    flow.setNextIncomingId(UnsignedInteger.ONE);
    flow.setNextOutgoingId(UnsignedInteger.ZERO);
    flow.setIncomingWindow(UnsignedInteger.valueOf(1024));
    flow.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    flow.setDrain(true);
    flow.setLinkCredit(UnsignedInteger.valueOf(credit));
    transport.handleFrame(new TransportFrame(0, flow, null));

    Delivery delivery = sendMessage(sender, "tag1", "content1");

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(3) instanceof Transfer);

    delivery.disposition(Accepted.getInstance());
    delivery.settle();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 5, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(4) instanceof Disposition);

    // Should not produce any new frames being written
    delivery.disposition(Accepted.getInstance());

    connection.close();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 6, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(5) instanceof Close);
}
 
Example #14
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private void doInvalidFlowProvokesDecodeErrorTestImpl(byte[] bytes, String description) {
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();

    Collector collector = Collector.Factory.create();
    connection.collect(collector);

    transport.bind(connection);
    connection.open();

    Session session = connection.session();
    session.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);

    // Provide the response bytes for the header
    transport.tail().put(AmqpHeader.HEADER);
    transport.process();


    // Send the necessary response to Open/Begin
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    begin.setNextOutgoingId(UnsignedInteger.ONE);
    begin.setIncomingWindow(UnsignedInteger.valueOf(1024));
    begin.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    transport.handleFrame(new TransportFrame(0, begin, null));

    int capacity = transport.capacity();
    assertTrue("Unexpected transport capacity: " + capacity, capacity > bytes.length);

    transport.tail().put(bytes);
    transport.process();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 3, transport.writes.size());
    FrameBody frameBody = transport.writes.get(2);
    assertTrue("Unexpected frame type", frameBody instanceof Close);

    // Expect the close frame generated to contain a decode error condition referencing the missing container-id.
    ErrorCondition expectedCondition = new ErrorCondition();
    expectedCondition.setCondition(AmqpError.DECODE_ERROR);
    expectedCondition.setDescription(description);

    assertEquals("Unexpected condition", expectedCondition, ((Close) frameBody).getError());
}
 
Example #15
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private void doInvalidBeginProvokesDecodeErrorTestImpl(byte[] bytes, String description) {
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();

    Collector collector = Collector.Factory.create();
    connection.collect(collector);

    transport.bind(connection);
    connection.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 1, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);

    // Provide the response bytes for the header
    transport.tail().put(AmqpHeader.HEADER);
    transport.process();

    // Send the necessary response to open
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    int capacity = transport.capacity();
    assertTrue("Unexpected transport capacity: " + capacity, capacity > bytes.length);

    transport.tail().put(bytes);
    transport.process();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());
    FrameBody frameBody = transport.writes.get(1);
    assertTrue("Unexpected frame type", frameBody instanceof Close);

    // Expect the close frame generated to contain a decode error condition referencing the missing container-id.
    ErrorCondition expectedCondition = new ErrorCondition();
    expectedCondition.setCondition(AmqpError.DECODE_ERROR);
    expectedCondition.setDescription(description);

    assertEquals("Unexpected condition", expectedCondition, ((Close) frameBody).getError());
}
 
Example #16
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private void doDeliveryIdThresholdsWrapsTestImpl(UnsignedInteger deliveryId1, UnsignedInteger deliveryId2, UnsignedInteger deliveryId3) {
    MockTransportImpl transport = new MockTransportImpl();
    transport.setEmitFlowEventOnSend(false);
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

    Session session = connection.session();
    session.open();

    String linkName1 = "myReceiver1";
    Receiver receiver1 = session.receiver(linkName1);
    receiver1.flow(5);
    receiver1.open();

    pumpMockTransport(transport);

    final UnsignedInteger r1handle = UnsignedInteger.ZERO;

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());

    // Give the necessary responses to open/begin/attach
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    begin.setNextOutgoingId(UnsignedInteger.ONE);
    begin.setIncomingWindow(UnsignedInteger.valueOf(1024));
    begin.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    transport.handleFrame(new TransportFrame(0, begin, null));

    Attach attach1 = new Attach();
    attach1.setHandle(r1handle);
    attach1.setRole(Role.SENDER);
    attach1.setName(linkName1);
    attach1.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach1, null));

    String deliveryTag1 = "tag1";
    String deliveryTag2 = "tag2";
    String deliveryTag3 = "tag3";

    // Send deliveries with the given delivery-id
    handlePartialTransfer(transport, r1handle, deliveryId1, deliveryTag1, new byte[]{ 1 }, false);
    handlePartialTransfer(transport, r1handle, deliveryId2, deliveryTag2, new byte[]{ 2 }, false);
    handlePartialTransfer(transport, r1handle, deliveryId3, deliveryTag3, new byte[]{ 3 }, false);

    // Verify deliveries arrived with expected payload
    verifyDeliveryRawPayload(receiver1, deliveryTag1, new byte[] { 1 });
    verifyDeliveryRawPayload(receiver1, deliveryTag2, new byte[] { 2 });
    verifyDeliveryRawPayload(receiver1, deliveryTag3, new byte[] { 3 });
}
 
Example #17
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeliveryWithIdOmittedOnContinuationTransfers() {
    MockTransportImpl transport = new MockTransportImpl();
    transport.setEmitFlowEventOnSend(false);
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

    Session session = connection.session();
    session.open();

    String linkName1 = "myReceiver1";
    Receiver receiver1 = session.receiver(linkName1);
    receiver1.flow(5);
    receiver1.open();

    String linkName2 = "myReceiver2";
    Receiver receiver2 = session.receiver(linkName2);
    receiver2.flow(5);
    receiver2.open();

    pumpMockTransport(transport);

    final UnsignedInteger r1handle = UnsignedInteger.ZERO;
    final UnsignedInteger r2handle = UnsignedInteger.ONE;

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 6, transport.writes.size());

    // Give the necessary responses to open/begin/attach
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    begin.setNextOutgoingId(UnsignedInteger.ONE);
    begin.setIncomingWindow(UnsignedInteger.valueOf(1024));
    begin.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    transport.handleFrame(new TransportFrame(0, begin, null));

    Attach attach1 = new Attach();
    attach1.setHandle(r1handle);
    attach1.setRole(Role.SENDER);
    attach1.setName(linkName1);
    attach1.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach1, null));

    Attach attach2 = new Attach();
    attach2.setHandle(r2handle);
    attach2.setRole(Role.SENDER);
    attach2.setName(linkName2);
    attach2.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach2, null));

    String deliveryTag1 = "tag1";
    String deliveryTag2 = "tag2";

    // Send multi-frame deliveries for each link, multiplexed together, and omit
    // the delivery-id on the continuation frames as allowed for by the spec.
    handlePartialTransfer(transport, r1handle, 1, deliveryTag1, new byte[]{ 1 }, true);
    handlePartialTransfer(transport, r2handle, 2, deliveryTag2, new byte[]{ 101 }, true);
    handlePartialTransfer(transport, r2handle, null, deliveryTag2, new byte[]{ 102 }, true);
    handlePartialTransfer(transport, r1handle, null, deliveryTag1, new byte[]{ 2 }, true);
    handlePartialTransfer(transport, r1handle, null, deliveryTag1, new byte[]{ 3 }, false);
    handlePartialTransfer(transport, r2handle, null, deliveryTag2, new byte[]{ 103 }, true);
    handlePartialTransfer(transport, r2handle, null, deliveryTag2, new byte[]{ 104 }, false);

    // Verify the transfer frames were all matched to compose the expected delivery payload.
    verifyDeliveryRawPayload(receiver1, deliveryTag1, new byte[] { 1, 2, 3 });
    verifyDeliveryRawPayload(receiver2, deliveryTag2, new byte[] { 101, 102, 103, 104 });
}
 
Example #18
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeliveryIdTrackingHandlesAbortedDelivery() {
    MockTransportImpl transport = new MockTransportImpl();
    transport.setEmitFlowEventOnSend(false);
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

    Session session = connection.session();
    session.open();

    String linkName1 = "myReceiver1";
    Receiver receiver1 = session.receiver(linkName1);
    receiver1.flow(5);
    receiver1.open();

    pumpMockTransport(transport);

    final UnsignedInteger r1handle = UnsignedInteger.ZERO;

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());

    // Give the necessary responses to open/begin/attach
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    begin.setNextOutgoingId(UnsignedInteger.ONE);
    begin.setIncomingWindow(UnsignedInteger.valueOf(1024));
    begin.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    transport.handleFrame(new TransportFrame(0, begin, null));

    Attach attach1 = new Attach();
    attach1.setHandle(r1handle);
    attach1.setRole(Role.SENDER);
    attach1.setName(linkName1);
    attach1.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach1, null));

    // Receive first transfer for a multi-frame delivery
    assertEquals("Unexpected queued count", 0, receiver1.getQueued());
    handlePartialTransfer(transport, r1handle, UnsignedInteger.ZERO, "tag1", new byte[]{ 1 }, true);
    assertEquals("Unexpected queued count", 1, receiver1.getQueued());
    // Receive second transfer for a multi-frame delivery, indicating it is aborted
    handlePartialTransfer(transport, r1handle, UnsignedInteger.ZERO, "tag1", new byte[]{ 2 }, true, true);
    assertEquals("Unexpected queued count", 1, receiver1.getQueued());

    // Receive first transfer for ANOTHER delivery, expect it not to fail, since the earlier delivery aborted
    handlePartialTransfer(transport, r1handle, UnsignedInteger.ONE, "tag2", new byte[]{ 3 }, false);
    assertEquals("Unexpected queued count", 2, receiver1.getQueued());

    receiver1.advance();
    verifyDeliveryRawPayload(receiver1, "tag2", new byte[] { 3 });
}
 
Example #19
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private void doInvalidDispositionProvokesDecodeErrorTestImpl(byte[] bytes, String description) {
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();

    Collector collector = Collector.Factory.create();
    connection.collect(collector);

    transport.bind(connection);
    connection.open();

    Session session = connection.session();
    session.open();

    String linkName = "mySender";
    Sender sender = session.sender(linkName);
    sender.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 3, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);
    assertTrue("Unexpected frame type", transport.writes.get(2) instanceof Attach);

    // Provide the response bytes for the header
    transport.tail().put(AmqpHeader.HEADER);
    transport.process();

    // Send the necessary response to Open/Begin/Attach plus some credit
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    begin.setNextOutgoingId(UnsignedInteger.ONE);
    begin.setIncomingWindow(UnsignedInteger.valueOf(1024));
    begin.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    transport.handleFrame(new TransportFrame(0, begin, null));

    Attach attach = new Attach();
    attach.setHandle(UnsignedInteger.ZERO);
    attach.setRole(Role.SENDER);
    attach.setName(linkName);
    attach.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach, null));

    int credit = 1;
    Flow flow = new Flow();
    flow.setHandle(UnsignedInteger.ZERO);
    flow.setDeliveryCount(UnsignedInteger.ZERO);
    flow.setNextIncomingId(UnsignedInteger.ONE);
    flow.setNextOutgoingId(UnsignedInteger.ZERO);
    flow.setIncomingWindow(UnsignedInteger.valueOf(1024));
    flow.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    flow.setDrain(true);
    flow.setLinkCredit(UnsignedInteger.valueOf(credit));
    transport.handleFrame(new TransportFrame(0, flow, null));

    sendMessage(sender, "tag1", "content1");

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(3) instanceof Transfer);

    int capacity = transport.capacity();
    assertTrue("Unexpected transport capacity: " + capacity, capacity > bytes.length);

    transport.tail().put(bytes);
    transport.process();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 5, transport.writes.size());
    FrameBody frameBody = transport.writes.get(4);
    assertTrue("Unexpected frame type", frameBody instanceof Close);

    // Expect the close frame generated to contain a decode error condition referencing the missing container-id.
    ErrorCondition expectedCondition = new ErrorCondition();
    expectedCondition.setCondition(AmqpError.DECODE_ERROR);
    expectedCondition.setDescription(description);

    assertEquals("Unexpected condition", expectedCondition, ((Close) frameBody).getError());
}
 
Example #20
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
/**
 * Verify that no Disposition frame is emitted by the Transport should a Delivery
 * have disposition applied after the delivery has been settled previously.
 */
@Test
public void testNoDispositionUpdatesAfterSettlementProceessedSender()
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

    Session session = connection.session();
    session.open();

    String linkName = "myReceiver";
    Receiver receiver = session.receiver(linkName);
    receiver.flow(5);
    receiver.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());

    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);
    assertTrue("Unexpected frame type", transport.writes.get(2) instanceof Attach);
    assertTrue("Unexpected frame type", transport.writes.get(3) instanceof Flow);

    Delivery delivery = receiver.current();
    assertNull("Should not yet have a delivery", delivery);

    // Send the necessary responses to open/begin/attach as well as a transfer
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    begin.setNextOutgoingId(UnsignedInteger.ONE);
    begin.setIncomingWindow(UnsignedInteger.valueOf(1024));
    begin.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    transport.handleFrame(new TransportFrame(0, begin, null));

    Attach attach = new Attach();
    attach.setHandle(UnsignedInteger.ZERO);
    attach.setRole(Role.SENDER);
    attach.setName(linkName);
    attach.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach, null));

    String deliveryTag = "tag1";
    String messageContent = "content1";
    handleTransfer(transport, 1, deliveryTag, messageContent);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());

    delivery = verifyDelivery(receiver, deliveryTag, messageContent);
    assertNotNull("Should now have a delivery", delivery);

    delivery.disposition(Accepted.getInstance());
    delivery.settle();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 5, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(4) instanceof Disposition);

    // Should not produce any new frames being written
    delivery.disposition(Accepted.getInstance());

    connection.close();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 6, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(5) instanceof Close);
}
 
Example #21
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeliveryIdOutOfSequenceCausesISE() {
    MockTransportImpl transport = new MockTransportImpl();
    transport.setEmitFlowEventOnSend(false);
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

    Session session = connection.session();
    session.open();

    String linkName1 = "myReceiver1";
    Receiver receiver1 = session.receiver(linkName1);
    receiver1.flow(5);
    receiver1.open();

    String linkName2 = "myReceiver2";
    Receiver receiver2 = session.receiver(linkName2);
    receiver2.flow(5);
    receiver2.open();

    pumpMockTransport(transport);

    final UnsignedInteger r1handle = UnsignedInteger.ZERO;
    final UnsignedInteger r2handle = UnsignedInteger.ONE;

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 6, transport.writes.size());

    // Give the necessary responses to open/begin/attach
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    begin.setNextOutgoingId(UnsignedInteger.ONE);
    begin.setIncomingWindow(UnsignedInteger.valueOf(1024));
    begin.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    transport.handleFrame(new TransportFrame(0, begin, null));

    Attach attach1 = new Attach();
    attach1.setHandle(r1handle);
    attach1.setRole(Role.SENDER);
    attach1.setName(linkName1);
    attach1.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach1, null));

    Attach attach2 = new Attach();
    attach2.setHandle(r2handle);
    attach2.setRole(Role.SENDER);
    attach2.setName(linkName2);
    attach2.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach2, null));

    String deliveryTag1 = "tag1";
    String deliveryTag2 = "tag2";

    handlePartialTransfer(transport, r2handle, 2, deliveryTag2, new byte[]{ 2 }, false);
    try {
        handlePartialTransfer(transport, r1handle, 1, deliveryTag1, new byte[]{ 1 }, false);
        fail("Expected an ISE");
    } catch(IllegalStateException ise) {
        // Expected
        assertTrue("Unexpected exception:" + ise, ise.getMessage().contains("Expected delivery-id 3, got 1"));
    }
}
 
Example #22
Source File: FrameParserTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private TransportFrame frameMatching(int channel, FrameBody frameBody)
{
    return argThat(new TransportFrameMatcher(channel, frameBody));
}
 
Example #23
Source File: FrameParserTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
TransportFrameMatcher(int expectedChannel, FrameBody expectedFrameBody)
{
    _expectedTransportFrame = new TransportFrame(expectedChannel, expectedFrameBody, null);
}
 
Example #24
Source File: AmqpProtocolTracer.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Override
public void receivedFrame(TransportFrame transportFrame) {
    logger.trace("[{}:{}] RECV: {}{}", transportIdentifier, transportFrame.getChannel(), transportFrame.getBody(), formatPayload(transportFrame));
}
 
Example #25
Source File: AmqpProtocolTracer.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Override
public void sentFrame(TransportFrame transportFrame) {
    logger.trace("[{}:{}] SENT: {}{}", transportIdentifier, transportFrame.getChannel(), transportFrame.getBody(), formatPayload(transportFrame));
}
 
Example #26
Source File: AmqpProtocolTracer.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
private String formatPayload(TransportFrame frame) {
    Binary payload = frame.getPayload();

    if (payload == null || payload.getLength() == 0 || payloadStringLimit <= 0) {
        return "";
    }

    final byte[] binData = payload.getArray();
    final int binLength = payload.getLength();
    final int offset = payload.getArrayOffset();

    StringBuilder builder = new StringBuilder();

    // Prefix the payload with total bytes which gives insight regardless of truncation.
    builder.append(" (").append(payload.getLength()).append(") ").append("\"");

    int size = 0;
    boolean truncated = false;
    for (int i = 0; i < binLength; i++) {
        byte c = binData[offset + i];

        if (c > 31 && c < 127 && c != '\\') {
            if (size + 1 <= payloadStringLimit) {
                size += 1;
                builder.append((char) c);
            } else {
                truncated = true;
                break;
            }
        } else {
            if (size + 4 <= payloadStringLimit) {
                size += 4;
                builder.append(String.format("\\x%02x", c));
            } else {
                truncated = true;
                break;
            }
        }
    }

    builder.append("\"");

    if (truncated) {
        builder.append("...(truncated)");
    }

    return builder.toString();
}
 
Example #27
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
/**
 * Verify that no End frame is emitted by the Transport should a Session close
 * after the Close frame was sent.
 */
@Test
public void testSessionEndAfterCloseSent()
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

    Session session = connection.session();
    session.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());

    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);

    // Send the necessary responses to open/begin
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    transport.handleFrame(new TransportFrame(0, begin, null));

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());

    // Cause a Close frame to be sent
    connection.close();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 3, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(2) instanceof Close);

    // Close the session and verify the transport doesn't
    // send any End frame, as a Close frame was sent already.
    session.close();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 3, transport.writes.size());
}
 
Example #28
Source File: LoggingProtocolTracer.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Override
public void sentFrame(TransportFrame transportFrame)
{
    SENT_LOGGER.finer(_logMessagePrefix + " writing frame: " + transportFrame);
}
 
Example #29
Source File: TransportImpl.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
void log(String event, TransportFrame frame)
{
    if (isTraceFramesEnabled()) {
        outputMessage(event, frame.getChannel(), frame.getBody(), frame.getPayload());
    }
}
 
Example #30
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
void doLinkAttachAfterEndSentTestImpl(boolean receiverLink)
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

    Session session = connection.session();
    session.open();

    Link link = null;
    if(receiverLink)
    {
        link = session.receiver("myReceiver");
    }
    else
    {
        link = session.sender("mySender");
    }

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());

    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);

    // Send the necessary responses to open/begin
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    transport.handleFrame(new TransportFrame(0, begin, null));

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());

    // Cause a End frame to be sent
    session.close();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 3, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(2) instanceof End);

    // Open the link and verify the transport doesn't
    // send any Attach frame, as an End frame was sent already.
    link.open();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 3, transport.writes.size());
}