-
Notifications
You must be signed in to change notification settings - Fork 71
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
Add a FreePort extension #141
Comments
How do you guarantee the port stays free between the time you've checked it and the test actually using it? Usually you can pass 0 to servers to assign a randomly allocated port and then query the port that's been allocated after they're started. This approach doesn't have the race condition problem. Do you have use cases where this approach wouldn't work? |
Yes, race condition is problematic, but in some cases it can't be avoided, and the proposition is still better than hardcoding port number.
As for 2, I don't recall the names, because I had those problems some time ago, but as for 1, I needed to create such extension to be able to test failure conditions. So maybe someone else would find it useful also. |
You of course cannot guarantee that the port remains free, but there are use cases for it. For example, Spring has |
I think this is an interesting idea, but I don't have enough experience with testing port-reliant code to add anything to the discussion except a few (possibly naive) ideas. Here's one: Would it make sense to design the extension so that the strategy to find a free port can be plugged in? That way, a Spring app (to use a random example) can use Spring's facilities to discover free ports and other apps use other mechanism. Or users could ask for random port scan or for a linear port scan in a certain range, ... What do you think, @krzyk? |
I would guess that for such things testcontainers are the right thing to use as they handle the port things out of the box? |
@Bukama Running docker just for a single test (or even for the whole suit) is a overkill in majority of projects. |
I think this idea is sound enough to give it a go. One more idea, maybe don't pas the port as an int, but as an instance of a class |
If anybody wants to work on this, let me know and I will mark it as in progress. |
As a first iteration, please pass an instance of the to-be-created class |
PR opend by @p1729 |
Introduces a general mechanism to inject resources into tests. These resources are either newly created for each test or shared across several tests - depending on which annotation was used. This change also introduces a specific resource that uses this mechanism, namely the temporary directory extension. Closes: #348 Relates to: #141 PR: #491
I'm gonna remove this from the the 2.0 milestone, so we can progress on that. |
This is quite similar to
TempDirectory
.There are frequently tests that depend on network layer that need to use a port that is not already used by any other service (either to start a server on it, or to test a failure scenario where there is no service at given port).
Proposed usage:
The text was updated successfully, but these errors were encountered: