提交 872cea62 编写于 作者: M msheppar

8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java

Summary: modified test to execute in a single thread to eliminate potential race condition
Reviewed-by: alanb, chegar, dfuchs
上级 9401c4b0
/* /*
* Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2013 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -31,20 +31,17 @@ ...@@ -31,20 +31,17 @@
import java.net.InetAddress; import java.net.InetAddress;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.io.InterruptedIOException;
public class PortUnreachable implements Runnable { public class PortUnreachable {
DatagramSocket clientSock; DatagramSocket clientSock;
int serverPort; int serverPort;
int clientPort; int clientPort;
public void run() { public void serverSend() {
try { try {
InetAddress addr = InetAddress.getLocalHost(); InetAddress addr = InetAddress.getLocalHost();
Thread.currentThread().sleep(1000);
Thread.currentThread().sleep(2000);
// send a delayed packet which should mean a delayed icmp // send a delayed packet which should mean a delayed icmp
// port unreachable // port unreachable
byte b[] = "A late msg".getBytes(); byte b[] = "A late msg".getBytes();
...@@ -52,11 +49,8 @@ public class PortUnreachable implements Runnable { ...@@ -52,11 +49,8 @@ public class PortUnreachable implements Runnable {
serverPort); serverPort);
clientSock.send(packet); clientSock.send(packet);
// wait before bringing the server up
Thread.currentThread().sleep(5000);
DatagramSocket sock = new DatagramSocket(serverPort); DatagramSocket sock = new DatagramSocket(serverPort);
b = "Grettings from the server".getBytes(); b = "Greetings from the server".getBytes();
packet = new DatagramPacket(b, b.length, addr, clientPort); packet = new DatagramPacket(b, b.length, addr, clientPort);
sock.send(packet); sock.send(packet);
sock.close(); sock.close();
...@@ -70,10 +64,13 @@ public class PortUnreachable implements Runnable { ...@@ -70,10 +64,13 @@ public class PortUnreachable implements Runnable {
clientSock = new DatagramSocket(); clientSock = new DatagramSocket();
clientPort = clientSock.getLocalPort(); clientPort = clientSock.getLocalPort();
}
void execute () throws Exception{
// pick a port for the server // pick a port for the server
DatagramSocket sock2 = new DatagramSocket(); DatagramSocket sock2 = new DatagramSocket();
serverPort = sock2.getLocalPort(); serverPort = sock2.getLocalPort();
sock2.close();
// send a burst of packets to the unbound port - we should get back // send a burst of packets to the unbound port - we should get back
// icmp port unreachable messages // icmp port unreachable messages
...@@ -82,23 +79,26 @@ public class PortUnreachable implements Runnable { ...@@ -82,23 +79,26 @@ public class PortUnreachable implements Runnable {
byte b[] = "Hello me".getBytes(); byte b[] = "Hello me".getBytes();
DatagramPacket packet = new DatagramPacket(b, b.length, addr, DatagramPacket packet = new DatagramPacket(b, b.length, addr,
serverPort); serverPort);
//close just before sending
sock2.close();
for (int i=0; i<100; i++) for (int i=0; i<100; i++)
clientSock.send(packet); clientSock.send(packet);
// start the server thread serverSend();
Thread thr = new Thread(this);
thr.start();
// try to receive // try to receive
b = new byte[25];
packet = new DatagramPacket(b, b.length, addr, serverPort);
clientSock.setSoTimeout(10000); clientSock.setSoTimeout(10000);
clientSock.receive(packet); clientSock.receive(packet);
System.out.println("client received data packet " + new String(packet.getData()));
// done // done
clientSock.close(); clientSock.close();
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
new PortUnreachable(); PortUnreachable test = new PortUnreachable();
test.execute();
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册