diff --git a/core/src/main/java/hudson/UDPBroadcastThread.java b/core/src/main/java/hudson/UDPBroadcastThread.java index 168279ba444e39238c33371963550f1f50205de8..384321ac9cae66acc022481d8045c52f0b7b1169 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 5ad31abf5c5705290a7c49a6224f5170299c491c..2562de9f7154b3957673bfcdf46dcae0578194a1 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();