2013-01-18 13:20:17 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2013-03-26 11:25:09 +01:00
|
|
|
package akka
|
2013-01-18 13:20:17 +01:00
|
|
|
|
2013-02-22 14:14:13 +01:00
|
|
|
import scala.collection.immutable
|
2013-11-27 15:03:27 +01:00
|
|
|
import scala.concurrent.duration.Duration
|
|
|
|
|
import java.net.{ SocketAddress, InetSocketAddress }
|
2013-04-22 11:43:38 +02:00
|
|
|
import java.nio.channels.{ DatagramChannel, ServerSocketChannel }
|
2013-11-27 15:03:27 +01:00
|
|
|
import akka.actor.{ ActorSystem, ActorRef }
|
2013-01-18 13:20:17 +01:00
|
|
|
import akka.testkit.TestProbe
|
|
|
|
|
|
|
|
|
|
object TestUtils {
|
|
|
|
|
|
2013-04-22 11:43:38 +02:00
|
|
|
// Structural type needed since DatagramSocket and ServerSocket has no common ancestor apart from Object
|
|
|
|
|
type GeneralSocket = {
|
|
|
|
|
def bind(sa: SocketAddress): Unit
|
|
|
|
|
def close(): Unit
|
|
|
|
|
def getLocalPort(): Int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def temporaryServerAddress(address: String = "127.0.0.1", udp: Boolean = false): InetSocketAddress =
|
|
|
|
|
temporaryServerAddresses(1, address, udp).head
|
2013-02-22 14:14:13 +01:00
|
|
|
|
2013-04-22 11:43:38 +02:00
|
|
|
def temporaryServerAddresses(numberOfAddresses: Int, hostname: String = "127.0.0.1", udp: Boolean = false): immutable.IndexedSeq[InetSocketAddress] = {
|
2013-04-20 21:01:32 -07:00
|
|
|
Vector.fill(numberOfAddresses) {
|
2013-04-22 11:43:38 +02:00
|
|
|
val serverSocket: GeneralSocket =
|
|
|
|
|
if (udp) DatagramChannel.open().socket()
|
|
|
|
|
else ServerSocketChannel.open().socket()
|
|
|
|
|
|
|
|
|
|
serverSocket.bind(new InetSocketAddress(hostname, 0))
|
|
|
|
|
(serverSocket, new InetSocketAddress(hostname, serverSocket.getLocalPort))
|
2013-04-20 21:01:32 -07:00
|
|
|
} collect { case (socket, address) ⇒ socket.close(); address }
|
2013-01-22 13:48:36 +01:00
|
|
|
}
|
|
|
|
|
|
2013-11-27 15:03:27 +01:00
|
|
|
def verifyActorTermination(actor: ActorRef, max: Duration = Duration.Undefined)(implicit system: ActorSystem): Unit = {
|
2013-01-18 13:20:17 +01:00
|
|
|
val watcher = TestProbe()
|
|
|
|
|
watcher.watch(actor)
|
2013-11-27 15:03:27 +01:00
|
|
|
watcher.expectTerminated(actor, max)
|
2013-01-18 13:20:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|