forked from GoCodeAlone/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_provider_test.go
More file actions
532 lines (444 loc) · 12.9 KB
/
config_provider_test.go
File metadata and controls
532 lines (444 loc) · 12.9 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
package modular
import (
"fmt"
"sync"
"testing"
)
// TestConfig is a simple config struct for testing
type TestConfig struct {
Host string
Port int
Tags []string
Metadata map[string]string
}
func TestStdConfigProvider(t *testing.T) {
t.Run("returns same reference", func(t *testing.T) {
cfg := &TestConfig{Host: "localhost", Port: 8080}
provider := NewStdConfigProvider(cfg)
cfg1 := provider.GetConfig().(*TestConfig)
cfg2 := provider.GetConfig().(*TestConfig)
// Should be the exact same pointer
if cfg1 != cfg2 {
t.Error("StdConfigProvider should return same reference")
}
// Modifications affect all consumers
cfg1.Port = 9090
if cfg2.Port != 9090 {
t.Error("Modifications should affect all consumers")
}
})
}
func TestIsolatedConfigProvider(t *testing.T) {
t.Run("returns independent copies", func(t *testing.T) {
cfg := &TestConfig{
Host: "localhost",
Port: 8080,
Tags: []string{"a", "b"},
Metadata: map[string]string{"key": "value"},
}
provider := NewIsolatedConfigProvider(cfg)
cfg1 := provider.GetConfig().(*TestConfig)
cfg2 := provider.GetConfig().(*TestConfig)
// Should be different pointers
if cfg1 == cfg2 {
t.Error("IsolatedConfigProvider should return different references")
}
// Modifications should NOT affect other copies
cfg1.Port = 9090
if cfg2.Port == 9090 {
t.Error("Modifications should not affect other copies")
}
if cfg2.Port != 8080 {
t.Errorf("Expected port 8080, got %d", cfg2.Port)
}
})
t.Run("deep copies nested structures", func(t *testing.T) {
cfg := &TestConfig{
Host: "localhost",
Port: 8080,
Tags: []string{"a", "b"},
Metadata: map[string]string{"key": "value"},
}
provider := NewIsolatedConfigProvider(cfg)
cfg1 := provider.GetConfig().(*TestConfig)
cfg2 := provider.GetConfig().(*TestConfig)
// Modify slice in cfg1
cfg1.Tags[0] = "modified"
if cfg2.Tags[0] == "modified" {
t.Error("Slice modifications should not affect other copies")
}
// Modify map in cfg1
cfg1.Metadata["key"] = "modified"
if cfg2.Metadata["key"] == "modified" {
t.Error("Map modifications should not affect other copies")
}
})
t.Run("original is not modified", func(t *testing.T) {
cfg := &TestConfig{Host: "localhost", Port: 8080}
provider := NewIsolatedConfigProvider(cfg)
copy := provider.GetConfig().(*TestConfig)
copy.Port = 9090
copy.Host = "example.com"
// Original should remain unchanged
if cfg.Port != 8080 {
t.Errorf("Original port should be 8080, got %d", cfg.Port)
}
if cfg.Host != "localhost" {
t.Errorf("Original host should be localhost, got %s", cfg.Host)
}
})
}
func TestImmutableConfigProvider(t *testing.T) {
t.Run("returns same reference from atomic value", func(t *testing.T) {
cfg := &TestConfig{Host: "localhost", Port: 8080}
provider := NewImmutableConfigProvider(cfg)
cfg1 := provider.GetConfig().(*TestConfig)
cfg2 := provider.GetConfig().(*TestConfig)
// Should be the same pointer (same atomic value)
if cfg1 != cfg2 {
t.Error("Should return same reference before update")
}
})
t.Run("atomic update changes returned value", func(t *testing.T) {
cfg := &TestConfig{Host: "localhost", Port: 8080}
provider := NewImmutableConfigProvider(cfg)
cfg1 := provider.GetConfig().(*TestConfig)
// Update with new config
newCfg := &TestConfig{Host: "example.com", Port: 443}
provider.UpdateConfig(newCfg)
cfg2 := provider.GetConfig().(*TestConfig)
// Should now return the new config
if cfg2.Host != "example.com" || cfg2.Port != 443 {
t.Error("Should return updated config")
}
// Old reference should still have old values
if cfg1.Host != "localhost" || cfg1.Port != 8080 {
t.Error("Old reference should be unchanged")
}
})
t.Run("concurrent reads are safe", func(t *testing.T) {
cfg := &TestConfig{Host: "localhost", Port: 8080}
provider := NewImmutableConfigProvider(cfg)
var wg sync.WaitGroup
errors := make(chan error, 100)
// 100 concurrent readers
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
cfg := provider.GetConfig().(*TestConfig)
if cfg == nil {
errors <- fmt.Errorf("config is nil")
}
}()
}
wg.Wait()
close(errors)
if len(errors) > 0 {
t.Errorf("Concurrent reads failed with %d errors", len(errors))
}
})
t.Run("concurrent reads during updates", func(t *testing.T) {
cfg := &TestConfig{Host: "localhost", Port: 8080}
provider := NewImmutableConfigProvider(cfg)
var wg sync.WaitGroup
errors := make(chan error, 100)
// 50 concurrent readers
for i := 0; i < 50; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 100; j++ {
cfg := provider.GetConfig().(*TestConfig)
if cfg == nil {
errors <- ErrConfigNil
return
}
}
}()
}
// 10 concurrent updaters
for i := 0; i < 10; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
for j := 0; j < 10; j++ {
newCfg := &TestConfig{
Host: "example.com",
Port: 8080 + id*100 + j,
}
provider.UpdateConfig(newCfg)
}
}(i)
}
wg.Wait()
close(errors)
if len(errors) > 0 {
t.Errorf("Concurrent operations failed with %d errors", len(errors))
}
})
}
func TestCopyOnWriteConfigProvider(t *testing.T) {
t.Run("GetConfig returns original reference", func(t *testing.T) {
cfg := &TestConfig{Host: "localhost", Port: 8080}
provider := NewCopyOnWriteConfigProvider(cfg)
cfg1 := provider.GetConfig().(*TestConfig)
cfg2 := provider.GetConfig().(*TestConfig)
// Should be the same reference
if cfg1 != cfg2 {
t.Error("GetConfig should return same reference")
}
})
t.Run("GetMutableConfig returns independent copy", func(t *testing.T) {
cfg := &TestConfig{
Host: "localhost",
Port: 8080,
Tags: []string{"a", "b"},
Metadata: map[string]string{"key": "value"},
}
provider := NewCopyOnWriteConfigProvider(cfg)
original := provider.GetConfig().(*TestConfig)
mutable, err := provider.GetMutableConfig()
if err != nil {
t.Fatalf("GetMutableConfig failed: %v", err)
}
mutableCfg := mutable.(*TestConfig)
// Should be different pointers
if original == mutableCfg {
t.Error("GetMutableConfig should return different reference")
}
// Modifications should not affect original
mutableCfg.Port = 9090
mutableCfg.Tags[0] = "modified"
mutableCfg.Metadata["key"] = "modified"
if original.Port != 8080 {
t.Error("Original should not be modified")
}
if original.Tags[0] != "a" {
t.Error("Original slice should not be modified")
}
if original.Metadata["key"] != "value" {
t.Error("Original map should not be modified")
}
})
t.Run("UpdateOriginal changes GetConfig result", func(t *testing.T) {
cfg := &TestConfig{Host: "localhost", Port: 8080}
provider := NewCopyOnWriteConfigProvider(cfg)
cfg1 := provider.GetConfig().(*TestConfig)
// Update original
newCfg := &TestConfig{Host: "example.com", Port: 443}
provider.UpdateOriginal(newCfg)
cfg2 := provider.GetConfig().(*TestConfig)
// Should return new config
if cfg2.Host != "example.com" || cfg2.Port != 443 {
t.Error("Should return updated config")
}
// Old reference unchanged
if cfg1.Host != "localhost" {
t.Error("Old reference should be unchanged")
}
})
t.Run("concurrent reads are safe", func(t *testing.T) {
cfg := &TestConfig{Host: "localhost", Port: 8080}
provider := NewCopyOnWriteConfigProvider(cfg)
var wg sync.WaitGroup
errors := make(chan error, 100)
// 100 concurrent readers
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
cfg := provider.GetConfig().(*TestConfig)
if cfg == nil {
errors <- fmt.Errorf("config is nil")
}
}()
}
wg.Wait()
close(errors)
if len(errors) > 0 {
t.Errorf("Concurrent reads failed with %d errors", len(errors))
}
})
t.Run("concurrent mutable copies are safe", func(t *testing.T) {
cfg := &TestConfig{
Host: "localhost",
Port: 8080,
Tags: []string{"a", "b"},
Metadata: map[string]string{"key": "value"},
}
provider := NewCopyOnWriteConfigProvider(cfg)
var wg sync.WaitGroup
errors := make(chan error, 50)
// 50 concurrent mutable copy requests
for i := 0; i < 50; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
mutable, err := provider.GetMutableConfig()
if err != nil {
errors <- err
return
}
mutableCfg := mutable.(*TestConfig)
mutableCfg.Port = 8080 + id
}(i)
}
wg.Wait()
close(errors)
if len(errors) > 0 {
t.Errorf("Concurrent mutable copies failed with %d errors", len(errors))
}
// Original should be unchanged
original := provider.GetConfig().(*TestConfig)
if original.Port != 8080 {
t.Errorf("Original port should be 8080, got %d", original.Port)
}
})
}
func TestIsolatedConfigProvider_ErrorFallback(t *testing.T) {
// Test the error fallback path in IsolatedConfigProvider.GetConfig()
// when DeepCopyConfig returns an error (e.g., nil config)
// Create provider with nil config - this will trigger error in DeepCopyConfig
provider := &IsolatedConfigProvider{cfg: nil}
// GetConfig should handle the error gracefully and return nil
result := provider.GetConfig()
if result != nil {
t.Errorf("Expected GetConfig to return nil for nil config, got %v", result)
}
}
func TestDeepCopyConfig(t *testing.T) {
t.Run("copies primitives", func(t *testing.T) {
cfg := &TestConfig{Host: "localhost", Port: 8080}
copied, err := DeepCopyConfig(cfg)
if err != nil {
t.Fatalf("DeepCopyConfig failed: %v", err)
}
copiedCfg := copied.(*TestConfig)
// Values should match
if copiedCfg.Host != cfg.Host || copiedCfg.Port != cfg.Port {
t.Error("Copied values should match original")
}
// Should be different pointer
if copiedCfg == cfg {
t.Error("Should be different pointer")
}
})
t.Run("deep copies slices", func(t *testing.T) {
cfg := &TestConfig{
Host: "localhost",
Port: 8080,
Tags: []string{"a", "b", "c"},
}
copied, err := DeepCopyConfig(cfg)
if err != nil {
t.Fatalf("DeepCopyConfig failed: %v", err)
}
copiedCfg := copied.(*TestConfig)
// Modify copy's slice
copiedCfg.Tags[0] = "modified"
// Original should be unchanged
if cfg.Tags[0] != "a" {
t.Error("Original slice should not be modified")
}
})
t.Run("deep copies maps", func(t *testing.T) {
cfg := &TestConfig{
Host: "localhost",
Port: 8080,
Metadata: map[string]string{"key1": "value1", "key2": "value2"},
}
copied, err := DeepCopyConfig(cfg)
if err != nil {
t.Fatalf("DeepCopyConfig failed: %v", err)
}
copiedCfg := copied.(*TestConfig)
// Modify copy's map
copiedCfg.Metadata["key1"] = "modified"
copiedCfg.Metadata["key3"] = "new"
// Original should be unchanged
if cfg.Metadata["key1"] != "value1" {
t.Error("Original map should not be modified")
}
if _, exists := cfg.Metadata["key3"]; exists {
t.Error("Original map should not have new key")
}
})
t.Run("handles nil config", func(t *testing.T) {
_, err := DeepCopyConfig(nil)
if err != ErrConfigNil {
t.Error("Should return ErrConfigNil for nil config")
}
})
}
// Benchmarks for performance comparison
func BenchmarkConfigProviders(b *testing.B) {
cfg := &TestConfig{
Host: "localhost",
Port: 8080,
Tags: []string{"a", "b", "c"},
Metadata: map[string]string{"key1": "value1", "key2": "value2"},
}
b.Run("StdConfigProvider", func(b *testing.B) {
provider := NewStdConfigProvider(cfg)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = provider.GetConfig()
}
})
b.Run("IsolatedConfigProvider", func(b *testing.B) {
provider := NewIsolatedConfigProvider(cfg)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = provider.GetConfig()
}
})
b.Run("ImmutableConfigProvider", func(b *testing.B) {
provider := NewImmutableConfigProvider(cfg)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = provider.GetConfig()
}
})
b.Run("CopyOnWriteConfigProvider_Read", func(b *testing.B) {
provider := NewCopyOnWriteConfigProvider(cfg)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = provider.GetConfig()
}
})
b.Run("CopyOnWriteConfigProvider_Mutable", func(b *testing.B) {
provider := NewCopyOnWriteConfigProvider(cfg)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = provider.GetMutableConfig()
}
})
}
// Benchmark concurrent access
func BenchmarkConcurrentReads(b *testing.B) {
cfg := &TestConfig{
Host: "localhost",
Port: 8080,
Tags: []string{"a", "b", "c"},
Metadata: map[string]string{"key1": "value1"},
}
b.Run("ImmutableConfigProvider", func(b *testing.B) {
provider := NewImmutableConfigProvider(cfg)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = provider.GetConfig()
}
})
})
b.Run("CopyOnWriteConfigProvider", func(b *testing.B) {
provider := NewCopyOnWriteConfigProvider(cfg)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = provider.GetConfig()
}
})
})
}