Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/crow/samples_test.go
4094 views
1
package crow
2
3
import (
4
"fmt"
5
"math/rand"
6
"strings"
7
"testing"
8
"time"
9
10
"github.com/prometheus/client_golang/prometheus"
11
"github.com/prometheus/common/expfmt"
12
"github.com/stretchr/testify/require"
13
)
14
15
func Test_sample_Ready(t *testing.T) {
16
tt := []struct {
17
sample sample
18
now time.Time
19
expect bool
20
}{
21
{
22
sample: sample{
23
ScrapeTime: time.Unix(100, 0).UTC(),
24
ValidationAttempt: 0,
25
},
26
now: time.Unix(100, 0).UTC(),
27
expect: false,
28
},
29
{
30
sample: sample{
31
ScrapeTime: time.Unix(100, 0).UTC(),
32
ValidationAttempt: 0,
33
},
34
now: time.Unix(500, 0).UTC(),
35
expect: true,
36
},
37
}
38
39
for _, tc := range tt {
40
ready := tc.sample.Ready(tc.now)
41
require.Equal(t, tc.expect, ready)
42
}
43
}
44
45
func Test_sampleBackoff(t *testing.T) {
46
tt := []struct {
47
attempt int
48
expect time.Duration
49
}{
50
{attempt: 0, expect: 1250 * time.Millisecond},
51
{attempt: 1, expect: 1500 * time.Millisecond},
52
{attempt: 2, expect: 2000 * time.Millisecond},
53
{attempt: 3, expect: 3000 * time.Millisecond},
54
{attempt: 4, expect: 5000 * time.Millisecond},
55
{attempt: 5, expect: 9000 * time.Millisecond},
56
}
57
58
for _, tc := range tt {
59
t.Run(fmt.Sprintf("%d", tc.attempt), func(t *testing.T) {
60
actual := sampleBackoff(tc.attempt)
61
require.Equal(t, tc.expect, actual)
62
})
63
}
64
}
65
66
func Test_sampleGenerator(t *testing.T) {
67
var (
68
reg = prometheus.NewRegistry()
69
)
70
71
gen := sampleGenerator{
72
numSamples: 10,
73
sendCh: make(chan<- []*sample, 10),
74
r: rand.New(rand.NewSource(0)),
75
}
76
reg.MustRegister(&gen)
77
78
mfs, err := reg.Gather()
79
require.NoError(t, err)
80
81
var sb strings.Builder
82
enc := expfmt.NewEncoder(&sb, expfmt.FmtText)
83
for _, mf := range mfs {
84
require.NoError(t, enc.Encode(mf))
85
}
86
87
expect := `# HELP crow_validation_sample Sample to validate
88
# TYPE crow_validation_sample gauge
89
crow_validation_sample{sample_num="sample_01"} 393152
90
crow_validation_sample{sample_num="sample_14"} 943416
91
crow_validation_sample{sample_num="sample_2f"} 980153
92
crow_validation_sample{sample_num="sample_51"} 637646
93
crow_validation_sample{sample_num="sample_55"} 976708
94
crow_validation_sample{sample_num="sample_94"} 995827
95
crow_validation_sample{sample_num="sample_c2"} 376202
96
crow_validation_sample{sample_num="sample_fa"} 126063
97
crow_validation_sample{sample_num="sample_fc"} 422456
98
crow_validation_sample{sample_num="sample_fd"} 197794
99
`
100
require.Equal(t, expect, sb.String())
101
}
102
103