|
16 | 16 | package com.rabbitmq.client.test;
|
17 | 17 |
|
18 | 18 | import com.rabbitmq.client.Address;
|
| 19 | +import com.rabbitmq.client.AddressResolver; |
19 | 20 | import com.rabbitmq.client.Connection;
|
20 | 21 | import com.rabbitmq.client.ConnectionFactory;
|
| 22 | +import com.rabbitmq.client.DnsRecordIpAddressResolver; |
| 23 | +import com.rabbitmq.client.ListAddressResolver; |
21 | 24 | import com.rabbitmq.client.MetricsCollector;
|
22 | 25 | import com.rabbitmq.client.impl.AMQConnection;
|
23 | 26 | import com.rabbitmq.client.impl.ConnectionParams;
|
24 | 27 | import com.rabbitmq.client.impl.CredentialsProvider;
|
25 | 28 | import com.rabbitmq.client.impl.FrameHandler;
|
26 | 29 | import com.rabbitmq.client.impl.FrameHandlerFactory;
|
| 30 | +import org.hamcrest.Matchers; |
| 31 | +import org.junit.Assert; |
27 | 32 | import org.junit.Test;
|
28 | 33 |
|
29 | 34 | import java.io.IOException;
|
| 35 | +import java.util.List; |
30 | 36 | import java.util.Queue;
|
31 | 37 | import java.util.concurrent.ArrayBlockingQueue;
|
32 | 38 | import java.util.concurrent.TimeoutException;
|
33 | 39 | import java.util.concurrent.atomic.AtomicBoolean;
|
| 40 | +import java.util.concurrent.atomic.AtomicReference; |
34 | 41 |
|
| 42 | +import static org.hamcrest.Matchers.allOf; |
| 43 | +import static org.hamcrest.Matchers.instanceOf; |
| 44 | +import static org.hamcrest.Matchers.notNullValue; |
35 | 45 | import static org.junit.Assert.*;
|
36 | 46 | import static org.mockito.Mockito.*;
|
37 | 47 |
|
@@ -90,4 +100,53 @@ protected AMQConnection createConnection(ConnectionParams params, FrameHandler f
|
90 | 100 | assertTrue(createCalled.get());
|
91 | 101 | }
|
92 | 102 |
|
| 103 | + @Test public void shouldUseDnsResolutionWhenOneAddressAndNoTls() throws Exception { |
| 104 | + AMQConnection connection = mock(AMQConnection.class); |
| 105 | + AtomicReference<AddressResolver> addressResolver = new AtomicReference<>(); |
| 106 | + |
| 107 | + ConnectionFactory connectionFactory = new ConnectionFactory() { |
| 108 | + @Override |
| 109 | + protected AMQConnection createConnection(ConnectionParams params, FrameHandler frameHandler, |
| 110 | + MetricsCollector metricsCollector) { |
| 111 | + return connection; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + protected AddressResolver createAddressResolver(List<Address> addresses) { |
| 116 | + addressResolver.set(super.createAddressResolver(addresses)); |
| 117 | + return addressResolver.get(); |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + doNothing().when(connection).start(); |
| 122 | + connectionFactory.newConnection(); |
| 123 | + |
| 124 | + assertThat(addressResolver.get(), allOf(notNullValue(), instanceOf(DnsRecordIpAddressResolver.class))); |
| 125 | + } |
| 126 | + |
| 127 | + @Test public void shouldNotUseDnsResolutionWhenOneAddressAndNoTls() throws Exception { |
| 128 | + AMQConnection connection = mock(AMQConnection.class); |
| 129 | + AtomicReference<AddressResolver> addressResolver = new AtomicReference<>(); |
| 130 | + |
| 131 | + ConnectionFactory connectionFactory = new ConnectionFactory() { |
| 132 | + @Override |
| 133 | + protected AMQConnection createConnection(ConnectionParams params, FrameHandler frameHandler, |
| 134 | + MetricsCollector metricsCollector) { |
| 135 | + return connection; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + protected AddressResolver createAddressResolver(List<Address> addresses) { |
| 140 | + addressResolver.set(super.createAddressResolver(addresses)); |
| 141 | + return addressResolver.get(); |
| 142 | + } |
| 143 | + }; |
| 144 | + |
| 145 | + doNothing().when(connection).start(); |
| 146 | + connectionFactory.useSslProtocol(); |
| 147 | + connectionFactory.newConnection(); |
| 148 | + |
| 149 | + assertThat(addressResolver.get(), allOf(notNullValue(), instanceOf(ListAddressResolver.class))); |
| 150 | + } |
| 151 | + |
93 | 152 | }
|
0 commit comments