From ea58ca6a2563ec36d2a963bd944f7ded10425bc1 Mon Sep 17 00:00:00 2001 From: Felix Yang Date: Wed, 15 Jul 2015 08:42:24 -0700 Subject: [PATCH] 8130394: DatagramChannel tests need to be hardended to ignore stray datagrams The patch updates tests to ignore stray datagrams, or at least print more information to ease troubleshooting. Reviewed-by: rriggs --- .../channels/DatagramChannel/ReceiveISA.java | 5 ++-- .../nio/channels/DatagramChannel/Sender.java | 28 +++++++++++++------ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/jdk/test/java/nio/channels/DatagramChannel/ReceiveISA.java b/jdk/test/java/nio/channels/DatagramChannel/ReceiveISA.java index f4fe119e670..ea646cf80a2 100644 --- a/jdk/test/java/nio/channels/DatagramChannel/ReceiveISA.java +++ b/jdk/test/java/nio/channels/DatagramChannel/ReceiveISA.java @@ -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 @@ public class ReceiveISA { 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(); } diff --git a/jdk/test/java/nio/channels/DatagramChannel/Sender.java b/jdk/test/java/nio/channels/DatagramChannel/Sender.java index 4d9ea010455..8ab4268e3a4 100644 --- a/jdk/test/java/nio/channels/DatagramChannel/Sender.java +++ b/jdk/test/java/nio/channels/DatagramChannel/Sender.java @@ -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 @@ public class Sender { 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 class Sender { 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();