| 1 |
package test |
| 2 |
|
| 3 |
import ( |
| 4 |
"net" |
| 5 |
"strconv" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
// WaitForPortUp waits up to 7s for a port to come up and fails t if that fails |
| 11 |
func WaitForPortUp(t *testing.T, port int) { |
| 12 |
success := false |
| 13 |
for i := 0; i < 500; i++ { |
| 14 |
startTime := time.Now() |
| 15 |
conn, _ := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", strconv.Itoa(port)), 10*time.Millisecond) |
| 16 |
if conn != nil { |
| 17 |
success = true |
| 18 |
conn.Close() |
| 19 |
break |
| 20 |
} |
| 21 |
if time.Since(startTime) < 10*time.Millisecond { |
| 22 |
time.Sleep(10*time.Millisecond - time.Since(startTime)) |
| 23 |
} |
| 24 |
} |
| 25 |
if !success { |
| 26 |
t.Fatalf("Failed waiting for port %d to be UP", port) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
// WaitForPortDown waits up to 5s for a port to come down and fails t if that fails |
| 31 |
func WaitForPortDown(t *testing.T, port int) { |
| 32 |
success := false |
| 33 |
for i := 0; i < 100; i++ { |
| 34 |
conn, _ := net.DialTimeout("tcp", net.JoinHostPort("", strconv.Itoa(port)), 50*time.Millisecond) |
| 35 |
if conn == nil { |
| 36 |
success = true |
| 37 |
break |
| 38 |
} |
| 39 |
conn.Close() |
| 40 |
} |
| 41 |
if !success { |
| 42 |
t.Fatalf("Failed waiting for port %d to be DOWN", port) |
| 43 |
} |
| 44 |
} |