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

The following examples show how to use org.apache.qpid.proton.engine.Connection#open() . 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: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testOutputTooBigToBeWrittenInOneGo()
{
    int smallMaxFrameSize = 512;
    _transport = new TransportImpl(smallMaxFrameSize);

    Connection conn = new ConnectionImpl();
    _transport.bind(conn);

    // Open frame sized in order to produce a frame that will almost fill output buffer
    conn.setHostname(stringOfLength("x", 500));
    conn.open();

    // Close the connection to generate a Close frame which will cause an overflow
    // internally - we'll get the remaining bytes on the next interaction.
    conn.close();

    ByteBuffer buf = _transport.getOutputBuffer();
    assertEquals("Expecting buffer to be full", smallMaxFrameSize, buf.remaining());
    buf.position(buf.limit());
    _transport.outputConsumed();

    buf  = _transport.getOutputBuffer();
    assertTrue("Expecting second buffer to have bytes", buf.remaining() > 0);
    assertTrue("Expecting second buffer to not be full", buf.remaining() < Transport.MIN_MAX_FRAME_SIZE);
}
 
Example 3
Source File: ReactorTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Test
public void connectionRefused() throws IOException {
    final ServerSocket serverSocket = new ServerSocket(0, 0);

    class ConnectionHandler extends TestHandler {
        @Override
        public void onConnectionInit(Event event) {
            super.onConnectionInit(event);
            Connection connection = event.getConnection();
            connection.open();
            try {
                serverSocket.close();
            } catch(IOException e) {
                AssertionFailedError afe = new AssertionFailedError();
                afe.initCause(e);
                throw afe;
            }
        }
    }
    TestHandler connectionHandler = new ConnectionHandler();
    reactor.connectionToHost("127.0.0.1", serverSocket.getLocalPort(), connectionHandler);
    reactor.run();
    reactor.free();
    serverSocket.close();
    connectionHandler.assertEvents(Type.CONNECTION_INIT, Type.CONNECTION_LOCAL_OPEN, Type.CONNECTION_BOUND, Type.TRANSPORT_ERROR, Type.TRANSPORT_TAIL_CLOSED,
            Type.TRANSPORT_HEAD_CLOSED, Type.TRANSPORT_CLOSED, Type.CONNECTION_UNBOUND, Type.TRANSPORT);
}
 
Example 4
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 5
Source File: Handshaker.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onConnectionRemoteOpen(Event evt) {
    Connection conn = evt.getConnection();
    if (conn.getLocalState() == EndpointState.UNINITIALIZED) {
        conn.open();
    }
}
 
Example 6
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
void doOpenLinkBeforeOpenConnectionTestImpl(boolean receiverLink)
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

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

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

    pumpMockTransport(transport);

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

    // Now open the connection, expect the Open, Begin, and Attach frames
    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);
}
 
Example 7
Source File: AMQPConnectionContext.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoteOpen(Connection connection) throws Exception {
   handler.requireHandler();
   try {
      initInternal();
   } catch (Exception e) {
      log.error("Error init connection", e);
   }
   if (!validateConnection(connection)) {
      connection.close();
   } else {
      connection.setContext(AMQPConnectionContext.this);
      connection.setContainer(containerId);
      connection.setProperties(connectionProperties);
      connection.setOfferedCapabilities(getConnectionCapabilitiesOffered());
      connection.open();
   }
   initialise();

   /*
    * This can be null which is in effect an empty map, also we really don't need to check this for in bound connections
    * but its here in case we add support for outbound connections.
    * */
   if (connection.getRemoteProperties() == null || !connection.getRemoteProperties().containsKey(CONNECTION_OPEN_FAILED)) {
      long nextKeepAliveTime = handler.tick(true);

      if (nextKeepAliveTime != 0 && scheduledPool != null) {
         scheduleOp.setDelay(nextKeepAliveTime - TimeUnit.NANOSECONDS.toMillis(System.nanoTime()));

         scheduledFutureRef.getAndUpdate(scheduleOp);
      }
   }
}
 
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
void doOpenLinkBeforeOpenSessionTestImpl(boolean receiverLink)
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    // Open the connection
    connection.open();

    // Create but don't open the session
    Session session = connection.session();

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

    pumpMockTransport(transport);

    // Expect only an Open frame, no attach should be sent as the session isn't open
    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 1, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);

    // Now open the session, expect the Begin
    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);
    // Note: an Attach wasn't sent because link is no longer 'modified' after earlier pump. It
    // could easily be argued it should, given how the engine generally handles things. Seems
    // unlikely to be of much real world concern.
    //assertTrue("Unexpected frame type", transport.writes.get(2) instanceof Attach);
}
 
