@@ -84,16 +84,16 @@ const (
84
84
)
85
85
86
86
type limits struct {
87
- Received float64
88
- Sent float64
89
- RecCount int64
87
+ Recv float64
88
+ Sent float64
89
+ RecvCount int64
90
90
SentCount int64
91
91
}
92
92
93
93
94
94
// checkTrafficAtLeast compares stats before and after some operation and
95
95
// checks that at least some amount of traffic has happened.
96
- func checkTrafficAtLeast (t * testing.T , statsBefore * driver.ServerStatistics , statsAfter * driver.ServerStatistics , which source , lim * limits ) {
96
+ func checkTrafficAtLeast (t * testing.T , statsBefore * driver.ServerStatistics , statsAfter * driver.ServerStatistics , which source , lim * limits , label string ) {
97
97
var before * driver.ClientStats
98
98
var after * driver.ClientStats
99
99
var name string
@@ -107,30 +107,30 @@ func checkTrafficAtLeast(t *testing.T, statsBefore *driver.ServerStatistics, sta
107
107
name = "Client"
108
108
}
109
109
diff := after .BytesReceived .Sum - before .BytesReceived .Sum
110
- if diff < lim .Received {
111
- t .Errorf ("Difference in %s.BytesReceived.Sum is too small (< %f): %f" ,
112
- name , lim .Received , diff )
110
+ if diff < lim .Recv {
111
+ t .Errorf ("%s: Difference in %s.BytesReceived.Sum is too small (< %f): %f" ,
112
+ label , name , lim .Recv , diff )
113
113
}
114
114
diff = after .BytesSent .Sum - before .BytesSent .Sum
115
115
if diff < lim .Sent {
116
- t .Errorf ("Difference in %s.BytesSent.Sum is too small (< %f): %f" ,
117
- name , lim .Sent , diff )
116
+ t .Errorf ("%s: Difference in %s.BytesSent.Sum is too small (< %f): %f" ,
117
+ label , name , lim .Sent , diff )
118
118
}
119
119
intDiff := after .BytesReceived .Count - before .BytesReceived .Count
120
- if intDiff < lim .RecCount {
121
- t .Errorf ("Difference in %s.BytesReceived.Count is too small (< %d): %d" ,
122
- name , lim .RecCount , intDiff )
120
+ if intDiff < lim .RecvCount {
121
+ t .Errorf ("%s: Difference in %s.BytesReceived.Count is too small (< %d): %d" ,
122
+ label , name , lim .RecvCount , intDiff )
123
123
}
124
124
intDiff = after .BytesSent .Count - before .BytesSent .Count
125
125
if intDiff < lim .SentCount {
126
- t .Errorf ("Difference in %s.BytesSent.Count is too small (< %d): %d" ,
127
- name , lim .SentCount , intDiff )
126
+ t .Errorf ("%s: Difference in %s.BytesSent.Count is too small (< %d): %d" ,
127
+ label , name , lim .SentCount , intDiff )
128
128
}
129
129
}
130
130
131
131
// checkTrafficAtMost compares stats before and after some operation and
132
132
// checks that at most some amount of traffic has happened.
133
- func checkTrafficAtMost (t * testing.T , statsBefore * driver.ServerStatistics , statsAfter * driver.ServerStatistics , which source , lim * limits ) {
133
+ func checkTrafficAtMost (t * testing.T , statsBefore * driver.ServerStatistics , statsAfter * driver.ServerStatistics , which source , lim * limits , label string ) {
134
134
var before * driver.ClientStats
135
135
var after * driver.ClientStats
136
136
var name string
@@ -144,24 +144,24 @@ func checkTrafficAtMost(t *testing.T, statsBefore *driver.ServerStatistics, stat
144
144
name = "Client"
145
145
}
146
146
diff := after .BytesReceived .Sum - before .BytesReceived .Sum
147
- if diff > lim .Received {
148
- t .Errorf ("Difference in %s.BytesReceived.Sum is too large (> %f): %f" ,
149
- name , lim .Received , diff )
147
+ if diff > lim .Recv {
148
+ t .Errorf ("%s: Difference in %s.BytesReceived.Sum is too large (> %f): %f" ,
149
+ label , name , lim .Recv , diff )
150
150
}
151
151
diff = after .BytesSent .Sum - before .BytesSent .Sum
152
152
if diff > lim .Sent {
153
- t .Errorf ("Difference in %s.BytesSent.Sum is too large (> %f): %f" ,
154
- name , lim .Sent , diff )
153
+ t .Errorf ("%s: Difference in %s.BytesSent.Sum is too large (> %f): %f" ,
154
+ label , name , lim .Sent , diff )
155
155
}
156
156
intDiff := after .BytesReceived .Count - before .BytesReceived .Count
157
- if intDiff > lim .RecCount {
158
- t .Errorf ("Difference in %s.BytesReceived.Count is too large (> %d): %d" ,
159
- name , lim .RecCount , intDiff )
157
+ if intDiff > lim .RecvCount {
158
+ t .Errorf ("%s: Difference in %s.BytesReceived.Count is too large (> %d): %d" ,
159
+ label , name , lim .RecvCount , intDiff )
160
160
}
161
161
intDiff = after .BytesSent .Count - before .BytesSent .Count
162
162
if intDiff > lim .SentCount {
163
- t .Errorf ("Difference in %s.BytesSent.Count is too large (> %d): %d" ,
164
- name , lim .SentCount , intDiff )
163
+ t .Errorf ("%s: Difference in %s.BytesSent.Count is too large (> %d): %d" ,
164
+ label , name , lim .SentCount , intDiff )
165
165
}
166
166
}
167
167
@@ -188,8 +188,8 @@ func TestServerStatisticsTraffic(t *testing.T) {
188
188
}
189
189
190
190
checkTrafficAtLeast (t , & statsBefore , & statsAfter , all ,
191
- & limits {Sent : 100000.0 , Received : 40000.0 ,
192
- SentCount : 1000 , RecCount : 1000 });
191
+ & limits {Sent : 100000.0 , Recv : 40000.0 ,
192
+ SentCount : 1000 , RecvCount : 1000 }, "Banana" );
193
193
194
194
// Now check if user only stats are there and see if they should have increased:
195
195
if statsBefore .ClientUser .BytesReceived .Counts != nil {
@@ -200,15 +200,15 @@ func TestServerStatisticsTraffic(t *testing.T) {
200
200
// Traffic is superuser, so nothing should be counted in ClientUser,
201
201
// not even the statistics calls.
202
202
checkTrafficAtMost (t , & statsBefore , & statsAfter , user ,
203
- & limits {Sent : 0.1 , Received : 0.1 ,
204
- SentCount : 0 , RecCount : 0 });
203
+ & limits {Sent : 0.1 , Recv : 0.1 ,
204
+ SentCount : 0 , RecvCount : 0 }, "Cherry" );
205
205
} else {
206
206
t .Logf ("Authentication %s is not jwt superuser, expecting to see user traffic..." , auth )
207
207
// Traffic is either unauthenticated or with password, so there should
208
208
// be traffic in ClientUser
209
209
checkTrafficAtLeast (t , & statsBefore , & statsAfter , user ,
210
- & limits {Sent : 100000.0 , Received : 40000.0 ,
211
- SentCount : 1000 , RecCount : 1000 });
210
+ & limits {Sent : 100000.0 , Recv : 40000.0 ,
211
+ SentCount : 1000 , RecvCount : 1000 }, "Apple" );
212
212
}
213
213
} else {
214
214
t .Log ("Skipping ClientUser tests for statistics, since API is not present." )
@@ -256,6 +256,8 @@ func TestServerStatisticsForwarding(t *testing.T) {
256
256
ctx1 := driver .WithEndpoint (context .Background (), endpoints [0 ])
257
257
ctx2 := driver .WithEndpoint (context .Background (), endpoints [1 ])
258
258
259
+ time .Sleep (time .Second ) // wait for statistics to settle
260
+
259
261
statsBefore , err := c .Statistics (ctx2 )
260
262
if err != nil {
261
263
t .Fatalf ("Error in statistics call: %s" , describe (err ))
@@ -277,8 +279,12 @@ func TestServerStatisticsForwarding(t *testing.T) {
277
279
278
280
// No traffic on second coordinator (besides statistics calls):
279
281
checkTrafficAtMost (t , & statsBefore , & statsAfter , all ,
280
- & limits {Received : 400 , Sent : 4000 ,
281
- RecCount : 2 , SentCount : 2 })
282
+ & limits {Recv : 400 , Sent : 4000 ,
283
+ RecvCount : 2 , SentCount : 2 }, "Pear" )
284
+
285
+ if statsAfter .ClientUser .BytesReceived .Counts == nil {
286
+ t .Skip ("Skipping ClientUser tests for statistics, since API is not present." )
287
+ }
282
288
283
289
// First ask for a cursor on coordinator 1:
284
290
req , err := conn .NewRequest ("POST" , "_db/statistics_test/_api/cursor" )
@@ -338,25 +344,26 @@ func TestServerStatisticsForwarding(t *testing.T) {
338
344
// the statistics calls):
339
345
t .Logf ("Checking user traffic on coordinator2..." )
340
346
checkTrafficAtMost (t , & statsBefore2 , & statsAfter2 , user ,
341
- & limits {Received : 400 , Sent : 4000 ,
342
- RecCount : 2 , SentCount : 2 })
347
+ & limits {Recv : 400 , Sent : 4000 ,
348
+ RecvCount : 2 , SentCount : 2 }, "Apricot" )
343
349
// However, first coordinator should have counted the user traffic,
344
350
// note: it was just a single request with nearly no upload but quite
345
351
// some download:
346
352
auth := os .Getenv ("TEST_AUTHENTICATION" )
347
353
if auth != "super:testing" {
348
354
t .Logf ("Checking user traffic on coordinator1..." )
355
+ t .Logf ("statsBefore1: %v\n statsAfter1: %v" , statsBefore1 .ClientUser .BytesSent , statsAfter1 .ClientUser .BytesSent )
349
356
checkTrafficAtLeast (t , & statsBefore1 , & statsAfter1 , user ,
350
- & limits {Received : 0 , Sent : 40000 ,
351
- RecCount : 1 , SentCount : 1 })
357
+ & limits {Recv : 0 , Sent : 40000 ,
358
+ RecvCount : 1 , SentCount : 1 }, "Jackfruit" )
352
359
} else {
353
360
t .Logf ("Checking traffic on coordinator1..." )
354
361
checkTrafficAtLeast (t , & statsBefore1 , & statsAfter1 , all ,
355
- & limits {Received : 0 , Sent : 40000 ,
356
- RecCount : 1 , SentCount : 1 })
362
+ & limits {Recv : 0 , Sent : 40000 ,
363
+ RecvCount : 1 , SentCount : 1 }, "Durian" )
357
364
checkTrafficAtMost (t , & statsBefore1 , & statsAfter1 , user ,
358
- & limits {Received : 0.1 , Sent : 0.1 ,
359
- RecCount : 0 , SentCount : 0 })
365
+ & limits {Recv : 0.1 , Sent : 0.1 ,
366
+ RecvCount : 0 , SentCount : 0 }, "Mango" )
360
367
}
361
368
}
362
369
0 commit comments