From 90ab89564a5d39fca152c012760d6aeb39007507 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Tue, 5 Mar 2013 12:44:46 -0500 Subject: [PATCH] =?UTF-8?q?UDPBroadcastThreadTest=20was=20often=20failing?= =?UTF-8?q?=20on=20ci.jenkins-ci.org;=20making=20more=20lenient.=20Mar=204?= =?UTF-8?q?,=202013=2010:55:46=20PM=20hudson.UDPBroadcastThread=20run=20WA?= =?UTF-8?q?RNING:=20UDP=20handling=20problem=20java.io.IOException:=20Oper?= =?UTF-8?q?ation=20not=20permitted=20=09at=20java.net.PlainDatagramSocketI?= =?UTF-8?q?mpl.send(Native=20Method)=20=09at=20java.net.DatagramSocket.sen?= =?UTF-8?q?d(DatagramSocket.java:625)=20=09at=20hudson.UDPBroadcastThread.?= =?UTF-8?q?run(UDPBroadcastThread.java:98)=20=E2=86=92=20java.net.SocketTi?= =?UTF-8?q?meoutException:=20Receive=20timed=20out=20=09at=20java.net.Plai?= =?UTF-8?q?nDatagramSocketImpl.receive0(Native=20Method)=20=09at=20java.ne?= =?UTF-8?q?t.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:?= =?UTF-8?q?145)=20=09at=20java.net.DatagramSocket.receive(DatagramSocket.j?= =?UTF-8?q?ava:725)=20=09at=20hudson.UDPBroadcastThreadTest.receiveAndVeri?= =?UTF-8?q?fy(UDPBroadcastThreadTest.java:70)=20=09at=20hudson.UDPBroadcas?= =?UTF-8?q?tThreadTest.testMulticast(UDPBroadcastThreadTest.java:51)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/hudson/UDPBroadcastThread.java | 4 +++ .../java/hudson/UDPBroadcastThreadTest.java | 35 ++++++++++++++----- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/hudson/UDPBroadcastThread.java b/core/src/main/java/hudson/UDPBroadcastThread.java index 168279ba44..384321ac9c 100644 --- a/core/src/main/java/hudson/UDPBroadcastThread.java +++ b/core/src/main/java/hudson/UDPBroadcastThread.java @@ -23,6 +23,7 @@ */ package hudson; +import edu.umd.cs.findbugs.annotations.SuppressWarnings; import hudson.model.Hudson; import jenkins.model.Jenkins; import hudson.util.OneShotEvent; @@ -52,6 +53,7 @@ public class UDPBroadcastThread extends Thread { public final OneShotEvent ready = new OneShotEvent(); private MulticastSocket mcs; private boolean shutdown; + static boolean udpHandlingProblem; // for tests /** * @deprecated as of 1.416 @@ -67,6 +69,7 @@ public class UDPBroadcastThread extends Thread { mcs = new MulticastSocket(PORT); } + @SuppressWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD") @Override public void run() { try { @@ -106,6 +109,7 @@ public class UDPBroadcastThread extends Thread { } catch (IOException e) { if (shutdown) return; // forcibly closed LOGGER.log(Level.WARNING, "UDP handling problem",e); + udpHandlingProblem = true; } } diff --git a/test/src/test/java/hudson/UDPBroadcastThreadTest.java b/test/src/test/java/hudson/UDPBroadcastThreadTest.java index 5ad31abf5c..2562de9f71 100644 --- a/test/src/test/java/hudson/UDPBroadcastThreadTest.java +++ b/test/src/test/java/hudson/UDPBroadcastThreadTest.java @@ -1,6 +1,5 @@ package hudson; -import org.jvnet.hudson.test.HudsonTestCase; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; @@ -12,30 +11,43 @@ import java.net.DatagramPacket; import java.net.InetAddress; import java.io.StringReader; import java.io.IOException; +import java.net.SocketTimeoutException; +import org.junit.Assume; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; /** * @author Kohsuke Kawaguchi */ -public class UDPBroadcastThreadTest extends HudsonTestCase { +public class UDPBroadcastThreadTest { + + @Rule public JenkinsRule j = new JenkinsRule(); + /** * Old unicast based clients should still be able to receive some reply, * as we haven't changed the port. */ - public void testLegacy() throws Exception { + @Test public void legacy() throws Exception { DatagramSocket s = new DatagramSocket(); sendQueryTo(s, InetAddress.getLocalHost()); s.setSoTimeout(15000); // to prevent test hang - receiveAndVerify(s); + try { + receiveAndVerify(s); + } catch (SocketTimeoutException x) { + Assume.assumeFalse(UDPBroadcastThread.udpHandlingProblem); + throw x; + } } /** * Multicast based clients should be able to receive multiple replies. */ - public void testMulticast() throws Exception { - UDPBroadcastThread second = new UDPBroadcastThread(jenkins); + @Test public void multicast() throws Exception { + UDPBroadcastThread second = new UDPBroadcastThread(j.jenkins); second.start(); - UDPBroadcastThread third = new UDPBroadcastThread(jenkins); + UDPBroadcastThread third = new UDPBroadcastThread(j.jenkins); third.start(); second.ready.block(); @@ -47,8 +59,13 @@ public class UDPBroadcastThreadTest extends HudsonTestCase { s.setSoTimeout(15000); // to prevent test hang // we should at least get two replies since we run two broadcasts - receiveAndVerify(s); - receiveAndVerify(s); + try { + receiveAndVerify(s); + receiveAndVerify(s); + } catch (SocketTimeoutException x) { + Assume.assumeFalse(UDPBroadcastThread.udpHandlingProblem); + throw x; + } } finally { third.interrupt(); second.interrupt(); -- GitLab