Skip to content

Conversation

@spikecurtis
Copy link
Contributor

@spikecurtis spikecurtis commented Nov 20, 2025

fixes coder/internal#1123

We want to tests that ports are not included after they are no longer used, but this isn't safe on the real OS networking stack because there is no way to guarantee a port won't be used. Instead, we introduce an interface and fake implementation for testing.

On order to leave the filtering logic in the test path, this PR also does some refactoring.

Caching logic is left in the real OS querying implementation and a new test case is added for it in this PR.

Copy link
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@spikecurtis spikecurtis requested a review from mafredri November 20, 2025 10:49
@spikecurtis spikecurtis marked this pull request as ready for review November 20, 2025 10:49
@spikecurtis spikecurtis force-pushed the spike/internal-1123-mock-getting-ports branch from 4dd485b to ff1df69 Compare November 20, 2025 10:52
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Solid change 👍🏻, some simplification suggestions inline.

IgnorePorts map[int]string
// ListeningPortsGetter is used to get the list of listening ports. Only
// tests should set this. If unset, a default that queries the OS will be used.
ListeningPortsGetter ListeningPortsGetter
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: These could be simplified a bit in naming by dropping "Getter". It makes these types somewhat verbose and limits future expansion of the type.

Hypothetical expansion: addition of ClearCache method.

A funny but legit alternative would be: type ListeningPortser interface { ListeningPorts() ... }.

@spikecurtis spikecurtis merged commit afd4043 into main Nov 25, 2025
31 checks passed
@spikecurtis spikecurtis deleted the spike/internal-1123-mock-getting-ports branch November 25, 2025 10:25
@github-actions github-actions bot locked and limited conversation to collaborators Nov 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

flake: TestWorkspaceAgentListeningPorts/LinuxAndWindows/OK_Mainline

3 participants