Skip to content

Commit

Permalink
WIP try lock for longer in GetFreePort
Browse files Browse the repository at this point in the history
  • Loading branch information
randmonkey committed Jul 31, 2024
1 parent b85dbb1 commit 8dd261c
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions test/helpers/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ func GetFreePort(t *testing.T) int {
freePort int
retriesLeft = 100
)
freePortLock.Lock()
defer freePortLock.Unlock()
for {
// Get a random free port, but do not use it yet...
var err error
freePortLock.Lock()
freePort, err = freeport.GetFreePort()
if err != nil {
freePortLock.Unlock()
continue
}

// ... First, check if the port has been used in this test run already to reduce chances of a race condition.
_, wasUsed := usedPorts.LoadOrStore(freePort, true)
freePortLock.Unlock()

// The port hasn't been used in this test run - we can use it. It was stored in usedPorts, so it will not be
// used again during this test run.
Expand Down

0 comments on commit 8dd261c

Please sign in to comment.