Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: mock Agent querying OS for listening ports in tests - #20842

Merged
spikecurtis merged 1 commit into
mainfrom
spike/internal-1123-mock-getting-ports
Nov 25, 2025
Merged

fix: mock Agent querying OS for listening ports in tests#20842
spikecurtis merged 1 commit into
mainfrom
spike/internal-1123-mock-getting-ports

Conversation

@spikecurtis

@spikecurtis spikecurtis commented Nov 20, 2025

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

@mafredri mafredri left a comment

Copy link
Copy Markdown
Member

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.

Comment thread agent/agent.go
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
Copy Markdown
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
26 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

2 participants