Commit 1cf33a46 authored by Oran Agra's avatar Oran Agra
Browse files

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.
parent e258a1c0
...@@ -344,21 +344,26 @@ proc roundFloat f { ...@@ -344,21 +344,26 @@ proc roundFloat f {
format "%.10g" $f format "%.10g" $f
} }
set ::last_port_attempted 0
proc find_available_port {start count} { proc find_available_port {start count} {
for {set j $start} {$j < $start+$count} {incr j} { set port [expr $::last_port_attempted + 1]
if {[catch {set fd1 [socket 127.0.0.1 $j]}] && for {set attempts 0} {$attempts < $count} {incr attempts} {
[catch {set fd2 [socket 127.0.0.1 [expr $j+10000]]}]} { if {$port < $start || $port >= $start+$count} {
return $j 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 { } else {
catch { catch {
close $fd1 close $fd1
close $fd2 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 # Test if TERM looks like to support colors
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment