TestServer: instead of hard coded sleep use the status route to wait until the server is up
@@ -103,7 +103,8 @@ case class TestServer() extends PlayJsonExtensions {
val process = pb.start
- Thread.sleep(100) // Offsets process startup latency
+ waitUntilServerIsUp(port)
+
(port, process)
}
@@ -140,5 +141,32 @@ case class TestServer() extends PlayJsonExtensions {
}
}
+ private def waitUntilServerIsUp(port: Int): Unit = {
+ val sleepTime = 5 // 5ms
+ val maxWaitTime = 2000 // 2s
+ var tryCount = 0
+ while(!isServerUp(port)) {
+ Thread.sleep(sleepTime)
+ if(tryCount * sleepTime > maxWaitTime) {
+ sys.error("TestServer did not start within maximum wait time")
+ }
+ tryCount += 1
+ }
+ }
+
+ private def isServerUp(port: Int): Boolean = {
+ val url = new URL(s"http://127.0.0.1:$port/status")
+ val con = url.openConnection().asInstanceOf[HttpURLConnection]
+ con.setRequestMethod("GET")
+ try {
+ val status = con.getResponseCode
+ status == 200
+ } catch {
+ case e: Throwable => false
+ } finally {
+ con.disconnect()
+ }
+ }
+
private def awaitInfinitely[T](awaitable: Awaitable[T]): T = Await.result(awaitable, Duration.Inf)
}
