|
| 1 | +// Copyright (c) 2007-Present Pivotal Software, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2 |
| 5 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 6 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | +// info@rabbitmq.com. |
| 15 | + |
| 16 | +package com.rabbitmq.client.test; |
| 17 | + |
| 18 | +import com.rabbitmq.client.Address; |
| 19 | +import com.rabbitmq.client.Connection; |
| 20 | +import com.rabbitmq.client.ConnectionFactory; |
| 21 | +import com.rabbitmq.client.MetricsCollector; |
| 22 | +import com.rabbitmq.client.impl.AMQConnection; |
| 23 | +import com.rabbitmq.client.impl.ConnectionParams; |
| 24 | +import com.rabbitmq.client.impl.FrameHandler; |
| 25 | +import com.rabbitmq.client.impl.FrameHandlerFactory; |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Test; |
| 28 | +import org.mockito.Mockito; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Queue; |
| 32 | +import java.util.concurrent.ArrayBlockingQueue; |
| 33 | +import java.util.concurrent.TimeoutException; |
| 34 | + |
| 35 | +import static org.junit.Assert.assertSame; |
| 36 | +import static org.mockito.Mockito.doNothing; |
| 37 | +import static org.mockito.Mockito.doThrow; |
| 38 | +import static org.mockito.Mockito.mock; |
| 39 | + |
| 40 | +public class ConnectionFactoryTest { |
| 41 | + |
| 42 | + // see https://github.com/rabbitmq/rabbitmq-java-client/issues/262 |
| 43 | + @Test public void tryNextAddressIfTimeoutExceptionNoAutoRecovery() throws IOException, TimeoutException { |
| 44 | + final AMQConnection connectionThatThrowsTimeout = mock(AMQConnection.class); |
| 45 | + final AMQConnection connectionThatSucceeds = mock(AMQConnection.class); |
| 46 | + final Queue<AMQConnection> connections = new ArrayBlockingQueue<AMQConnection>(10); |
| 47 | + connections.add(connectionThatThrowsTimeout); |
| 48 | + connections.add(connectionThatSucceeds); |
| 49 | + ConnectionFactory connectionFactory = new ConnectionFactory() { |
| 50 | + |
| 51 | + @Override |
| 52 | + protected AMQConnection createConnection(ConnectionParams params, FrameHandler frameHandler, MetricsCollector metricsCollector) { |
| 53 | + return connections.poll(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected synchronized FrameHandlerFactory createFrameHandlerFactory() throws IOException { |
| 58 | + return mock(FrameHandlerFactory.class); |
| 59 | + } |
| 60 | + }; |
| 61 | + connectionFactory.setAutomaticRecoveryEnabled(false); |
| 62 | + doThrow(TimeoutException.class).when(connectionThatThrowsTimeout).start(); |
| 63 | + doNothing().when(connectionThatSucceeds).start(); |
| 64 | + Connection returnedConnection = connectionFactory.newConnection( |
| 65 | + new Address[] { new Address("host1"), new Address("host2") } |
| 66 | + ); |
| 67 | + assertSame(connectionThatSucceeds, returnedConnection); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments