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 26 * @bug 8087112 8177935 27 * @library /lib/testlibrary server 28 * @build jdk.testlibrary.SimpleSSLContext 29 * @modules jdk.incubator.httpclient/jdk.incubator.http.internal.common 30 * jdk.incubator.httpclient/jdk.incubator.http.internal.frame 31 * jdk.incubator.httpclient/jdk.incubator.http.internal.hpack 32 * @run testng/othervm -Djdk.httpclient.HttpClient.log=ssl,requests,responses,errors FixedThreadPoolTest 33 */ 34 35 import java.net.*; 36 import jdk.incubator.http.*; 37 import static jdk.incubator.http.HttpClient.Version.HTTP_2; 38 import javax.net.ssl.*; 39 import java.nio.file.*; 40 import java.util.concurrent.*; 41 import jdk.testlibrary.SimpleSSLContext; 42 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromFile; 43 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString; 44 import static jdk.incubator.http.HttpResponse.BodyHandler.asFile; 45 import static jdk.incubator.http.HttpResponse.BodyHandler.asString; 46 47 import org.testng.annotations.Test; 48 49 @Test 50 public class FixedThreadPoolTest { 51 static int httpPort, httpsPort; 52 static Http2TestServer httpServer, httpsServer; 53 static HttpClient client = null; 54 static ExecutorService exec; 55 static SSLContext sslContext; 56 57 static String httpURIString, httpsURIString; 58 59 static void initialize() throws Exception { 60 try { 61 SimpleSSLContext sslct = new SimpleSSLContext(); 62 sslContext = sslct.get(); 63 client = getClient(); 64 httpServer = new Http2TestServer(false, 0, exec, sslContext); 65 httpServer.addHandler(new Http2EchoHandler(), "/"); 66 httpPort = httpServer.getAddress().getPort(); 67 68 httpsServer = new Http2TestServer(true, 0, exec, sslContext); 69 httpsServer.addHandler(new Http2EchoHandler(), "/"); 70 71 httpsPort = httpsServer.getAddress().getPort(); 72 httpURIString = "http://127.0.0.1:" + httpPort + "/foo/"; 73 httpsURIString = "https://127.0.0.1:" + httpsPort + "/bar/"; 74 75 httpServer.start(); 76 httpsServer.start(); 77 } catch (Throwable e) { 78 System.err.println("Throwing now"); 79 e.printStackTrace(); 80 throw e; 81 } 82 } 83 84 @Test(timeOut=3000000) 85 public static void test() throws Exception { 86 try { 87 initialize(); 88 simpleTest(false); 89 simpleTest(true); 90 streamTest(false); 91 streamTest(true); 92 paramsTest(); 93 Thread.sleep(1000 * 4); 94 } catch (Exception | Error tt) { 95 tt.printStackTrace(); 96 throw tt; 97 } finally { 98 httpServer.stop(); 99 httpsServer.stop(); 100 exec.shutdownNow(); 101 } 102 } 103 104 static HttpClient getClient() { 105 if (client == null) { 106 exec = Executors.newCachedThreadPool(); 107 client = HttpClient.newBuilder() 108 .executor(Executors.newFixedThreadPool(2)) 109 .sslContext(sslContext) 110 .version(HTTP_2) 111 .build(); 112 } 113 return client; 114 } 115 116 static URI getURI(boolean secure) { 117 if (secure) 118 return URI.create(httpsURIString); 119 else 120 return URI.create(httpURIString); 121 } 122 123 static void checkStatus(int expected, int found) throws Exception { 124 if (expected != found) { 125 System.err.printf ("Test failed: wrong status code %d/%d\n", 126 expected, found); | 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 26 * @bug 8087112 8177935 27 * @library /lib/testlibrary server 28 * @build jdk.testlibrary.SimpleSSLContext 29 * @modules java.base/sun.net.www.http 30 * jdk.incubator.httpclient/jdk.incubator.http.internal.common 31 * jdk.incubator.httpclient/jdk.incubator.http.internal.frame 32 * jdk.incubator.httpclient/jdk.incubator.http.internal.hpack 33 * @run testng/othervm -Djdk.httpclient.HttpClient.log=ssl,requests,responses,errors FixedThreadPoolTest 34 */ 35 36 import java.net.*; 37 import jdk.incubator.http.*; 38 import static jdk.incubator.http.HttpClient.Version.HTTP_2; 39 import javax.net.ssl.*; 40 import java.nio.file.*; 41 import java.util.concurrent.*; 42 import jdk.testlibrary.SimpleSSLContext; 43 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromFile; 44 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString; 45 import static jdk.incubator.http.HttpResponse.BodyHandler.asFile; 46 import static jdk.incubator.http.HttpResponse.BodyHandler.asString; 47 48 import org.testng.annotations.Test; 49 50 @Test 51 public class FixedThreadPoolTest { 52 static int httpPort, httpsPort; 53 static Http2TestServer httpServer, httpsServer; 54 static HttpClient client = null; 55 static ExecutorService exec; 56 static SSLContext sslContext; 57 58 static String httpURIString, httpsURIString; 59 60 static void initialize() throws Exception { 61 try { 62 SimpleSSLContext sslct = new SimpleSSLContext(); 63 sslContext = sslct.get(); 64 client = getClient(); 65 httpServer = new Http2TestServer(false, 0, exec, sslContext); 66 httpServer.addHandler(new Http2EchoHandler(), "/"); 67 httpPort = httpServer.getAddress().getPort(); 68 69 httpsServer = new Http2TestServer(true, 0, exec, sslContext); 70 httpsServer.addHandler(new Http2EchoHandler(), "/"); 71 72 httpsPort = httpsServer.getAddress().getPort(); 73 httpURIString = "http://127.0.0.1:" + httpPort + "/foo/"; 74 httpsURIString = "https://127.0.0.1:" + httpsPort + "/bar/"; 75 76 httpServer.start(); 77 httpsServer.start(); 78 } catch (Throwable e) { 79 System.err.println("Throwing now"); 80 e.printStackTrace(); 81 throw e; 82 } 83 } 84 85 @Test 86 public static void test() throws Exception { 87 try { 88 initialize(); 89 simpleTest(false); 90 simpleTest(true); 91 streamTest(false); 92 streamTest(true); 93 paramsTest(); 94 Thread.sleep(1000 * 4); 95 } catch (Exception | Error tt) { 96 tt.printStackTrace(); 97 throw tt; 98 } finally { 99 httpServer.stop(); 100 httpsServer.stop(); 101 exec.shutdownNow(); 102 } 103 } 104 105 static HttpClient getClient() { 106 if (client == null) { 107 exec = Executors.newCachedThreadPool(); 108 // Executor e1 = Executors.newFixedThreadPool(1); 109 // Executor e = (Runnable r) -> e1.execute(() -> { 110 // System.out.println("[" + Thread.currentThread().getName() 111 // + "] Executing: " 112 // + r.getClass().getName()); 113 // r.run(); 114 // }); 115 client = HttpClient.newBuilder() 116 .executor(Executors.newFixedThreadPool(2)) 117 .sslContext(sslContext) 118 .version(HTTP_2) 119 .build(); 120 } 121 return client; 122 } 123 124 static URI getURI(boolean secure) { 125 if (secure) 126 return URI.create(httpsURIString); 127 else 128 return URI.create(httpURIString); 129 } 130 131 static void checkStatus(int expected, int found) throws Exception { 132 if (expected != found) { 133 System.err.printf ("Test failed: wrong status code %d/%d\n", 134 expected, found); |