Java Code Examples for org.apache.qpid.proton.engine.Connection#collect()

The following examples show how to use org.apache.qpid.proton.engine.Connection#collect() . 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: Pool.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
private <T extends Link> T resolve(String remote, String local,
                                   LinkResolver<T> resolver,
                                   LinkConstructor<T> constructor) {
    String host = remote.substring(2).split("/", 2)[0];
    T link = resolver.resolve(remote);
    if (link == null) {
        Connection conn = connections.get(host);
        if (conn == null) {
            conn = Connection.Factory.create();
            conn.collect(collector);
            conn.setHostname(host);
            conn.open();
            connections.put(host, conn);
        }

        Session ssn = conn.session();
        ssn.open();

        link = constructor.create(ssn, remote, local);
        link.open();
    }
    return link;
}
 
Example 2
Source File: ProtonTransport.java    From vertx-proton with Apache License 2.0 6 votes vote down vote up
ProtonTransport(Connection connection, Vertx vertx, NetClient netClient, NetSocket socket,
                ProtonSaslAuthenticator authenticator, ProtonTransportOptions options) {
  this.connection = connection;
  this.vertx = vertx;
  this.netClient = netClient;
  this.socket = socket;
  int maxFrameSize = options.getMaxFrameSize() == 0 ? DEFAULT_MAX_FRAME_SIZE : options.getMaxFrameSize();
  transport.setMaxFrameSize(maxFrameSize);
  transport.setOutboundFrameSizeLimit(maxFrameSize);
  transport.setEmitFlowEventOnSend(false); // TODO: make configurable
  transport.setIdleTimeout(2 * options.getHeartbeat());
  ((TransportInternal) transport).setUseReadOnlyOutputBuffer(false);
  if (authenticator != null) {
    authenticator.init(this.socket, (ProtonConnection) this.connection.getContext(), transport);
  }
  this.authenticator = authenticator;
  transport.bind(connection);
  connection.collect(collector);
  socket.endHandler(this::handleSocketEnd);
  socket.handler(this::handleSocketBuffer);
}
 
Example 3
Source File: Driver.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
public void selected() throws IOException {
    SocketChannel sock = socket.accept();
    System.out.println("ACCEPTED: " + sock);
    Connection conn = Connection.Factory.create();
    conn.collect(collector);
    Transport transport = Transport.Factory.create();
    Sasl sasl = transport.sasl();
    sasl.setMechanisms("ANONYMOUS");
    sasl.server();
    sasl.done(Sasl.PN_SASL_OK);
    transport.bind(conn);
    new ChannelHandler(sock, SelectionKey.OP_READ, transport);
}
 
Example 4
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testSenderSendBeforeOpenConnection()
{
    MockTransportImpl transport = new MockTransportImpl();

    Connection connection = Proton.connection();
    transport.bind(connection);

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

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

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

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

    pumpMockTransport(transport);

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

    // Now open the connection, expect the Open and Begin and Attach frames but
    // nothing else as we the sender wont have credit yet.
    connection.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 then give sender credit
    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));

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

    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.setLinkCredit(UnsignedInteger.valueOf(10));

    transport.handleFrame(new TransportFrame(0, flow, null));

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

    // Now pump the transport again and expect a transfer for the message
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(3) instanceof Transfer);
}
 
Example 5
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
void doEmitFlowOnSendTestImpl(boolean emitFlowEventOnSend)
{
    MockTransportImpl transport = new MockTransportImpl();
    transport.setEmitFlowEventOnSend(emitFlowEventOnSend);

    Connection connection = Proton.connection();
    transport.bind(connection);

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

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

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

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

    pumpMockTransport(transport);

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

    assertEvents(collector, Event.Type.CONNECTION_INIT, Event.Type.SESSION_INIT, Event.Type.SESSION_LOCAL_OPEN,
                            Event.Type.TRANSPORT, Event.Type.LINK_INIT, Event.Type.LINK_LOCAL_OPEN, Event.Type.TRANSPORT);

    // Now open the connection, expect the Open and Begin frames but
    // nothing else as we haven't opened the receiver itself yet.
    connection.open();

    pumpMockTransport(transport);

    assertEvents(collector, Event.Type.CONNECTION_LOCAL_OPEN, Event.Type.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 then give sender credit
    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));

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

    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.setLinkCredit(UnsignedInteger.valueOf(10));

    transport.handleFrame(new TransportFrame(0, flow, null));

    assertEvents(collector, Event.Type.CONNECTION_REMOTE_OPEN, Event.Type.SESSION_REMOTE_OPEN,
                            Event.Type.LINK_REMOTE_OPEN, Event.Type.LINK_FLOW);

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

    // Now pump the transport again and expect a transfer for the message
    pumpMockTransport(transport);

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

    // Verify that we did, or did not, emit a flow event
    if(emitFlowEventOnSend)
    {
        assertEvents(collector, Event.Type.LINK_FLOW);
    }
    else
    {
        assertNoEvents(collector);
    }
}
 
