2014-08-19 14:02:23 +03:00
|
|
|
/**
|
2016-02-23 12:58:39 +01:00
|
|
|
* Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.com>
|
2014-08-19 14:02:23 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package docs.io
|
|
|
|
|
|
2015-05-05 16:44:49 +02:00
|
|
|
import java.net.{ Inet6Address, InetSocketAddress, NetworkInterface, StandardProtocolFamily }
|
2014-08-19 14:02:23 +03:00
|
|
|
import java.nio.channels.DatagramChannel
|
2016-04-13 13:30:04 +02:00
|
|
|
import scala.util.Random
|
2015-05-05 16:44:49 +02:00
|
|
|
import akka.actor.{ ActorSystem, Props }
|
2014-09-05 10:17:57 +02:00
|
|
|
import akka.io.Udp
|
2014-08-19 14:02:23 +03:00
|
|
|
import akka.testkit.TestKit
|
2015-05-05 16:44:49 +02:00
|
|
|
import org.scalatest.{ BeforeAndAfter, WordSpecLike }
|
2014-08-19 14:02:23 +03:00
|
|
|
import scala.collection.JavaConversions.enumerationAsScalaIterator
|
2016-05-30 12:54:27 +02:00
|
|
|
import org.scalatest.BeforeAndAfterAll
|
2014-08-19 14:02:23 +03:00
|
|
|
|
2016-05-30 12:54:27 +02:00
|
|
|
class ScalaUdpMulticastSpec extends TestKit(ActorSystem("ScalaUdpMulticastSpec")) with WordSpecLike with BeforeAndAfterAll {
|
2014-08-19 14:02:23 +03:00
|
|
|
|
|
|
|
|
"listener" should {
|
|
|
|
|
"send message back to sink" in {
|
2016-04-13 13:30:04 +02:00
|
|
|
val ipv6ifaces =
|
|
|
|
|
NetworkInterface.getNetworkInterfaces.toSeq.filter(iface =>
|
|
|
|
|
iface.supportsMulticast &&
|
|
|
|
|
iface.isUp &&
|
|
|
|
|
iface.getInetAddresses.exists(_.isInstanceOf[Inet6Address]))
|
2015-12-08 16:42:16 +01:00
|
|
|
|
2016-04-13 13:30:04 +02:00
|
|
|
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
|
2014-08-19 14:02:23 +03:00
|
|
|
|
2016-04-13 13:30:04 +02:00
|
|
|
} catch {
|
|
|
|
|
case _: AssertionError =>
|
|
|
|
|
system.log.info("Failed to run test on interface {}", ipv6iface.getDisplayName)
|
|
|
|
|
false
|
2014-08-19 14:02:23 +03:00
|
|
|
|
2016-04-13 13:30:04 +02:00
|
|
|
} finally {
|
|
|
|
|
// unbind
|
|
|
|
|
system.stop(listener)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-08-19 14:02:23 +03:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-30 12:54:27 +02:00
|
|
|
override def afterAll(): Unit = {
|
2014-08-19 14:02:23 +03:00
|
|
|
TestKit.shutdownActorSystem(system)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object TestUtils {
|
|
|
|
|
def temporaryUdpIpv6Port(iface: NetworkInterface) = {
|
|
|
|
|
val serverSocket = DatagramChannel.open(StandardProtocolFamily.INET6).socket()
|
|
|
|
|
serverSocket.bind(new InetSocketAddress(iface.getInetAddresses.nextElement(), 0))
|
|
|
|
|
val port = serverSocket.getLocalPort
|
|
|
|
|
serverSocket.close()
|
|
|
|
|
port
|
|
|
|
|
}
|
|
|
|
|
}
|