51
51
* IdleConnectionTimeoutTest
52
52
*/
53
53
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 ;
54
58
import org .testng .annotations .BeforeTest ;
55
59
import org .testng .annotations .Test ;
56
60
57
61
import java .io .IOException ;
62
+ import java .io .InputStream ;
58
63
import java .io .PrintStream ;
59
- import java .net .InetSocketAddress ;
60
64
import java .net .URI ;
61
65
import java .net .http .HttpClient ;
66
+ import java .net .http .HttpHeaders ;
62
67
import java .net .http .HttpRequest ;
63
68
import java .net .http .HttpResponse ;
64
69
import java .util .concurrent .CompletableFuture ;
65
70
import jdk .httpclient .test .lib .http2 .Http2TestServer ;
66
71
import jdk .httpclient .test .lib .http2 .Http2TestExchange ;
67
72
import jdk .httpclient .test .lib .http2 .Http2Handler ;
68
73
74
+ import javax .net .ssl .SSLSession ;
75
+
69
76
import static java .nio .charset .StandardCharsets .UTF_8 ;
70
77
import static java .net .http .HttpClient .Version .HTTP_2 ;
71
78
import static org .testng .Assert .assertEquals ;
72
79
73
80
public class IdleConnectionTimeoutTest {
74
81
75
- Http2TestServer http2TestServer ;
82
+ static Http2TestServer http2TestServer ;
76
83
URI timeoutUri ;
77
84
URI noTimeoutUri ;
78
85
final String IDLE_CONN_PROPERTY = "jdk.httpclient.keepalive.timeout.h2" ;
@@ -86,6 +93,7 @@ public void setup() throws Exception {
86
93
http2TestServer = new Http2TestServer (false , 0 );
87
94
http2TestServer .addHandler (new ServerTimeoutHandler (), TIMEOUT_PATH );
88
95
http2TestServer .addHandler (new ServerNoTimeoutHandler (), NO_TIMEOUT_PATH );
96
+ http2TestServer .setExchangeSupplier (TestExchangeSupplier ::new );
89
97
90
98
http2TestServer .start ();
91
99
int port = http2TestServer .getAddress ().getPort ();
@@ -154,40 +162,66 @@ private HttpResponse<String> runRequest(HttpClient hc, HttpRequest req, int slee
154
162
155
163
static class ServerTimeoutHandler implements Http2Handler {
156
164
157
- InetSocketAddress remote ;
165
+ volatile Object firstConnection = null ;
158
166
159
167
@ Override
160
168
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
+ }
170
186
}
171
187
}
172
188
}
173
189
174
190
static class ServerNoTimeoutHandler implements Http2Handler {
175
191
176
- InetSocketAddress oldRemote ;
192
+ volatile Object firstConnection = null ;
177
193
178
194
@ Override
179
195
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
+ }
190
213
}
191
214
}
192
215
}
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