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

Refactor unit tests with hardcoded ports #1248

Open
ccoVeille opened this issue Nov 27, 2024 · 2 comments
Open

Refactor unit tests with hardcoded ports #1248

ccoVeille opened this issue Nov 27, 2024 · 2 comments
Labels
tests Related to linters, tests, coverage

Comments

@ccoVeille
Copy link
Contributor

The tests are using hardcoded port, it's not a good pattern. Unit tests cannot be launched in parallel.

Any of these packages could be used

https://github.com/facebookarchive/freeport
https://github.com/projectdiscovery/freeport
https://github.com/phayes/freeport
https://github.com/Konstantin8105/FreePort
https://github.com/jamielxcarter/dort
https://github.com/prithviramesh/free-port-finder

Of course, as the feature is very basic, it could be added via a simple helper. By copy pasting something like this.

https://gist.github.com/sevkin/96bdae9274465b2d09191384f86ef39d

From a library with a copyleft license

@ccoVeille
Copy link
Contributor Author

ccoVeille commented Nov 27, 2024

Here is an example

https://github.com/gofr-dev/gofr/blob/development/pkg%2Fgofr%2Fhttp%2Frouter_test.go

All the tests are using the 8080 port.

I didn't check further, and maybe it's a bad example as calls are mocked, but there are unit tests, that expect ports to be available.

Imagine code expecting the 8080 port to be available, but the developer has a webserver, proxy, whatsoever.

The unit tests will fail and the developer will become nuts before thinking about the local webserver hosted on :8080

@vikash
Copy link
Contributor

vikash commented Nov 27, 2024

This is a great idea - whenever we are actually testing a call over a port.

@Umang01-hash Umang01-hash added the tests Related to linters, tests, coverage label Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Related to linters, tests, coverage
Projects
None yet
Development

No branches or pull requests

3 participants