2013-01-22 13:48:36 +01:00
|
|
|
/**
|
2017-01-04 17:37:10 +01:00
|
|
|
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
2013-01-22 13:48:36 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.io
|
|
|
|
|
|
|
|
|
|
import scala.annotation.tailrec
|
2013-06-14 14:45:43 +02:00
|
|
|
import scala.collection.immutable
|
2013-01-22 13:48:36 +01:00
|
|
|
import akka.testkit.{ AkkaSpec, TestProbe }
|
|
|
|
|
import akka.actor.ActorRef
|
2013-02-05 11:48:47 +01:00
|
|
|
import akka.io.Inet.SocketOption
|
2015-04-01 14:03:06 +02:00
|
|
|
import akka.testkit.SocketUtil._
|
2013-01-22 13:48:36 +01:00
|
|
|
import Tcp._
|
|
|
|
|
|
2013-02-06 12:17:52 +01:00
|
|
|
trait TcpIntegrationSpecSupport { _: AkkaSpec ⇒
|
2013-01-22 13:48:36 +01:00
|
|
|
|
2014-08-29 11:17:49 +02:00
|
|
|
class TestSetup(shouldBindServer: Boolean = true) {
|
2013-01-22 13:48:36 +01:00
|
|
|
val bindHandler = TestProbe()
|
|
|
|
|
val endpoint = temporaryServerAddress()
|
|
|
|
|
|
2014-08-29 11:17:49 +02:00
|
|
|
if (shouldBindServer) bindServer()
|
2013-01-22 13:48:36 +01:00
|
|
|
|
|
|
|
|
def bindServer(): Unit = {
|
|
|
|
|
val bindCommander = TestProbe()
|
|
|
|
|
bindCommander.send(IO(Tcp), Bind(bindHandler.ref, endpoint, options = bindOptions))
|
2013-04-13 20:53:52 +02:00
|
|
|
bindCommander.expectMsg(Bound(endpoint))
|
2013-01-22 13:48:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def establishNewClientConnection(): (TestProbe, ActorRef, TestProbe, ActorRef) = {
|
|
|
|
|
val connectCommander = TestProbe()
|
|
|
|
|
connectCommander.send(IO(Tcp), Connect(endpoint, options = connectOptions))
|
|
|
|
|
val Connected(`endpoint`, localAddress) = connectCommander.expectMsgType[Connected]
|
|
|
|
|
val clientHandler = TestProbe()
|
2014-01-16 15:16:35 +01:00
|
|
|
connectCommander.sender() ! Register(clientHandler.ref)
|
2013-01-22 13:48:36 +01:00
|
|
|
|
|
|
|
|
val Connected(`localAddress`, `endpoint`) = bindHandler.expectMsgType[Connected]
|
|
|
|
|
val serverHandler = TestProbe()
|
2014-01-16 15:16:35 +01:00
|
|
|
bindHandler.sender() ! Register(serverHandler.ref)
|
2013-01-22 13:48:36 +01:00
|
|
|
|
2014-01-16 15:16:35 +01:00
|
|
|
(clientHandler, connectCommander.sender(), serverHandler, bindHandler.sender())
|
2013-01-22 13:48:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@tailrec final def expectReceivedData(handler: TestProbe, remaining: Int): Unit =
|
|
|
|
|
if (remaining > 0) {
|
|
|
|
|
val recv = handler.expectMsgType[Received]
|
|
|
|
|
expectReceivedData(handler, remaining - recv.data.size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** allow overriding socket options for server side channel */
|
2013-01-22 16:03:22 +01:00
|
|
|
def bindOptions: immutable.Traversable[SocketOption] = Nil
|
2013-01-22 13:48:36 +01:00
|
|
|
|
|
|
|
|
/** allow overriding socket options for client side channel */
|
2013-01-22 16:03:22 +01:00
|
|
|
def connectOptions: immutable.Traversable[SocketOption] = Nil
|
2013-01-22 13:48:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|