I want to test my UDP server implementation. I am adopting the UDP server example here
class Listener(addr: InetSocketAddress) extends Actor {
import context.system
IO(Udp) ! Udp.Bind(self, addr)
def receive = {
case Udp.Bound(local) =>
context.become(ready(sender()))
}
def ready(socket: ActorRef): Receive = {
case Udp.Received(data, remote) =>
val processed = // parse data etc., e.g. using PipelineStage
socket ! Udp.Send(processed, remote) // example server echoes back
case Udp.Unbind => socket ! Udp.Unbind
case Udp.Unbound => context.stop(self)
}
}
I have found an example spec online
The relevant snippet:
abstract class UdpDocSpec extends AkkaSpec {
def listenerProps(next: ActorRef): Props
def simpleSenderProps(remote: InetSocketAddress): Props
def connectedProps(remote: InetSocketAddress): Props
"demonstrate Udp" in {
val probe = TestProbe()
val listen = watch(system.actorOf(listenerProps(probe.ref)))
val local = probe.expectMsgType[InetSocketAddress]
val listener = probe.lastSender
val send = system.actorOf(simpleSenderProps(local))
probe.expectMsg("hello")
send ! "world"
probe.expectMsg("world")
listen ! Udp.Unbind
expectTerminated(listen)
}
...
It seems to me that his way of testing is to
- construct a udp sender/client
- send relevant sequence of data from client
- verify whether server churns out expected reply
this SO thread serves an excellent example on how to test on actors by directly sending message to the actor you created. However in my case, I am not responsible for sending message to the actor I created (the UDP server)
My question is, is there a way to test my server without involving IO (port listening, client construction, etc)? If so, how? Or what is the recommended way of testing?
Aucun commentaire:
Enregistrer un commentaire