-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine_reconfig_test.go
More file actions
315 lines (256 loc) · 8.5 KB
/
engine_reconfig_test.go
File metadata and controls
315 lines (256 loc) · 8.5 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package workflow
import (
"context"
"fmt"
"testing"
"github.com/GoCodeAlone/modular"
"github.com/GoCodeAlone/workflow/config"
)
// mockReconfigurableModule is a test module that supports reconfiguration.
type mockReconfigurableModule struct {
name string
config map[string]any
calls int
failOn string // if set, Reconfigure returns error when name matches
}
func (m *mockReconfigurableModule) Name() string { return m.name }
func (m *mockReconfigurableModule) Init(app modular.Application) error { return nil }
func (m *mockReconfigurableModule) Reconfigure(_ context.Context, cfg map[string]any) error {
if m.failOn != "" && m.name == m.failOn {
return fmt.Errorf("reconfigure failed for %s", m.name)
}
m.config = cfg
m.calls++
return nil
}
// mockNonReconfigurableModule is a test module that does NOT support reconfiguration.
type mockNonReconfigurableModule struct {
name string
}
func (m *mockNonReconfigurableModule) Name() string { return m.name }
func (m *mockNonReconfigurableModule) Init(app modular.Application) error { return nil }
// TestReconfigureModules_Success tests successful reconfiguration of a single module.
func TestReconfigureModules_Success(t *testing.T) {
app := newMockApplication()
engine := NewStdEngine(app, app.Logger())
// Register a reconfigurable module
mod := &mockReconfigurableModule{
name: "test-module",
config: map[string]any{"version": "v1"},
}
app.RegisterModule(mod)
ctx := context.Background()
newConfig := map[string]any{"version": "v2", "timeout": 30}
changes := []config.ModuleConfigChange{
{
Name: "test-module",
OldConfig: map[string]any{"version": "v1"},
NewConfig: newConfig,
},
}
failed, err := engine.ReconfigureModules(ctx, changes)
if err != nil {
t.Fatalf("ReconfigureModules returned error: %v", err)
}
if len(failed) != 0 {
t.Fatalf("Expected 0 failed modules, got %d: %v", len(failed), failed)
}
if mod.calls != 1 {
t.Fatalf("Expected Reconfigure to be called once, got %d", mod.calls)
}
if mod.config["version"] != "v2" {
t.Fatalf("Expected config version to be v2, got %v", mod.config["version"])
}
if mod.config["timeout"] != 30 {
t.Fatalf("Expected timeout to be 30, got %v", mod.config["timeout"])
}
}
// TestReconfigureModules_NotReconfigurable tests that non-reconfigurable modules
// are reported in failedModules.
func TestReconfigureModules_NotReconfigurable(t *testing.T) {
app := newMockApplication()
engine := NewStdEngine(app, app.Logger())
// Register a non-reconfigurable module
mod := &mockNonReconfigurableModule{name: "static-module"}
app.RegisterModule(mod)
ctx := context.Background()
changes := []config.ModuleConfigChange{
{
Name: "static-module",
OldConfig: map[string]any{},
NewConfig: map[string]any{"key": "value"},
},
}
failed, err := engine.ReconfigureModules(ctx, changes)
if err != nil {
t.Fatalf("ReconfigureModules returned error: %v", err)
}
if len(failed) != 1 {
t.Fatalf("Expected 1 failed module, got %d: %v", len(failed), failed)
}
if failed[0] != "static-module" {
t.Fatalf("Expected failed module to be 'static-module', got %s", failed[0])
}
}
// TestReconfigureModules_ModuleNotFound tests that non-existent modules
// are reported in failedModules.
func TestReconfigureModules_ModuleNotFound(t *testing.T) {
app := newMockApplication()
engine := NewStdEngine(app, app.Logger())
ctx := context.Background()
changes := []config.ModuleConfigChange{
{
Name: "nonexistent-module",
OldConfig: map[string]any{},
NewConfig: map[string]any{"key": "value"},
},
}
failed, err := engine.ReconfigureModules(ctx, changes)
if err != nil {
t.Fatalf("ReconfigureModules returned error: %v", err)
}
if len(failed) != 1 {
t.Fatalf("Expected 1 failed module, got %d: %v", len(failed), failed)
}
if failed[0] != "nonexistent-module" {
t.Fatalf("Expected failed module to be 'nonexistent-module', got %s", failed[0])
}
}
// TestReconfigureModules_ReconfigureFails tests that when Reconfigure returns
// an error, the module is added to failedModules.
func TestReconfigureModules_ReconfigureFails(t *testing.T) {
app := newMockApplication()
engine := NewStdEngine(app, app.Logger())
// Register a module that fails on reconfigure
mod := &mockReconfigurableModule{
name: "failing-module",
config: map[string]any{},
failOn: "failing-module",
}
app.RegisterModule(mod)
ctx := context.Background()
changes := []config.ModuleConfigChange{
{
Name: "failing-module",
OldConfig: map[string]any{},
NewConfig: map[string]any{"key": "value"},
},
}
failed, err := engine.ReconfigureModules(ctx, changes)
if err != nil {
t.Fatalf("ReconfigureModules returned error: %v", err)
}
if len(failed) != 1 {
t.Fatalf("Expected 1 failed module, got %d: %v", len(failed), failed)
}
if failed[0] != "failing-module" {
t.Fatalf("Expected failed module to be 'failing-module', got %s", failed[0])
}
}
// TestReconfigureModules_MultipleModules tests reconfiguration of multiple
// modules with different outcomes (success, not found, not reconfigurable, error).
func TestReconfigureModules_MultipleModules(t *testing.T) {
app := newMockApplication()
engine := NewStdEngine(app, app.Logger())
// Register modules
reconfigMod := &mockReconfigurableModule{
name: "reconfig-module",
config: map[string]any{},
}
nonReconfigMod := &mockNonReconfigurableModule{name: "static-module"}
failingMod := &mockReconfigurableModule{
name: "failing-module",
config: map[string]any{},
failOn: "failing-module",
}
app.RegisterModule(reconfigMod)
app.RegisterModule(nonReconfigMod)
app.RegisterModule(failingMod)
ctx := context.Background()
changes := []config.ModuleConfigChange{
{
Name: "reconfig-module",
OldConfig: map[string]any{},
NewConfig: map[string]any{"key": "value1"},
},
{
Name: "static-module",
OldConfig: map[string]any{},
NewConfig: map[string]any{"key": "value2"},
},
{
Name: "failing-module",
OldConfig: map[string]any{},
NewConfig: map[string]any{"key": "value3"},
},
{
Name: "nonexistent-module",
OldConfig: map[string]any{},
NewConfig: map[string]any{"key": "value4"},
},
}
failed, err := engine.ReconfigureModules(ctx, changes)
if err != nil {
t.Fatalf("ReconfigureModules returned error: %v", err)
}
// Should have 3 failed modules: static-module (not reconfigurable),
// failing-module (error), and nonexistent-module (not found)
if len(failed) != 3 {
t.Fatalf("Expected 3 failed modules, got %d: %v", len(failed), failed)
}
// Verify the successful module was reconfigured
if reconfigMod.calls != 1 {
t.Fatalf("Expected reconfig-module Reconfigure to be called once, got %d", reconfigMod.calls)
}
if reconfigMod.config["key"] != "value1" {
t.Fatalf("Expected reconfig-module config key to be value1, got %v", reconfigMod.config["key"])
}
}
// TestReconfigureModules_EmptyChanges tests that empty changes list returns
// success with no failed modules.
func TestReconfigureModules_EmptyChanges(t *testing.T) {
app := newMockApplication()
engine := NewStdEngine(app, app.Logger())
ctx := context.Background()
changes := []config.ModuleConfigChange{}
failed, err := engine.ReconfigureModules(ctx, changes)
if err != nil {
t.Fatalf("ReconfigureModules returned error: %v", err)
}
if len(failed) != 0 {
t.Fatalf("Expected 0 failed modules, got %d: %v", len(failed), failed)
}
}
// TestReconfigureModules_ContextCancellation tests that context cancellation
// is respected during reconfiguration.
func TestReconfigureModules_ContextCancellation(t *testing.T) {
app := newMockApplication()
engine := NewStdEngine(app, app.Logger())
// Register a module with a slow reconfigure
mod := &mockReconfigurableModule{
name: "test-module",
config: map[string]any{},
}
app.RegisterModule(mod)
// Create a cancelled context
ctx, cancel := context.WithCancel(context.Background())
cancel()
changes := []config.ModuleConfigChange{
{
Name: "test-module",
OldConfig: map[string]any{},
NewConfig: map[string]any{"key": "value"},
},
}
// This should still work - our mock doesn't check context cancellation,
// but in a real scenario with a context-aware Reconfigure, it would respect it
failed, err := engine.ReconfigureModules(ctx, changes)
if err != nil {
t.Fatalf("ReconfigureModules returned error: %v", err)
}
// Even with cancelled context, the module should be reconfigured
// (our mock implementation doesn't check context)
if len(failed) != 0 {
t.Fatalf("Expected 0 failed modules, got %d: %v", len(failed), failed)
}
}