|
| 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.AddressResolver; |
| 20 | +import com.rabbitmq.client.Connection; |
| 21 | +import com.rabbitmq.client.MetricsCollector; |
| 22 | +import com.rabbitmq.client.impl.ConnectionParams; |
| 23 | +import com.rabbitmq.client.impl.FrameHandler; |
| 24 | +import com.rabbitmq.client.impl.FrameHandlerFactory; |
| 25 | +import com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnection; |
| 26 | +import com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Queue; |
| 33 | +import java.util.concurrent.ArrayBlockingQueue; |
| 34 | +import java.util.concurrent.TimeoutException; |
| 35 | + |
| 36 | +import static org.junit.Assert.assertSame; |
| 37 | +import static org.mockito.Mockito.*; |
| 38 | + |
| 39 | +public class RecoveryAwareAMQConnectionFactoryTest { |
| 40 | + |
| 41 | + // see https://github.com/rabbitmq/rabbitmq-java-client/issues/262 |
| 42 | + @Test public void tryNextAddressIfTimeoutException() throws IOException, TimeoutException { |
| 43 | + final RecoveryAwareAMQConnection connectionThatThrowsTimeout = mock(RecoveryAwareAMQConnection.class); |
| 44 | + final RecoveryAwareAMQConnection connectionThatSucceeds = mock(RecoveryAwareAMQConnection.class); |
| 45 | + final Queue<RecoveryAwareAMQConnection> connections = new ArrayBlockingQueue<RecoveryAwareAMQConnection>(10); |
| 46 | + connections.add(connectionThatThrowsTimeout); |
| 47 | + connections.add(connectionThatSucceeds); |
| 48 | + AddressResolver addressResolver = new AddressResolver() { |
| 49 | + @Override |
| 50 | + public List<Address> getAddresses() throws IOException { |
| 51 | + return Arrays.asList(new Address("host1"), new Address("host2")); |
| 52 | + } |
| 53 | + }; |
| 54 | + RecoveryAwareAMQConnectionFactory connectionFactory = new RecoveryAwareAMQConnectionFactory( |
| 55 | + new ConnectionParams(), mock(FrameHandlerFactory.class), addressResolver |
| 56 | + ) { |
| 57 | + @Override |
| 58 | + protected RecoveryAwareAMQConnection createConnection(ConnectionParams params, FrameHandler handler, MetricsCollector metricsCollector) { |
| 59 | + return connections.poll(); |
| 60 | + } |
| 61 | + }; |
| 62 | + doThrow(TimeoutException.class).when(connectionThatThrowsTimeout).start(); |
| 63 | + doNothing().when(connectionThatSucceeds).start(); |
| 64 | + Connection returnedConnection = connectionFactory.newConnection(); |
| 65 | + assertSame(connectionThatSucceeds, returnedConnection); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments