Skip to content

Commit 30bae84

Browse files
committed
Fix test
1 parent f38f83d commit 30bae84

3 files changed

Lines changed: 104 additions & 57 deletions

File tree

httpclient5/src/test/java/org/apache/hc/client5/http/Rfc6724AddressSelectingDnsResolverTest.java

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
127
package org.apache.hc.client5.http;
228

329
import static org.junit.jupiter.api.Assertions.assertEquals;
430
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
531
import static org.junit.jupiter.api.Assertions.assertNotEquals;
6-
import static org.junit.jupiter.api.Assertions.assertTrue;
732
import static org.mockito.Mockito.when;
833

934
import java.net.Inet4Address;
@@ -19,17 +44,17 @@
1944
import org.junit.jupiter.api.Test;
2045
import org.mockito.Mockito;
2146

22-
public class Rfc6724AddressSelectingDnsResolverTest {
47+
class Rfc6724AddressSelectingDnsResolverTest {
2348

2449
private DnsResolver delegate;
2550

2651
@BeforeEach
27-
public void setUp() {
52+
void setUp() {
2853
delegate = Mockito.mock(DnsResolver.class);
2954
}
3055

3156
@Test
32-
public void ipv4Only_filtersOutIPv6() throws Exception {
57+
void ipv4Only_filtersOutIPv6() throws Exception {
3358
final InetAddress v4 = InetAddress.getByName("203.0.113.10"); // TEST-NET-3
3459
final InetAddress v6 = InetAddress.getByName("2001:db8::10"); // documentation prefix
3560

@@ -39,13 +64,14 @@ public void ipv4Only_filtersOutIPv6() throws Exception {
3964
new Rfc6724AddressSelectingDnsResolver(delegate, ProtocolFamilyPreference.IPV4_ONLY);
4065

4166
final InetAddress[] ordered = r.resolve("dual.example");
67+
Assertions.assertNotNull(ordered);
4268
assertEquals(1, ordered.length);
43-
assertTrue(ordered[0] instanceof Inet4Address);
69+
assertInstanceOf(Inet4Address.class, ordered[0]);
4470
assertEquals(v4, ordered[0]);
4571
}
4672

4773
@Test
48-
public void ipv6Only_filtersOutIPv4() throws Exception {
74+
void ipv6Only_filtersOutIPv4() throws Exception {
4975
final InetAddress v4 = InetAddress.getByName("192.0.2.1"); // TEST-NET-1
5076
final InetAddress v6 = InetAddress.getByName("2001:db8::1");
5177

@@ -62,7 +88,7 @@ public void ipv6Only_filtersOutIPv4() throws Exception {
6288
}
6389

6490
@Test
65-
public void ipv4Only_emptyWhenNoIPv4Candidates() throws Exception {
91+
void ipv4Only_emptyWhenNoIPv4Candidates() throws Exception {
6692
final InetAddress v6a = InetAddress.getByName("2001:db8::1");
6793
final InetAddress v6b = InetAddress.getByName("2001:db8::2");
6894

@@ -77,7 +103,7 @@ public void ipv4Only_emptyWhenNoIPv4Candidates() throws Exception {
77103
}
78104

79105
@Test
80-
public void preferIpv6_groupsAllV6First_preservingRelativeOrder() throws Exception {
106+
void preferIpv6_groupsAllV6First_preservingRelativeOrder() throws Exception {
81107
final InetAddress v4a = InetAddress.getByName("192.0.2.1");
82108
final InetAddress v6a = InetAddress.getByName("2001:db8::1");
83109
final InetAddress v4b = InetAddress.getByName("203.0.113.10");
@@ -93,7 +119,8 @@ public void preferIpv6_groupsAllV6First_preservingRelativeOrder() throws Excepti
93119
// all v6 first, in original relative order
94120
final List<InetAddress> v6Seen = new ArrayList<>();
95121
final List<InetAddress> v4Seen = new ArrayList<>();
96-
for (InetAddress a : out) {
122+
Assertions.assertNotNull(out);
123+
for (final InetAddress a : out) {
97124
if (a instanceof Inet6Address) {
98125
v6Seen.add(a);
99126
} else {
@@ -104,11 +131,11 @@ public void preferIpv6_groupsAllV6First_preservingRelativeOrder() throws Excepti
104131
assertEquals(Arrays.asList(v4a, v4b), v4Seen);
105132

106133
// ensure first element is IPv6 (grouping actually happened)
107-
assertTrue(out[0] instanceof Inet6Address);
134+
assertInstanceOf(Inet6Address.class, out[0]);
108135
}
109136

110137
@Test
111-
public void interleave_alternatesFamilies_and_preservesRelativeOrder() throws Exception {
138+
void interleave_alternatesFamilies_and_preservesRelativeOrder() throws Exception {
112139
final InetAddress v6a = InetAddress.getByName("2001:db8::1");
113140
final InetAddress v6b = InetAddress.getByName("2001:db8::2");
114141
final InetAddress v4a = InetAddress.getByName("192.0.2.1");
@@ -122,9 +149,10 @@ public void interleave_alternatesFamilies_and_preservesRelativeOrder() throws Ex
122149
final InetAddress[] out = r.resolve("dual.example");
123150

124151
// Preserve per-family relative order
125-
final List<InetAddress> v6Seen = new ArrayList<InetAddress>();
126-
final List<InetAddress> v4Seen = new ArrayList<InetAddress>();
127-
for (InetAddress a : out) {
152+
final List<InetAddress> v6Seen = new ArrayList<>();
153+
final List<InetAddress> v4Seen = new ArrayList<>();
154+
Assertions.assertNotNull(out);
155+
for (final InetAddress a : out) {
128156
if (a instanceof Inet6Address) {
129157
v6Seen.add(a);
130158
} else {
@@ -135,15 +163,15 @@ public void interleave_alternatesFamilies_and_preservesRelativeOrder() throws Ex
135163
assertEquals(Arrays.asList(v4a, v4b), v4Seen);
136164

137165
// Alternation (as far as both families have remaining items)
138-
int pairs = Math.min(v6Seen.size(), v4Seen.size());
166+
final int pairs = Math.min(v6Seen.size(), v4Seen.size());
139167
for (int i = 1; i < pairs * 2; i++) {
140168
assertNotEquals(out[i - 1] instanceof Inet6Address, out[i] instanceof Inet6Address,
141169
"adjacent entries should alternate family under INTERLEAVE");
142170
}
143171
}
144172

145173
@Test
146-
public void canonicalHostname_delegates() throws Exception {
174+
void canonicalHostname_delegates() throws Exception {
147175
when(delegate.resolveCanonicalHostname("example.org")).thenReturn("canon.example.org");
148176
final Rfc6724AddressSelectingDnsResolver r =
149177
new Rfc6724AddressSelectingDnsResolver(delegate, ProtocolFamilyPreference.INTERLEAVE);

httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHappyEyeballs.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ public void cancelled() {
219219
}
220220
});
221221

222-
future.get();
222+
try {
223+
future.get();
224+
} catch (java.util.concurrent.ExecutionException e) {
225+
System.out.println(request + " -> " + e.getCause());
226+
}
223227
}
224228

225229
System.out.println("Shutting down");

httpclient5/src/test/java/org/apache/hc/client5/http/impl/nio/MultihomeIOSessionRequesterTest.java

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
127
package org.apache.hc.client5.http.impl.nio;
228

329
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
431
import static org.junit.jupiter.api.Assertions.assertNotNull;
532
import static org.junit.jupiter.api.Assertions.assertSame;
633
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -35,21 +62,21 @@
3562
import org.junit.jupiter.api.Test;
3663
import org.mockito.Mockito;
3764

38-
public class MultihomeIOSessionRequesterTest {
65+
class MultihomeIOSessionRequesterTest {
3966

4067
private DnsResolver dnsResolver;
4168
private ConnectionInitiator connectionInitiator;
4269
private NamedEndpoint namedEndpoint;
4370

4471
@BeforeEach
45-
public void setUp() {
72+
void setUp() {
4673
dnsResolver = Mockito.mock(DnsResolver.class);
4774
connectionInitiator = Mockito.mock(ConnectionInitiator.class);
4875
namedEndpoint = Mockito.mock(NamedEndpoint.class);
4976
}
5077

5178
@Test
52-
public void testConnectWithMultipleAddresses_allFail_surfaceLastFailure() throws Exception {
79+
void testConnectWithMultipleAddresses_allFail_surfaceLastFailure() throws Exception {
5380
final MultihomeIOSessionRequester sessionRequester =
5481
new MultihomeIOSessionRequester(dnsResolver, ConnectionConfig.custom()
5582
.setStaggeredConnectEnabled(false)
@@ -69,7 +96,7 @@ public void testConnectWithMultipleAddresses_allFail_surfaceLastFailure() throws
6996
when(connectionInitiator.connect(any(), any(), any(), any(), any(), any()))
7097
.thenAnswer(invocation -> {
7198
final FutureCallback<IOSession> cb = invocation.getArgument(5);
72-
final CompletableFuture<IOSession> f = new CompletableFuture<IOSession>();
99+
final CompletableFuture<IOSession> f = new CompletableFuture<>();
73100
final IOException io = new IOException("Simulated connection failure");
74101
cb.failed(io);
75102
f.completeExceptionally(io);
@@ -82,12 +109,12 @@ public void testConnectWithMultipleAddresses_allFail_surfaceLastFailure() throws
82109

83110
assertTrue(future.isDone());
84111
final ExecutionException ex = assertThrows(ExecutionException.class, future::get);
85-
assertTrue(ex.getCause() instanceof IOException);
112+
assertInstanceOf(IOException.class, ex.getCause());
86113
assertEquals("Simulated connection failure", ex.getCause().getMessage());
87114
}
88115

89116
@Test
90-
public void testConnectSuccessfulAfterRetries() throws Exception {
117+
void testConnectSuccessfulAfterRetries() throws Exception {
91118
final MultihomeIOSessionRequester sessionRequester =
92119
new MultihomeIOSessionRequester(dnsResolver, ConnectionConfig.custom()
93120
.setStaggeredConnectEnabled(false)
@@ -108,7 +135,7 @@ public void testConnectSuccessfulAfterRetries() throws Exception {
108135
.thenAnswer(invocation -> {
109136
final FutureCallback<IOSession> cb = invocation.getArgument(5);
110137
final InetSocketAddress remoteAddress = invocation.getArgument(1);
111-
final CompletableFuture<IOSession> f = new CompletableFuture<IOSession>();
138+
final CompletableFuture<IOSession> f = new CompletableFuture<>();
112139
if (remoteAddress.getAddress().equals(address1)) {
113140
final IOException io = new IOException("Simulated connection failure");
114141
cb.failed(io);
@@ -130,7 +157,7 @@ public void testConnectSuccessfulAfterRetries() throws Exception {
130157
}
131158

132159
@Test
133-
public void testHappyEyeballs_fastV4BeatsSlowerV6() throws Exception {
160+
void testHappyEyeballs_fastV4BeatsSlowerV6() throws Exception {
134161
final MultihomeIOSessionRequester sessionRequester =
135162
new MultihomeIOSessionRequester(dnsResolver, ConnectionConfig.custom()
136163
.setStaggeredConnectEnabled(true)
@@ -155,25 +182,19 @@ public void testHappyEyeballs_fastV4BeatsSlowerV6() throws Exception {
155182
.thenAnswer(invocation -> {
156183
final InetSocketAddress remote = invocation.getArgument(1);
157184
final FutureCallback<IOSession> cb = invocation.getArgument(5);
158-
final CompletableFuture<IOSession> f = new CompletableFuture<IOSession>();
185+
final CompletableFuture<IOSession> f = new CompletableFuture<>();
159186
final ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
160187
if (remote.equals(aV6)) {
161-
ses.schedule(new Runnable() {
162-
@Override
163-
public void run() {
164-
cb.completed(v6Session);
165-
f.complete(v6Session);
166-
ses.shutdown();
167-
}
188+
ses.schedule(() -> {
189+
cb.completed(v6Session);
190+
f.complete(v6Session);
191+
ses.shutdown();
168192
}, 200, TimeUnit.MILLISECONDS);
169193
} else {
170-
ses.schedule(new Runnable() {
171-
@Override
172-
public void run() {
173-
cb.completed(v4Session);
174-
f.complete(v4Session);
175-
ses.shutdown();
176-
}
194+
ses.schedule(() -> {
195+
cb.completed(v4Session);
196+
f.complete(v4Session);
197+
ses.shutdown();
177198
}, 50, TimeUnit.MILLISECONDS);
178199
}
179200
return f;
@@ -189,7 +210,7 @@ public void run() {
189210
}
190211

191212
@Test
192-
public void testHappyEyeballs_v6Fails_v4Succeeds() throws Exception {
213+
void testHappyEyeballs_v6Fails_v4Succeeds() throws Exception {
193214
final MultihomeIOSessionRequester sessionRequester =
194215
new MultihomeIOSessionRequester(dnsResolver, ConnectionConfig.custom()
195216
.setStaggeredConnectEnabled(true)
@@ -213,26 +234,20 @@ public void testHappyEyeballs_v6Fails_v4Succeeds() throws Exception {
213234
.thenAnswer(invocation -> {
214235
final InetSocketAddress remote = invocation.getArgument(1);
215236
final FutureCallback<IOSession> cb = invocation.getArgument(5);
216-
final CompletableFuture<IOSession> f = new CompletableFuture<IOSession>();
237+
final CompletableFuture<IOSession> f = new CompletableFuture<>();
217238
final ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
218239
if (remote.equals(aV6)) {
219-
ses.schedule(new Runnable() {
220-
@Override
221-
public void run() {
222-
final IOException io = new IOException("v6 down");
223-
cb.failed(io);
224-
f.completeExceptionally(io);
225-
ses.shutdown();
226-
}
240+
ses.schedule(() -> {
241+
final IOException io = new IOException("v6 down");
242+
cb.failed(io);
243+
f.completeExceptionally(io);
244+
ses.shutdown();
227245
}, 30, TimeUnit.MILLISECONDS);
228246
} else {
229-
ses.schedule(new Runnable() {
230-
@Override
231-
public void run() {
232-
cb.completed(v4Session);
233-
f.complete(v4Session);
234-
ses.shutdown();
235-
}
247+
ses.schedule(() -> {
248+
cb.completed(v4Session);
249+
f.complete(v4Session);
250+
ses.shutdown();
236251
}, 60, TimeUnit.MILLISECONDS);
237252
}
238253
return f;

0 commit comments

Comments
 (0)