From 55e08682c5f6a5b63e622847f6779436bf260eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Andr=C3=A9n?= Date: Wed, 13 Apr 2016 13:30:04 +0200 Subject: [PATCH] doc,io #16262 UDP multicast tests work on all platforms (#20290) --- .../code/docs/io/JavaUdpMulticastTest.java | 83 +++++++++++-------- .../code/docs/io/ScalaUdpMulticastSpec.scala | 68 +++++++++------ 2 files changed, 90 insertions(+), 61 deletions(-) diff --git a/akka-docs/rst/java/code/docs/io/JavaUdpMulticastTest.java b/akka-docs/rst/java/code/docs/io/JavaUdpMulticastTest.java index cd86487f81..c4176f4ad5 100644 --- a/akka-docs/rst/java/code/docs/io/JavaUdpMulticastTest.java +++ b/akka-docs/rst/java/code/docs/io/JavaUdpMulticastTest.java @@ -17,13 +17,10 @@ import org.junit.Test; import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; -import java.util.Enumeration; -import java.util.Random; +import java.util.*; -// not part of the test suite because we have not figured out -// a way to find an interface that is sure to work on all platforms -// to listen for udp on -public class JavaUdpMulticastTest { + +public class JavaUdpMulticastTest extends AbstractJavaTest { static ActorSystem system; @@ -35,43 +32,61 @@ public class JavaUdpMulticastTest { @Test public void testUdpMulticast() throws Exception { new JavaTestKit(system) {{ - NetworkInterface ipv6Iface = null; - for (Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements() && ipv6Iface == null;) { + List ipv6Ifaces = new ArrayList<>(); + for (Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements(); ) { NetworkInterface interf = interfaces.nextElement(); - // awdl0 is a special interface on OSX that we cannot use - if (!interf.getDisplayName().equals("awdl0") && - // we do not want to use virtual docker interfaces - !interf.getDisplayName().contains("docker")) { - for (Enumeration addresses = interf.getInetAddresses(); addresses.hasMoreElements() && ipv6Iface == null; ) { + if (interf.isUp() && interf.supportsMulticast()) { + for (Enumeration addresses = interf.getInetAddresses(); addresses.hasMoreElements(); ) { InetAddress address = addresses.nextElement(); if (address instanceof Inet6Address) { - ipv6Iface = interf; + ipv6Ifaces.add(interf); } } } } + if (ipv6Ifaces.isEmpty()) { + system.log().info("JavaUdpMulticastTest skipped since no ipv6 interface supporting multicast could be found"); + } else { + // lots of problems with choosing the wrong interface for this test depending + // on the platform (awsdl0 can't be used on OSX, docker[0-9] can't be used in a docker machine etc.) + // therefore: try hard to find an interface that _does_ work, and only fail if there was any potentially + // working interfaces but all failed + for (Iterator interfaceIterator = ipv6Ifaces.iterator(); interfaceIterator.hasNext(); ) { + NetworkInterface ipv6Iface = interfaceIterator.next(); + // host assigned link local multicast address http://tools.ietf.org/html/rfc3307#section-4.3.2 + // generate a random 32 bit multicast address with the high order bit set + final String randomAddress = Long.toHexString(((long) Math.abs(new Random().nextInt())) | (1L << 31)).toUpperCase(); + final StringBuilder groupBuilder = new StringBuilder("FF02:"); + for (int i = 0; i < 2; i += 1) { + groupBuilder.append(":"); + groupBuilder.append(randomAddress.subSequence(i * 4, i * 4 + 4)); + } + final String group = groupBuilder.toString(); + final Integer port = TestUtils.temporaryUdpIpv6Port(ipv6Iface); + final String msg = "ohi"; + final ActorRef sink = getRef(); + final String iface = ipv6Iface.getName(); - // host assigned link local multicast address http://tools.ietf.org/html/rfc3307#section-4.3.2 - // generate a random 32 bit multicast address with the high order bit set - final String randomAddress = Long.toHexString(((long) Math.abs(new Random().nextInt())) | (1L << 31)).toUpperCase(); - final StringBuilder groupBuilder = new StringBuilder("FF02:"); - for (int i = 0; i < 2; i += 1) { - groupBuilder.append(":"); - groupBuilder.append(randomAddress.subSequence(i * 4, i * 4 + 4)); + final ActorRef listener = system.actorOf(Props.create(JavaUdpMulticast.Listener.class, iface, group, port, sink)); + + try { + expectMsgClass(Udp.Bound.class); + final ActorRef sender = system.actorOf(Props.create(JavaUdpMulticast.Sender.class, iface, group, port, msg)); + expectMsgEquals(msg); + // success with one interface is enough + break; + + } catch (AssertionError ex) { + if (!interfaceIterator.hasNext()) throw ex; + else { + system.log().info("Failed to run test on interface {}", ipv6Iface.getDisplayName()); + } + } finally { + // unbind + system.stop(listener); + } + } } - final String group = groupBuilder.toString(); - final Integer port = TestUtils.temporaryUdpIpv6Port(ipv6Iface); - final String msg = "ohi"; - final ActorRef sink = getRef(); - final String iface = ipv6Iface.getName(); - - final ActorRef listener = system.actorOf(Props.create(JavaUdpMulticast.Listener.class, iface, group, port, sink)); - expectMsgClass(Udp.Bound.class); - final ActorRef sender = system.actorOf(Props.create(JavaUdpMulticast.Sender.class, iface, group, port, msg)); - expectMsgEquals(msg); - - // unbind - system.stop(listener); }}; } diff --git a/akka-docs/rst/scala/code/docs/io/ScalaUdpMulticastSpec.scala b/akka-docs/rst/scala/code/docs/io/ScalaUdpMulticastSpec.scala index 6265568379..ab3cb5eb59 100644 --- a/akka-docs/rst/scala/code/docs/io/ScalaUdpMulticastSpec.scala +++ b/akka-docs/rst/scala/code/docs/io/ScalaUdpMulticastSpec.scala @@ -6,8 +6,8 @@ package docs.io import java.net.{ Inet6Address, InetSocketAddress, NetworkInterface, StandardProtocolFamily } import java.nio.channels.DatagramChannel -import scala.util.Random +import scala.util.Random import akka.actor.{ ActorSystem, Props } import akka.io.Udp import akka.testkit.TestKit @@ -19,35 +19,49 @@ class ScalaUdpMulticastSpec extends TestKit(ActorSystem("ScalaUdpMulticastSpec") "listener" should { "send message back to sink" in { - // TODO make this work consistently on all platforms - pending + val ipv6ifaces = + NetworkInterface.getNetworkInterfaces.toSeq.filter(iface => + iface.supportsMulticast && + iface.isUp && + iface.getInetAddresses.exists(_.isInstanceOf[Inet6Address])) - def okInterfaceToUse(iface: NetworkInterface): Boolean = { - iface.getInetAddresses.exists(_.isInstanceOf[Inet6Address]) && - // awdl0 is a special interface on OSX that we cannot use - iface.getDisplayName != "awdl0" && - // we do not want to use virtual docker interfaces - !iface.getDisplayName.contains("docker") + if (ipv6ifaces.isEmpty) { + // IPv6 not supported for any interface on this platform + pending + } else { + // lots of problems with choosing the wrong interface for this test depending + // on the platform (awsdl0 can't be used on OSX, docker[0-9] can't be used in a docker machine etc.) + // therefore: try hard to find an interface that _does_ work, and only fail if there was any potentially + // working interfaces but all failed + ipv6ifaces.exists { ipv6iface => + // host assigned link local multicast address http://tools.ietf.org/html/rfc3307#section-4.3.2 + // generate a random 32 bit multicast address with the high order bit set + val randomAddress: String = (Random.nextInt().abs.toLong | (1L << 31)).toHexString.toUpperCase + val group = randomAddress.grouped(4).mkString("FF02::", ":", "") + val port = TestUtils.temporaryUdpIpv6Port(ipv6iface) + val msg = "ohi" + val sink = testActor + val iface = ipv6iface.getName + val listener = system.actorOf(Props(classOf[Listener], iface, group, port, sink)) + try { + expectMsgType[Udp.Bound] + val sender = system.actorOf(Props(classOf[Sender], iface, group, port, msg)) + // fails here, so binding succeeds but sending a message does not + expectMsg(msg) + true + + } catch { + case _: AssertionError => + system.log.info("Failed to run test on interface {}", ipv6iface.getDisplayName) + false + + } finally { + // unbind + system.stop(listener) + } + } } - val Some(ipv6Iface) = NetworkInterface.getNetworkInterfaces.find(okInterfaceToUse) - // host assigned link local multicast address http://tools.ietf.org/html/rfc3307#section-4.3.2 - // generate a random 32 bit multicast address with the high order bit set - val randomAddress: String = (Random.nextInt().abs.toLong | (1L << 31)).toHexString.toUpperCase - val group = randomAddress.grouped(4).mkString("FF02::", ":", "") - val port = TestUtils.temporaryUdpIpv6Port(ipv6Iface) - val msg = "ohi" - val sink = testActor - val iface = ipv6Iface.getName - - val listener = system.actorOf(Props(classOf[Listener], iface, group, port, sink)) - expectMsgType[Udp.Bound] - val sender = system.actorOf(Props(classOf[Sender], iface, group, port, msg)) - // fails here, so binding succeeds but sending a message does not - expectMsg(msg) - - // unbind - system.stop(listener) } }