8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
authorbpb
Wed, 15 Jul 2015 08:42:24 -0700
changeset 31719 8c4b63e942fb
parent 31718 c10ac6f1e391
child 31720 6139ed9855d0
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams Summary: The patch updates tests to ignore stray datagrams, or at least print more information to ease troubleshooting. Reviewed-by: rriggs Contributed-by: Felix Yang <felix.yang@oracle.com>
jdk/test/java/nio/channels/DatagramChannel/ReceiveISA.java
jdk/test/java/nio/channels/DatagramChannel/Sender.java
--- a/jdk/test/java/nio/channels/DatagramChannel/ReceiveISA.java	Wed Jul 15 15:07:58 2015 +0100
+++ b/jdk/test/java/nio/channels/DatagramChannel/ReceiveISA.java	Wed Jul 15 08:42:24 2015 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 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
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 4503641
+ * @bug 4503641 8130394
  * @summary Check that DatagramChannel.receive returns a new SocketAddress
  *          when it receives a packet from the same source address but
  *          different endpoint.
@@ -63,6 +63,7 @@
         SocketAddress sa[] = new SocketAddress[3];
         for (int i=0; i<3; i++) {
             sa[i] = dc3.receive(rb);
+            System.out.println("received "+ sa[i] );
             rb.clear();
         }
 
--- a/jdk/test/java/nio/channels/DatagramChannel/Sender.java	Wed Jul 15 15:07:58 2015 +0100
+++ b/jdk/test/java/nio/channels/DatagramChannel/Sender.java	Wed Jul 15 08:42:24 2015 -0700
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -22,20 +22,24 @@
  */
 
 /* @test
- * @bug 4669040
+ * @bug 4669040 8130394
  * @summary Test DatagramChannel subsequent receives with no datagram ready
  * @author Mike McCloskey
  */
 
-import java.io.*;
-import java.net.*;
-import java.nio.*;
-import java.nio.channels.*;
-import java.nio.charset.*;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.channels.DatagramChannel;
 
 public class Sender {
 
     static PrintStream log = System.err;
+    static volatile SocketAddress clientISA = null;
 
     public static void main(String[] args) throws Exception {
         test();
@@ -81,6 +85,7 @@
                 InetAddress address = InetAddress.getLocalHost();
                 InetSocketAddress isa = new InetSocketAddress(address, port);
                 dc.connect(isa);
+                clientISA = dc.getLocalAddress();
                 dc.write(bb);
             } catch (Exception ex) {
                 e = ex;
@@ -118,13 +123,20 @@
         public void run() {
             SocketAddress sa = null;
 
+
             try {
                 ByteBuffer bb = ByteBuffer.allocateDirect(12);
                 bb.clear();
                 // Get the one valid datagram
                 dc.configureBlocking(false);
-                while (sa == null)
+                while (sa == null) {
                     sa = dc.receive(bb);
+                    if (sa != null && clientISA != null && !clientISA.equals(sa)) {
+                        log.println("Ignore a possible stray diagram from " + sa);
+                        sa = null;
+                    }
+                }
+                showBuffer("Received:", bb);
                 sa = null;
                 for (int i=0; i<100; i++) {
                     bb.clear();