-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetutil_test.go
45 lines (38 loc) · 1.13 KB
/
netutil_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
package netutil
import (
"net"
"strconv"
"testing"
"time"
)
func TestCanDial(t *testing.T) {
ln, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatalf("Failed to set up mock server: %v", err)
}
defer ln.Close()
port := ln.Addr().(*net.TCPAddr).Port
if !CanDial("localhost", strconv.Itoa(port)) {
t.Errorf("Expected CanDial to return true for open port, got false")
}
// Test with a known closed port
if CanDial("localhost", "9999") {
t.Errorf("Expected CanDial to return false for closed port, got true")
}
}
func TestCanDialWithTimeout(t *testing.T) {
// Test with a known open port
ln, err := net.Listen("tcp", ":0") // Listen on a random open port
if err != nil {
t.Fatalf("Failed to set up mock server: %v", err)
}
defer ln.Close()
port := ln.Addr().(*net.TCPAddr).Port
if !CanDialWithTimeout("localhost", strconv.Itoa(port), 1*time.Second) {
t.Errorf("Expected CanDialWithTimeout to return true for open port, got false")
}
// Test with a known closed port
if CanDialWithTimeout("localhost", "9999", 1*time.Second) {
t.Errorf("Expected CanDialWithTimeout to return false for closed port, got true")
}
}