-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric_test.go
168 lines (151 loc) · 4.78 KB
/
metric_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
// (c) 2022 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)
package batch_test
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/elgopher/batch"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestProcessor_SubscribeBatchMetrics(t *testing.T) {
const key = "key"
ctx := context.Background()
t.Run("should get closed channel when subscribing metrics on stopped processor", func(t *testing.T) {
processor := batch.StartProcessor(batch.Options[empty]{})
processor.Stop()
// when
_, ok := <-processor.SubscribeBatchMetrics()
// then
assert.False(t, ok, "metrics channel should be closed")
})
t.Run("subscription should get batch metrics", func(t *testing.T) {
var (
loadResourceDuration = 100 * time.Millisecond
saveResourceDuration = 50 * time.Millisecond
operationDuration = 10 * time.Millisecond
totalDuration = loadResourceDuration + saveResourceDuration + operationDuration
)
processor := batch.StartProcessor(batch.Options[empty]{
MinDuration: loadResourceDuration,
LoadResource: func(_ context.Context, key string) (empty, error) {
time.Sleep(loadResourceDuration)
return empty{}, nil
},
SaveResource: func(_ context.Context, key string, r empty) error {
time.Sleep(saveResourceDuration)
return nil
},
})
defer processor.Stop()
subscription := processor.SubscribeBatchMetrics()
err := processor.Run(ctx, key, func(empty) {
time.Sleep(operationDuration)
})
require.NoError(t, err)
// when
metric := <-subscription
// then
assert.Equal(t, "key", metric.ResourceKey)
assert.NotZero(t, metric.BatchStart)
assert.Equal(t, metric.OperationCount, 1)
assertDurationInDelta(t, totalDuration, metric.TotalDuration, 10*time.Millisecond)
assertDurationInDelta(t, loadResourceDuration, metric.LoadResourceDuration, 10*time.Millisecond)
assertDurationInDelta(t, saveResourceDuration, metric.SaveResourceDuration, 10*time.Millisecond)
assert.NoError(t, metric.Error)
})
deliberateError := errors.New("fail")
t.Run("should get error in metric when LoadResource failed", func(t *testing.T) {
processor := batch.StartProcessor(batch.Options[empty]{
MinDuration: time.Millisecond,
LoadResource: func(_ context.Context, key string) (empty, error) {
return empty{}, deliberateError
},
})
metrics := processor.SubscribeBatchMetrics()
err := processor.Run(ctx, key, func(empty) {})
require.Error(t, err)
// when
metric := <-metrics
// then
assert.ErrorIs(t, metric.Error, deliberateError)
})
t.Run("should get error in metric when SaveResource failed", func(t *testing.T) {
processor := batch.StartProcessor(batch.Options[empty]{
MinDuration: time.Millisecond,
SaveResource: func(_ context.Context, key string, _ empty) error {
return deliberateError
},
})
metrics := processor.SubscribeBatchMetrics()
err := processor.Run(ctx, key, func(empty) {})
require.Error(t, err)
// when
metric := <-metrics
// then
assert.ErrorIs(t, metric.Error, deliberateError)
})
t.Run("each subscription should get all batch metrics", func(t *testing.T) {
processor := batch.StartProcessor(batch.Options[empty]{
MinDuration: time.Millisecond,
})
defer processor.Stop()
subscription1 := processor.SubscribeBatchMetrics()
subscription2 := processor.SubscribeBatchMetrics()
err := processor.Run(ctx, key, func(empty) {})
require.NoError(t, err)
// when
metrics1 := <-subscription1
metrics2 := <-subscription2
// then
assert.Equal(t, metrics1, metrics2)
})
t.Run("stopping processor should close subscription", func(t *testing.T) {
processor := batch.StartProcessor(batch.Options[empty]{
MinDuration: time.Millisecond,
})
subscription := processor.SubscribeBatchMetrics()
// when
processor.Stop()
_, ok := <-subscription
// then
assert.False(t, ok, "metrics channel should be closed")
})
t.Run("should not data race when run with -race flag", func(t *testing.T) {
processor := batch.StartProcessor(batch.Options[empty]{
MinDuration: time.Millisecond,
})
defer processor.Stop()
var g sync.WaitGroup
const goroutines = 1000
g.Add(goroutines)
for i := 0; i < goroutines/2; i++ {
go func() {
metrics := processor.SubscribeBatchMetrics()
g.Done()
for range metrics {
}
}()
k := fmt.Sprintf("%d", i)
go func() {
err := processor.Run(ctx, k, func(empty) {})
require.NoError(t, err)
g.Done()
}()
}
g.Wait()
})
}
func assertDurationInDelta(t *testing.T, expected time.Duration, actual time.Duration, delta time.Duration) {
diff := expected - actual
if diff < 0 {
diff *= -1
}
if diff > delta {
require.Failf(t, "invalid duration", "actual duration %s different than expected %s (max difference is %s)", actual, expected, delta)
}
}