@@ -2,6 +2,8 @@ package expv2
2
2
3
3
import (
4
4
"strconv"
5
+ "sync"
6
+ "sync/atomic"
5
7
"testing"
6
8
7
9
"github.com/sirupsen/logrus"
@@ -56,11 +58,12 @@ func TestMetricsFlusherFlushInBatchWithinBucket(t *testing.T) {
56
58
bq := & bucketQ {}
57
59
pm := & pusherMock {}
58
60
mf := metricsFlusher {
59
- bq : bq ,
60
- client : pm ,
61
- logger : logger ,
62
- discardedLabels : make (map [string ]struct {}),
63
- maxSeriesInBatch : 3 ,
61
+ bq : bq ,
62
+ client : pm ,
63
+ logger : logger ,
64
+ discardedLabels : make (map [string ]struct {}),
65
+ maxSeriesInBatch : 3 ,
66
+ batchPushConcurrency : 5 ,
64
67
}
65
68
66
69
bq .buckets = make ([]timeBucket , 0 , tc .series )
@@ -78,7 +81,7 @@ func TestMetricsFlusherFlushInBatchWithinBucket(t *testing.T) {
78
81
bq .Push ([]timeBucket {{Time : 1 , Sinks : sinks }})
79
82
err := mf .flush ()
80
83
require .NoError (t , err )
81
- assert .Equal (t , tc .expFlushCalls , pm .pushCalled )
84
+ assert .Equal (t , tc .expFlushCalls , pm .timesCalled () )
82
85
}
83
86
}
84
87
@@ -101,11 +104,12 @@ func TestMetricsFlusherFlushInBatchAcrossBuckets(t *testing.T) {
101
104
bq := & bucketQ {}
102
105
pm := & pusherMock {}
103
106
mf := metricsFlusher {
104
- bq : bq ,
105
- client : pm ,
106
- logger : logger ,
107
- discardedLabels : make (map [string ]struct {}),
108
- maxSeriesInBatch : 3 ,
107
+ bq : bq ,
108
+ client : pm ,
109
+ logger : logger ,
110
+ discardedLabels : make (map [string ]struct {}),
111
+ maxSeriesInBatch : 3 ,
112
+ batchPushConcurrency : 5 ,
109
113
}
110
114
111
115
bq .buckets = make ([]timeBucket , 0 , tc .series )
@@ -127,7 +131,7 @@ func TestMetricsFlusherFlushInBatchAcrossBuckets(t *testing.T) {
127
131
128
132
err := mf .flush ()
129
133
require .NoError (t , err )
130
- assert .Equal (t , tc .expFlushCalls , pm .pushCalled )
134
+ assert .Equal (t , tc .expFlushCalls , pm .timesCalled () )
131
135
}
132
136
}
133
137
@@ -136,21 +140,25 @@ func TestFlushWithReservedLabels(t *testing.T) {
136
140
137
141
logger , hook := testutils .NewLoggerWithHook (t )
138
142
143
+ mutex := sync.Mutex {}
139
144
collected := make ([]* pbcloud.MetricSet , 0 )
140
145
141
146
bq := & bucketQ {}
142
147
pm := & pusherMock {
143
148
hook : func (ms * pbcloud.MetricSet ) {
149
+ mutex .Lock ()
144
150
collected = append (collected , ms )
151
+ mutex .Unlock ()
145
152
},
146
153
}
147
154
148
155
mf := metricsFlusher {
149
- bq : bq ,
150
- client : pm ,
151
- maxSeriesInBatch : 2 ,
152
- logger : logger ,
153
- discardedLabels : make (map [string ]struct {}),
156
+ bq : bq ,
157
+ client : pm ,
158
+ maxSeriesInBatch : 2 ,
159
+ logger : logger ,
160
+ discardedLabels : make (map [string ]struct {}),
161
+ batchPushConcurrency : 5 ,
154
162
}
155
163
156
164
r := metrics .NewRegistry ()
@@ -186,7 +194,7 @@ func TestFlushWithReservedLabels(t *testing.T) {
186
194
require .NoError (t , err )
187
195
188
196
loglines := hook .Drain ()
189
- assert . Equal (t , 1 , len ( collected ) )
197
+ require . Len (t , collected , 1 )
190
198
191
199
// check that warnings sown only once per label
192
200
assert .Len (t , testutils .FilterEntries (loglines , logrus .WarnLevel , "Tag __name__ has been discarded since it is reserved for Cloud operations." ), 1 )
@@ -207,14 +215,18 @@ func TestFlushWithReservedLabels(t *testing.T) {
207
215
208
216
type pusherMock struct {
209
217
hook func (* pbcloud.MetricSet )
210
- pushCalled int
218
+ pushCalled int64
219
+ }
220
+
221
+ func (pm * pusherMock ) timesCalled () int {
222
+ return int (atomic .LoadInt64 (& pm .pushCalled ))
211
223
}
212
224
213
225
func (pm * pusherMock ) push (ms * pbcloud.MetricSet ) error {
214
226
if pm .hook != nil {
215
227
pm .hook (ms )
216
228
}
217
229
218
- pm .pushCalled ++
230
+ atomic . AddInt64 ( & pm .pushCalled , 1 )
219
231
return nil
220
232
}
0 commit comments