author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 42460 | jdk/test/java/net/httpclient/http2/ServerPush.java@7133f144981a |
child 48083 | b1c1b4ef4be2 |
child 55763 | 634d8e14c172 |
permissions | -rw-r--r-- |
37720 | 1 |
/* |
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
26 |
* @bug 8087112 8159814 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
27 |
* @library /lib/testlibrary server |
37720 | 28 |
* @build jdk.testlibrary.SimpleSSLContext |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
29 |
* @modules jdk.incubator.httpclient/jdk.incubator.http.internal.common |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
30 |
* jdk.incubator.httpclient/jdk.incubator.http.internal.frame |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
31 |
* jdk.incubator.httpclient/jdk.incubator.http.internal.hpack |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
32 |
* @run testng/othervm -Djdk.httpclient.HttpClient.log=errors,requests,responses ServerPush |
37720 | 33 |
*/ |
34 |
||
35 |
import java.io.*; |
|
36 |
import java.net.*; |
|
37 |
import java.nio.file.*; |
|
38 |
import java.nio.file.attribute.*; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
39 |
import jdk.incubator.http.*; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
40 |
import jdk.incubator.http.HttpResponse.MultiProcessor; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
41 |
import jdk.incubator.http.HttpResponse.BodyHandler; |
37720 | 42 |
import java.util.*; |
43 |
import java.util.concurrent.*; |
|
44 |
import org.testng.annotations.Test; |
|
45 |
||
46 |
public class ServerPush { |
|
47 |
||
48 |
static ExecutorService e = Executors.newCachedThreadPool(); |
|
49 |
||
50 |
static final int LOOPS = 13; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
51 |
static final int FILE_SIZE = 512 * 1024 + 343; |
37720 | 52 |
|
53 |
static Path tempFile; |
|
54 |
||
55 |
@Test(timeOut=30000) |
|
56 |
public static void test() throws Exception { |
|
57 |
Http2TestServer server = null; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
58 |
final Path dir = Files.createTempDirectory("serverPush"); |
37720 | 59 |
try { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
60 |
server = new Http2TestServer(false, 0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
61 |
server.addHandler(new PushHandler(FILE_SIZE, LOOPS), "/"); |
37720 | 62 |
tempFile = TestUtil.getAFile(FILE_SIZE); |
63 |
||
64 |
System.err.println("Server listening on port " + server.getAddress().getPort()); |
|
65 |
server.start(); |
|
66 |
int port = server.getAddress().getPort(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
67 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
68 |
// use multi-level path |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
69 |
URI uri = new URI("http://127.0.0.1:" + port + "/foo/a/b/c"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
70 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
37720 | 71 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
72 |
CompletableFuture<MultiMapResult<Path>> cf = |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
73 |
HttpClient.newBuilder().version(HttpClient.Version.HTTP_2) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
74 |
.executor(e).build().sendAsync( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
75 |
request, MultiProcessor.asMap((req) -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
76 |
URI u = req.uri(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
77 |
Path path = Paths.get(dir.toString(), u.getPath()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
78 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
79 |
Files.createDirectories(path.getParent()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
80 |
} catch (IOException ee) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
81 |
throw new UncheckedIOException(ee); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
82 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
83 |
return Optional.of(BodyHandler.asFile(path)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
84 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
85 |
)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
86 |
MultiMapResult<Path> results = cf.get(); |
37720 | 87 |
|
88 |
//HttpResponse resp = request.response(); |
|
89 |
System.err.println(results.size()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
90 |
Set<HttpRequest> requests = results.keySet(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
91 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
92 |
for (HttpRequest r : requests) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
93 |
URI u = r.uri(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
94 |
Path result = results.get(r).get().body(); |
37720 | 95 |
System.err.printf("%s -> %s\n", u.toString(), result.toString()); |
96 |
TestUtil.compareFiles(result, tempFile); |
|
97 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
98 |
if (requests.size() != LOOPS + 1) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
99 |
throw new RuntimeException("some results missing"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
100 |
System.out.println("TEST OK: sleeping for 5 sec"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
101 |
Thread.sleep (5 * 1000); |
37720 | 102 |
} finally { |
103 |
e.shutdownNow(); |
|
104 |
server.stop(); |
|
105 |
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { |
|
106 |
public FileVisitResult postVisitDirectory(Path dir, IOException exc) { |
|
107 |
dir.toFile().delete(); |
|
108 |
return FileVisitResult.CONTINUE; |
|
109 |
} |
|
110 |
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) { |
|
111 |
path.toFile().delete(); |
|
112 |
return FileVisitResult.CONTINUE; |
|
113 |
} |
|
114 |
}); |
|
115 |
} |
|
116 |
} |
|
117 |
} |