avoid infinite blocking in TcpConnectionSpec #21375

* use socket timeout
* additional cleanup of socket utils
This commit is contained in:
Patrik Nordwall 2016-11-09 17:36:04 +01:00 committed by Johan Andrén
parent 72925ba392
commit 4f013a3d1e
8 changed files with 42 additions and 79 deletions

View file

@ -13,6 +13,7 @@ import akka.testkit.TestKit
import org.scalatest.{ BeforeAndAfter, WordSpecLike }
import scala.collection.JavaConversions.enumerationAsScalaIterator
import org.scalatest.BeforeAndAfterAll
import akka.testkit.SocketUtil
class ScalaUdpMulticastSpec extends TestKit(ActorSystem("ScalaUdpMulticastSpec")) with WordSpecLike with BeforeAndAfterAll {
@ -37,7 +38,7 @@ class ScalaUdpMulticastSpec extends TestKit(ActorSystem("ScalaUdpMulticastSpec")
// generate a random 32 bit multicast address with the high order bit set
val randomAddress: String = (Random.nextInt().abs.toLong | (1L << 31)).toHexString.toUpperCase
val group = randomAddress.grouped(4).mkString("FF02::", ":", "")
val port = TestUtils.temporaryUdpIpv6Port(ipv6iface)
val port = SocketUtil.temporaryUdpIpv6Port(ipv6iface)
val msg = "ohi"
val sink = testActor
val iface = ipv6iface.getName
@ -70,12 +71,3 @@ class ScalaUdpMulticastSpec extends TestKit(ActorSystem("ScalaUdpMulticastSpec")
}
object TestUtils {
def temporaryUdpIpv6Port(iface: NetworkInterface) = {
val serverSocket = DatagramChannel.open(StandardProtocolFamily.INET6).socket()
serverSocket.bind(new InetSocketAddress(iface.getInetAddresses.nextElement(), 0))
val port = serverSocket.getLocalPort
serverSocket.close()
port
}
}

View file

@ -11,9 +11,9 @@ import akka.stream.scaladsl._
import akka.testkit.AkkaSpec
import akka.testkit.TestProbe
import akka.util.ByteString
import docs.utils.TestUtils
import scala.concurrent.Future
import akka.testkit.SocketUtil
class StreamTcpDocSpec extends AkkaSpec {
@ -37,7 +37,7 @@ class StreamTcpDocSpec extends AkkaSpec {
//#echo-server-simple-bind
}
{
val (host, port) = TestUtils.temporaryServerHostnameAndPort()
val (host, port) = SocketUtil.temporaryServerHostnameAndPort()
//#echo-server-simple-handle
import akka.stream.scaladsl.Framing
@ -62,7 +62,7 @@ class StreamTcpDocSpec extends AkkaSpec {
}
"initial server banner echo server" in {
val localhost = TestUtils.temporaryServerAddress()
val localhost = SocketUtil.temporaryServerAddress()
val connections = Tcp().bind(localhost.getHostName, localhost.getPort) // TODO getHostString in Java7
val serverProbe = TestProbe()

View file

@ -1,24 +0,0 @@
/**
* Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.com>
*/
package docs.utils
import java.net.InetSocketAddress
import java.nio.channels.ServerSocketChannel
object TestUtils {
def temporaryServerAddress(interface: String = "127.0.0.1"): InetSocketAddress = {
val serverSocket = ServerSocketChannel.open()
try {
serverSocket.socket.bind(new InetSocketAddress(interface, 0))
val port = serverSocket.socket.getLocalPort
new InetSocketAddress(interface, port)
} finally serverSocket.close()
}
def temporaryServerHostnameAndPort(interface: String = "127.0.0.1"): (String, Int) = {
val socketAddress = temporaryServerAddress(interface)
socketAddress.getHostName -> socketAddress.getPort // TODO getHostString in Java7
}
}