Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented HTTP2Client connect timeout.
- Loading branch information
Showing
8 changed files
with
280 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...y-http2/http2-client/src/test/java/org/eclipse/jetty/http2/client/ConnectTimeoutTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// ======================================================================== | ||
// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd. | ||
// ------------------------------------------------------------------------ | ||
// All rights reserved. This program and the accompanying materials | ||
// are made available under the terms of the Eclipse Public License v1.0 | ||
// and Apache License v2.0 which accompanies this distribution. | ||
// | ||
// The Eclipse Public License is available at | ||
// http://www.eclipse.org/legal/epl-v10.html | ||
// | ||
// The Apache License v2.0 is available at | ||
// http://www.opensource.org/licenses/apache2.0.php | ||
// | ||
// You may elect to redistribute this code under either of these licenses. | ||
// ======================================================================== | ||
// | ||
|
||
package org.eclipse.jetty.http2.client; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.Socket; | ||
import java.net.SocketTimeoutException; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.eclipse.jetty.http2.api.Session; | ||
import org.eclipse.jetty.http2.api.server.ServerSessionListener; | ||
import org.eclipse.jetty.util.Promise; | ||
import org.junit.Assert; | ||
import org.junit.Assume; | ||
import org.junit.Test; | ||
|
||
public class ConnectTimeoutTest extends AbstractTest | ||
{ | ||
@Test | ||
public void testConnectTimeout() throws Exception | ||
{ | ||
final String host = "10.255.255.1"; | ||
final int port = 80; | ||
int connectTimeout = 1000; | ||
assumeConnectTimeout(host, port, connectTimeout); | ||
|
||
start(new ServerSessionListener.Adapter()); | ||
client.setConnectTimeout(connectTimeout); | ||
|
||
InetSocketAddress address = new InetSocketAddress(host, port); | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
client.connect(address, new Session.Listener.Adapter(), new Promise.Adapter<Session>() | ||
{ | ||
@Override | ||
public void failed(Throwable x) | ||
{ | ||
Assert.assertTrue(x instanceof SocketTimeoutException); | ||
latch.countDown(); | ||
} | ||
}); | ||
|
||
Assert.assertTrue(latch.await(2 * connectTimeout, TimeUnit.MILLISECONDS)); | ||
} | ||
|
||
private void assumeConnectTimeout(String host, int port, int connectTimeout) throws IOException | ||
{ | ||
try (Socket socket = new Socket()) | ||
{ | ||
// Try to connect to a private address in the 10.x.y.z range. | ||
// These addresses are usually not routed, so an attempt to | ||
// connect to them will hang the connection attempt, which is | ||
// what we want to simulate in this test. | ||
socket.connect(new InetSocketAddress(host, port), connectTimeout); | ||
// Abort the test if we can connect. | ||
Assume.assumeTrue(false); | ||
} | ||
catch (SocketTimeoutException x) | ||
{ | ||
// Expected timeout during connect, continue the test. | ||
Assume.assumeTrue(true); | ||
} | ||
catch (Throwable x) | ||
{ | ||
// Abort if any other exception happens. | ||
Assume.assumeTrue(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.