2016-06-20 40 views
1

我目前正在從遊戲2.4遷移到2.5。當運行多個NettyServer時,連接被拒絕2.5

我試圖在測試運行許多NettyServer但我只能訪問最後創建

import akka.actor.ActorSystem 
import akka.stream.ActorMaterializer 
import org.scalatest.{FlatSpec, Matchers} 
import org.scalatest.concurrent.ScalaFutures 
import org.scalatest.time.{Seconds, Span} 
import play.api.libs.ws.ahc.AhcWSClient 
import play.api.mvc.{Action, Results} 
import play.core.server.{NettyServer, ServerConfig} 
import play.api.routing.sird._ 

class NettyServerTest extends FlatSpec with Matchers with ScalaFutures { 

    override implicit val patienceConfig = PatienceConfig(Span(5, Seconds)) 

    implicit val system = ActorSystem() 
    implicit val materializer = ActorMaterializer() 

    private val wsClient = AhcWSClient() 

    it should "print 'Hello I am the secondServer' then 'Hello I am the firstServer'" in { 

    val firstServer = createServer("firstServer") 
    val secondServer = createServer("secondServer") 
    try { 
     println(wsClient.url(s"http://localhost:${secondServer.httpPort.get}/hello").get().futureValue.body) 
     println(wsClient.url(s"http://localhost:${firstServer.httpPort.get}/hello").get().futureValue.body) 
    } finally { 
     firstServer.stop() 
     secondServer.stop() 
    } 
    } 

    def createServer(server: String): NettyServer = { 
    NettyServer.fromRouter(ServerConfig(port = Some(0))) { 
     case GET(p"/hello") => Action { 
     Results.Ok(s"Hello I am the $server") 
     } 
    } 
    } 
} 

當我運行測試只打印「你好我是secondServer」,然後我得到的錯誤:

The future returned an exception of type: java.net.ConnectException, with message: Connection refused: no further information: localhost/0:0:0:0:0:0:0:1:58096. 
ScalaTestFailureLocation: NettyServerTest$$anonfun$1 at (NettyServerTest.scala:28) 
org.scalatest.exceptions.TestFailedException: The future returned an exception of type: java.net.ConnectException, with message: Connection refused: no further information: localhost/0:0:0:0:0:0:0:1:58096. 
    at org.scalatest.concurrent.Futures$FutureConcept$class.tryTryAgain$1(Futures.scala:531) 
    at org.scalatest.concurrent.Futures$FutureConcept$class.futureValue(Futures.scala:558) 
    at org.scalatest.concurrent.ScalaFutures$$anon$1.futureValue(ScalaFutures.scala:74) 
    at NettyServerTest$$anonfun$1.apply$mcV$sp(NettyServerTest.scala:28) 
    at NettyServerTest$$anonfun$1.apply(NettyServerTest.scala:22) 
... 

我有以下依賴性:

"com.typesafe.play" %% "play-netty-server" % "2.5.4" 
"com.typesafe.play" %% "play-ws" % "2.5.4" 

該試驗使用發揮工作2.4

回答

1

最後我成功了,使通過覆蓋由網狀服務器使用的actorSystem它的工作:

import akka.actor.ActorSystem 
import akka.stream.ActorMaterializer 
import org.scalatest.concurrent.ScalaFutures 
import org.scalatest.time.{Seconds, Span} 
import org.scalatest.{FlatSpec, Matchers} 
import play.api.BuiltInComponents 
import play.api.libs.ws.ahc.AhcWSClient 
import play.api.mvc.{Action, Results} 
import play.api.routing.Router 
import play.api.routing.sird._ 
import play.core.server.{NettyServer, NettyServerComponents, ServerConfig} 

class NettyServerTest extends FlatSpec with Matchers with ScalaFutures { 

    override implicit val patienceConfig = PatienceConfig(Span(5, Seconds)) 

    implicit val system = ActorSystem() 
    implicit val materializer = ActorMaterializer() 

    private val wsClient = AhcWSClient() 

    it should "print 'Hello I am the secondServer' then 'Hello I am the firstServer'" in { 

    val firstServer = createServer("firstServer") 
    val secondServer = createServer("secondServer") 
    try { 
     wsClient.url(s"http://localhost:${firstServer.httpPort.get}/hello").get().futureValue.body shouldBe "Hello I am the firstServer" 
     wsClient.url(s"http://localhost:${secondServer.httpPort.get}/hello").get().futureValue.body shouldBe "Hello I am the secondServer" 
    } finally { 
     firstServer.stop() 
     secondServer.stop() 
    } 
    } 

    def createServer(serverName: String): NettyServer = { 
    new NettyServerComponents with BuiltInComponents { 
     lazy val router = Router.from { 
     case GET(p"/hello") => Action { 
      Results.Ok(s"Hello I am the $serverName") 
     } 
     } 
     override lazy val serverConfig = ServerConfig(port = Some(0)) 
     override lazy val actorSystem: ActorSystem = ActorSystem() 
    }.server 
    } 
} 
+0

非常感謝你爲這個!我創建了多臺服務器來模擬外部服務,事實證明這非常有用。但現在想想它;創建一臺服務器可能會更好,只需將路由/部分功能一起更改即可。 – Grogs