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+ */
127package org .apache .hc .client5 .http .impl .nio ;
228
329import static org .junit .jupiter .api .Assertions .assertEquals ;
30+ import static org .junit .jupiter .api .Assertions .assertInstanceOf ;
431import static org .junit .jupiter .api .Assertions .assertNotNull ;
532import static org .junit .jupiter .api .Assertions .assertSame ;
633import static org .junit .jupiter .api .Assertions .assertThrows ;
3562import org .junit .jupiter .api .Test ;
3663import 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