-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics_test.go
59 lines (52 loc) · 1.21 KB
/
metrics_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
package metrics
import (
"bytes"
"runtime"
"slices"
"strings"
"sync"
"testing"
"go.withmatt.com/metrics/internal/assert"
)
// assertMarshal compares lines of output ignoring order
func assertMarshal(tb testing.TB, set *Set, expected []string) {
tb.Helper()
var b bytes.Buffer
set.WritePrometheusUnthrottled(&b)
out := strings.Trim(b.String(), "\n")
lines := splitLines(out)
expected = splitLines(strings.Join(expected, "\n"))
assert.LinesEqual(tb, lines, expected)
}
func assertMarshalUnordered(tb testing.TB, set *Set, expected []string) {
tb.Helper()
var b bytes.Buffer
set.WritePrometheusUnthrottled(&b)
out := strings.Trim(b.String(), "\n")
lines := splitLines(out)
expected = splitLines(strings.Join(expected, "\n"))
slices.Sort(lines)
slices.Sort(expected)
assert.LinesEqual(tb, lines, expected)
}
func hammer(tb testing.TB, n int, f func(int)) {
tb.Helper()
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(4))
var wg sync.WaitGroup
for i := range n {
wg.Add(1)
go func(i int) {
defer func() {
assert.Nil(tb, recover())
wg.Done()
}()
f(i)
}(i)
}
wg.Wait()
}
func splitLines(s string) []string {
lines := strings.SplitAfter(s, "\n")
lines[len(lines)-1] += "\n"
return lines
}