|
Akka/Scala example source code file (UdpConnectedIntegrationSpec.scala)
The UdpConnectedIntegrationSpec.scala Akka example source code
/**
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.io
import java.net.InetSocketAddress
import akka.testkit.{ TestProbe, ImplicitSender, AkkaSpec }
import akka.util.ByteString
import akka.actor.ActorRef
import akka.TestUtils._
class UdpConnectedIntegrationSpec extends AkkaSpec("""
akka.loglevel = INFO
akka.actor.serialize-creators = on
""") with ImplicitSender {
val addresses = temporaryServerAddresses(3, udp = true)
def bindUdp(address: InetSocketAddress, handler: ActorRef): ActorRef = {
val commander = TestProbe()
commander.send(IO(Udp), Udp.Bind(handler, address))
commander.expectMsg(Udp.Bound(address))
commander.sender()
}
def connectUdp(localAddress: Option[InetSocketAddress], remoteAddress: InetSocketAddress, handler: ActorRef): ActorRef = {
val commander = TestProbe()
commander.send(IO(UdpConnected), UdpConnected.Connect(handler, remoteAddress, localAddress, Nil))
commander.expectMsg(UdpConnected.Connected)
commander.sender()
}
"The UDP connection oriented implementation" must {
"be able to send and receive without binding" in {
val serverAddress = addresses(0)
val server = bindUdp(serverAddress, testActor)
val data1 = ByteString("To infinity and beyond!")
val data2 = ByteString("All your datagram belong to us")
connectUdp(localAddress = None, serverAddress, testActor) ! UdpConnected.Send(data1)
val clientAddress = expectMsgPF() {
case Udp.Received(d, a) ⇒
d should be(data1)
a
}
server ! Udp.Send(data2, clientAddress)
expectMsgType[UdpConnected.Received].data should be(data2)
}
"be able to send and receive with binding" in {
val serverAddress = addresses(1)
val clientAddress = addresses(2)
val server = bindUdp(serverAddress, testActor)
val data1 = ByteString("To infinity and beyond!")
val data2 = ByteString("All your datagram belong to us")
connectUdp(Some(clientAddress), serverAddress, testActor) ! UdpConnected.Send(data1)
expectMsgPF() {
case Udp.Received(d, a) ⇒
d should be(data1)
a should be(clientAddress)
}
server ! Udp.Send(data2, clientAddress)
expectMsgType[UdpConnected.Received].data should be(data2)
}
}
}
Other Akka source code examplesHere is a short list of links related to this Akka UdpConnectedIntegrationSpec.scala source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.