Repository navigation
Expand file tree
/
Copy pathwait_deps_test.go
More file actions
124 lines (98 loc) · 3.01 KB
/
Copy pathwait_deps_test.go
File metadata and controls
124 lines (98 loc) · 3.01 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"net"
"net/url"
"path/filepath"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// closedTCPPort returns a local TCP port with nothing listening on it.
func closedTCPPort(t *testing.T) int {
t.Helper()
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
port := l.Addr().(*net.TCPAddr).Port
require.NoError(t, l.Close())
return port
}
// waitForDependenciesReturns runs waitForDependencies and fails the test if it does not return within d.
func waitForDependenciesReturns(t *testing.T, d time.Duration) {
t.Helper()
done := make(chan struct{})
go func() {
defer close(done)
waitForDependencies()
}()
select {
case <-done:
case <-time.After(d):
t.Fatalf("waitForDependencies did not return within %s", d)
}
}
func TestWaitForDependenciesUnsupportedSchemeFatal(t *testing.T) {
if fatalCase("unsupported-scheme") {
waitTimeoutFlag = 10 * time.Second
waitRetryInterval = 10 * time.Millisecond
urls = []url.URL{{Scheme: "ftp", Host: "x"}}
waitForDependencies()
return
}
exitCode, stderr := runFatalCase(t, "TestWaitForDependenciesUnsupportedSchemeFatal", "unsupported-scheme")
assert.Equal(t, 1, exitCode)
assert.Contains(t, stderr, "invalid host protocol provided: ftp")
}
func TestWaitForDependenciesTimeoutFatal(t *testing.T) {
if fatalCase("timeout") {
port := closedTCPPort(t)
waitTimeoutFlag = 300 * time.Millisecond
waitRetryInterval = 10 * time.Millisecond
urls = []url.URL{{Scheme: "tcp", Host: "127.0.0.1:" + strconv.Itoa(port)}}
waitForDependencies()
return
}
exitCode, stderr := runFatalCase(t, "TestWaitForDependenciesTimeoutFatal", "timeout")
assert.Equal(t, 1, exitCode)
assert.Contains(t, stderr, "Timeout after")
}
func TestWaitForDependenciesUnixSocket(t *testing.T) {
resetGlobals(t)
sockPath := filepath.Join(t.TempDir(), "s.sock")
l, err := net.Listen("unix", sockPath)
require.NoError(t, err)
defer func() { _ = l.Close() }()
u, err := url.Parse("unix://" + sockPath)
require.NoError(t, err)
urls = []url.URL{*u}
waitTimeoutFlag = 5 * time.Second
waitRetryInterval = 10 * time.Millisecond
waitForDependenciesReturns(t, 10*time.Second)
}
func TestWaitForDependenciesTCP4(t *testing.T) {
resetGlobals(t)
l, err := net.Listen("tcp4", "127.0.0.1:0")
require.NoError(t, err)
defer func() { _ = l.Close() }()
u, err := url.Parse("tcp4://" + l.Addr().String())
require.NoError(t, err)
urls = []url.URL{*u}
waitTimeoutFlag = 5 * time.Second
waitRetryInterval = 10 * time.Millisecond
waitForDependenciesReturns(t, 10*time.Second)
}
func TestWaitForDependenciesTCP6(t *testing.T) {
l, err := net.Listen("tcp6", "[::1]:0")
if err != nil {
t.Skipf("IPv6 loopback not available: %v", err)
}
defer func() { _ = l.Close() }()
resetGlobals(t)
u, err := url.Parse("tcp6://" + l.Addr().String())
require.NoError(t, err)
urls = []url.URL{*u}
waitTimeoutFlag = 5 * time.Second
waitRetryInterval = 10 * time.Millisecond
waitForDependenciesReturns(t, 10*time.Second)
}