-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
tcping_test.go
279 lines (239 loc) · 6.23 KB
/
tcping_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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"net"
"net/netip"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// createTestStats should be used to create new stats structs.
// it uses "127.0.0.1:12345" as default values, because
// [testServerListen] use the same values.
// It'll call t.Errorf if netip.ParseAddr has failed.
func createTestStats(t *testing.T) *tcping {
addr, err := netip.ParseAddr("127.0.0.1")
s := tcping{
printer: &dummyPrinter{},
userInput: userInput{
ip: addr,
port: 12345,
intervalBetweenProbes: time.Second,
timeout: time.Second,
},
ticker: time.NewTicker(time.Second),
}
if err != nil {
t.Errorf("ip parse: %v", err)
}
return &s
}
// testServerListen creates a new listener
// on port 12345 and automatically starts it.
//
// Use t.Cleanup with srv.Close() to close it after
// the test, so that other tests are not affected.
//
// It could fail if net.Listen or Accept has failed.
func testServerListen(t *testing.T) net.Listener {
srv, err := net.Listen("tcp", ":12345")
if err != nil {
t.Errorf("test server: %v", err)
}
go func() {
for {
c, err := srv.Accept()
if err != nil {
return
}
c.Close()
}
}()
return srv
}
func TestProbeSuccess(t *testing.T) {
stats := createTestStats(t)
stats.ticker = time.NewTicker(time.Nanosecond)
srv := testServerListen(t)
t.Cleanup(func() {
if err := srv.Close(); err != nil {
t.Errorf("srv close: %v", err)
}
})
expectedSuccessful := 100
for i := 0; i < expectedSuccessful; i++ {
tcpProbe(stats)
}
assert.Equal(t, stats.totalSuccessfulProbes, uint(expectedSuccessful))
assert.Equal(t, stats.ongoingSuccessfulProbes, uint(expectedSuccessful))
assert.Equal(t, stats.totalUptime, 100*time.Second)
}
func TestProbeSuccessInterval(t *testing.T) {
stats := createTestStats(t)
stats.userInput.intervalBetweenProbes = 10 * time.Second
stats.ticker = time.NewTicker(time.Nanosecond)
srv := testServerListen(t)
t.Cleanup(func() {
if err := srv.Close(); err != nil {
t.Errorf("srv close: %v", err)
}
})
expectedSuccessful := 100
for i := 0; i < expectedSuccessful; i++ {
tcpProbe(stats)
}
assert.Equal(t, stats.totalSuccessfulProbes, uint(expectedSuccessful))
assert.Equal(t, stats.ongoingSuccessfulProbes, uint(expectedSuccessful))
assert.Equal(t, stats.totalUptime, 16*time.Minute+40*time.Second)
}
func TestProbeFail(t *testing.T) {
stats := createTestStats(t)
stats.ticker = time.NewTicker(time.Nanosecond)
expectedFailed := 100
for i := 0; i < expectedFailed; i++ {
tcpProbe(stats)
}
assert.Equal(t, stats.totalUnsuccessfulProbes, uint(expectedFailed))
assert.Equal(t, stats.ongoingUnsuccessfulProbes, uint(expectedFailed))
assert.Equal(t, stats.totalDowntime, 100*time.Second)
}
func TestProbeFailInterval(t *testing.T) {
stats := createTestStats(t)
stats.userInput.intervalBetweenProbes = 10 * time.Second
stats.ticker = time.NewTicker(time.Nanosecond)
expectedFailed := 100
for i := 0; i < expectedFailed; i++ {
tcpProbe(stats)
}
assert.Equal(t, stats.totalUnsuccessfulProbes, uint(expectedFailed))
assert.Equal(t, stats.ongoingUnsuccessfulProbes, uint(expectedFailed))
assert.Equal(t, stats.totalDowntime, 16*time.Minute+40*time.Second)
}
func TestPermuteArgs(t *testing.T) {
type args struct {
args []string
}
tests := []struct {
name string
args args
want []string
}{
{
"host/ip before option",
args{args: []string{"127.0.0.1", "8080", "-r", "3"}},
[]string{"-r", "3", "127.0.0.1", "8080"},
},
{
"host/ip after option",
args{args: []string{"-r", "3", "127.0.0.1", "8080"}},
[]string{"-r", "3", "127.0.0.1", "8080"},
},
{
"check for updates",
args{args: []string{"-u"}},
[]string{"-u"},
},
/**
* cases in which the value of the option does not exist are not listed.
* they call directly usage() and exit with code 1.
*/
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
permuteArgs(tt.args.args)
assert.Equal(t, tt.want, tt.args.args)
})
}
}
func TestNanoToMilliseconds(t *testing.T) {
t.Parallel()
tests := []struct {
d time.Duration
want float32
}{
{d: time.Millisecond, want: 1},
{d: 100*time.Millisecond + 123*time.Nanosecond, want: 100.000123},
{d: time.Second, want: 1000},
{d: time.Second + 100*time.Nanosecond, want: 1000.000123},
}
for _, tt := range tests {
tt := tt
t.Run(tt.d.String(), func(t *testing.T) {
t.Parallel()
got := nanoToMillisecond(tt.d.Nanoseconds())
assert.Equal(t, tt.want, got)
})
}
}
func TestSelectResolvedIPv4(t *testing.T) {
userInputV4 := userInput{
useIPv4: true,
}
stats := createTestStats(t)
stats.userInput = userInputV4
var (
ip1 = netip.MustParseAddr("172.20.10.238")
ip2 = netip.MustParseAddr("8.8.8.8")
)
t.Run("IPv4 Selection", func(t *testing.T) {
actual := selectResolvedIP(stats, []netip.Addr{ip1, ip2})
if !actual.IsValid() {
t.Errorf("Expected an IP but got invalid address")
}
if actual != ip1 && actual != ip2 {
t.Errorf("Expected an IP but got invalid address")
}
})
}
func TestSelectResolvedIPv6(t *testing.T) {
userInputV6 := userInput{
useIPv6: true,
}
stats := createTestStats(t)
stats.userInput = userInputV6
var (
ip1 = netip.MustParseAddr("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
ip2 = netip.MustParseAddr("2001:4860:4860::8888")
)
t.Run("IPv6 Selection", func(t *testing.T) {
actual := selectResolvedIP(stats, []netip.Addr{ip1, ip2})
if !actual.IsValid() {
t.Errorf("Expected an IP but got invalid address")
}
if actual != ip1 && actual != ip2 {
t.Errorf("Expected an IP but got invalid address")
}
})
}
func TestSecondsToDuration(t *testing.T) {
tests := []struct {
name string
seconds float64
duration time.Duration
}{
{
name: "positive integer",
seconds: 2,
duration: 2 * time.Second,
},
{
name: "positive float",
seconds: 1.5, // 1.5 = 3 / 2
duration: time.Second * 3 / 2,
},
{
name: "negative integer",
seconds: -3,
duration: -3 * time.Second,
},
{
name: "negative float",
seconds: -2.5, // -2.5 = -5 / 2
duration: time.Second * -5 / 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.duration, secondsToDuration(tt.seconds))
})
}
}