-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmodule_test.go
123 lines (101 loc) · 3 KB
/
module_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package fxmetrics_test
import (
"strings"
"testing"
"github.com/ankorstore/yokai/fxconfig"
"github.com/ankorstore/yokai/fxlog"
"github.com/ankorstore/yokai/fxmetrics"
"github.com/ankorstore/yokai/fxmetrics/testdata/factory"
"github.com/ankorstore/yokai/fxmetrics/testdata/metrics"
"github.com/ankorstore/yokai/fxmetrics/testdata/spy"
"github.com/ankorstore/yokai/log/logtest"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
)
func TestModule(t *testing.T) {
t.Setenv("APP_CONFIG_PATH", "testdata/config")
var logBuffer logtest.TestLogBuffer
var registry *prometheus.Registry
fxtest.New(
t,
fx.NopLogger,
fxconfig.FxConfigModule,
fxlog.FxLogModule,
fxmetrics.FxMetricsModule,
fx.Options(
fxmetrics.AsMetricsCollector(metrics.FxMetricsTestCounter),
),
fx.Invoke(func() {
metrics.FxMetricsTestCounter.Add(9)
}),
fx.Populate(&logBuffer, ®istry),
).RequireStart().RequireStop()
logtest.AssertHasLogRecord(t, logBuffer, map[string]interface{}{
"level": "debug",
"message": "registered metrics collector *prometheus.counter",
})
// go metric
expectedMetric := `
# HELP go_memstats_lookups_total Total number of pointer lookups.
# TYPE go_memstats_lookups_total counter
go_memstats_lookups_total 0
`
err := testutil.GatherAndCompare(
registry,
strings.NewReader(expectedMetric),
"go_memstats_lookups_total",
)
assert.NoError(t, err)
// custom metric
expectedMetric = `
# HELP test_total test help
# TYPE test_total counter
test_total 9
`
err = testutil.GatherAndCompare(
registry,
strings.NewReader(expectedMetric),
"test_total",
)
assert.NoError(t, err)
}
func TestModuleErrorWithDuplicatedCollector(t *testing.T) {
t.Setenv("APP_CONFIG_PATH", "testdata/config")
var logBuffer logtest.TestLogBuffer
var registry *prometheus.Registry
spyTB := spy.NewSpyTB()
fxtest.New(
spyTB,
fx.NopLogger,
fxconfig.FxConfigModule,
fxlog.FxLogModule,
fxmetrics.FxMetricsModule,
fx.Options(
fxmetrics.AsMetricsCollectors(metrics.FxMetricsTestCounter, metrics.FxMetricsDuplicatedTestCounter),
),
fx.Populate(&logBuffer, ®istry),
).RequireStart().RequireStop()
assert.Empty(t, spyTB.Logs())
assert.NotZero(t, spyTB.Failures())
assert.Contains(t, spyTB.Errors().String(), "duplicate metrics collector registration attempted")
}
func TestModuleDecoration(t *testing.T) {
t.Setenv("APP_CONFIG_PATH", "testdata/config")
var logBuffer logtest.TestLogBuffer
var registry *prometheus.Registry
spyTB := spy.NewSpyTB()
fxtest.New(
spyTB,
fxconfig.FxConfigModule,
fxlog.FxLogModule,
fxmetrics.FxMetricsModule,
fx.Decorate(factory.NewTestMetricsRegistryFactory),
fx.Populate(&logBuffer, ®istry),
).RequireStart().RequireStop()
assert.Contains(t, spyTB.Logs().String(), "NewTestMetricsRegistryFactory")
assert.NotZero(t, spyTB.Failures())
assert.Contains(t, spyTB.Errors().String(), "custom error")
}