Skip to content

Commit 1aee695

Browse files
oranagraantirez
authored andcommitted
tests: find_available_port start search from next port
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.
1 parent a2ae463 commit 1aee695

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

tests/support/util.tcl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,21 +344,26 @@ proc roundFloat f {
344344
format "%.10g" $f
345345
}
346346

347+
set ::last_port_attempted 0
347348
proc find_available_port {start count} {
348-
for {set j $start} {$j < $start+$count} {incr j} {
349-
if {[catch {set fd1 [socket 127.0.0.1 $j]}] &&
350-
[catch {set fd2 [socket 127.0.0.1 [expr $j+10000]]}]} {
351-
return $j
349+
set port [expr $::last_port_attempted + 1]
350+
for {set attempts 0} {$attempts < $count} {incr attempts} {
351+
if {$port < $start || $port >= $start+$count} {
352+
set port $start
353+
}
354+
if {[catch {set fd1 [socket 127.0.0.1 $port]}] &&
355+
[catch {set fd2 [socket 127.0.0.1 [expr $port+10000]]}]} {
356+
set ::last_port_attempted $port
357+
return $port
352358
} else {
353359
catch {
354360
close $fd1
355361
close $fd2
356362
}
357363
}
364+
incr port
358365
}
359-
if {$j == $start+$count} {
360-
error "Can't find a non busy port in the $start-[expr {$start+$count-1}] range."
361-
}
366+
error "Can't find a non busy port in the $start-[expr {$start+$count-1}] range."
362367
}
363368

364369
# Test if TERM looks like to support colors

0 commit comments

Comments
 (0)