diff --git a/akka-actor-tests/src/test/scala/akka/io/TcpConnectionSpec.scala b/akka-actor-tests/src/test/scala/akka/io/TcpConnectionSpec.scala index b10df3320c..11e58842d3 100644 --- a/akka-actor-tests/src/test/scala/akka/io/TcpConnectionSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/io/TcpConnectionSpec.scala @@ -544,6 +544,16 @@ class TcpConnectionSpec extends AkkaSpec(""" } } + "report failed connection attempt when target cannot be resolved" in + new UnacceptedConnectionTest() { + val address = new InetSocketAddress("notthere.local", 666) + override lazy val connectionActor = createConnectionActorWithoutRegistration(serverAddress = address) + run { + connectionActor ! newChannelRegistration + userHandler.expectMsg(30.seconds, CommandFailed(Connect(address))) + } + } + "report failed connection attempt when timing out" in new UnacceptedConnectionTest() { override lazy val connectionActor = createConnectionActor(serverAddress = UnboundAddress, timeout = Option(100.millis)) @@ -758,16 +768,24 @@ class TcpConnectionSpec extends AkkaSpec(""" def createConnectionActor(serverAddress: InetSocketAddress = serverAddress, options: immutable.Seq[SocketOption] = Nil, timeout: Option[FiniteDuration] = None): TestActorRef[TcpOutgoingConnection] = { - val ref = TestActorRef( - new TcpOutgoingConnection(Tcp(system), this, userHandler.ref, Connect(serverAddress, options = options, timeout = timeout)) { - override def postRestart(reason: Throwable): Unit = context.stop(self) // ensure we never restart - }) - ref ! new ChannelRegistration { + val ref = createConnectionActorWithoutRegistration(serverAddress, options, timeout) + ref ! newChannelRegistration + ref + } + + def newChannelRegistration: ChannelRegistration = + new ChannelRegistration { def enableInterest(op: Int): Unit = interestCallReceiver.ref ! op def disableInterest(op: Int): Unit = interestCallReceiver.ref ! -op } - ref - } + + def createConnectionActorWithoutRegistration(serverAddress: InetSocketAddress = serverAddress, + options: immutable.Seq[SocketOption] = Nil, + timeout: Option[FiniteDuration] = None): TestActorRef[TcpOutgoingConnection] = + TestActorRef( + new TcpOutgoingConnection(Tcp(system), this, userHandler.ref, Connect(serverAddress, options = options, timeout = timeout)) { + override def postRestart(reason: Throwable): Unit = context.stop(self) // ensure we never restart + }) } trait SmallRcvBuffer { _: LocalServerTest ⇒ diff --git a/akka-actor/src/main/scala/akka/io/TcpOutgoingConnection.scala b/akka-actor/src/main/scala/akka/io/TcpOutgoingConnection.scala index 6ac141b3cc..6df4c226c3 100644 --- a/akka-actor/src/main/scala/akka/io/TcpOutgoingConnection.scala +++ b/akka-actor/src/main/scala/akka/io/TcpOutgoingConnection.scala @@ -4,9 +4,8 @@ package akka.io -import java.io.IOException import java.nio.channels.{ SelectionKey, SocketChannel } -import java.net.ConnectException +import scala.util.control.NonFatal import scala.collection.immutable import scala.concurrent.duration._ import akka.actor.{ ReceiveTimeout, ActorRef } @@ -42,7 +41,7 @@ private[io] class TcpOutgoingConnection(_tcp: TcpExt, try { thunk } catch { - case e: IOException ⇒ + case NonFatal(e) ⇒ log.debug("Could not establish connection to [{}] due to {}", remoteAddress, e) stop() }