File: IdleConnectionTimeoutTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (227 lines) | stat: -rw-r--r-- 11,509 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8288717
 * @summary Tests that when the idleConnectionTimeoutEvent is configured in HTTP/2,
 *          an HTTP/2 connection will close within the specified interval if there
 *          are no active streams on the connection.
 * @library /test/lib /test/jdk/java/net/httpclient/lib
 * @build jdk.httpclient.test.lib.http2.Http2TestServer
 *
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=errors -Djdk.httpclient.keepalive.timeout=1
 *                                                             IdleConnectionTimeoutTest
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=errors -Djdk.httpclient.keepalive.timeout=2
 *                                                             IdleConnectionTimeoutTest
 *
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=errors -Djdk.httpclient.keepalive.timeout.h2=1
 *                                                             IdleConnectionTimeoutTest
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=errors -Djdk.httpclient.keepalive.timeout.h2=2
 *                                                             IdleConnectionTimeoutTest
 *
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=errors -Djdk.httpclient.keepalive.timeout.h2=1
 *                                                            -Djdk.httpclient.keepalive.timeout=2
 *                                                             IdleConnectionTimeoutTest
 *
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=errors IdleConnectionTimeoutTest
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=errors -Djdk.httpclient.keepalive.timeout.h2=-1
 *                                                             IdleConnectionTimeoutTest
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=errors,trace -Djdk.httpclient.keepalive.timeout.h2=abc
 *                                                                   IdleConnectionTimeoutTest
 */

import jdk.httpclient.test.lib.http2.BodyOutputStream;
import jdk.httpclient.test.lib.http2.Http2TestExchangeImpl;
import jdk.httpclient.test.lib.http2.Http2TestServerConnection;
import jdk.internal.net.http.common.HttpHeadersBuilder;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
import jdk.httpclient.test.lib.http2.Http2TestServer;
import jdk.httpclient.test.lib.http2.Http2TestExchange;
import jdk.httpclient.test.lib.http2.Http2Handler;

import javax.net.ssl.SSLSession;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.net.http.HttpClient.Version.HTTP_2;
import static org.testng.Assert.assertEquals;

public class IdleConnectionTimeoutTest {

    static Http2TestServer http2TestServer;
    URI timeoutUri;
    URI noTimeoutUri;
    final String IDLE_CONN_PROPERTY = "jdk.httpclient.keepalive.timeout.h2";
    final String KEEP_ALIVE_PROPERTY = "jdk.httpclient.keepalive.timeout";
    final String TIMEOUT_PATH = "/serverTimeoutHandler";
    final String NO_TIMEOUT_PATH = "/noServerTimeoutHandler";
    static final PrintStream testLog = System.err;

    @BeforeTest
    public void setup() throws Exception {
        http2TestServer = new Http2TestServer(false, 0);
        http2TestServer.addHandler(new ServerTimeoutHandler(), TIMEOUT_PATH);
        http2TestServer.addHandler(new ServerNoTimeoutHandler(), NO_TIMEOUT_PATH);
        http2TestServer.setExchangeSupplier(TestExchangeSupplier::new);

        http2TestServer.start();
        int port = http2TestServer.getAddress().getPort();
        timeoutUri = new URI("http://localhost:" + port + TIMEOUT_PATH);
        noTimeoutUri = new URI("http://localhost:" + port + NO_TIMEOUT_PATH);
    }

