2014-09-19 15:36:32 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2014 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package io.netty.resolver.dns;
|
|
|
|
|
|
|
|
import io.netty.util.NetUtil;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.IdentityHashMap;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2017-03-31 22:52:46 +02:00
|
|
|
import static io.netty.resolver.dns.DefaultDnsServerAddressStreamProvider.defaultAddressList;
|
2014-09-19 15:36:32 +02:00
|
|
|
import static org.hamcrest.Matchers.*;
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
public class DnsServerAddressesTest {
|
|
|
|
|
|
|
|
private static final InetSocketAddress ADDR1 = new InetSocketAddress(NetUtil.LOCALHOST, 1);
|
|
|
|
private static final InetSocketAddress ADDR2 = new InetSocketAddress(NetUtil.LOCALHOST, 2);
|
|
|
|
private static final InetSocketAddress ADDR3 = new InetSocketAddress(NetUtil.LOCALHOST, 3);
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testDefaultAddresses() {
|
2017-03-31 22:52:46 +02:00
|
|
|
assertThat(defaultAddressList().size(), is(greaterThan(0)));
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testSequential() {
|
2015-08-19 04:51:15 +02:00
|
|
|
DnsServerAddresses seq = DnsServerAddresses.sequential(ADDR1, ADDR2, ADDR3);
|
|
|
|
assertThat(seq.stream(), is(not(sameInstance(seq.stream()))));
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
for (int j = 0; j < 2; j ++) {
|
2015-08-19 04:51:15 +02:00
|
|
|
DnsServerAddressStream i = seq.stream();
|
2014-09-19 15:36:32 +02:00
|
|
|
assertNext(i, ADDR1);
|
|
|
|
assertNext(i, ADDR2);
|
|
|
|
assertNext(i, ADDR3);
|
|
|
|
assertNext(i, ADDR1);
|
|
|
|
assertNext(i, ADDR2);
|
|
|
|
assertNext(i, ADDR3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testRotational() {
|
2015-08-19 04:51:15 +02:00
|
|
|
DnsServerAddresses seq = DnsServerAddresses.rotational(ADDR1, ADDR2, ADDR3);
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2015-08-19 04:51:15 +02:00
|
|
|
DnsServerAddressStream i = seq.stream();
|
2014-09-19 15:36:32 +02:00
|
|
|
assertNext(i, ADDR1);
|
|
|
|
assertNext(i, ADDR2);
|
|
|
|
assertNext(i, ADDR3);
|
|
|
|
assertNext(i, ADDR1);
|
|
|
|
assertNext(i, ADDR2);
|
|
|
|
assertNext(i, ADDR3);
|
|
|
|
|
2015-08-19 04:51:15 +02:00
|
|
|
i = seq.stream();
|
2014-09-19 15:36:32 +02:00
|
|
|
assertNext(i, ADDR2);
|
|
|
|
assertNext(i, ADDR3);
|
|
|
|
assertNext(i, ADDR1);
|
|
|
|
assertNext(i, ADDR2);
|
|
|
|
assertNext(i, ADDR3);
|
|
|
|
assertNext(i, ADDR1);
|
|
|
|
|
2015-08-19 04:51:15 +02:00
|
|
|
i = seq.stream();
|
2014-09-19 15:36:32 +02:00
|
|
|
assertNext(i, ADDR3);
|
|
|
|
assertNext(i, ADDR1);
|
|
|
|
assertNext(i, ADDR2);
|
|
|
|
assertNext(i, ADDR3);
|
|
|
|
assertNext(i, ADDR1);
|
|
|
|
assertNext(i, ADDR2);
|
|
|
|
|
2015-08-19 04:51:15 +02:00
|
|
|
i = seq.stream();
|
2014-09-19 15:36:32 +02:00
|
|
|
assertNext(i, ADDR1);
|
|
|
|
assertNext(i, ADDR2);
|
|
|
|
assertNext(i, ADDR3);
|
|
|
|
assertNext(i, ADDR1);
|
|
|
|
assertNext(i, ADDR2);
|
|
|
|
assertNext(i, ADDR3);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testShuffled() {
|
2015-08-19 04:51:15 +02:00
|
|
|
DnsServerAddresses seq = DnsServerAddresses.shuffled(ADDR1, ADDR2, ADDR3);
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
// Ensure that all three addresses are returned by the iterator.
|
|
|
|
// In theory, this test can fail at extremely low chance, but we don't really care.
|
2019-01-22 16:07:26 +01:00
|
|
|
Set<InetSocketAddress> set = Collections.newSetFromMap(new IdentityHashMap<>());
|
2015-08-19 04:51:15 +02:00
|
|
|
DnsServerAddressStream i = seq.stream();
|
2014-09-19 15:36:32 +02:00
|
|
|
for (int j = 0; j < 1048576; j ++) {
|
|
|
|
set.add(i.next());
|
|
|
|
}
|
|
|
|
|
|
|
|
assertThat(set.size(), is(3));
|
2015-08-19 04:51:15 +02:00
|
|
|
assertThat(seq.stream(), is(not(sameInstance(seq.stream()))));
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testSingleton() {
|
2015-08-19 04:51:15 +02:00
|
|
|
DnsServerAddresses seq = DnsServerAddresses.singleton(ADDR1);
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
// Should return the same iterator instance for least possible footprint.
|
2015-08-19 04:51:15 +02:00
|
|
|
assertThat(seq.stream(), is(sameInstance(seq.stream())));
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2015-08-19 04:51:15 +02:00
|
|
|
DnsServerAddressStream i = seq.stream();
|
2014-09-19 15:36:32 +02:00
|
|
|
assertNext(i, ADDR1);
|
|
|
|
assertNext(i, ADDR1);
|
|
|
|
assertNext(i, ADDR1);
|
|
|
|
}
|
|
|
|
|
2015-08-19 04:51:15 +02:00
|
|
|
private static void assertNext(DnsServerAddressStream i, InetSocketAddress addr) {
|
2014-09-19 15:36:32 +02:00
|
|
|
assertThat(i.next(), is(sameInstance(addr)));
|
|
|
|
}
|
|
|
|
}
|