-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider_test.go
More file actions
123 lines (100 loc) · 3.06 KB
/
provider_test.go
File metadata and controls
123 lines (100 loc) · 3.06 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
package runtime
import (
"context"
"testing"
"github.com/origadmin/runtime/contracts/component"
"github.com/origadmin/runtime/contracts/iterator"
"github.com/origadmin/runtime/engine"
"github.com/origadmin/runtime/engine/container"
)
// Mock structs mimicking protobuf generated code
type MockConfig struct {
Name string
}
func (m *MockConfig) GetName() string { return m.Name }
type MockContainer struct {
Active string
Default *MockConfig
Configs []*MockConfig
}
func (m *MockContainer) GetActive() string { return m.Active }
func (m *MockContainer) GetDefault() *MockConfig { return m.Default }
func (m *MockContainer) GetConfigs() []*MockConfig { return m.Configs }
func TestDefaultResolvers_Functionality(t *testing.T) {
ctx := context.Background()
// Use individual container for this test to avoid shared state
customResolver := func(ctx context.Context, source any, opts *component.LoadOptions) (*component.ModuleConfig, error) {
if c, ok := source.(*MockContainer); ok {
res := &component.ModuleConfig{Active: c.Active}
for _, cfg := range c.Configs {
res.Entries = append(res.Entries, component.ConfigEntry{Name: cfg.Name, Value: cfg})
}
return res, nil
}
return nil, nil
}
reg := container.NewContainer(container.WithCategoryResolvers(map[component.Category]component.ConfigResolver{
"mocks": customResolver,
}))
reg.Register("mocks", func(ctx context.Context, h component.Handle) (any, error) {
return "mock", nil
})
c := &MockContainer{
Configs: []*MockConfig{{Name: "A"}, {Name: "B"}},
}
if err := reg.Load(ctx, c); err != nil {
t.Fatalf("Load failed: %v", err)
}
h := reg.In("mocks")
foundA := false
var it iterator.Iterator = h.Iter(ctx)
for it.Next() {
name, _ := it.Value()
if name == "A" {
foundA = true
}
}
if !foundA {
t.Error("Expected to find instance A")
}
}
func TestDefaultResolvers_PassThrough(t *testing.T) {
ctx := context.Background()
regRaw := container.NewContainer(nil)
single := &MockConfig{Name: "Solo"}
var capturedConfig any
regRaw.Register("raw_config", func(ctx context.Context, h component.Handle) (any, error) {
capturedConfig = h.Config()
return "ok", nil
})
if err := regRaw.Load(ctx, single, engine.ForCategory("raw_config")); err != nil {
t.Fatal(err)
}
// Trigger instantiation
_, err := regRaw.In("raw_config").Get(ctx, component.DefaultName)
if err != nil {
t.Fatalf("Get failed: %v", err)
}
if capturedConfig != single {
t.Errorf("Pass-through mode should return original source, got %T", capturedConfig)
}
}
func TestContainer_LifecycleLock(t *testing.T) {
reg := container.NewContainer(nil)
ctx := context.Background()
reg.Register("first", func(ctx context.Context, h component.Handle) (any, error) {
return "ok", nil
})
if err := reg.Load(ctx, "root"); err != nil {
t.Fatal(err)
}
// Subsequent registration should panic
defer func() {
if r := recover(); r == nil {
t.Error("Register after Load should have panicked")
}
}()
reg.Register("second", func(ctx context.Context, h component.Handle) (any, error) {
return "fail", nil
})
}