-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathutils_test.go
152 lines (120 loc) · 3.61 KB
/
utils_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
package awsping
import (
"bytes"
"testing"
"time"
)
func TestDuration(t *testing.T) {
input := time.Duration(1 * time.Second)
want := 1000.0
got := Duration2ms(input)
if got != want {
t.Errorf("Duration was incorrect, got: %f, want: %f.", got, want)
}
}
func TestRandomString(t *testing.T) {
tests := []struct {
n int
res string
}{
{1, ""},
{5, ""},
{10, ""},
}
for idx, test := range tests {
test.res = mkRandomString(test.n)
if len(test.res) != test.n {
t.Errorf("Try %d: n=%d, got: %s (len=%d), want: %d.",
idx, test.n, test.res, len(test.res), test.n)
}
}
}
func TestOutputShowOnlyRegions(t *testing.T) {
var b bytes.Buffer
lo := NewOutput(ShowOnlyRegions, 0)
lo.w = &b
regions := GetRegions()[:2]
lo.Show(®ions)
got := b.String()
want := "af-south-1 Africa (Cape Town)\n" +
"ap-east-1 Asia Pacific (Hong Kong)\n"
if got != want {
t.Errorf("Show:\ngot =%q\nwant=%q", got, want)
}
}
func TestOutputShow0(t *testing.T) {
var b bytes.Buffer
lo := NewOutput(0, 0)
lo.w = &b
regions := GetRegions()[:2]
regions[0].Latencies = []time.Duration{15 * time.Millisecond}
regions[1].Latencies = []time.Duration{25 * time.Millisecond}
lo.Show(®ions)
want := "Africa (Cape Town) 15.00 ms\n" +
"Asia Pacific (Hong Kong) 25.00 ms\n"
got := b.String()
if got != want {
t.Errorf("Show0 failed:\ngot =%q\nwant=%q", got, want)
}
}
func TestOutputShow1(t *testing.T) {
var b bytes.Buffer
lo := NewOutput(1, 0)
lo.w = &b
regions := GetRegions()[:2]
regions[0].Latencies = []time.Duration{15 * time.Millisecond}
regions[1].Latencies = []time.Duration{25 * time.Millisecond}
lo.Show(®ions)
got := b.String()
want := " Code Region Latency\n" +
" 0 af-south-1 Africa (Cape Town) 15.00 ms\n" +
" 1 ap-east-1 Asia Pacific (Hong Kong) 25.00 ms\n"
if got != want {
t.Errorf("Show1 failed:\ngot =%q\nwant=%q", got, want)
}
}
func TestOutputShow2(t *testing.T) {
var b bytes.Buffer
lo := NewOutput(2, 2)
lo.w = &b
regions := GetRegions()[:2]
regions[0].Latencies = []time.Duration{15 * time.Millisecond, 17 * time.Millisecond}
regions[1].Latencies = []time.Duration{25 * time.Millisecond, 26 * time.Millisecond}
lo.Show(®ions)
got := b.String()
want := " Code Region Try #1 Try #2 Avg Latency\n" +
" 0 af-south-1 Africa (Cape Town) 15.00 ms 17.00 ms 16.00 ms\n" +
" 1 ap-east-1 Asia Pacific (Hong Kong) 25.00 ms 26.00 ms 25.50 ms\n"
if got != want {
t.Errorf("Show2 failed:\ngot =%q\nwant=%q", got, want)
}
}
func TestCalcLatency(t *testing.T) {
regions := GetRegions()[:3]
regions[0].Request = &testRequest{duration: 30 * time.Millisecond}
regions[1].Request = &testRequest{duration: 7 * time.Millisecond}
regions[2].Request = &testRequest{duration: 15 * time.Millisecond}
regionsStats := make(AWSRegions, regions.Len())
checkSort := func(origIndex, sortedIdx int) {
got := regionsStats[sortedIdx].Name
want := regions[origIndex].Name
if got != want {
t.Errorf("CalcLatency failed:\ngot=%q\nwant=%q\norig=%d\nsorted=%d",
got, want, origIndex, sortedIdx)
}
}
for i := 1; i < 4; i++ {
copy(regionsStats, regions)
switch i {
case 1:
CalcLatency(regionsStats, 1, false, false, "ec2")
case 2:
CalcLatency(regionsStats, 1, true, false, "ec2")
default:
CalcLatency(regionsStats, 1, true, true, "ec2")
}
checkSort(0, 2)
checkSort(1, 0)
checkSort(2, 1)
}
}