From 7616b40e2982ead1eff9d665a12cca018abe26f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martynas=20Mickevi=C4=8Dius?= Date: Thu, 26 Mar 2015 20:56:49 +0200 Subject: [PATCH] =sam #16905 multiple fixes for java-lambda doc samples * use a free port instead of a hardcoded one * do not use null for stream elements * use defined java8 home key --- .../code/docs/stream/StreamTcpDocSpec.scala | 48 +++++++++++-------- .../rst/scala/code/docs/utils/TestUtils.scala | 24 ++++++++++ 2 files changed, 53 insertions(+), 19 deletions(-) create mode 100644 akka-docs-dev/rst/scala/code/docs/utils/TestUtils.scala diff --git a/akka-docs-dev/rst/scala/code/docs/stream/StreamTcpDocSpec.scala b/akka-docs-dev/rst/scala/code/docs/stream/StreamTcpDocSpec.scala index 9dee09ab24..f387903d99 100644 --- a/akka-docs-dev/rst/scala/code/docs/stream/StreamTcpDocSpec.scala +++ b/akka-docs-dev/rst/scala/code/docs/stream/StreamTcpDocSpec.scala @@ -13,6 +13,7 @@ import akka.stream.testkit.AkkaSpec import akka.testkit.TestProbe import akka.util.ByteString import cookbook.RecipeParseLines +import docs.utils.TestUtils import StreamTcp._ import scala.concurrent.Future @@ -25,33 +26,41 @@ class StreamTcpDocSpec extends AkkaSpec { // silence sysout def println(s: String) = () - val localhost = new InetSocketAddress("127.0.0.1", 8888) - "simple server connection" ignore { - //#echo-server-simple-bind - val localhost = new InetSocketAddress("127.0.0.1", 8888) - //#echo-server-simple-handle - val connections: Source[IncomingConnection, Future[ServerBinding]] = StreamTcp().bind(localhost) - //#echo-server-simple-bind - - connections runForeach { connection => - println(s"New connection from: ${connection.remoteAddress}") - - val echo = Flow[ByteString] - .transform(() => RecipeParseLines.parseLines("\n", maximumLineBytes = 256)) - .map(_ + "!!!\n") - .map(ByteString(_)) - - connection.handleWith(echo) + { + //#echo-server-simple-bind + val localhost = new InetSocketAddress("127.0.0.1", 8888) + //#echo-server-simple-bind + } + { + val localhost = TestUtils.temporaryServerAddress() + //#echo-server-simple-bind + val connections: Source[IncomingConnection, Future[ServerBinding]] = + StreamTcp().bind(localhost) + //#echo-server-simple-bind + + //#echo-server-simple-handle + connections runForeach { connection => + println(s"New connection from: ${connection.remoteAddress}") + + val echo = Flow[ByteString] + .transform(() => RecipeParseLines.parseLines("\n", maximumLineBytes = 256)) + .map(_ + "!!!\n") + .map(ByteString(_)) + + connection.handleWith(echo) + } + //#echo-server-simple-handle } - //#echo-server-simple-handle } "simple repl client" ignore { val sys: ActorSystem = ??? + val localhost = TestUtils.temporaryServerAddress() //#repl-client - val connection: Flow[ByteString, ByteString, Future[OutgoingConnection]] = StreamTcp().outgoingConnection(localhost) + val connection: Flow[ByteString, ByteString, Future[OutgoingConnection]] = + StreamTcp().outgoingConnection(localhost) val repl = Flow[ByteString] .transform(() => RecipeParseLines.parseLines("\n", maximumLineBytes = 256)) @@ -68,6 +77,7 @@ class StreamTcpDocSpec extends AkkaSpec { } "initial server banner echo server" ignore { + val localhost = TestUtils.temporaryServerAddress() val connections = StreamTcp().bind(localhost) val serverProbe = TestProbe() diff --git a/akka-docs-dev/rst/scala/code/docs/utils/TestUtils.scala b/akka-docs-dev/rst/scala/code/docs/utils/TestUtils.scala new file mode 100644 index 0000000000..5ffc5febf8 --- /dev/null +++ b/akka-docs-dev/rst/scala/code/docs/utils/TestUtils.scala @@ -0,0 +1,24 @@ +/** + * Copyright (C) 2009-2015 Typesafe Inc. + */ + +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 + } +}