http-client-branch: (WebSocket) SelectorTest http-client-branch
authorprappo
Tue, 05 Dec 2017 22:44:33 +0300
branchhttp-client-branch
changeset 55957 37126be36316
parent 55955 64298b1e890b
child 55958 24e637188a68
http-client-branch: (WebSocket) SelectorTest
test/jdk/java/net/httpclient/whitebox/jdk.incubator.httpclient/jdk/incubator/http/SelectorTest.java
--- a/test/jdk/java/net/httpclient/whitebox/jdk.incubator.httpclient/jdk/incubator/http/SelectorTest.java	Tue Dec 05 10:28:45 2017 +0000
+++ b/test/jdk/java/net/httpclient/whitebox/jdk.incubator.httpclient/jdk/incubator/http/SelectorTest.java	Tue Dec 05 22:44:33 2017 +0300
@@ -88,57 +88,58 @@
             t.start();
             out.println("Started server thread");
 
-            final RawChannel chan = getARawChannel(port);
-
-            chan.registerEvent(new RawChannel.RawEvent() {
-                @Override
-                public int interestOps() {
-                    return SelectionKey.OP_READ;
-                }
+            try (RawChannel chan = getARawChannel(port)) {
 
-                @Override
-                public void handle() {
-                    readSomeBytes(chan);
-                    out.printf("OP_READ\n");
-                    final int count = counter.get();
-                    if (count != 1) {
-                        out.printf("OP_READ error counter = %d\n", count);
-                        error = true;
+                chan.registerEvent(new RawChannel.RawEvent() {
+                    @Override
+                    public int interestOps() {
+                        return SelectionKey.OP_READ;
                     }
-                }
-            });
 
-            chan.registerEvent(new RawChannel.RawEvent() {
-                @Override
-                public int interestOps() {
-                    return SelectionKey.OP_WRITE;
-                }
-
-                @Override
-                public void handle() {
-                    out.printf("OP_WRITE\n");
-                    final int count = counter.get();
-                    if (count != 0) {
-                        out.printf("OP_WRITE error counter = %d\n", count);
-                        error = true;
-                    } else {
-                        ByteBuffer bb = ByteBuffer.wrap(TestServer.INPUT);
-                        counter.incrementAndGet();
-                        try {
-                            chan.write(new ByteBuffer[]{bb}, 0, 1);
-                        } catch (IOException e) {
-                            throw new UncheckedIOException(e);
+                    @Override
+                    public void handle() {
+                        readSomeBytes(chan);
+                        out.printf("OP_READ\n");
+                        final int count = counter.get();
+                        if (count != 1) {
+                            out.printf("OP_READ error counter = %d\n", count);
+                            error = true;
                         }
                     }
-                }
+                });
+
+                chan.registerEvent(new RawChannel.RawEvent() {
+                    @Override
+                    public int interestOps() {
+                        return SelectionKey.OP_WRITE;
+                    }
 
-            });
-            out.println("Events registered. Waiting");
-            finishingGate.await(30, SECONDS);
-            if (error)
-                throw new RuntimeException("Error");
-            else
-                out.println("No error");
+                    @Override
+                    public void handle() {
+                        out.printf("OP_WRITE\n");
+                        final int count = counter.get();
+                        if (count != 0) {
+                            out.printf("OP_WRITE error counter = %d\n", count);
+                            error = true;
+                        } else {
+                            ByteBuffer bb = ByteBuffer.wrap(TestServer.INPUT);
+                            counter.incrementAndGet();
+                            try {
+                                chan.write(new ByteBuffer[]{bb}, 0, 1);
+                            } catch (IOException e) {
+                                throw new UncheckedIOException(e);
+                            }
+                        }
+                    }
+
+                });
+                out.println("Events registered. Waiting");
+                finishingGate.await(30, SECONDS);
+                if (error)
+                    throw new RuntimeException("Error");
+                else
+                    out.println("No error");
+            }
         }
     }
 
@@ -146,6 +147,10 @@
         URI uri = URI.create("http://127.0.0.1:" + port + "/");
         out.println("client connecting to " + uri.toString());
         HttpRequest req = HttpRequest.newBuilder(uri).build();
+        // Otherwise HttpClient will think this is an ordinary connection and
+        // thus all ordinary procedures apply to it, e.g. it must be put into
+        // the cache
+        ((HttpRequestImpl) req).isWebSocket(true);
         HttpResponse<?> r = defaultClient().send(req, discard(null));
         r.body();
         return ((HttpResponseImpl) r).rawChannel();