Example 10
Source File: SslTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private void doMultiplePemTrustCertificatesTestImpl(String serverKeystore) throws Exception {
    Transport clientTransport = Proton.transport();
    Transport serverTransport = Proton.transport();

    TransportPumper pumper = new TransportPumper(clientTransport, serverTransport);

    Connection clientConnection = Proton.connection();
    Connection serverConnection = Proton.connection();

    SslDomain clientSslDomain = SslDomain.Factory.create();
    clientSslDomain.init(Mode.CLIENT);
    clientSslDomain.setPeerAuthentication(VerifyMode.VERIFY_PEER);
    clientSslDomain.setTrustedCaDb(CA_CERTS);
    clientTransport.ssl(clientSslDomain);

    SslDomain serverSslDomain = SslDomain.Factory.create();
    serverSslDomain.init(Mode.SERVER);
    SSLContext serverSslContext = createSslContext(serverKeystore, PASSWORD, SERVER_JKS_TRUSTSTORE, PASSWORD);
    serverSslDomain.setSslContext(serverSslContext);
    serverTransport.ssl(serverSslDomain);

    clientConnection.setContainer(CLIENT_CONTAINER);
    serverConnection.setContainer(SERVER_CONTAINER);

    clientTransport.bind(clientConnection);
    serverTransport.bind(serverConnection);

    assertConditions(clientTransport);
    assertConditions(serverTransport);

    clientConnection.open();

    assertEndpointState(clientConnection, ACTIVE, UNINITIALIZED);
    assertEndpointState(serverConnection, UNINITIALIZED, UNINITIALIZED);

    assertConditions(clientTransport);
    assertConditions(serverTransport);

    pumper.pumpAll();

    assertEndpointState(clientConnection, ACTIVE, UNINITIALIZED);
    assertEndpointState(serverConnection, UNINITIALIZED, ACTIVE);

    assertConditions(clientTransport);
    assertConditions(serverTransport);

    serverConnection.open();

    assertEndpointState(clientConnection, ACTIVE, UNINITIALIZED);
    assertEndpointState(serverConnection, ACTIVE, ACTIVE);

    assertConditions(clientTransport);
    assertConditions(serverTransport);

    pumper.pumpAll();

    assertEndpointState(clientConnection, ACTIVE, ACTIVE);
    assertEndpointState(serverConnection, ACTIVE, ACTIVE);

    assertConditions(clientTransport);
    assertConditions(serverTransport);
}
 
Example 11
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
void doLinkDetachAfterEndSentTestImpl(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");
    }
    link.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
    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), 3, transport.writes.size());

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

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

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

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());
}
 
Example 12
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 13
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiplexDeliveriesOnSameReceiverLinkCausesISE() {
    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
    handlePartialTransfer(transport, r1handle, 1, "tag1", new byte[]{ 1 }, true);

    // Receive first transfer for ANOTHER multi-frame delivery, expect it to fail
    // as multiplexing deliveries on a single link is forbidden by the spec.
    try {
        handlePartialTransfer(transport, r1handle, 2, "tag2", new byte[]{ 2 }, true);
        fail("Expected an ISE");
    } catch(IllegalStateException ise) {
        // Expected
        assertEquals("Unexpected message", "Illegal multiplex of deliveries on same link with delivery-id 1 and 2", ise.getMessage());
    }
}
 
