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);
}
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());
}
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);
}
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());
}
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);
}
Aggregations