-
Notifications
You must be signed in to change notification settings - Fork 2
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
chore(e2e): better port selection #60
Conversation
e2etest/container/container.go
Outdated
|
||
// listen on a port for a bit before closing, this should be just enough time for when other tests start | ||
// when they try to net.Listen they should get an err and try a different port | ||
time.Sleep(200 * time.Millisecond) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems hacky...
I would feel better by setting a specific port range for each test, but also doesn't seem too good
Any other idea to solve this? @babylonlabs-io/core-dev
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we assume there is no other processes using our test ports we could probably:
- have global map of used ports
- change semantics of this function to be more like
acquirePort
i.e each call generates port, checks if it is not used (stored in map), and if it is not used it stores it in this map. (of course this operations must be behind mutex) - at the end of the test port is released (removed from map) . This can by accomplished by
t.Cleanup(..)
and of course it also must be protected by mutex
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, but tests do not share state, so having a mutex or a map here doesn't help. One way would be to have a test which would be ran first (e.g called 01Setup and without parallel flag), which would create a file and a lockfile, then all other parallel tests startup and read/write port that they are acquiring to this shared file (locking and unlocking).
I feel like this is a bit much, but if we start having collisions I would implement it.
Thoughts @KonradStaniec @RafilxTenfen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also reason for needing to have a test called 01Setup (or something like that), is that because all tests start in parallel so they would also be having race condition for the initial file creation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not super hard to implement, I'll try it, see if it works and is worth the extra code. Thorugh this was clever and a nice hack :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, but tests do not share state, so having a mutex or a map here doesn't help.
They do not share it now but we could introduce it. You could create file port_map.go
in our e2etest
package have there var port_map = map[int]bool
and protect access to it. It seems simpler that having file with lock file.
Or do I miss something about how go shares state between tests ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, you're right, pushed a new version
if err := listener.Close(); err != nil { | ||
continue | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably not critical right now becouse there is not much tests but we should also have here:
t.Cleanup(func() {
portMutex.Lock()
defer portMutex.Unlock()
delete(allocatedPorts, port)
})
Sometimes due to parallel tests, we could end up with the same port allocated. In this PR I try to introduce a small timeout when testing port availability by opening it for a short time which should decrease the chance of collision.
Tested with no collision couple of times with:
References issue