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

chore(e2e): better port selection #60

Merged
merged 5 commits into from
Sep 20, 2024
Merged

chore(e2e): better port selection #60

merged 5 commits into from
Sep 20, 2024

Conversation

Lazar955
Copy link
Member

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:

go test -mod=readonly -failfast -timeout=15m -v $(PACKAGES_E2E) -count=30 --parallel 12 --tags=e2e

References issue


// 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)
Copy link
Contributor

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

Copy link
Collaborator

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:

  1. have global map of used ports
  2. 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)
  3. 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

Copy link
Member Author

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

Copy link
Member Author

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

Copy link
Member Author

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

Copy link
Collaborator

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 ?

Copy link
Member Author

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
}

Copy link
Collaborator

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)
		})

@Lazar955 Lazar955 merged commit 1da3bf0 into main Sep 20, 2024
8 checks passed
@Lazar955 Lazar955 deleted the lazar/less-flaky-port branch September 20, 2024 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants