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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
package hotline
import (
"encoding/json"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestStats_Increment(t *testing.T) {
tests := []struct {
name string
keys []StatKey
expected map[StatKey]int
}{
{
name: "single key increment",
keys: []StatKey{StatCurrentlyConnected},
expected: map[StatKey]int{
StatCurrentlyConnected: 1,
},
},
{
name: "multiple keys increment",
keys: []StatKey{StatCurrentlyConnected, StatDownloadCounter, StatUploadCounter},
expected: map[StatKey]int{
StatCurrentlyConnected: 1,
StatDownloadCounter: 1,
StatUploadCounter: 1,
},
},
{
name: "duplicate keys increment",
keys: []StatKey{StatCurrentlyConnected, StatCurrentlyConnected},
expected: map[StatKey]int{
StatCurrentlyConnected: 2,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stats := NewStats()
stats.Increment(tt.keys...)
for key, expectedVal := range tt.expected {
assert.Equal(t, expectedVal, stats.Get(key))
}
})
}
}
func TestStats_Increment_Multiple_Calls(t *testing.T) {
stats := NewStats()
stats.Increment(StatCurrentlyConnected)
assert.Equal(t, 1, stats.Get(StatCurrentlyConnected))
stats.Increment(StatCurrentlyConnected)
assert.Equal(t, 2, stats.Get(StatCurrentlyConnected))
stats.Increment(StatCurrentlyConnected, StatDownloadCounter)
assert.Equal(t, 3, stats.Get(StatCurrentlyConnected))
assert.Equal(t, 1, stats.Get(StatDownloadCounter))
}
func TestStats_Decrement(t *testing.T) {
tests := []struct {
name string
setupValue int
key StatKey
expected int
}{
{
name: "decrement from positive value",
setupValue: 5,
key: StatCurrentlyConnected,
expected: 4,
},
{
name: "decrement from zero stays zero",
setupValue: 0,
key: StatCurrentlyConnected,
expected: 0,
},
{
name: "decrement from one",
setupValue: 1,
key: StatCurrentlyConnected,
expected: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stats := NewStats()
stats.Set(tt.key, tt.setupValue)
stats.Decrement(tt.key)
assert.Equal(t, tt.expected, stats.Get(tt.key))
})
}
}
func TestStats_Decrement_Multiple_Calls(t *testing.T) {
stats := NewStats()
stats.Set(StatCurrentlyConnected, 10)
assert.Equal(t, 10, stats.Get(StatCurrentlyConnected))
stats.Decrement(StatCurrentlyConnected)
assert.Equal(t, 9, stats.Get(StatCurrentlyConnected))
stats.Decrement(StatCurrentlyConnected)
assert.Equal(t, 8, stats.Get(StatCurrentlyConnected))
}
func TestStats_Set(t *testing.T) {
tests := []struct {
name string
key StatKey
value int
expected int
}{
{
name: "set positive value",
key: StatCurrentlyConnected,
value: 42,
expected: 42,
},
{
name: "set zero value",
key: StatDownloadCounter,
value: 0,
expected: 0,
},
{
name: "set negative value",
key: StatUploadCounter,
value: -5,
expected: -5,
},
{
name: "overwrite existing value",
key: StatConnectionPeak,
value: 100,
expected: 100,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stats := NewStats()
if tt.name == "overwrite existing value" {
stats.Set(tt.key, 50)
}
stats.Set(tt.key, tt.value)
assert.Equal(t, tt.expected, stats.Get(tt.key))
})
}
}
func TestStats_Get(t *testing.T) {
tests := []struct {
name string
key StatKey
setValue int
expected int
}{
{
name: "get initialized value",
key: StatCurrentlyConnected,
setValue: 0,
expected: 0,
},
{
name: "get set value",
key: StatDownloadCounter,
setValue: 25,
expected: 25,
},
{
name: "get after increment",
key: StatUploadCounter,
setValue: -1,
expected: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stats := NewStats()
if tt.name == "get after increment" {
stats.Increment(tt.key)
} else {
stats.Set(tt.key, tt.setValue)
}
result := stats.Get(tt.key)
assert.Equal(t, tt.expected, result)
})
}
}
func TestStats_Get_Default_Values(t *testing.T) {
stats := NewStats()
expectedDefaults := map[StatKey]int{
StatCurrentlyConnected: 0,
StatDownloadsInProgress: 0,
StatUploadsInProgress: 0,
StatWaitingDownloads: 0,
StatConnectionPeak: 0,
StatDownloadCounter: 0,
StatUploadCounter: 0,
StatConnectionCounter: 0,
}
for key, expected := range expectedDefaults {
assert.Equal(t, expected, stats.Get(key))
}
}
func TestStats_Values(t *testing.T) {
stats := NewStats()
// Test default values
values := stats.Values()
assert.Equal(t, 0, values.CurrentlyConnected)
assert.Equal(t, 0, values.DownloadsInProgress)
assert.Equal(t, 0, values.UploadsInProgress)
assert.Equal(t, 0, values.WaitingDownloads)
assert.Equal(t, 0, values.ConnectionPeak)
assert.Equal(t, 0, values.ConnectionCounter)
assert.Equal(t, 0, values.DownloadCounter)
assert.Equal(t, 0, values.UploadCounter)
assert.False(t, values.Since.IsZero(), "Since should be set")
}
func TestStats_Values_WithModifiedStats(t *testing.T) {
stats := NewStats()
// Modify some stats
stats.Set(StatCurrentlyConnected, 10)
stats.Set(StatDownloadsInProgress, 5)
stats.Increment(StatConnectionCounter)
stats.Increment(StatDownloadCounter, StatUploadCounter)
values := stats.Values()
assert.Equal(t, 10, values.CurrentlyConnected)
assert.Equal(t, 5, values.DownloadsInProgress)
assert.Equal(t, 0, values.UploadsInProgress)
assert.Equal(t, 0, values.WaitingDownloads)
assert.Equal(t, 0, values.ConnectionPeak)
assert.Equal(t, 1, values.ConnectionCounter)
assert.Equal(t, 1, values.DownloadCounter)
assert.Equal(t, 1, values.UploadCounter)
}
// TestStats_Values_JSONKeys locks the JSON wire format served at /api/v1/stats.
func TestStats_Values_JSONKeys(t *testing.T) {
b, err := json.Marshal(NewStats().Values())
assert.NoError(t, err)
var decoded map[string]interface{}
assert.NoError(t, json.Unmarshal(b, &decoded))
expectedKeys := []string{
"CurrentlyConnected",
"DownloadsInProgress",
"UploadsInProgress",
"WaitingDownloads",
"ConnectionPeak",
"ConnectionCounter",
"DownloadCounter",
"UploadCounter",
"Since",
}
for _, key := range expectedKeys {
_, exists := decoded[key]
assert.True(t, exists, "Key %s should exist in JSON output", key)
}
assert.Equal(t, len(expectedKeys), len(decoded))
}
func TestStats_Max(t *testing.T) {
stats := NewStats()
stats.Max(StatConnectionPeak, 5)
assert.Equal(t, 5, stats.Get(StatConnectionPeak), "raises to a larger value")
stats.Max(StatConnectionPeak, 3)
assert.Equal(t, 5, stats.Get(StatConnectionPeak), "no-op for a smaller value")
stats.Max(StatConnectionPeak, 5)
assert.Equal(t, 5, stats.Get(StatConnectionPeak), "no-op for an equal value")
stats.Max(StatConnectionPeak, 10)
assert.Equal(t, 10, stats.Get(StatConnectionPeak), "raises again")
}
// TestStats_Concurrent exercises the counter from many goroutines so the race
// detector can catch unsynchronized access (run with -race).
func TestStats_Concurrent(t *testing.T) {
stats := NewStats()
const goroutines = 50
var wg sync.WaitGroup
wg.Add(goroutines)
for i := 0; i < goroutines; i++ {
go func(n int) {
defer wg.Done()
stats.Increment(StatConnectionCounter)
stats.Max(StatConnectionPeak, n)
stats.Set(StatCurrentlyConnected, n)
_ = stats.Get(StatCurrentlyConnected)
_ = stats.Values()
stats.Decrement(StatConnectionCounter)
}(i)
}
wg.Wait()
assert.Equal(t, 0, stats.Get(StatConnectionCounter), "increments and decrements balance out")
assert.Equal(t, goroutines-1, stats.Get(StatConnectionPeak), "peak captures the largest value")
}
|