2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2020-01-02 07:24:59 -05:00
|
|
|
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
|
2014-08-19 14:02:23 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package docs.io
|
|
|
|
|
|
2019-11-26 18:45:31 +01:00
|
|
|
import java.net.Inet6Address
|
|
|
|
|
import java.net.NetworkInterface
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorSystem
|
|
|
|
|
import akka.actor.Props
|
2014-09-05 10:17:57 +02:00
|
|
|
import akka.io.Udp
|
2016-11-09 17:36:04 +01:00
|
|
|
import akka.testkit.SocketUtil
|
2019-11-26 18:45:31 +01:00
|
|
|
import akka.testkit.TestKit
|
2019-05-24 08:11:50 +02:00
|
|
|
import akka.util.ccompat.JavaConverters._
|
2019-11-26 18:45:31 +01:00
|
|
|
import org.scalatest.BeforeAndAfterAll
|
2020-01-17 16:33:13 +03:00
|
|
|
import org.scalatest.matchers.should.Matchers
|
|
|
|
|
import org.scalatest.wordspec.AnyWordSpecLike
|
2019-11-26 18:45:31 +01:00
|
|
|
|
|
|
|
|
import scala.util.Random
|
2014-08-19 14:02:23 +03:00
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
class ScalaUdpMulticastSpec
|
|
|
|
|
extends TestKit(ActorSystem("ScalaUdpMulticastSpec"))
|
2020-01-17 16:33:13 +03:00
|
|
|
with AnyWordSpecLike
|
2019-11-26 18:45:31 +01:00
|
|
|
with Matchers
|
2019-03-11 10:38:24 +01:00
|
|
|
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 =
|
2019-03-11 10:38:24 +01:00
|
|
|
NetworkInterface.getNetworkInterfaces.asScala.toSeq.filter(
|
|
|
|
|
iface =>
|
|
|
|
|
iface.supportsMulticast &&
|
2016-04-13 13:30:04 +02:00
|
|
|
iface.isUp &&
|
2018-11-22 16:18:10 +01:00
|
|
|
iface.getInetAddresses.asScala.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
|
2019-11-26 18:45:31 +01:00
|
|
|
var failures: List[AssertionError] = Nil
|
|
|
|
|
var foundOneThatWorked = false
|
|
|
|
|
val iterator = ipv6ifaces.iterator
|
|
|
|
|
while (!foundOneThatWorked && iterator.hasNext) {
|
|
|
|
|
val ipv6iface = iterator.next()
|
2016-04-13 13:30:04 +02:00
|
|
|
// 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::", ":", "")
|
2016-11-09 17:36:04 +01:00
|
|
|
val port = SocketUtil.temporaryUdpIpv6Port(ipv6iface)
|
2016-04-13 13:30:04 +02:00
|
|
|
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)
|
2019-11-26 18:45:31 +01:00
|
|
|
foundOneThatWorked = true
|
2014-08-19 14:02:23 +03:00
|
|
|
|
2016-04-13 13:30:04 +02:00
|
|
|
} catch {
|
2019-11-26 18:45:31 +01:00
|
|
|
case ex: AssertionError =>
|
2016-04-13 13:30:04 +02:00
|
|
|
system.log.info("Failed to run test on interface {}", ipv6iface.getDisplayName)
|
2019-11-26 18:45:31 +01:00
|
|
|
failures = ex :: failures
|
2014-08-19 14:02:23 +03:00
|
|
|
|
2016-04-13 13:30:04 +02:00
|
|
|
} finally {
|
|
|
|
|
// unbind
|
|
|
|
|
system.stop(listener)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-26 18:45:31 +01:00
|
|
|
|
|
|
|
|
if (failures.size == ipv6ifaces.size)
|
|
|
|
|
fail(s"Multicast failed on all available interfaces: ${failures}")
|
2016-04-13 13:30:04 +02:00
|
|
|
}
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|