doc,io #16262 UDP multicast tests work on all platforms (#20290)

This commit is contained in:
Johan Andrén 2016-04-13 13:30:04 +02:00 committed by Konrad Malawski
parent f3ac18a362
commit 55e08682c5
2 changed files with 90 additions and 61 deletions

View file

@ -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,22 +32,27 @@ public class JavaUdpMulticastTest {
@Test
public void testUdpMulticast() throws Exception {
new JavaTestKit(system) {{
NetworkInterface ipv6Iface = null;
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements() && ipv6Iface == null;) {
List<NetworkInterface> ipv6Ifaces = new ArrayList<>();
for (Enumeration<NetworkInterface> 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<InetAddress> addresses = interf.getInetAddresses(); addresses.hasMoreElements() && ipv6Iface == null; ) {
if (interf.isUp() && interf.supportsMulticast()) {
for (Enumeration<InetAddress> 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<NetworkInterface> 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();
@ -66,12 +68,25 @@ public class JavaUdpMulticastTest {
final String iface = ipv6Iface.getName();
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);
}
}
}
}};
}

View file

@ -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,37 +19,51 @@ class ScalaUdpMulticastSpec extends TestKit(ActorSystem("ScalaUdpMulticastSpec")
"listener" should {
"send message back to sink" in {
// TODO make this work consistently on all platforms
val ipv6ifaces =
NetworkInterface.getNetworkInterfaces.toSeq.filter(iface =>
iface.supportsMulticast &&
iface.isUp &&
iface.getInetAddresses.exists(_.isInstanceOf[Inet6Address]))
if (ipv6ifaces.isEmpty) {
// IPv6 not supported for any interface on this platform
pending
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")
}
val Some(ipv6Iface) = NetworkInterface.getNetworkInterfaces.find(okInterfaceToUse)
} 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 port = TestUtils.temporaryUdpIpv6Port(ipv6iface)
val msg = "ohi"
val sink = testActor
val iface = ipv6Iface.getName
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)
}
}
}
}
}
def afterAll(): Unit = {
TestKit.shutdownActorSystem(system)