Search in sources :

Example 66 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class ReconnectTest method testConnectionNotifiesReconnectionLifecycleEvents.

@Test
public void testConnectionNotifiesReconnectionLifecycleEvents() throws Exception {
    try (ProtonTestServer firstPeer = new ProtonTestServer();
        ProtonTestServer finalPeer = new ProtonTestServer()) {
        firstPeer.expectSASLAnonymousConnect();
        firstPeer.expectOpen().withContainerId(any(String.class)).respond();
        firstPeer.dropAfterLastHandler(5);
        firstPeer.start();
        finalPeer.expectSASLAnonymousConnect();
        finalPeer.expectOpen().withContainerId(any(String.class)).respond();
        finalPeer.start();
        final URI primaryURI = firstPeer.getServerURI();
        final URI backupURI = finalPeer.getServerURI();
        final CountDownLatch connected = new CountDownLatch(1);
        final CountDownLatch disconnected = new CountDownLatch(1);
        final CountDownLatch reconnected = new CountDownLatch(1);
        final CountDownLatch failed = new CountDownLatch(1);
        ConnectionOptions options = new ConnectionOptions();
        options.reconnectOptions().reconnectEnabled(true);
        options.reconnectOptions().maxReconnectAttempts(5);
        options.reconnectOptions().reconnectDelay(10);
        options.reconnectOptions().useReconnectBackOff(false);
        options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
        options.connectedHandler((connection, context) -> {
            connected.countDown();
        });
        options.interruptedHandler((connection, context) -> {
            disconnected.countDown();
        });
        options.reconnectedHandler((connection, context) -> {
            reconnected.countDown();
        });
        options.disconnectedHandler((connection, context) -> {
            failed.countDown();
        });
        Client container = Client.create();
        Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
        firstPeer.waitForScriptToComplete();
        connection.openFuture().get();
        finalPeer.waitForScriptToComplete();
        finalPeer.expectBegin().respond();
        finalPeer.expectEnd().respond();
        finalPeer.dropAfterLastHandler(10);
        Session session = connection.openSession().openFuture().get();
        session.close();
        finalPeer.waitForScriptToComplete();
        assertTrue(connected.await(5, TimeUnit.SECONDS));
        assertTrue(disconnected.await(5, TimeUnit.SECONDS));
        assertTrue(reconnected.await(5, TimeUnit.SECONDS));
        assertTrue(failed.await(5, TimeUnit.SECONDS));
        connection.close();
        finalPeer.waitForScriptToComplete();
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 67 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class ReconnectTest method testConnectionReportsFailedAfterMaxinitialReconnectAttempts.

@Test
public void testConnectionReportsFailedAfterMaxinitialReconnectAttempts() throws Exception {
    try (ProtonTestServer firstPeer = new ProtonTestServer()) {
        firstPeer.start();
        final URI primaryURI = firstPeer.getServerURI();
        firstPeer.close();
        ConnectionOptions options = new ConnectionOptions();
        options.reconnectOptions().reconnectEnabled(true);
        // Try forever if connect succeeds once.
        options.reconnectOptions().maxReconnectAttempts(-1);
        options.reconnectOptions().maxInitialConnectionAttempts(3);
        options.reconnectOptions().warnAfterReconnectAttempts(5);
        options.reconnectOptions().reconnectDelay(10);
        options.reconnectOptions().useReconnectBackOff(false);
        Client container = Client.create();
        Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
        try {
            connection.openFuture().get();
            fail("Should not successfully connect.");
        } catch (ExecutionException exe) {
            assertTrue(exe.getCause() instanceof ClientConnectionRemotelyClosedException);
        }
        try {
            connection.defaultSender();
            fail("Connection should be in a failed state now.");
        } catch (ClientConnectionRemotelyClosedException cliEx) {
        }
        connection.close();
        try {
            connection.defaultSender();
            fail("Connection should be in a closed state now.");
        } catch (ClientIllegalStateException cliEx) {
        }
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientConnectionRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException) Connection(org.apache.qpid.protonj2.client.Connection) ClientIllegalStateException(org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 68 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class ReconnectTest method testConnectHandlesSaslTempFailureAndReconnects.

@Test
public void testConnectHandlesSaslTempFailureAndReconnects() throws Exception {
    try (ProtonTestServer firstPeer = new ProtonTestServer();
        ProtonTestServer finalPeer = new ProtonTestServer()) {
        firstPeer.expectFailingSASLPlainConnect(SaslCode.SYS_TEMP.byteValue());
        firstPeer.dropAfterLastHandler();
        firstPeer.start();
        finalPeer.expectSASLPlainConnect("test", "pass");
        finalPeer.expectOpen().respond();
        finalPeer.start();
        final URI primaryURI = firstPeer.getServerURI();
        final URI backupURI = finalPeer.getServerURI();
        final CountDownLatch connected = new CountDownLatch(1);
        final AtomicReference<String> connectedHost = new AtomicReference<>();
        final AtomicReference<Integer> connectedPort = new AtomicReference<>();
        ConnectionOptions options = new ConnectionOptions();
        options.user("test");
        options.password("pass");
        options.reconnectOptions().reconnectEnabled(true);
        options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
        options.connectedHandler((connection, event) -> {
            connectedHost.set(event.host());
            connectedPort.set(event.port());
            connected.countDown();
        });
        Client container = Client.create();
        Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
        firstPeer.waitForScriptToComplete();
        connection.openFuture().get();
        assertTrue(connected.await(5, TimeUnit.SECONDS));
        // Should never have connected and exchanged Open performatives with first peer
        // so we won't have had a connection established event there.
        assertEquals(backupURI.getHost(), connectedHost.get());
        assertEquals(backupURI.getPort(), connectedPort.get());
        finalPeer.waitForScriptToComplete();
        finalPeer.expectClose().respond();
        connection.close();
        finalPeer.waitForScriptToComplete();
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 69 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class ReconnectTest method testReconnectHandlesDropThenRejectionCloseAfterConnect.

@Test
public void testReconnectHandlesDropThenRejectionCloseAfterConnect() throws Exception {
    try (ProtonTestServer firstPeer = new ProtonTestServer();
        ProtonTestServer secondPeer = new ProtonTestServer();
        ProtonTestServer thirdPeer = new ProtonTestServer()) {
        firstPeer.expectSASLAnonymousConnect();
        firstPeer.expectOpen().respond();
        firstPeer.start();
        secondPeer.expectSASLAnonymousConnect();
        secondPeer.expectOpen().reject(AmqpError.INVALID_FIELD.toString(), "Connection configuration has invalid field");
        secondPeer.expectClose();
        secondPeer.start();
        thirdPeer.expectSASLAnonymousConnect();
        thirdPeer.expectOpen().respond();
        thirdPeer.start();
        final CountDownLatch connected = new CountDownLatch(1);
        final CountDownLatch disconnected = new CountDownLatch(2);
        final CountDownLatch reconnected = new CountDownLatch(2);
        final CountDownLatch failed = new CountDownLatch(1);
        final URI firstURI = firstPeer.getServerURI();
        final URI secondURI = secondPeer.getServerURI();
        final URI thirdURI = thirdPeer.getServerURI();
        ConnectionOptions options = new ConnectionOptions();
        options.reconnectOptions().reconnectEnabled(true);
        options.reconnectOptions().addReconnectLocation(secondURI.getHost(), secondURI.getPort()).addReconnectLocation(thirdURI.getHost(), thirdURI.getPort());
        options.connectedHandler((connection, context) -> {
            connected.countDown();
        });
        options.interruptedHandler((connection, context) -> {
            disconnected.countDown();
        });
        options.reconnectedHandler((connection, context) -> {
            reconnected.countDown();
        });
        options.disconnectedHandler((connection, context) -> {
            // Not expecting any failure in this test case
            failed.countDown();
        });
        Connection connection = Client.create().connect(firstURI.getHost(), firstURI.getPort(), options);
        firstPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        connection.openFuture().get();
        firstPeer.close();
        secondPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        // Should connect, then fail and attempt to connect to second and be rejected then reconnect to third.
        assertTrue(connected.await(5, TimeUnit.SECONDS));
        assertTrue(disconnected.await(5, TimeUnit.SECONDS));
        assertTrue(reconnected.await(5, TimeUnit.SECONDS));
        assertEquals(1, failed.getCount());
        thirdPeer.expectClose().respond();
        connection.close();
        thirdPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 70 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class SaslConnectionTest method testSaslAnonymousConnection.

@Test
public void testSaslAnonymousConnection() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer(serverOptions())) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectClose().respond();
        peer.start();
        URI remoteURI = peer.getServerURI();
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        connection.openFuture().get(10, TimeUnit.SECONDS);
        connection.close();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Aggregations

Connection (org.apache.qpid.protonj2.client.Connection)368 Client (org.apache.qpid.protonj2.client.Client)367 URI (java.net.URI)354 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)352 Test (org.junit.jupiter.api.Test)250 Session (org.apache.qpid.protonj2.client.Session)166 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)112 Sender (org.apache.qpid.protonj2.client.Sender)89 Receiver (org.apache.qpid.protonj2.client.Receiver)79 ExecutionException (java.util.concurrent.ExecutionException)72 StreamReceiver (org.apache.qpid.protonj2.client.StreamReceiver)65 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)60 StreamSender (org.apache.qpid.protonj2.client.StreamSender)49 StreamDelivery (org.apache.qpid.protonj2.client.StreamDelivery)46 TransferPayloadCompositeMatcher (org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher)44 Tracker (org.apache.qpid.protonj2.client.Tracker)41 StreamSenderMessage (org.apache.qpid.protonj2.client.StreamSenderMessage)38 ReceiverOptions (org.apache.qpid.protonj2.client.ReceiverOptions)34 SenderOptions (org.apache.qpid.protonj2.client.SenderOptions)33 OutputStream (java.io.OutputStream)31