< prev index next >

test/jdk/java/net/httpclient/whitebox/jdk.incubator.httpclient/jdk/incubator/http/RawChannelTest.java

Print this page

        

@@ -22,10 +22,11 @@
  */
 
 package jdk.incubator.http;
 
 import jdk.incubator.http.internal.websocket.RawChannel;
+import jdk.incubator.http.internal.websocket.WebSocketRequest;
 import org.testng.annotations.Test;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;

@@ -81,10 +82,11 @@
         try (ServerSocket server = new ServerSocket(0)) {
             int port = server.getLocalPort();
             new TestServer(server).start();
 
             final RawChannel chan = channelOf(port);
+            print("RawChannel is %s", String.valueOf(chan));
             initialWriteStall.await();
 
             // It's very important not to forget the initial bytes, possibly
             // left from the HTTP thingy
             int initialBytes = chan.initialByteBuffer().remaining();

@@ -183,13 +185,25 @@
 
     private static RawChannel channelOf(int port) throws Exception {
         URI uri = URI.create("http://127.0.0.1:" + port + "/");
         print("raw channel to %s", uri.toString());
         HttpRequest req = HttpRequest.newBuilder(uri).build();
-        HttpResponse<?> r = HttpClient.newHttpClient().send(req, discard(null));
+        // Switch on isWebSocket flag to prevent the connection from
+        // being returned to the pool.
+        ((WebSocketRequest)req).isWebSocket(true);
+        HttpClient client = HttpClient.newHttpClient();
+        try {
+            HttpResponse<?> r = client.send(req, discard(null));
         r.body();
         return ((HttpResponseImpl) r).rawChannel();
+        } finally {
+           // Need to hold onto the client until the RawChannel is
+           // created. This would not be needed if we had created
+           // a WebSocket, but here we are fiddling directly
+           // with the internals of HttpResponseImpl!
+           java.lang.ref.Reference.reachabilityFence(client);
+        }
     }
 
     private class TestServer extends Thread { // Powered by Slowpokes
 
         private final ServerSocket server;
< prev index next >