-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager_dynamic_loading_test.go
More file actions
198 lines (169 loc) · 6.25 KB
/
manager_dynamic_loading_test.go
File metadata and controls
198 lines (169 loc) · 6.25 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
// manager_dynamic_loading_safety_test.go: Safety tests for dynamic loading methods before refactoring
//
// Copyright (c) 2025 AGILira - A. Giordano
// Series: an AGILira library
// SPDX-License-Identifier: MPL-2.0
package goplugins
import (
"context"
"testing"
"time"
)
// TestManager_DynamicLoadingSafety tests that all dynamic loading methods are accessible and work
func TestManager_DynamicLoadingSafety(t *testing.T) {
logger := NewLogger(nil)
manager := NewManager[TestRequest, TestResponse](logger)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
t.Run("EnableDynamicLoading", func(t *testing.T) {
err := manager.EnableDynamicLoading(ctx)
// We expect this to work (even if no plugins are discovered)
if err != nil {
t.Logf("EnableDynamicLoading returned error (may be expected): %v", err)
}
})
t.Run("GetDiscoveredPlugins", func(t *testing.T) {
plugins := manager.GetDiscoveredPlugins()
if plugins == nil {
t.Error("GetDiscoveredPlugins returned nil")
}
t.Logf("Discovered plugins: %d", len(plugins))
})
t.Run("GetDynamicLoadingStatus", func(t *testing.T) {
status := manager.GetDynamicLoadingStatus()
if status == nil {
t.Error("GetDynamicLoadingStatus returned nil")
}
t.Logf("Dynamic loading status: %d plugins", len(status))
})
t.Run("GetDependencyGraph", func(t *testing.T) {
graph := manager.GetDependencyGraph()
if graph == nil {
t.Error("GetDependencyGraph returned nil")
}
})
t.Run("GetDynamicLoadingMetrics", func(t *testing.T) {
// Just verify we can call it without panic
manager.GetDynamicLoadingMetrics()
t.Logf("Dynamic loading metrics retrieved successfully")
})
t.Run("SetPluginCompatibilityRule", func(t *testing.T) {
// This should not panic
manager.SetPluginCompatibilityRule("test-plugin", "^1.0.0")
t.Log("SetPluginCompatibilityRule completed successfully")
})
t.Run("ConfigureDiscovery", func(t *testing.T) {
config := ExtendedDiscoveryConfig{
DiscoveryConfig: DiscoveryConfig{
Enabled: false,
Directories: []string{},
Patterns: []string{"*.so"},
},
}
err := manager.ConfigureDiscovery(config)
// ConfigureDiscovery is now implemented, should succeed with valid config
if err != nil {
t.Errorf("ConfigureDiscovery should succeed with valid config, got error: %v", err)
}
t.Logf("ConfigureDiscovery completed successfully")
})
t.Run("LoadDiscoveredPlugin", func(t *testing.T) {
err := manager.LoadDiscoveredPlugin(ctx, "non-existent-plugin")
// This should return an error since the plugin doesn't exist
if err == nil {
t.Error("LoadDiscoveredPlugin should return error for non-existent plugin")
}
t.Logf("LoadDiscoveredPlugin returned expected error: %v", err)
})
t.Run("UnloadDynamicPlugin", func(t *testing.T) {
err := manager.UnloadDynamicPlugin(ctx, "non-existent-plugin", false)
// This should return an error since the plugin doesn't exist
if err == nil {
t.Error("UnloadDynamicPlugin should return error for non-existent plugin")
}
t.Logf("UnloadDynamicPlugin returned expected error: %v", err)
})
t.Run("DisableDynamicLoading", func(t *testing.T) {
err := manager.DisableDynamicLoading()
// This should work
if err != nil {
t.Logf("DisableDynamicLoading returned error: %v", err)
}
})
// Test shutdown
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 2*time.Second)
defer shutdownCancel()
if err := manager.Shutdown(shutdownCtx); err != nil {
t.Errorf("Manager shutdown failed: %v", err)
}
}
// TestManager_DynamicLoadingAfterShutdown tests behavior after shutdown
func TestManager_DynamicLoadingAfterShutdown(t *testing.T) {
logger := NewLogger(nil)
manager := NewManager[TestRequest, TestResponse](logger)
// Shutdown the manager first
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := manager.Shutdown(ctx); err != nil {
t.Fatalf("Manager shutdown failed: %v", err)
}
// Now test that methods return appropriate errors
t.Run("EnableDynamicLoading_AfterShutdown", func(t *testing.T) {
err := manager.EnableDynamicLoading(ctx)
if err == nil || err.Error() != "manager is shut down" {
t.Errorf("Expected 'manager is shut down' error, got: %v", err)
}
})
t.Run("DisableDynamicLoading_AfterShutdown", func(t *testing.T) {
err := manager.DisableDynamicLoading()
if err == nil || err.Error() != "manager is shut down" {
t.Errorf("Expected 'manager is shut down' error, got: %v", err)
}
})
t.Run("ConfigureDiscovery_AfterShutdown", func(t *testing.T) {
config := ExtendedDiscoveryConfig{}
err := manager.ConfigureDiscovery(config)
if err == nil || err.Error() != "manager is shut down" {
t.Errorf("Expected 'manager is shut down' error, got: %v", err)
}
})
t.Run("LoadDiscoveredPlugin_AfterShutdown", func(t *testing.T) {
err := manager.LoadDiscoveredPlugin(ctx, "test")
if err == nil || err.Error() != "manager is shut down" {
t.Errorf("Expected 'manager is shut down' error, got: %v", err)
}
})
t.Run("UnloadDynamicPlugin_AfterShutdown", func(t *testing.T) {
err := manager.UnloadDynamicPlugin(ctx, "test", false)
if err == nil || err.Error() != "manager is shut down" {
t.Errorf("Expected 'manager is shut down' error, got: %v", err)
}
})
// Methods that should work even after shutdown
t.Run("GetDiscoveredPlugins_AfterShutdown", func(t *testing.T) {
plugins := manager.GetDiscoveredPlugins()
if len(plugins) != 0 {
t.Error("GetDiscoveredPlugins should return empty map after shutdown")
}
})
t.Run("GetDynamicLoadingStatus_AfterShutdown", func(t *testing.T) {
status := manager.GetDynamicLoadingStatus()
if len(status) != 0 {
t.Error("GetDynamicLoadingStatus should return empty map after shutdown")
}
})
t.Run("GetDependencyGraph_AfterShutdown", func(t *testing.T) {
graph := manager.GetDependencyGraph()
if graph == nil {
t.Error("GetDependencyGraph should return empty graph, not nil")
}
})
t.Run("GetDynamicLoadingMetrics_AfterShutdown", func(t *testing.T) {
// Should return zero metrics - just verify we can call it without panic
manager.GetDynamicLoadingMetrics()
})
t.Run("SetPluginCompatibilityRule_AfterShutdown", func(t *testing.T) {
// This should return early without panic
manager.SetPluginCompatibilityRule("test", "^1.0.0")
})
}