Skip to content

Commit

Permalink
tests: find_available_port start search from next port
Browse files Browse the repository at this point in the history
i.e. don't start the search from scratch hitting the used ones again.
this will also reduce the likelihood of collisions (if there are any
left) by increasing the time until we re-use a port we did use in the
past.
  • Loading branch information
oranagra authored and antirez committed May 28, 2020
1 parent a2ae463 commit 1aee695
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions tests/support/util.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -344,21 +344,26 @@ proc roundFloat f {
format "%.10g" $f
}

set ::last_port_attempted 0
proc find_available_port {start count} {
for {set j $start} {$j < $start+$count} {incr j} {
if {[catch {set fd1 [socket 127.0.0.1 $j]}] &&
[catch {set fd2 [socket 127.0.0.1 [expr $j+10000]]}]} {
return $j
set port [expr $::last_port_attempted + 1]
for {set attempts 0} {$attempts < $count} {incr attempts} {
if {$port < $start || $port >= $start+$count} {
set port $start
}
if {[catch {set fd1 [socket 127.0.0.1 $port]}] &&
[catch {set fd2 [socket 127.0.0.1 [expr $port+10000]]}]} {
set ::last_port_attempted $port
return $port
} else {
catch {
close $fd1
close $fd2
}
}
incr port
}
if {$j == $start+$count} {
error "Can't find a non busy port in the $start-[expr {$start+$count-1}] range."
}
error "Can't find a non busy port in the $start-[expr {$start+$count-1}] range."
}

# Test if TERM looks like to support colors
Expand Down

0 comments on commit 1aee695

Please sign in to comment.