Skip to content

Commit 92474f1

Browse files
committed
8301243: java/net/httpclient/http2/IdleConnectionTimeoutTest.java intermittent failure
Reviewed-by: dfuchs
1 parent ee5f6e1 commit 92474f1

File tree

1 file changed

+58
-24
lines changed

1 file changed

+58
-24
lines changed

test/jdk/java/net/httpclient/http2/IdleConnectionTimeoutTest.java

+58-24
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,35 @@
5151
* IdleConnectionTimeoutTest
5252
*/
5353

54+
import jdk.httpclient.test.lib.http2.BodyOutputStream;
55+
import jdk.httpclient.test.lib.http2.Http2TestExchangeImpl;
56+
import jdk.httpclient.test.lib.http2.Http2TestServerConnection;
57+
import jdk.internal.net.http.common.HttpHeadersBuilder;
5458
import org.testng.annotations.BeforeTest;
5559
import org.testng.annotations.Test;
5660

5761
import java.io.IOException;
62+
import java.io.InputStream;
5863
import java.io.PrintStream;
59-
import java.net.InetSocketAddress;
6064
import java.net.URI;
6165
import java.net.http.HttpClient;
66+
import java.net.http.HttpHeaders;
6267
import java.net.http.HttpRequest;
6368
import java.net.http.HttpResponse;
6469
import java.util.concurrent.CompletableFuture;
6570
import jdk.httpclient.test.lib.http2.Http2TestServer;
6671
import jdk.httpclient.test.lib.http2.Http2TestExchange;
6772
import jdk.httpclient.test.lib.http2.Http2Handler;
6873

74+
import javax.net.ssl.SSLSession;
75+
6976
import static java.nio.charset.StandardCharsets.UTF_8;
7077
import static java.net.http.HttpClient.Version.HTTP_2;
7178
import static org.testng.Assert.assertEquals;
7279

7380
public class IdleConnectionTimeoutTest {
7481

75-
Http2TestServer http2TestServer;
82+
static Http2TestServer http2TestServer;
7683
URI timeoutUri;
7784
URI noTimeoutUri;
7885
final String IDLE_CONN_PROPERTY = "jdk.httpclient.keepalive.timeout.h2";
@@ -86,6 +93,7 @@ public void setup() throws Exception {
8693
http2TestServer = new Http2TestServer(false, 0);
8794
http2TestServer.addHandler(new ServerTimeoutHandler(), TIMEOUT_PATH);
8895
http2TestServer.addHandler(new ServerNoTimeoutHandler(), NO_TIMEOUT_PATH);
96+
http2TestServer.setExchangeSupplier(TestExchangeSupplier::new);
8997

9098
http2TestServer.start();
9199
int port = http2TestServer.getAddress().getPort();
@@ -154,40 +162,66 @@ private HttpResponse<String> runRequest(HttpClient hc, HttpRequest req, int slee
154162

155163
static class ServerTimeoutHandler implements Http2Handler {
156164

157-
InetSocketAddress remote;
165+
volatile Object firstConnection = null;
158166

159167
@Override
160168
public void handle(Http2TestExchange exchange) throws IOException {
161-
if (remote == null) {
162-
remote = exchange.getRemoteAddress();
163-
exchange.sendResponseHeaders(200, 0);
164-
} else if (!remote.equals(exchange.getRemoteAddress())) {
165-
testLog.println("ServerTimeoutHandler: New Connection was used, idleConnectionTimeoutEvent fired."
166-
+ " First remote: " + remote + ", Second Remote: " + exchange.getRemoteAddress());
167-
exchange.sendResponseHeaders(200, 0);
168-
} else {
169-
exchange.sendResponseHeaders(400, 0);
169+
if (exchange instanceof TestExchangeSupplier exch) {
170+
if (firstConnection == null) {
171+
firstConnection = exch.getTestConnection();
172+
exch.sendResponseHeaders(200, 0);
173+
} else {
174+
var secondConnection = exch.getTestConnection();
175+
176+
if (firstConnection != secondConnection) {
177+
testLog.println("ServerTimeoutHandler: New Connection was used, idleConnectionTimeoutEvent fired."
178+
+ " First Connection Hash: " + firstConnection + ", Second Connection Hash: " + secondConnection);
179+
exch.sendResponseHeaders(200, 0);
180+
} else {
181+
testLog.println("ServerTimeoutHandler: Same Connection was used, idleConnectionTimeoutEvent did not fire."
182+
+ " First Connection Hash: " + firstConnection + ", Second Connection Hash: " + secondConnection);
183+
exch.sendResponseHeaders(400, 0);
184+
}
185+
}
170186
}
171187
}
172188
}
173189

174190
static class ServerNoTimeoutHandler implements Http2Handler {
175191

176-
InetSocketAddress oldRemote;
192+
volatile Object firstConnection = null;
177193

178194
@Override
179195
public void handle(Http2TestExchange exchange) throws IOException {
180-
InetSocketAddress newRemote = exchange.getRemoteAddress();
181-
if (oldRemote == null) {
182-
oldRemote = newRemote;
183-
exchange.sendResponseHeaders(200, 0);
184-
} else if (oldRemote.equals(newRemote)) {
185-
testLog.println("ServerNoTimeoutHandler: Same Connection was used, idleConnectionTimeoutEvent did not fire."
186-
+ " First remote: " + oldRemote + ", Second Remote: " + newRemote);
187-
exchange.sendResponseHeaders(200, 0);
188-
} else {
189-
exchange.sendResponseHeaders(400, 0);
196+
if (exchange instanceof TestExchangeSupplier exch) {
197+
if (firstConnection == null) {
198+
firstConnection = exch.getTestConnection();
199+
exch.sendResponseHeaders(200, 0);
200+
} else {
201+
var secondConnection = exch.getTestConnection();
202+
203+
if (firstConnection == secondConnection) {
204+
testLog.println("ServerTimeoutHandler: Same Connection was used, idleConnectionTimeoutEvent did not fire."
205+
+ " First Connection Hash: " + firstConnection + ", Second Connection Hash: " + secondConnection);
206+
exch.sendResponseHeaders(200, 0);
207+
} else {
208+
testLog.println("ServerTimeoutHandler: Different Connection was used, idleConnectionTimeoutEvent fired."
209+
+ " First Connection Hash: " + firstConnection + ", Second Connection Hash: " + secondConnection);
210+
exch.sendResponseHeaders(400, 0);
211+
}
212+
}
190213
}
191214
}
192215
}
193-
}
216+
217+
static class TestExchangeSupplier extends Http2TestExchangeImpl {
218+
219+
public TestExchangeSupplier(int streamid, String method, HttpHeaders reqheaders, HttpHeadersBuilder rspheadersBuilder, URI uri, InputStream is, SSLSession sslSession, BodyOutputStream os, Http2TestServerConnection conn, boolean pushAllowed) {
220+
super(streamid, method, reqheaders, rspheadersBuilder, uri, is, sslSession, os, conn, pushAllowed);
221+
}
222+
223+
public Http2TestServerConnection getTestConnection() {
224+
return this.conn;
225+
}
226+
}
227+
}

0 commit comments

Comments
 (0)