Search in sources :

Example 1 with ConnectionParams

use of com.rabbitmq.client.impl.ConnectionParams in project rabbitmq-java-client by rabbitmq.

the class ConnectionFactoryTest method tryNextAddressIfTimeoutExceptionNoAutoRecovery.

// see https://github.com/rabbitmq/rabbitmq-java-client/issues/262
@Test
public void tryNextAddressIfTimeoutExceptionNoAutoRecovery() throws IOException, TimeoutException {
    final AMQConnection connectionThatThrowsTimeout = mock(AMQConnection.class);
    final AMQConnection connectionThatSucceeds = mock(AMQConnection.class);
    final Queue<AMQConnection> connections = new ArrayBlockingQueue<AMQConnection>(10);
    connections.add(connectionThatThrowsTimeout);
    connections.add(connectionThatSucceeds);
    ConnectionFactory connectionFactory = new ConnectionFactory() {

        @Override
        protected AMQConnection createConnection(ConnectionParams params, FrameHandler frameHandler, MetricsCollector metricsCollector) {
            return connections.poll();
        }

        @Override
        protected synchronized FrameHandlerFactory createFrameHandlerFactory() throws IOException {
            return mock(FrameHandlerFactory.class);
        }
    };
    connectionFactory.setAutomaticRecoveryEnabled(false);
    doThrow(TimeoutException.class).when(connectionThatThrowsTimeout).start();
    doNothing().when(connectionThatSucceeds).start();
    Connection returnedConnection = connectionFactory.newConnection(new Address[] { new Address("host1"), new Address("host2") });
    assertSame(connectionThatSucceeds, returnedConnection);
}
Also used : MetricsCollector(com.rabbitmq.client.MetricsCollector) FrameHandler(com.rabbitmq.client.impl.FrameHandler) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Address(com.rabbitmq.client.Address) AMQConnection(com.rabbitmq.client.impl.AMQConnection) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Connection(com.rabbitmq.client.Connection) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) Test(org.junit.Test)

Example 2 with ConnectionParams

use of com.rabbitmq.client.impl.ConnectionParams in project rabbitmq-java-client by rabbitmq.

the class ConnectionFactoryTest method customizeCredentialsProvider.

// see https://github.com/rabbitmq/rabbitmq-java-client/pull/350
@Test
public void customizeCredentialsProvider() throws Exception {
    final CredentialsProvider provider = mock(CredentialsProvider.class);
    final AMQConnection connection = mock(AMQConnection.class);
    final AtomicBoolean createCalled = new AtomicBoolean(false);
    ConnectionFactory connectionFactory = new ConnectionFactory() {

        @Override
        protected AMQConnection createConnection(ConnectionParams params, FrameHandler frameHandler, MetricsCollector metricsCollector) {
            assertSame(provider, params.getCredentialsProvider());
            createCalled.set(true);
            return connection;
        }
    };
    connectionFactory.setCredentialsProvider(provider);
    connectionFactory.setAutomaticRecoveryEnabled(false);
    doNothing().when(connection).start();
    Connection returnedConnection = connectionFactory.newConnection();
    assertSame(returnedConnection, connection);
    assertTrue(createCalled.get());
}
Also used : MetricsCollector(com.rabbitmq.client.MetricsCollector) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FrameHandler(com.rabbitmq.client.impl.FrameHandler) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) AMQConnection(com.rabbitmq.client.impl.AMQConnection) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Connection(com.rabbitmq.client.Connection) CredentialsProvider(com.rabbitmq.client.impl.CredentialsProvider) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) Test(org.junit.Test)

Example 3 with ConnectionParams

use of com.rabbitmq.client.impl.ConnectionParams in project rabbitmq-java-client by rabbitmq.

the class AMQConnectionTest method connectionSendsSingleHeaderAndTimesOut.

/**
 * Check the AMQConnection does send exactly 1 initial header, and deal correctly with
 * the frame handler throwing an exception when we try to read data
 */
@Test
public void connectionSendsSingleHeaderAndTimesOut() throws TimeoutException {
    IOException exception = new SocketTimeoutException();
    _mockFrameHandler.setExceptionOnReadingFrames(exception);
    assertEquals(0, _mockFrameHandler.countHeadersSent());
    try {
        ConnectionParams params = factory.params(Executors.newFixedThreadPool(1));
        new AMQConnection(params, _mockFrameHandler).start();
        fail("Connection should have thrown exception");
    } catch (IOException signal) {
    // As expected
    }
    assertEquals(1, _mockFrameHandler.countHeadersSent());
    // _connection.close(0, CLOSE_MESSAGE);
    List<Throwable> exceptionList = exceptionHandler.getHandledExceptions();
    assertEquals(Collections.<Throwable>singletonList(exception), exceptionList);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) AMQConnection(com.rabbitmq.client.impl.AMQConnection) IOException(java.io.IOException) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) Test(org.junit.Test)

Example 4 with ConnectionParams

use of com.rabbitmq.client.impl.ConnectionParams in project rabbitmq-java-client by rabbitmq.

