playframework icon indicating copy to clipboard operation
playframework copied to clipboard

`TestServer.isRunning` throws an exception when the server is not running.

Open tanin47 opened this issue 2 years ago • 3 comments

Play Version

Latest

API

Scala

Operating System

N/A

JDK

N/A

Library Dependencies

Expected Behavior

I'd expect it to return true or false

Actual Behavior

It raises an exception when the server is not running.

Reproducible Test Case

https://github.com/playframework/playframework/blob/a77da07e0b2d594af5d538c38803ba2466e20517/testkit/play-test/src/main/scala/play/api/test/TestServer.scala#L28

Here's the code:

case class TestServer(config: ServerConfig, application: Application, serverProvider: Option[ServerProvider]) {
  ...

  private def getTestServerIfRunning: Server = {
    val s = server
    if (s == null) {
      throw new IllegalStateException("Test server not running")
    }
    s
  }
  ...

  def runningHttpPort: Option[Int] = getTestServerIfRunning.httpPort

  @ApiMayChange
  def isRunning: Boolean = runningHttpPort.nonEmpty || runningHttpsPort.nonEmpty
}

I wonder if we should catch an exception in isRunning.

tanin47 avatar Jan 19 '24 16:01 tanin47

Best is probably if getTestServerIfRunning would return an Option[Server] instead of thrown an exception... Ahh.. I see it's private so we could change that right now. What do you think? you want to provide pull request?

mkurz avatar Jan 22 '24 15:01 mkurz

Sounds good. I can do that this weekend. Would love to help contribute.

tanin47 avatar Jan 23 '24 03:01 tanin47

When I said "this weekend", I mean "a weekend" 🤣 . Here's the PR: https://github.com/playframework/playframework/pull/12352

tanin47 avatar Feb 03 '24 19:02 tanin47