forked from nuveo/tcp-port-wait
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpPortWait_test.go
61 lines (57 loc) · 1.07 KB
/
tcpPortWait_test.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package tcpPortWait
import (
"fmt"
"net"
"os"
"testing"
"time"
)
func simpleServer() {
rAddr, err := net.ResolveTCPAddr("tcp", ":9999")
if err != nil {
panic(err)
}
l, err := net.ListenTCP("tcp", rAddr)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
fmt.Println("Server listen at ", rAddr)
// Close the listener when the application closes.
defer func() {
cErr := l.Close()
if cErr != nil {
fmt.Println(cErr)
}
}()
for {
// Listen for an incoming connection.
conn, err := l.AcceptTCP()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
continue
}
fmt.Println("client at", conn.RemoteAddr())
}
}
func TestCheck(t *testing.T) {
var p Port
p.Timeout = time.Duration(1) * time.Second
timeout, err := p.Check(":9999")
if err != nil {
t.Fatal(err)
}
if !timeout {
t.Fatal("Expected timeout true")
}
go simpleServer()
p.Timeout = 0
<-time.After(time.Duration(1) * time.Second)
timeout, err = p.Check(":9999")
if err != nil {
t.Fatal(err)
}
if timeout {
t.Fatal("Expected timeout false")
}
}