-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_provider_app_loading_test.go
More file actions
219 lines (206 loc) · 7.67 KB
/
config_provider_app_loading_test.go
File metadata and controls
219 lines (206 loc) · 7.67 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package modular
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func Test_loadAppConfig(t *testing.T) {
t.Parallel()
// Tests now rely on per-application feeders (SetConfigFeeders) instead of mutating
// the global ConfigFeeders slice to support safe parallelization.
tests := []struct {
name string
setupApp func() *StdApplication
setupFeeders func() []Feeder
expectError bool
validateResult func(t *testing.T, app *StdApplication)
}{
{
name: "successful config load",
setupApp: func() *StdApplication {
mockLogger := new(MockLogger)
mockLogger.On("Debug", "Added main config for loading", mock.Anything).Return()
mockLogger.On("Debug", "Added section config for loading", mock.Anything).Return()
mockLogger.On("Debug", "Updated main config", mock.Anything).Return()
mockLogger.On("Debug", "Updated section config", mock.Anything).Return()
app := &StdApplication{
logger: mockLogger,
cfgProvider: NewStdConfigProvider(&testCfg{Str: "old", Num: 0}),
cfgSections: make(map[string]ConfigProvider),
}
app.cfgSections["section1"] = NewStdConfigProvider(&testSectionCfg{Enabled: false, Name: "old"})
return app
},
setupFeeders: func() []Feeder {
feeder := new(MockComplexFeeder)
// Setup to handle any Feed call - let the Run function determine the type
feeder.On("Feed", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
if cfg, ok := args.Get(0).(*testCfg); ok {
cfg.Str = updatedValue
cfg.Num = 42
} else if cfg, ok := args.Get(0).(*testSectionCfg); ok {
cfg.Enabled = true
cfg.Name = "updated"
}
})
// Setup for main config FeedKey calls
feeder.On("FeedKey", "_main", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
cfg := args.Get(1).(*testCfg)
cfg.Str = updatedValue
cfg.Num = 42
})
// Setup for section config FeedKey calls
feeder.On("FeedKey", "section1", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
cfg := args.Get(1).(*testSectionCfg)
cfg.Enabled = true
cfg.Name = "updated"
})
return []Feeder{feeder}
},
expectError: false,
validateResult: func(t *testing.T, app *StdApplication) {
mainCfg := app.cfgProvider.GetConfig().(*testCfg)
assert.Equal(t, updatedValue, mainCfg.Str)
assert.Equal(t, 42, mainCfg.Num)
sectionCfg := app.cfgSections["section1"].GetConfig().(*testSectionCfg)
assert.True(t, sectionCfg.Enabled)
assert.Equal(t, "updated", sectionCfg.Name)
},
},
{
name: "feed error",
setupApp: func() *StdApplication {
mockLogger := new(MockLogger)
mockLogger.On("Debug", "Added main config for loading", mock.Anything).Return()
app := &StdApplication{
logger: mockLogger,
cfgProvider: NewStdConfigProvider(&testCfg{Str: "old", Num: 0}),
cfgSections: make(map[string]ConfigProvider),
}
return app
},
setupFeeders: func() []Feeder {
feeder := new(MockComplexFeeder)
feeder.On("Feed", mock.Anything).Return(ErrFeedFailed)
return []Feeder{feeder}
},
expectError: true,
validateResult: func(t *testing.T, app *StdApplication) {
// Config should remain unchanged
mainCfg := app.cfgProvider.GetConfig().(*testCfg)
assert.Equal(t, "old", mainCfg.Str)
assert.Equal(t, 0, mainCfg.Num)
},
},
{
name: "feedKey error",
setupApp: func() *StdApplication {
mockLogger := new(MockLogger)
mockLogger.On("Debug", "Added main config for loading", mock.Anything).Return()
mockLogger.On("Debug", "Added section config for loading", mock.Anything).Return()
app := &StdApplication{
logger: mockLogger,
cfgProvider: NewStdConfigProvider(&testCfg{Str: "old", Num: 0}),
cfgSections: make(map[string]ConfigProvider),
}
app.cfgSections["section1"] = NewStdConfigProvider(&testSectionCfg{Enabled: false, Name: "old"})
return app
},
setupFeeders: func() []Feeder {
feeder := new(MockComplexFeeder)
feeder.On("Feed", mock.Anything).Return(nil)
// Due to map iteration order being random, either key could be processed first
// If "section1" is processed first, it will fail and stop processing
// If "_main" is processed first, it will succeed, then "section1" will fail
feeder.On("FeedKey", "_main", mock.Anything).Return(nil).Maybe()
feeder.On("FeedKey", "section1", mock.Anything).Return(ErrFeedKeyFailed)
return []Feeder{feeder}
},
expectError: true,
validateResult: func(t *testing.T, app *StdApplication) {
// Configs should remain unchanged
mainCfg := app.cfgProvider.GetConfig().(*testCfg)
assert.Equal(t, "old", mainCfg.Str)
sectionCfg := app.cfgSections["section1"].GetConfig().(*testSectionCfg)
assert.False(t, sectionCfg.Enabled)
},
},
{
name: "non-pointer configs",
setupApp: func() *StdApplication {
mockLogger := new(MockLogger)
mockLogger.On("Debug",
"Creating new provider with updated config (original was non-pointer)",
[]any(nil)).Return()
mockLogger.On("Debug", "Creating new provider for section", []any{"section", "section1"}).Return()
mockLogger.On("Debug", "Added main config for loading", mock.Anything).Return()
mockLogger.On("Debug", "Added section config for loading", mock.Anything).Return()
mockLogger.On("Debug", "Updated main config", mock.Anything).Return()
mockLogger.On("Debug", "Updated section config", mock.Anything).Return()
app := &StdApplication{
logger: mockLogger,
cfgProvider: NewStdConfigProvider(testCfg{Str: "old", Num: 0}), // non-pointer
cfgSections: make(map[string]ConfigProvider),
}
app.cfgSections["section1"] = NewStdConfigProvider(testSectionCfg{Enabled: false, Name: "old"}) // non-pointer
return app
},
setupFeeders: func() []Feeder {
feeder := new(MockComplexFeeder)
feeder.On("Feed", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
if cfg, ok := args.Get(0).(*testCfg); ok {
cfg.Str = updatedValue
cfg.Num = 42
} else if cfg, ok := args.Get(0).(*testSectionCfg); ok {
cfg.Enabled = true
cfg.Name = "updated"
}
})
feeder.On("FeedKey", "_main", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
cfg := args.Get(1).(*testCfg)
cfg.Str = updatedValue
cfg.Num = 42
})
feeder.On("FeedKey", "section1", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
cfg := args.Get(1).(*testSectionCfg)
cfg.Enabled = true
cfg.Name = "updated"
})
return []Feeder{feeder}
},
expectError: false,
validateResult: func(t *testing.T, app *StdApplication) {
mainCfg := app.cfgProvider.GetConfig()
assert.Equal(t, updatedValue, mainCfg.(testCfg).Str)
assert.Equal(t, 42, mainCfg.(testCfg).Num)
sectionCfg := app.cfgSections["section1"].GetConfig()
assert.True(t, sectionCfg.(testSectionCfg).Enabled)
assert.Equal(t, "updated", sectionCfg.(testSectionCfg).Name)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app := tt.setupApp()
// Use per-app feeders; StdApplication exposes SetConfigFeeders directly.
app.SetConfigFeeders(tt.setupFeeders())
err := loadAppConfig(app)
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
tt.validateResult(t, app)
}
// Assert that all mock expectations were met on the feeders we injected
for _, feeder := range app.configFeeders {
if mockFeeder, ok := feeder.(*MockComplexFeeder); ok {
mockFeeder.AssertExpectations(t)
}
}
if mockLogger, ok := app.logger.(*MockLogger); ok {
mockLogger.AssertExpectations(t)
}
})
}
}