2013-01-30 18:10:45 +01:00
|
|
|
/**
|
2014-02-02 19:05:45 -06:00
|
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
2013-01-30 18:10:45 +01:00
|
|
|
*/
|
|
|
|
|
package akka.io
|
|
|
|
|
|
2013-06-14 14:45:43 +02:00
|
|
|
import java.net.InetSocketAddress
|
2013-09-08 09:29:45 -05:00
|
|
|
import java.nio.channels.DatagramChannel
|
2013-01-30 18:10:45 +01:00
|
|
|
import akka.testkit.{ TestProbe, ImplicitSender, AkkaSpec }
|
|
|
|
|
import akka.util.ByteString
|
|
|
|
|
import akka.actor.ActorRef
|
2013-06-14 14:45:43 +02:00
|
|
|
import akka.io.Udp._
|
2013-09-08 09:29:45 -05:00
|
|
|
import akka.io.Inet._
|
2013-06-14 14:45:43 +02:00
|
|
|
import akka.TestUtils._
|
2013-01-30 18:10:45 +01:00
|
|
|
|
2013-05-29 16:13:10 +02:00
|
|
|
class UdpIntegrationSpec extends AkkaSpec("""
|
|
|
|
|
akka.loglevel = INFO
|
|
|
|
|
akka.actor.serialize-creators = on""") with ImplicitSender {
|
2013-01-30 18:10:45 +01:00
|
|
|
|
2014-08-19 14:02:23 +03:00
|
|
|
val addresses = temporaryServerAddresses(6, udp = true)
|
2013-02-22 14:14:13 +01:00
|
|
|
|
|
|
|
|
def bindUdp(address: InetSocketAddress, handler: ActorRef): ActorRef = {
|
2013-01-30 18:10:45 +01:00
|
|
|
val commander = TestProbe()
|
2013-03-25 10:02:50 +01:00
|
|
|
commander.send(IO(Udp), Bind(handler, address))
|
2013-04-13 20:53:52 +02:00
|
|
|
commander.expectMsg(Bound(address))
|
2014-01-16 15:16:35 +01:00
|
|
|
commander.sender()
|
2013-01-30 18:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val simpleSender: ActorRef = {
|
|
|
|
|
val commander = TestProbe()
|
2013-03-25 10:02:50 +01:00
|
|
|
commander.send(IO(Udp), SimpleSender)
|
2013-05-26 18:29:23 +02:00
|
|
|
commander.expectMsg(SimpleSenderReady)
|
2014-01-16 15:16:35 +01:00
|
|
|
commander.sender()
|
2013-01-30 18:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"The UDP Fire-and-Forget implementation" must {
|
|
|
|
|
|
|
|
|
|
"be able to send without binding" in {
|
2013-02-22 14:14:13 +01:00
|
|
|
val serverAddress = addresses(0)
|
|
|
|
|
val server = bindUdp(serverAddress, testActor)
|
2013-01-30 18:10:45 +01:00
|
|
|
val data = ByteString("To infinity and beyond!")
|
|
|
|
|
simpleSender ! Send(data, serverAddress)
|
|
|
|
|
|
2014-01-31 11:14:13 +01:00
|
|
|
expectMsgType[Received].data should be(data)
|
2013-02-10 13:52:52 +01:00
|
|
|
|
2013-01-30 18:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2013-02-27 09:20:00 +01:00
|
|
|
"be able to send several packet back and forth with binding" in {
|
2013-02-22 14:14:13 +01:00
|
|
|
val serverAddress = addresses(1)
|
|
|
|
|
val clientAddress = addresses(2)
|
|
|
|
|
val server = bindUdp(serverAddress, testActor)
|
|
|
|
|
val client = bindUdp(clientAddress, testActor)
|
2013-01-30 18:10:45 +01:00
|
|
|
val data = ByteString("Fly little packet!")
|
|
|
|
|
|
2013-02-27 09:20:00 +01:00
|
|
|
def checkSendingToClient(): Unit = {
|
|
|
|
|
server ! Send(data, clientAddress)
|
|
|
|
|
expectMsgPF() {
|
|
|
|
|
case Received(d, a) ⇒
|
2014-01-31 11:14:13 +01:00
|
|
|
d should be(data)
|
|
|
|
|
a should be(serverAddress)
|
2013-02-27 09:20:00 +01:00
|
|
|
}
|
2013-01-30 18:10:45 +01:00
|
|
|
}
|
2013-02-27 09:20:00 +01:00
|
|
|
def checkSendingToServer(): Unit = {
|
|
|
|
|
client ! Send(data, serverAddress)
|
|
|
|
|
expectMsgPF() {
|
|
|
|
|
case Received(d, a) ⇒
|
2014-01-31 11:14:13 +01:00
|
|
|
d should be(data)
|
|
|
|
|
a should be(clientAddress)
|
2013-02-27 09:20:00 +01:00
|
|
|
}
|
2013-02-07 17:54:42 +01:00
|
|
|
}
|
2013-01-30 18:10:45 +01:00
|
|
|
|
2013-02-27 09:20:00 +01:00
|
|
|
(0 until 20).foreach(_ ⇒ checkSendingToServer())
|
|
|
|
|
(0 until 20).foreach(_ ⇒ checkSendingToClient())
|
|
|
|
|
(0 until 20).foreach { i ⇒
|
|
|
|
|
if (i % 2 == 0) checkSendingToServer()
|
|
|
|
|
else checkSendingToClient()
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-08 09:29:45 -05:00
|
|
|
|
|
|
|
|
"call SocketOption.beforeBind method before bind." in {
|
|
|
|
|
val commander = TestProbe()
|
|
|
|
|
val assertOption = AssertBeforeBind()
|
|
|
|
|
commander.send(IO(Udp), Bind(testActor, addresses(3), options = List(assertOption)))
|
|
|
|
|
commander.expectMsg(Bound(addresses(3)))
|
|
|
|
|
assert(assertOption.beforeCalled === 1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"call SocketOption.afterConnect method after binding." in {
|
|
|
|
|
val commander = TestProbe()
|
|
|
|
|
val assertOption = AssertAfterConnect()
|
|
|
|
|
commander.send(IO(Udp), Bind(testActor, addresses(4), options = List(assertOption)))
|
|
|
|
|
commander.expectMsg(Bound(addresses(4)))
|
|
|
|
|
assert(assertOption.afterCalled === 1)
|
|
|
|
|
}
|
2014-08-19 14:02:23 +03:00
|
|
|
|
|
|
|
|
"call DatagramChannelCreator.create method when opening channel" in {
|
|
|
|
|
val commander = TestProbe()
|
|
|
|
|
val assertOption = AssertOpenDatagramChannel()
|
|
|
|
|
commander.send(IO(Udp), Bind(testActor, addresses(5), options = List(assertOption)))
|
|
|
|
|
commander.expectMsg(Bound(addresses(5)))
|
|
|
|
|
assert(assertOption.openCalled === 1)
|
|
|
|
|
}
|
2013-09-08 09:29:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private case class AssertBeforeBind() extends SocketOption {
|
|
|
|
|
var beforeCalled = 0
|
|
|
|
|
|
|
|
|
|
override def beforeBind(c: DatagramChannel) = {
|
|
|
|
|
assert(!c.socket.isBound)
|
|
|
|
|
beforeCalled += 1
|
2013-01-30 18:10:45 +01:00
|
|
|
}
|
2013-09-08 09:29:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private case class AssertAfterConnect() extends SocketOption {
|
|
|
|
|
var afterCalled = 0
|
2013-01-30 18:10:45 +01:00
|
|
|
|
2013-09-08 09:29:45 -05:00
|
|
|
override def afterConnect(c: DatagramChannel) = {
|
|
|
|
|
assert(c.socket.isBound)
|
|
|
|
|
afterCalled += 1
|
|
|
|
|
}
|
2013-01-30 18:10:45 +01:00
|
|
|
}
|
2014-08-19 14:02:23 +03:00
|
|
|
|
|
|
|
|
private case class AssertOpenDatagramChannel() extends DatagramChannelCreator {
|
|
|
|
|
var openCalled = 0
|
|
|
|
|
|
|
|
|
|
override def create() = {
|
|
|
|
|
openCalled += 1
|
|
|
|
|
super.create()
|
|
|
|
|
}
|
|
|
|
|
}
|