test/java/nio/channels/DatagramChannel/EmptyBuffer.java

Print this page
rev 12172 : 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
Summary: Only count datagrams received from the client within the test.
Reviewed-by: XXX

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -20,20 +20,19 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
 /* @test
- * @bug 4669040
+ * @bug 4669040 8087304
  * @summary Test DatagramChannel receive with empty buffer
  * @author Mike McCloskey
  */
 
 import java.io.*;
 import java.net.*;
 import java.nio.*;
 import java.nio.channels.*;
-import java.nio.charset.*;
 
 public class EmptyBuffer {
 
     static PrintStream log = System.err;
 

@@ -52,10 +51,11 @@
             bb.putInt(1).putLong(1);
             bb.flip();
             InetAddress address = InetAddress.getLocalHost();
             InetSocketAddress isa = new InetSocketAddress(address, server.port());
             dc.connect(isa);
+            server.setClientAddress(dc.getLocalAddress());
             dc.write(bb);
             bb.rewind();
             dc.write(bb);
             bb.rewind();
             dc.write(bb);

@@ -67,16 +67,21 @@
         }
     }
 
     public static class Server implements Runnable {
         final DatagramChannel dc;
+        private SocketAddress clientAddress;
         Exception e = null;
 
         Server() throws IOException {
             this.dc = DatagramChannel.open().bind(new InetSocketAddress(0));
         }
 
+        void setClientAddress(SocketAddress clientAddress) {
+            this.clientAddress = clientAddress;
+        }
+
         int port() {
             return dc.socket().getLocalPort();
         }
 
         void throwException() throws Exception {

@@ -92,12 +97,13 @@
                 log.print(element);
             }
             log.println();
         }
 
+        @Override
         public void run() {
-            SocketAddress sa = null;
+            SocketAddress sa;
             int numberReceived = 0;
             try {
                 ByteBuffer bb = ByteBuffer.allocateDirect(12);
                 bb.clear();
                 // Only one clear. The buffer will be full after

@@ -107,19 +113,25 @@
                     try {
                         sa = dc.receive(bb);
                     } catch (ClosedByInterruptException cbie) {
                         // Expected
                         log.println("Took expected exit");
+                        // Verify that enough packets were received
+                        if (numberReceived != 3)
+                            throw new RuntimeException("Failed: Too few datagrams");
                         break;
                     }
                     if (sa != null) {
                         log.println("Client: " + sa);
+                        // Check client address so as not to count stray packets
+                        if (sa.equals(clientAddress)) {
                         showBuffer("RECV", bb);
-                        sa = null;
                         numberReceived++;
+                        }
                         if (numberReceived > 3)
-                            throw new RuntimeException("Test failed");
+                            throw new RuntimeException("Failed: Too many datagrams");
+                        sa = null;
                     }
                 }
             } catch (Exception ex) {
                 e = ex;
             } finally {