    /*
       If the InetSocketAddress of the first remote connection is not equal to the address of the
       second remote connection, then the idleConnectionTimeoutEvent has occurred and a new connection
       was made to carry out the second request by the client.
    */
    @Test
    public void test() throws InterruptedException {
        String timeoutVal = System.getProperty(IDLE_CONN_PROPERTY);
        String keepAliveVal = System.getProperty(KEEP_ALIVE_PROPERTY);
        testLog.println("Test run for " + IDLE_CONN_PROPERTY + "=" + timeoutVal);

        int sleepTime = 0;
        HttpClient hc = HttpClient.newBuilder().version(HTTP_2).build();
        HttpRequest hreq;
        HttpResponse<String> hresp;
        if (timeoutVal != null) {
            if (keepAliveVal != null) {
                // In this case, specified h2 timeout should override keep alive timeout.
                // Timeout should occur
                hreq = HttpRequest.newBuilder(timeoutUri).version(HTTP_2).GET().build();
                sleepTime = 2000;
                hresp = runRequest(hc, hreq, sleepTime);
                assertEquals(hresp.statusCode(), 200, "idleConnectionTimeoutEvent was expected but did not occur");
            } else if (timeoutVal.equals("1")) {
                // Timeout should occur
                hreq = HttpRequest.newBuilder(timeoutUri).version(HTTP_2).GET().build();
                sleepTime = 2000;
                hresp = runRequest(hc, hreq, sleepTime);
                assertEquals(hresp.statusCode(), 200, "idleConnectionTimeoutEvent was expected but did not occur");
            } else if (timeoutVal.equals("2")) {
                // Timeout should not occur
                hreq = HttpRequest.newBuilder(noTimeoutUri).version(HTTP_2).GET().build();
                sleepTime = 1000;
                hresp = runRequest(hc, hreq, sleepTime);
                assertEquals(hresp.statusCode(), 200, "idleConnectionTimeoutEvent was not expected but occurred");
            } else if (timeoutVal.equals("abc") || timeoutVal.equals("-1")) {
                // Timeout should not occur
                hreq = HttpRequest.newBuilder(noTimeoutUri).version(HTTP_2).GET().build();
                hresp = runRequest(hc, hreq, sleepTime);
                assertEquals(hresp.statusCode(), 200, "idleConnectionTimeoutEvent was not expected but occurred");
            }
        } else {
            // When no value is specified then no timeout should occur (default keep alive value of 600 used)
            hreq = HttpRequest.newBuilder(noTimeoutUri).version(HTTP_2).GET().build();
            hresp = runRequest(hc, hreq, sleepTime);
            assertEquals(hresp.statusCode(), 200, "idleConnectionTimeoutEvent should not occur, no value was specified for this property");
        }
    }

    private HttpResponse<String> runRequest(HttpClient hc, HttpRequest req, int sleepTime) throws InterruptedException {
        CompletableFuture<HttpResponse<String>> request = hc.sendAsync(req, HttpResponse.BodyHandlers.ofString(UTF_8));
        HttpResponse<String> hresp = request.join();
        assertEquals(hresp.statusCode(), 200);

        Thread.sleep(sleepTime);
        request = hc.sendAsync(req, HttpResponse.BodyHandlers.ofString(UTF_8));
        return request.join();
    }

    static class ServerTimeoutHandler implements Http2Handler {

        volatile Object firstConnection = null;

        @Override
        public void handle(Http2TestExchange exchange) throws IOException {
            if (exchange instanceof TestExchangeSupplier exch) {
                if (firstConnection == null) {
                    firstConnection = exch.getTestConnection();
                    exch.sendResponseHeaders(200, 0);
                } else {
                    var secondConnection = exch.getTestConnection();

                    if (firstConnection != secondConnection) {
                        testLog.println("ServerTimeoutHandler: New Connection was used, idleConnectionTimeoutEvent fired."
                                + " First Connection Hash: " + firstConnection + ", Second Connection Hash: " + secondConnection);
                        exch.sendResponseHeaders(200, 0);
                    } else {
                        testLog.println("ServerTimeoutHandler: Same Connection was used, idleConnectionTimeoutEvent did not fire."
                                + " First Connection Hash: " + firstConnection + ", Second Connection Hash: " + secondConnection);
                        exch.sendResponseHeaders(400, 0);
                    }
                }
            }
        }
    }

    static class ServerNoTimeoutHandler implements Http2Handler {

        volatile Object firstConnection = null;

        @Override
        public void handle(Http2TestExchange exchange) throws IOException {
            if (exchange instanceof TestExchangeSupplier exch) {
                if (firstConnection == null) {
                    firstConnection = exch.getTestConnection();
                    exch.sendResponseHeaders(200, 0);
                } else {
                    var secondConnection = exch.getTestConnection();

                    if (firstConnection == secondConnection) {
                        testLog.println("ServerTimeoutHandler: Same Connection was used, idleConnectionTimeoutEvent did not fire."
                                + " First Connection Hash: " + firstConnection + ", Second Connection Hash: " + secondConnection);
                        exch.sendResponseHeaders(200, 0);
                    } else {
                        testLog.println("ServerTimeoutHandler: Different Connection was used, idleConnectionTimeoutEvent fired."
                                + " First Connection Hash: " + firstConnection + ", Second Connection Hash: " + secondConnection);
                        exch.sendResponseHeaders(400, 0);
                    }
                }
            }
        }
    }

    static class TestExchangeSupplier extends Http2TestExchangeImpl {

        public TestExchangeSupplier(int streamid, String method, HttpHeaders reqheaders, HttpHeadersBuilder rspheadersBuilder, URI uri, InputStream is, SSLSession sslSession, BodyOutputStream os, Http2TestServerConnection conn, boolean pushAllowed) {
            super(streamid, method, reqheaders, rspheadersBuilder, uri, is, sslSession, os, conn, pushAllowed);
        }

        public Http2TestServerConnection getTestConnection() {
            return this.conn;
        }
    }
}