/
expvar.go
77 lines (71 loc) · 2.55 KB
/
expvar.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
// Copyright 2021 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
package testutil
import (
"expvar"
"log"
"testing"
"time"
)
// TestGetExpvar fetches the expvar metric `name`, and returns the expvar.
// Callers are responsible for type assertions on the returned value.
func TestGetExpvar(tb testing.TB, name string) expvar.Var {
tb.Helper()
v := expvar.Get(name)
log.Println("Var %q is %v", name, v)
return v
}
const defaultDoOrTimeoutDeadline = 10 * time.Second
// ExpectExpvarDeltaWithDeadline returns a deferrable function which tests if the expvar metric with name has changed by delta within the given deadline, once the function begins. Before returning, it fetches the original value for comparison.
func ExpectExpvarDeltaWithDeadline(tb testing.TB, name string, want int64) func() {
tb.Helper()
deadline := defaultDoOrTimeoutDeadline
start := TestGetExpvar(tb, name).(*expvar.Int).Value()
check := func() (bool, error) {
tb.Helper()
now := TestGetExpvar(tb, name).(*expvar.Int).Value()
log.Printf("now is %v", now)
return now-start == want, nil
}
return func() {
tb.Helper()
ok, err := DoOrTimeout(check, deadline, 10*time.Millisecond)
FatalIfErr(tb, err)
if !ok {
now := TestGetExpvar(tb, name).(*expvar.Int).Value()
tb.Errorf("Did not see %s have delta by deadline: got %v - %v = %d, want %d", name, now, start, now-start, want)
}
}
}
// ExpectMapExpvarMetricDeltaWithDeadline returns a deferrable function which tests if the expvar map metric with name and key has changed by delta within the given deadline, once the function begins. Before returning, it fetches the original value for comparison.
func ExpectMapExpvarDeltaWithDeadline(tb testing.TB, name, key string, want int64) func() {
tb.Helper()
deadline := defaultDoOrTimeoutDeadline
startVar := TestGetExpvar(tb, name).(*expvar.Map).Get(key)
var start int64
if startVar != nil {
start = startVar.(*expvar.Int).Value()
}
check := func() (bool, error) {
tb.Helper()
nowVar := TestGetExpvar(tb, name).(*expvar.Map).Get(key)
var now int64
if nowVar != nil {
now = nowVar.(*expvar.Int).Value()
}
return now-start == want, nil
}
return func() {
tb.Helper()
ok, err := DoOrTimeout(check, deadline, 10*time.Millisecond)
FatalIfErr(tb, err)
if !ok {
nowVar := TestGetExpvar(tb, name).(*expvar.Map).Get(key)
var now int64
if nowVar != nil {
now = nowVar.(*expvar.Int).Value()
}
tb.Errorf("Did not see %s[%s] have delta by deadline: got %v - %v = %d, want %d", name, key, now, start, now-start, want)
}
}
}