Commit 5f605d4
test: poll for port release in TcpServerTest to fix flake (#166)
**Requirements**
- [x] I have added test coverage for new or changed functionality
- [x] I have followed the repository's [pull request submission
guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests)
- [x] I have validated my changes against all supported platform
versions
**Related issues**
Fixes a flaky `TcpServerTest.listensOnSpecificPort` test observed in CI.
**Describe the solution you've provided**
After `TcpServer.close()`, there can be a brief OS-level delay before
the port is fully released. The test was immediately checking
`doesPortHaveListener()` which could return true in that window.
Replaced the immediate `assertFalse` calls with a polling helper
(`assertPortReleased`) that retries for up to 1 second before failing.
Applied to both `listensOnSpecificPort` and `listensOnAnyAvailablePort`
for consistency.
**Describe alternatives you've considered**
- Adding a fixed `Thread.sleep()` before the assertion — less robust and
adds unnecessary delay in the common case.
- Modifying `TcpServer.close()` to join the accept thread — would be an
application code change rather than a test fix.
**Additional context**
The flake was observed on `ubuntu-latest` with Java 19 during CI.
Link to Devin session:
https://app.devin.ai/sessions/89320790abd2401e90b44eb0fcfb66be
Requested by: @kinyoklion
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Low Risk**
> Test-only timing change in shared test helpers; no production or
security impact.
>
> **Overview**
> **TcpServerTest** no longer asserts that a port is free the instant
`TcpServer` closes. Both `listensOnAnyAvailablePort` and
`listensOnSpecificPort` now call a new **`assertPortReleased`** helper
that polls **`doesPortHaveListener`** for up to one second (50 ms
between attempts) before failing with the same message as before.
>
> This addresses CI flakes where the OS can still report a listener
briefly after close.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
9d42e36. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>1 parent 1c17de0 commit 5f605d4
1 file changed
Lines changed: 17 additions & 2 deletions
File tree
Lines changed: 17 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
41 | 56 | | |
42 | 57 | | |
0 commit comments