forked from GoCodeAlone/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_emission_fix_test.go
More file actions
215 lines (179 loc) · 8.7 KB
/
event_emission_fix_test.go
File metadata and controls
215 lines (179 loc) · 8.7 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
package modular
import (
"context"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// TestModuleEventEmissionWithoutSubject tests that all modules handle missing subjects gracefully
// without printing noisy error messages to stdout during tests.
func TestModuleEventEmissionWithoutSubject(t *testing.T) {
t.Run("chimux module handles nil subject gracefully", func(t *testing.T) {
testModuleNilSubjectHandling(t, "modules/chimux", "chimux")
})
t.Run("scheduler module handles nil subject gracefully", func(t *testing.T) {
testModuleNilSubjectHandling(t, "modules/scheduler", "scheduler")
})
t.Run("letsencrypt module handles nil subject gracefully", func(t *testing.T) {
testModuleNilSubjectHandling(t, "modules/letsencrypt", "letsencrypt")
})
t.Run("reverseproxy module handles nil subject gracefully", func(t *testing.T) {
testModuleNilSubjectHandling(t, "modules/reverseproxy", "reverseproxy")
})
t.Run("database module handles nil subject gracefully", func(t *testing.T) {
testModuleNilSubjectHandling(t, "modules/database", "database")
})
t.Run("eventbus module handles nil subject gracefully", func(t *testing.T) {
testModuleNilSubjectHandling(t, "modules/eventbus", "eventbus")
})
t.Run("cache module handles nil subject gracefully", func(t *testing.T) {
testModuleNilSubjectHandling(t, "modules/cache", "cache")
})
t.Run("httpserver module handles nil subject gracefully", func(t *testing.T) {
testModuleNilSubjectHandling(t, "modules/httpserver", "httpserver")
})
t.Run("httpclient module handles nil subject gracefully", func(t *testing.T) {
testModuleNilSubjectHandling(t, "modules/httpclient", "httpclient")
})
t.Run("auth module handles nil subject gracefully", func(t *testing.T) {
testModuleNilSubjectHandling(t, "modules/auth", "auth")
})
t.Run("jsonschema module handles nil subject gracefully", func(t *testing.T) {
testModuleNilSubjectHandling(t, "modules/jsonschema", "jsonschema")
})
t.Run("eventlogger module handles nil subject gracefully", func(t *testing.T) {
testModuleNilSubjectHandling(t, "modules/eventlogger", "eventlogger")
})
}
// testModuleNilSubjectHandling is a helper function that tests nil subject handling for a specific module
func testModuleNilSubjectHandling(t *testing.T, modulePath, moduleName string) {
// Create a mock application for testing
app := &mockApplicationForNilSubjectTest{}
// Create a test module that implements ObservableModule but has no subject registered
testModule := &testObservableModuleForNilSubject{
moduleName: moduleName,
app: app,
}
// This should not cause any panic or noisy output
err := testModule.EmitEvent(context.Background(), NewCloudEvent("test.event", "test-module", nil, nil))
// The error should be handled gracefully - either nil (if module checks before emitting)
// or the expected "no subject available" error
if err != nil {
assert.Equal(t, "no subject available for event emission", err.Error(),
"Module %s should return the expected error message when no subject is available", moduleName)
}
// Test the emitEvent helper pattern - this should not panic and should handle nil subject gracefully
// We can't call the actual module's emitEvent helper directly since it's private,
// but we can verify the pattern works by testing that no panic occurs
testModule.testEmitEventHelper(context.Background(), "test.event.type", map[string]interface{}{
"test_key": "test_value",
})
}
// TestHandleEventEmissionErrorUtility tests the utility function for consistent error handling
func TestHandleEventEmissionErrorUtility(t *testing.T) {
// Test with ErrNoSubjectForEventEmission
handled := HandleEventEmissionError(ErrNoSubjectForEventEmission, nil, "test-module", "test.event")
assert.True(t, handled, "Should handle ErrNoSubjectForEventEmission error")
// Test with string-based error message (for backward compatibility with module-specific errors)
err := &testEmissionError{message: "no subject available for event emission"}
handled = HandleEventEmissionError(err, nil, "test-module", "test.event")
assert.True(t, handled, "Should handle 'no subject available' error message")
// Test with other error and no logger
err = &testEmissionError{message: "some other error"}
handled = HandleEventEmissionError(err, nil, "test-module", "test.event")
assert.False(t, handled, "Should not handle other errors when no logger is available")
// Test with logger
logger := &mockTestLogger{}
err = &testEmissionError{message: "some other error"}
handled = HandleEventEmissionError(err, logger, "test-module", "test.event")
assert.True(t, handled, "Should handle other errors when logger is available")
assert.Equal(t, "Failed to emit event", logger.lastDebugMessage)
}
// Test types for the emission fix tests
type testObservableModuleForNilSubject struct {
subject Subject
moduleName string
app Application
}
func (t *testObservableModuleForNilSubject) RegisterObservers(subject Subject) error {
t.subject = subject
return nil
}
func (t *testObservableModuleForNilSubject) EmitEvent(ctx context.Context, event CloudEvent) error {
if t.subject == nil {
return ErrNoSubjectForEventEmission
}
return t.subject.NotifyObservers(ctx, event)
}
// testEmitEventHelper simulates the pattern used by modules' emitEvent helper methods
func (t *testObservableModuleForNilSubject) testEmitEventHelper(ctx context.Context, eventType string, data map[string]interface{}) {
// This simulates the pattern used in modules - check for nil subject first
if t.subject == nil {
return // Should return silently without error
}
event := NewCloudEvent(eventType, t.moduleName+"-service", data, nil)
if emitErr := t.EmitEvent(ctx, event); emitErr != nil {
// Use the HandleEventEmissionError utility for consistent error handling
if !HandleEventEmissionError(emitErr, nil, t.moduleName, eventType) {
// Handle other types of errors here (in real modules, this might log or handle differently)
}
}
}
type testEmissionError struct {
message string
}
func (e *testEmissionError) Error() string {
return e.message
}
type mockTestLogger struct {
lastDebugMessage string
}
func (l *mockTestLogger) Debug(msg string, args ...interface{}) {
l.lastDebugMessage = msg
}
func (l *mockTestLogger) Info(msg string, args ...interface{}) {}
func (l *mockTestLogger) Warn(msg string, args ...interface{}) {}
func (l *mockTestLogger) Error(msg string, args ...interface{}) {}
type mockApplicationForNilSubjectTest struct{}
func (m *mockApplicationForNilSubjectTest) ConfigProvider() ConfigProvider { return nil }
func (m *mockApplicationForNilSubjectTest) SvcRegistry() ServiceRegistry { return nil }
func (m *mockApplicationForNilSubjectTest) RegisterModule(module Module) {}
func (m *mockApplicationForNilSubjectTest) RegisterConfigSection(section string, cp ConfigProvider) {}
func (m *mockApplicationForNilSubjectTest) ConfigSections() map[string]ConfigProvider { return nil }
func (m *mockApplicationForNilSubjectTest) GetConfigSection(section string) (ConfigProvider, error) {
return nil, ErrConfigSectionNotFound
}
func (m *mockApplicationForNilSubjectTest) RegisterService(name string, service any) error {
return nil
}
func (m *mockApplicationForNilSubjectTest) GetService(name string, target any) error {
return ErrServiceNotFound
}
func (m *mockApplicationForNilSubjectTest) Init() error { return nil }
func (m *mockApplicationForNilSubjectTest) Start() error { return nil }
func (m *mockApplicationForNilSubjectTest) Stop() error { return nil }
func (m *mockApplicationForNilSubjectTest) Run() error { return nil }
func (m *mockApplicationForNilSubjectTest) Logger() Logger { return &mockTestLogger{} }
func (m *mockApplicationForNilSubjectTest) SetLogger(logger Logger) {}
func (m *mockApplicationForNilSubjectTest) SetVerboseConfig(enabled bool) {}
func (m *mockApplicationForNilSubjectTest) IsVerboseConfig() bool { return false }
func (m *mockApplicationForNilSubjectTest) GetServicesByModule(moduleName string) []string {
return nil
}
func (m *mockApplicationForNilSubjectTest) GetServiceEntry(serviceName string) (*ServiceRegistryEntry, bool) {
return nil, false
}
func (m *mockApplicationForNilSubjectTest) GetServicesByInterface(interfaceType reflect.Type) []*ServiceRegistryEntry {
return nil
}
func (m *mockApplicationForNilSubjectTest) StartTime() time.Time {
return time.Time{}
}
func (m *mockApplicationForNilSubjectTest) GetModule(name string) Module {
return nil
}
func (m *mockApplicationForNilSubjectTest) GetAllModules() map[string]Module {
return nil
}
func (m *mockApplicationForNilSubjectTest) OnConfigLoaded(hook func(Application) error) {}