File: util.go

package info (click to toggle)
ntfy 2.11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 19,364 kB
  • sloc: javascript: 16,782; makefile: 282; sh: 105; php: 21; python: 19
file content (44 lines) | stat: -rw-r--r-- 1,050 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package test

import (
	"net"
	"strconv"
	"testing"
	"time"
)

// WaitForPortUp waits up to 7s for a port to come up and fails t if that fails
func WaitForPortUp(t *testing.T, port int) {
	success := false
	for i := 0; i < 500; i++ {
		startTime := time.Now()
		conn, _ := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", strconv.Itoa(port)), 10*time.Millisecond)
		if conn != nil {
			success = true
			conn.Close()
			break
		}
		if time.Since(startTime) < 10*time.Millisecond {
			time.Sleep(10*time.Millisecond - time.Since(startTime))
		}
	}
	if !success {
		t.Fatalf("Failed waiting for port %d to be UP", port)
	}
}

// WaitForPortDown waits up to 5s for a port to come down and fails t if that fails
func WaitForPortDown(t *testing.T, port int) {
	success := false
	for i := 0; i < 100; i++ {
		conn, _ := net.DialTimeout("tcp", net.JoinHostPort("", strconv.Itoa(port)), 50*time.Millisecond)
		if conn == nil {
			success = true
			break
		}
		conn.Close()
	}
	if !success {
		t.Fatalf("Failed waiting for port %d to be DOWN", port)
	}
}