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

@ -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)
}
}