-
Notifications
You must be signed in to change notification settings - Fork 4
/
kod_test.go
153 lines (126 loc) · 3.42 KB
/
kod_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package kod
import (
"context"
"testing"
"time"
"github.com/knadh/koanf/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m,
goleak.IgnoreAnyFunction("github.com/go-kod/kod/interceptor/internal/ratelimit.cpuproc"),
goleak.IgnoreAnyFunction("go.opentelemetry.io/otel/sdk/metric.(*PeriodicReader).run"),
goleak.IgnoreAnyFunction("go.opentelemetry.io/otel/sdk/trace.(*batchSpanProcessor).processQueue"),
goleak.IgnoreAnyFunction("go.opentelemetry.io/otel/sdk/log.exportSync.func1"),
goleak.IgnoreAnyFunction("go.opentelemetry.io/otel/sdk/log.(*BatchProcessor).poll.func1"),
)
}
func TestConfigNoSuffix(t *testing.T) {
k, err := newKod(context.Background())
assert.Nil(t, err)
assert.EqualError(t, k.parseConfig("nosuffix"), "read config file: Unsupported Config Type \"\"")
}
func TestConfigNoFile(t *testing.T) {
k, err := newKod(context.Background())
assert.Nil(t, err)
assert.EqualError(t, k.parseConfig("notfound.yaml"), "read config file: open notfound.yaml: no such file or directory")
}
func TestConfigEnv(t *testing.T) {
k, err := newKod(context.Background())
assert.Nil(t, err)
assert.Equal(t, k.config.Name, "kod.test")
assert.Equal(t, k.config.Version, "")
assert.Equal(t, k.config.Env, "local")
t.Setenv("KOD_NAME", "test")
t.Setenv("KOD_VERSION", "1.0.0")
t.Setenv("KOD_ENV", "dev")
k, err = newKod(context.Background())
assert.Nil(t, err)
assert.Equal(t, k.config.Name, "test")
assert.Equal(t, k.config.Version, "1.0.0")
assert.Equal(t, k.config.Env, "dev")
}
type testComponent struct {
Implements[testInterface]
WithConfig[testConfig]
initialized bool
initErr error
shutdown bool
shutdownErr error
}
type testConfig struct {
Value string `default:"default"`
}
type testInterface interface {
IsInitialized() bool
}
func (c *testComponent) Init(context.Context) error {
c.initialized = true
return c.initErr
}
func (c *testComponent) Shutdown(context.Context) error {
c.shutdown = true
return c.shutdownErr
}
func (c *testComponent) IsInitialized() bool {
return c.initialized
}
func (c *testComponent) implements(testInterface) {}
func TestConfigurationLoading(t *testing.T) {
tests := []struct {
name string
koanf *koanf.Koanf
filename string
wantErr bool
}{
{
name: "custom koanf",
koanf: koanf.New("."), // 使用 koanf.New() 替代空实例
},
{
name: "invalid file extension",
filename: "config.invalid",
wantErr: true,
},
{
name: "missing file",
filename: "notexist.yaml",
wantErr: true, // Should use defaults
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := []func(*options){
WithConfigFile(tt.filename),
}
if tt.koanf != nil {
opts = append(opts, WithKoanf(tt.koanf))
}
k, err := newKod(context.Background(), opts...)
if tt.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err)
cfg := k.Config()
assert.NotEmpty(t, cfg.Name)
assert.NotEmpty(t, cfg.Env)
assert.Equal(t, 5*time.Second, cfg.ShutdownTimeout)
})
}
}
func TestDeferHooks(t *testing.T) {
k, err := newKod(context.Background())
require.NoError(t, err)
executed := false
k.Defer("test", func(context.Context) error {
executed = true
return nil
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
k.hooker.Do(ctx)
assert.True(t, executed)
}