Example 6
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
/**
 * Verify that no Flow frame is emitted by the Transport should a Sender
 * have credit drained added after the Close frame was sent.
 */
@Test
public void testSenderFlowAfterCloseSent()
{
    MockTransportImpl transport = new MockTransportImpl();

    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

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

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

    assertFalse("Should not be in drain yet", sender.getDrain());

    // Send the necessary responses to open/begin/attach then give sender credit and drain
    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));

    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 = 10;
    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));

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

    assertTrue("Should not be in drain", sender.getDrain());
    assertEquals("Should have credit", credit, sender.getCredit());

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

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

    // Drain the credit and verify the transport doesn't
    // send any Flow frame, as a Close frame was sent already.
    int drained = sender.drained();
    assertEquals("Should have drained all credit", credit, drained);

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());
}
 
Example 7
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
/**
 * Verify that no Transfer frame is emitted by the Transport should a Delivery
 * be sendable after the Close frame was sent.
 */
@Test
public void testTransferAfterCloseSent()
{
    MockTransportImpl transport = new MockTransportImpl();

    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

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

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

    // Send the necessary responses to open/begin/attach then give sender credit
    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));

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

    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.setLinkCredit(UnsignedInteger.valueOf(10));

    transport.handleFrame(new TransportFrame(0, flow, null));

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

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

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

    // Send a new message and verify the transport doesn't
    // send any Transfer frame, as a Close frame was sent already.
    sendMessage(sender, "tag1", "content1");
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());
}
 
Example 8
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testEndpointOpenAndCloseAreIdempotent()
{
    MockTransportImpl transport = new MockTransportImpl();

    Connection connection = Proton.connection();
    transport.bind(connection);

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

    connection.open();
    connection.open();

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

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

    pumpMockTransport(transport);

    assertEvents(collector, Event.Type.CONNECTION_INIT, Event.Type.CONNECTION_LOCAL_OPEN, Event.Type.TRANSPORT,
                            Event.Type.SESSION_INIT, Event.Type.SESSION_LOCAL_OPEN,
                            Event.Type.TRANSPORT, Event.Type.LINK_INIT, Event.Type.LINK_LOCAL_OPEN, Event.Type.TRANSPORT);

    pumpMockTransport(transport);

    connection.open();
    session.open();
    sender.open();

    assertNoEvents(collector);

    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 then give sender credit
    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));

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

    assertEvents(collector, Event.Type.CONNECTION_REMOTE_OPEN, Event.Type.SESSION_REMOTE_OPEN,
                            Event.Type.LINK_REMOTE_OPEN);

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

    // Now close the link and expect one event
    sender.close();
    sender.close();

    assertEvents(collector, Event.Type.LINK_LOCAL_CLOSE, Event.Type.TRANSPORT);

    pumpMockTransport(transport);

    sender.close();

    assertNoEvents(collector);

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(3) instanceof Detach);
}
 
Example 9
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testCloseFrameErrorAfterDecodeError() {
    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);

    assertEvents(collector, Event.Type.CONNECTION_INIT, Event.Type.CONNECTION_BOUND, Event.Type.CONNECTION_LOCAL_OPEN, Event.Type.TRANSPORT);

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

    // Provide the bytes for Open, but omit the mandatory container-id to provoke a decode error.
    byte[] bytes = new byte[] {  0x00, 0x00, 0x00, 0x0C, // Frame size = 12 bytes.
                                 0x02, 0x00, 0x00, 0x00, // DOFF, TYPE, 2x CHANNEL
                                 0x00, 0x53, 0x10, 0x45};// Described-type, ulong type, open descriptor, list0.

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

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

    assertEvents(collector, Event.Type.TRANSPORT_ERROR, Event.Type.TRANSPORT_TAIL_CLOSED);

    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("The container-id field cannot be omitted");

    assertEquals("Unexpected condition", expectedCondition, ((Close) frameBody).getError());
}
 
Example 10
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 11
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 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
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());
}