-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathroundtrip_test.go
More file actions
132 lines (108 loc) · 3.59 KB
/
Copy pathroundtrip_test.go
File metadata and controls
132 lines (108 loc) · 3.59 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
125
126
127
128
129
130
131
132
package memmetrics
import (
"runtime"
"testing"
"time"
"github.com/mailgun/timetools"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vulcand/oxy/testutils"
)
func TestDefaults(t *testing.T) {
rr, err := NewRTMetrics(RTClock(testutils.GetClock()))
require.NoError(t, err)
require.NotNil(t, rr)
rr.Record(200, time.Second)
rr.Record(502, 2*time.Second)
rr.Record(200, time.Second)
rr.Record(200, time.Second)
assert.EqualValues(t, 1, rr.NetworkErrorCount())
assert.EqualValues(t, 4, rr.TotalCount())
assert.Equal(t, map[int]int64{502: 1, 200: 3}, rr.StatusCodesCounts())
assert.Equal(t, float64(1)/float64(4), rr.NetworkErrorRatio())
assert.Equal(t, 1.0/3.0, rr.ResponseCodeRatio(500, 503, 200, 300))
h, err := rr.LatencyHistogram()
require.NoError(t, err)
assert.Equal(t, 2, int(h.LatencyAtQuantile(100)/time.Second))
rr.Reset()
assert.EqualValues(t, 0, rr.NetworkErrorCount())
assert.EqualValues(t, 0, rr.TotalCount())
assert.Equal(t, map[int]int64{}, rr.StatusCodesCounts())
assert.Equal(t, float64(0), rr.NetworkErrorRatio())
assert.Equal(t, float64(0), rr.ResponseCodeRatio(500, 503, 200, 300))
h, err = rr.LatencyHistogram()
require.NoError(t, err)
assert.Equal(t, time.Duration(0), h.LatencyAtQuantile(100))
}
func TestAppend(t *testing.T) {
clock := testutils.GetClock()
rr, err := NewRTMetrics(RTClock(clock))
require.NoError(t, err)
require.NotNil(t, rr)
rr.Record(200, time.Second)
rr.Record(502, 2*time.Second)
rr.Record(200, time.Second)
rr.Record(200, time.Second)
rr2, err := NewRTMetrics(RTClock(clock))
require.NoError(t, err)
require.NotNil(t, rr2)
rr2.Record(200, 3*time.Second)
rr2.Record(501, 3*time.Second)
rr2.Record(200, 3*time.Second)
rr2.Record(200, 3*time.Second)
require.NoError(t, rr2.Append(rr))
assert.Equal(t, map[int]int64{501: 1, 502: 1, 200: 6}, rr2.StatusCodesCounts())
assert.EqualValues(t, 1, rr2.NetworkErrorCount())
h, err := rr2.LatencyHistogram()
require.NoError(t, err)
assert.EqualValues(t, 3, h.LatencyAtQuantile(100)/time.Second)
}
func TestConcurrentRecords(t *testing.T) {
// This test asserts a race condition which requires concurrency. Set
// GOMAXPROCS high for this test, then restore after test completes.
n := runtime.GOMAXPROCS(0)
runtime.GOMAXPROCS(100)
defer runtime.GOMAXPROCS(n)
rr, err := NewRTMetrics(RTClock(testutils.GetClock()))
require.NoError(t, err)
for code := 0; code < 100; code++ {
for numRecords := 0; numRecords < 10; numRecords++ {
go func(statusCode int) {
rr.Record(statusCode, time.Second)
}(code)
}
}
}
func TestRTMetricExportReturnsNewCopy(t *testing.T) {
a := RTMetrics{
clock: &timetools.RealTime{},
statusCodes: map[int]*RollingCounter{},
histogram: &RollingHDRHistogram{},
}
var err error
a.total, err = NewCounter(1, time.Second, CounterClock(a.clock))
require.NoError(t, err)
a.netErrors, err = NewCounter(1, time.Second, CounterClock(a.clock))
require.NoError(t, err)
a.newCounter = func() (*RollingCounter, error) {
return NewCounter(counterBuckets, counterResolution, CounterClock(a.clock))
}
a.newHist = func() (*RollingHDRHistogram, error) {
return NewRollingHDRHistogram(histMin, histMax, histSignificantFigures, histPeriod, histBuckets, RollingClock(a.clock))
}
b := a.Export()
a.total = nil
a.netErrors = nil
a.statusCodes = nil
a.histogram = nil
a.newCounter = nil
a.newHist = nil
a.clock = nil
assert.NotNil(t, b.total)
assert.NotNil(t, b.netErrors)
assert.NotNil(t, b.statusCodes)
assert.NotNil(t, b.histogram)
assert.NotNil(t, b.newCounter)
assert.NotNil(t, b.newHist)
assert.NotNil(t, b.clock)
}