Example 14
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 15
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 16
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 17
Source File: LinkTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testMaxMessageSizeValue() throws Exception
{
    LOGGER.fine(bold("======== About to create transports"));

    Transport clientTransport = Proton.transport();
    getClient().setTransport(clientTransport);
    ProtocolTracerEnabler.setProtocolTracer(clientTransport, TestLoggingHelper.CLIENT_PREFIX);

    Transport serverTransport = Proton.transport();
    getServer().setTransport(serverTransport);
    ProtocolTracerEnabler.setProtocolTracer(serverTransport, "            " + TestLoggingHelper.SERVER_PREFIX);

    doOutputInputCycle();

    Connection clientConnection = Proton.connection();
    getClient().setConnection(clientConnection);
    clientTransport.bind(clientConnection);

    Connection serverConnection = Proton.connection();
    getServer().setConnection(serverConnection);
    serverTransport.bind(serverConnection);

    LOGGER.fine(bold("======== About to open connections"));
    clientConnection.open();
    serverConnection.open();

    doOutputInputCycle();

    LOGGER.fine(bold("======== About to open sessions"));
    Session clientSession = clientConnection.session();
    getClient().setSession(clientSession);
    clientSession.open();

    pumpClientToServer();

    Session serverSession = serverConnection.sessionHead(of(UNINITIALIZED), of(ACTIVE));
    getServer().setSession(serverSession);

    serverSession.open();

    pumpServerToClient();

    LOGGER.fine(bold("======== About to create receiver"));

    Source clientSource = new Source();
    getClient().setSource(clientSource);
    clientSource.setAddress(_sourceAddress);

    Target clientTarget = new Target();
    getClient().setTarget(clientTarget);
    clientTarget.setAddress(null);

    Receiver clientReceiver = clientSession.receiver("link1");
    getClient().setReceiver(clientReceiver);
    clientReceiver.setTarget(clientTarget);
    clientReceiver.setSource(clientSource);

    clientReceiver.setReceiverSettleMode(ReceiverSettleMode.FIRST);
    clientReceiver.setSenderSettleMode(SenderSettleMode.UNSETTLED);

    // Set the local link max-message-size
    assertNull("Expected no value to be set", clientReceiver.getMaxMessageSize());
    clientReceiver.setMaxMessageSize(CLIENT_MAX_MSG_SIZE);
    assertEquals("Expected value to be set", CLIENT_MAX_MSG_SIZE, clientReceiver.getMaxMessageSize());

    clientReceiver.open();
    pumpClientToServer();

    LOGGER.fine(bold("======== About to set up implicitly created sender"));

    Sender serverSender = (Sender) getServer().getConnection().linkHead(of(UNINITIALIZED), of(ACTIVE));
    getServer().setSender(serverSender);

    serverSender.setReceiverSettleMode(serverSender.getRemoteReceiverSettleMode());
    serverSender.setSenderSettleMode(serverSender.getRemoteSenderSettleMode());

    org.apache.qpid.proton.amqp.transport.Source serverRemoteSource = serverSender.getRemoteSource();
    serverSender.setSource(serverRemoteSource);

    assertEquals("Expected value to be set", CLIENT_MAX_MSG_SIZE, serverSender.getRemoteMaxMessageSize());

    // Set the local link max-message-size
    assertNull("Expected no value to be set", serverSender.getMaxMessageSize());
    serverSender.setMaxMessageSize(SERVER_MAX_MSG_SIZE);
    assertEquals("Expected value to be set", SERVER_MAX_MSG_SIZE, serverSender.getMaxMessageSize());

    serverSender.open();

    assertNull("Expected no value to be present yet", clientReceiver.getRemoteMaxMessageSize());

    pumpServerToClient();

    assertEquals("Expected value to be set", SERVER_MAX_MSG_SIZE, clientReceiver.getRemoteMaxMessageSize());
}
 
Example 18
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 Receiver
 * have pending drain when a detach is sent for that receiver.
 */
@Test
public void testNoReceiverFlowAfterDetachSentWhileDraining()
{
    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.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
    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));

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

    // Start a drain for the Receiver and verify the transport doesn't
    // send any Flow frame, due to the detach being initiated.
    receiver.drain(10);
    pumpMockTransport(transport);

    // Cause the Detach frame to be sent
    receiver.detach();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 5, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(4) instanceof Detach);
}
 
Example 19
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 20
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());
}