the class AMQConnectionTest method connectionHangInNegotiation.

/**
 * Check we can open a connection once, but not twice.
 * @throws IOException
 */
// public void testCanOpenConnectionOnceOnly() throws IOException {
// AMQConnection connection = new AMQConnection(_mockFrameHandler);
// connection.open();
// try {
// connection.open();
// fail("We shouldn't have been able to open this connection more than once.");
// } catch(IOException ex) {
// // as expected
// }
// }
/**
 * Test that we catch timeout between connect and negotiation of the connection being finished.
 */
@Test
public void connectionHangInNegotiation() {
    // to limit hang
    this._mockFrameHandler.setTimeoutCount(10);
    assertEquals(0, this._mockFrameHandler.countHeadersSent());
    try {
        ConnectionParams params = factory.params(Executors.newFixedThreadPool(1));
        new AMQConnection(params, this._mockFrameHandler).start();
        fail("Connection should have thrown exception");
    } catch (IOException signal) {
    // expected
    } catch (TimeoutException te) {
    // also fine: continuation timed out first
    }
    assertEquals(1, this._mockFrameHandler.countHeadersSent());
    List<Throwable> exceptionList = exceptionHandler.getHandledExceptions();
    assertEquals("Only one exception expected", 1, exceptionList.size());
    assertEquals("Wrong type of exception returned.", SocketTimeoutException.class, exceptionList.get(0).getClass());
}
Also used : AMQConnection(com.rabbitmq.client.impl.AMQConnection) IOException(java.io.IOException) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) TimeoutException(java.util.concurrent.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) Test(org.junit.Test)

Example 5 with ConnectionParams

use of com.rabbitmq.client.impl.ConnectionParams in project rabbitmq-java-client by rabbitmq.

the class RecoveryAwareAMQConnectionFactoryTest method tryNextAddressIfTimeoutException.

// see https://github.com/rabbitmq/rabbitmq-java-client/issues/262
@Test
public void tryNextAddressIfTimeoutException() throws IOException, TimeoutException {
    final RecoveryAwareAMQConnection connectionThatThrowsTimeout = mock(RecoveryAwareAMQConnection.class);
    final RecoveryAwareAMQConnection connectionThatSucceeds = mock(RecoveryAwareAMQConnection.class);
    final Queue<RecoveryAwareAMQConnection> connections = new ArrayBlockingQueue<RecoveryAwareAMQConnection>(10);
    connections.add(connectionThatThrowsTimeout);
    connections.add(connectionThatSucceeds);
    AddressResolver addressResolver = () -> Arrays.asList(new Address("host1"), new Address("host2"));
    RecoveryAwareAMQConnectionFactory connectionFactory = new RecoveryAwareAMQConnectionFactory(new ConnectionParams(), mock(FrameHandlerFactory.class), addressResolver) {

        @Override
        protected RecoveryAwareAMQConnection createConnection(ConnectionParams params, FrameHandler handler, MetricsCollector metricsCollector) {
            return connections.poll();
        }
    };
    doThrow(TimeoutException.class).when(connectionThatThrowsTimeout).start();
    doNothing().when(connectionThatSucceeds).start();
    Connection returnedConnection = connectionFactory.newConnection();
    assertSame(connectionThatSucceeds, returnedConnection);
}
Also used : RecoveryAwareAMQConnectionFactory(com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory) FrameHandlerFactory(com.rabbitmq.client.impl.FrameHandlerFactory) MetricsCollector(com.rabbitmq.client.MetricsCollector) AddressResolver(com.rabbitmq.client.AddressResolver) FrameHandler(com.rabbitmq.client.impl.FrameHandler) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Address(com.rabbitmq.client.Address) RecoveryAwareAMQConnection(com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnection) RecoveryAwareAMQConnection(com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnection) Connection(com.rabbitmq.client.Connection) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) Test(org.junit.Test)

Aggregations

ConnectionParams (com.rabbitmq.client.impl.ConnectionParams)7 AMQConnection (com.rabbitmq.client.impl.AMQConnection)5 Test (org.junit.Test)5 FrameHandler (com.rabbitmq.client.impl.FrameHandler)4 Connection (com.rabbitmq.client.Connection)3 MetricsCollector (com.rabbitmq.client.MetricsCollector)3 IOException (java.io.IOException)3 Address (com.rabbitmq.client.Address)2 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)2 FrameHandlerFactory (com.rabbitmq.client.impl.FrameHandlerFactory)2 SocketTimeoutException (java.net.SocketTimeoutException)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 TimeoutException (java.util.concurrent.TimeoutException)2 AddressResolver (com.rabbitmq.client.AddressResolver)1 CredentialsProvider (com.rabbitmq.client.impl.CredentialsProvider)1 SocketFrameHandlerFactory (com.rabbitmq.client.impl.SocketFrameHandlerFactory)1 SocketChannelFrameHandlerFactory (com.rabbitmq.client.impl.nio.SocketChannelFrameHandlerFactory)1 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)1 RecoveryAwareAMQConnection (com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnection)1 RecoveryAwareAMQConnectionFactory (com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory)1