Reset the port of specs2 `WithServer` and `WithBrowser` also when an example fails by mkurz · Pull Request #14338 · playframework/playframework · GitHub
Skip to content

Reset the port of specs2 WithServer and WithBrowser also when an example fails - #14338

Merged
mkurz merged 1 commit into
playframework:mainfrom
mkurz:specs2-restore-port
Oct 8, 2026
Merged

mkurz merged 1 commit into
playframework:mainfrom
mkurz:specs2-restore-port

Conversation

@mkurz

@mkurz mkurz commented Oct 8, 2026

Copy link
Copy Markdown
Member

Summary

The specs2 scopes WithServer and WithBrowser set port to the port the test server actually uses while an example runs. With the default port 0, that's a random port. Afterwards they reset port to the port they were given.

That reset ran after the server block. However, AsResult.effectively throws for every result that isn't a success: a failure, an error, or a skipped or pending example. So after such an example, port still contained the server's port.

This PR resets port in a finally block in both scopes. In WithBrowser, the existing finally block that quits the browser now resets the port first.

The impact is small. wrap runs only once per scope instance, so the stale port was only visible to code that reads port after the example, for example through a reference to the scope. Java's WithServer and WithBrowser aren't affected: they reset the port in a JUnit @After method, which also runs when a test fails.

Docs: the Scaladoc of the port parameter of both scopes and the specs2 functional testing guide now describe what port contains during and after an example.

Tests

The new WithServerPortSpec runs a successful and a failing example in each scope, with port 0. It checks that port is the server's port during the example, and 0 again afterwards. Without the fix, the two failing-example tests fail, for example with 33997 != 0.

The spec is in play-integration-test, because the scopes need a server backend, and play-specs2's own tests don't have one on their classpath.

Locally:

  • WithServerPortSpec passes on Scala 2.13.18 and 3.3.8.
  • The Play-Specs2 tests and MiMa pass on Scala 2.13.18 and 3.3.8.
  • The documentation's ScalaFunctionalTestSpec, which uses both scopes, passes, and so does the documentation validation.
  • validateCode passes in the root and in documentation/.

AI assistance

OpenAI Codex noticed this problem while reviewing #14337. Claude (Anthropic) was used to implement the fix, write the tests and draft this pull request. I reviewed the result before submitting.

🤖 Generated with Claude Code

…ple fails

WithServer and WithBrowser set `port` to the port the test server
actually uses while an example runs (a random one for port 0), and
reset it to the given port afterwards. The reset ran after the server
block, but AsResult.effectively throws for every result that isn't a
success. So after a failing, erroring, skipped or pending example,
`port` kept the server's port. Reset it in a finally block instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mkurz
mkurz merged commit 73a8172 into playframework:main Oct 8, 2026
89 checks passed
@mkurz
mkurz deleted the specs2-restore-port branch October 8, 2026 19:47
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.

1 participant