Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve getTestServerIfRunning #12352

Closed
wants to merge 1 commit into from
Closed

Conversation

tanin47
Copy link
Sponsor Contributor

@tanin47 tanin47 commented Feb 3, 2024

Pull Request Checklist

Helpful things

Fixes

Fixes #12331

Purpose

Improve the interface of TestServer. Currently, if a TestServer doesn't exist, a few functions raise an exception.

Those functions should have return None (e.g. def runningHttpPort: Option[Int]) or false (e.g. def isRunning: Boolean) instead of raising an exception.

@@ -71,17 +65,23 @@ case class TestServer(config: ServerConfig, application: Application, serverProv
/**
* The address that the server is running on.
*/
def runningAddress: String = getTestServerIfRunning.mainAddress.getAddress.getHostAddress
def runningAddress: String = {
Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same behavior of raising an exception is kept.


/**
* The HTTP port that the server is running on.
*/
def runningHttpPort: Option[Int] = getTestServerIfRunning.httpPort
def runningHttpPort: Option[Int] = getTestServer.map(_.httpPort)
Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return None instead of raising an exception for both functions. The return types are the same.


/**
* The HTTPS port that the server is running on.
*/
def runningHttpsPort: Option[Int] = getTestServerIfRunning.httpsPort
def runningHttpsPort: Option[Int] = getTestServer.map(_.httpsPort)

/**
* True if the server is running either on HTTP or HTTPS port.
Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function wouldn't raise an exception anymore. it would just return false.

@mkurz
Copy link
Member

mkurz commented Feb 26, 2024

I know the approach taken was my idea in #12331 (comment), however looking at the pull request now, I would say the changes in behaviour are too much.
Now I think it does make sense for runningHttp[s]Port and runningAddress to throw an exception. Let's take runningHttpPort: Right now if it returns None that means a server is running, but the http port is not set, which is absolutely fine. If it throws an exception however, it indicates to the user that the server is not running, and because this is a utility for testing, makes the test fail. I think this is what we want actually: Make the user aware that the test setup has a problem by throwing an exception. If we would also return None in case the server is not running, the user can not determine anymore why None is returned, is it because of a port not set (and server running) or because the server is not running? In worst case, a user expects a server is running but handles the None case in a way it makes the test pass where a test should actually fail.
So throwing an exception is a good thing here IMHO.

Of course, the thing that still needs fixing is to make isRunning return false instead of throwing an exception, because here it does absolutely make no sense. That fix is easy:

@tanin47 Thanks for taking the time, specially for reporting the problem. I will go with #12409 because IMHO that is all that needs to be done right now. Thanks again!

@mkurz mkurz closed this Feb 26, 2024
@tanin47
Copy link
Sponsor Contributor Author

tanin47 commented Feb 27, 2024

No worries. Thank you for improving it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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