< prev index next >

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

Print this page

        

@@ -60,16 +60,18 @@
      * Since at this level we don't have any control over the low level socket
      * parameters, this latch ensures a write to the channel will stall at least
      * once (socket's send buffer filled up).
      */
     private final CountDownLatch writeStall = new CountDownLatch(1);
+    private final CountDownLatch initialWriteStall = new CountDownLatch(1);
 
     /*
      * This one works similarly by providing means to ensure a read from the
      * channel will stall at least once (no more data available on the socket).
      */
     private final CountDownLatch readStall = new CountDownLatch(1);
+    private final CountDownLatch initialReadStall = new CountDownLatch(1);
 
     private final AtomicInteger writeHandles = new AtomicInteger();
     private final AtomicInteger readHandles = new AtomicInteger();
 
     private final CountDownLatch exit = new CountDownLatch(1);

@@ -77,18 +79,26 @@
     @Test
     public void test() throws Exception {
         try (ServerSocket server = new ServerSocket(0)) {
             int port = server.getLocalPort();
             new TestServer(server).start();
+
             final RawChannel chan = channelOf(port);
+            initialWriteStall.await();
 
             // It's very important not to forget the initial bytes, possibly
             // left from the HTTP thingy
             int initialBytes = chan.initialByteBuffer().remaining();
             print("RawChannel has %s initial bytes", initialBytes);
             clientRead.addAndGet(initialBytes);
 
+            // tell the server we have read the initial bytes, so
+            // that it makes sure there is something for us to
+            // read next in case the initialBytes have already drained the
+            // channel dry.
+            initialReadStall.countDown();
+
             chan.registerEvent(new RawChannel.RawEvent() {
 
                 private final ByteBuffer reusableBuffer = ByteBuffer.allocate(32768);
 
                 @Override

@@ -160,11 +170,11 @@
                         }
                         int r = read.remaining();
                         total += r;
                         clientRead.addAndGet(r);
                     }
-                    print("OP_READ read %s bytes", total);
+                    print("OP_READ read %s bytes (%s total)", total, clientRead.get());
                 }
             });
             exit.await(); // All done, we need to compare results:
             assertEquals(clientRead.get(), serverWritten.get());
             assertEquals(serverRead.get(), clientWritten.get());

@@ -232,10 +242,18 @@
 
         private void processHttp(InputStream is, OutputStream os)
                 throws IOException
         {
             os.write("HTTP/1.1 200 OK\r\nContent-length: 0\r\n\r\n".getBytes());
+
+            // write some initial bytes
+            byte[] initial = byteArrayOfSize(1024);
+            os.write(initial);
+            os.flush();
+            serverWritten.addAndGet(initial.length);
+            initialWriteStall.countDown();
+
             byte[] buf = new byte[1024];
             String s = "";
             while (true) {
                 int n = is.read(buf);
                 if (n <= 0) {

@@ -250,16 +268,29 @@
 
         private long writeSlowly(OutputStream os) throws Exception {
             byte[] first = byteArrayOfSize(1024);
             long total = first.length;
             os.write(first);
+            os.flush();
+
+            // wait until initial bytes were read
+            initialReadStall.await();
+
+            // make sure there is something to read, otherwise readStall
+            // will never be counted down.
+            first = byteArrayOfSize(1024);
+            os.write(first);
+            os.flush();
+            total += first.length;
+
             // Let's wait for the signal from the raw channel that its read has
             // stalled, and then continue sending a bit more stuff
             readStall.await();
             for (int i = 0; i < 32; i++) {
                 byte[] b = byteArrayOfSize(1024);
                 os.write(b);
+                os.flush();
                 total += b.length;
                 TimeUnit.MILLISECONDS.sleep(1);
             }
             return total;
         }
< prev index next >