-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbdd_multi_engine_error_isolation_test.go
More file actions
204 lines (167 loc) · 5.92 KB
/
bdd_multi_engine_error_isolation_test.go
File metadata and controls
204 lines (167 loc) · 5.92 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
package eventbus
import (
"context"
"fmt"
"time"
)
// ==============================================================================
// MULTI-ENGINE ERROR ISOLATION
// ==============================================================================
// This file handles error isolation scenarios in multi-engine configurations.
// Additional simplified implementations
func (ctx *EventBusBDDTestContext) iHaveMultipleEnginesConfigured() error {
err := ctx.iHaveAMultiEngineEventbusConfiguration()
if err != nil {
return err
}
// Initialize the eventbus module to set up the service
return ctx.theEventbusModuleIsInitialized()
}
func (ctx *EventBusBDDTestContext) oneEngineEncountersAnError() error {
ctx.mutex.Lock()
defer ctx.mutex.Unlock()
if ctx.service == nil {
return fmt.Errorf("no eventbus service available")
}
// Ensure service is started before trying to publish
if !ctx.service.isStarted.Load() {
err := ctx.service.Start(context.Background())
if err != nil {
return fmt.Errorf("failed to start eventbus: %w", err)
}
}
// Simulate an error condition by trying to publish to a topic that would route to an unavailable engine
// For example, redis.error topic if redis engine is not configured or available
errorTopic := "redis.error.simulation"
// Store the error for verification in other steps
err := ctx.service.Publish(context.Background(), errorTopic, map[string]interface{}{
"test": "error-simulation",
"error": true,
})
// Store the error (might be nil if fallback works)
ctx.lastError = err
// For BDD testing, we simulate error by attempting to use unavailable engines
// The error might not occur if fallback routing is working properly
ctx.errorTopic = errorTopic
return nil
}
func (ctx *EventBusBDDTestContext) otherEnginesShouldContinueOperatingNormally() error {
ctx.mutex.Lock()
defer ctx.mutex.Unlock()
// Test that other engines (not the failing one) continue to work normally
testTopics := []string{"memory.normal", "user.normal", "auth.normal"}
for _, topic := range testTopics {
// Skip the error topic if it matches our test topics
if topic == ctx.errorTopic {
continue
}
// Test subscription
received := make(chan bool, 1)
subscription, err := ctx.service.Subscribe(context.Background(), topic, func(ctx context.Context, event Event) error {
select {
case received <- true:
default:
}
return nil
})
if err != nil {
return fmt.Errorf("failed to subscribe to working engine topic %s: %w", topic, err)
}
// Test publishing
err = ctx.service.Publish(context.Background(), topic, map[string]interface{}{
"test": "normal-operation",
"topic": topic,
})
if err != nil {
_ = subscription.Cancel()
return fmt.Errorf("failed to publish to working engine topic %s: %w", topic, err)
}
// Verify event is received
select {
case <-received:
// Good - engine is working normally
case <-time.After(1 * time.Second):
_ = subscription.Cancel()
return fmt.Errorf("event not received on working engine topic %s", topic)
}
// Clean up
_ = subscription.Cancel()
}
return nil
}
func (ctx *EventBusBDDTestContext) theErrorShouldBeIsolatedToFailingEngine() error {
ctx.mutex.Lock()
defer ctx.mutex.Unlock()
// Verify that the error from one engine doesn't affect other engines
// This is verified by ensuring:
// 1. The error topic (if any) doesn't prevent other topics from working
// 2. System-wide operations like creating subscriptions still work
// 3. New subscriptions can still be created
// Test that we can still perform basic operations (creating subscriptions)
testTopic := "isolation.test.before"
testSub, err := ctx.service.Subscribe(context.Background(), testTopic, func(ctx context.Context, event Event) error {
return nil
})
if err != nil {
return fmt.Errorf("system-wide operation failed due to engine error: %w", err)
}
if testSub != nil {
_ = testSub.Cancel()
}
// Test that new subscriptions can still be created
testTopic2 := "isolation.test"
subscription, err := ctx.service.Subscribe(context.Background(), testTopic2, func(ctx context.Context, event Event) error {
return nil
})
if err != nil {
return fmt.Errorf("failed to create new subscription after engine error: %w", err)
}
// Test that publishing to non-failing engines still works
err = ctx.service.Publish(context.Background(), testTopic2, map[string]interface{}{
"test": "error-isolation",
})
if err != nil {
_ = subscription.Cancel()
return fmt.Errorf("failed to publish after engine error: %w", err)
}
// Clean up
_ = subscription.Cancel()
// If we had an error from the failing engine, verify it didn't propagate
if ctx.lastError != nil && ctx.errorTopic != "" {
// The error should be contained - we should still be able to use other functionality
// This is implicitly tested by the successful operations above
}
return nil
}
func (ctx *EventBusBDDTestContext) iHaveSubscriptionsAcrossMultipleEngines() error {
// Set up multi-engine configuration first
err := ctx.iHaveAMultiEngineEventbusConfiguration()
if err != nil {
return err
}
// Initialize the service
err = ctx.theEventbusModuleIsInitialized()
if err != nil {
return err
}
// Now subscribe to topics on different engines
return ctx.iSubscribeToTopicsOnDifferentEngines()
}
func (ctx *EventBusBDDTestContext) iQueryForActiveTopics() error {
ctx.activeTopics = ctx.service.Topics()
return nil
}
func (ctx *EventBusBDDTestContext) allTopicsFromAllEnginesShouldBeReturned() error {
if len(ctx.activeTopics) < 2 {
return fmt.Errorf("expected at least 2 active topics, got %d", len(ctx.activeTopics))
}
return nil
}
func (ctx *EventBusBDDTestContext) subscriberCountsShouldBeAggregatedCorrectly() error {
// Calculate the total subscriber count
totalCount := ctx.service.SubscriberCount("user.created") + ctx.service.SubscriberCount("analytics.pageview")
if totalCount != 2 {
return fmt.Errorf("expected total count of 2, got %d", totalCount)
}
return nil
}