-
Notifications
You must be signed in to change notification settings - Fork 3
/
engine_test.go
259 lines (197 loc) · 6.21 KB
/
engine_test.go
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
package gokaf
import (
"os"
"testing"
"time"
)
func TestEngineStop(t *testing.T) {
// Create a new Engine with a mock logger
engine := NewEngine(mockLogger)
// Simulate an interrupt signal to trigger the Stop method
go func() {
time.Sleep(100 * time.Millisecond)
engine.Stop()
}()
engine.wg.Wait()
// Check if the logger was called with the expected message
expectedMsg := "Engine Shutting Down"
logs := getMockLogs()
last := logs[len(logs)-1]
if last != expectedMsg {
t.Errorf("Expected log message: %s, got: %s", expectedMsg, last)
}
}
func TestInterruptSignalHandling(t *testing.T) {
// Create a new Engine with a mock logger
engine := NewEngine(mockLogger)
// Simulate sending a kill signal to the Engine after a short delay
go func() {
time.Sleep(100 * time.Millisecond)
engine.sigChan <- os.Interrupt
}()
engine.wg.Wait()
// Check if the logger was called with the expected message
logs := getMockLogs()
expectedMsg := "Received signal: interrupt"
secondLast := logs[len(logs)-2]
if secondLast != expectedMsg {
t.Errorf("Expected log message: %s, got: %s", expectedMsg, secondLast)
}
expectedMsg = "Engine Shutting Down"
last := logs[len(logs)-1]
if last != expectedMsg {
t.Errorf("Expected log message: %s, got: %s", expectedMsg, last)
}
}
func TestTopicExists(t *testing.T) {
engine := NewEngine(mockLogger)
// Example: Register a topic
topicName := "exampleTopic"
err := engine.RegisterTopic(topicName, 0)
if err != nil {
t.Errorf("Unexpected error while registering a new topic: %v", err)
}
// Test: Check if a topic exists
t.Run("ExistingTopic", func(t *testing.T) {
if !engine.TopicExists(topicName) {
t.Error("Expected the topic to exist, but it doesn't.")
}
})
// Test: Check if nonExistingTopic exists
t.Run("NonExistingTopic", func(t *testing.T) {
if engine.TopicExists("nonExistingTopic") {
t.Error("Expected the topic not to exist, but it does.")
}
})
}
func TestRegisterTopic(t *testing.T) {
engine := NewEngine(mockLogger)
// Test: Register a new topic
t.Run("RegisterNewTopic", func(t *testing.T) {
topicName := "newTopic"
err := engine.RegisterTopic(topicName, 0)
if err != nil {
t.Errorf("Unexpected error while registering a new topic: %v", err)
}
if !engine.TopicExists(topicName) {
t.Error("Expected the newly registered topic to exist, but it doesn't.")
}
})
// Test: Attempt to register an existing topic
t.Run("RegisterExistingTopic", func(t *testing.T) {
topicName := "existingTopic"
err := engine.RegisterTopic(topicName, 0)
if err != nil {
t.Errorf("Unexpected error while registering a new topic: %v", err)
}
err = engine.RegisterTopic(topicName, 0)
if err == nil {
t.Error("Expected an error when attempting to register an existing topic, but got nil.")
}
expectedError := newTopicExistsError(topicName).Error()
if err.Error() != expectedError {
t.Errorf("Expected error message '%s', but got '%s'", expectedError, err.Error())
}
})
}
func TestGetProducer(t *testing.T) {
// Create a new instance of your Engine
engine := NewEngine(mockLogger)
t.Run("GetProducerNoneExistingTopic", func(t *testing.T) {
// Test case 1: Non-existing topic
topicName := "nonexistentTopic"
producer1, err1 := engine.GetProducer(topicName)
if err1 == nil {
t.Errorf("Unexpected error for non-existing topic: %v", err1)
}
if producer1 != nil {
t.Error("Expected a nil producer for non-existing topic, but got non-nil")
}
})
t.Run("GetProducerExistingTopic", func(t *testing.T) {
// Test case 2: Existing topic
existingTopic := "existingTopic"
err := engine.RegisterTopic(existingTopic, 0)
if err != nil {
t.Errorf("Unexpected error while registering a new topic: %v", err)
}
producer2, err2 := engine.GetProducer(existingTopic)
if err2 != nil {
t.Errorf("Expected error for existing topic %s: %v", existingTopic, err2)
}
if producer2 == nil {
t.Error("Expected a producer for existing topic, but got nil")
}
})
}
func TestGetConsumer(t *testing.T) {
// Create a new instance of your Engine
engine := NewEngine(mockLogger)
// Mock handler function for the consumer
mockHandler := func(interface{}) {}
t.Run("GetConsumerNoneExistingTopic", func(t *testing.T) {
// Test case 1: Non-existing topic
topicName := "nonexistentTopic"
consumer1, err1 := engine.GetConsumer(topicName, mockHandler)
if err1 == nil {
t.Errorf("Unexpected error for non-existing topic: %v", err1)
}
if consumer1 != nil {
t.Error("Expected a nil consumer for non-existing topic, but got non-nil")
}
})
t.Run("GetConsumerExistingTopic", func(t *testing.T) {
// Test case 2: Existing topic
existingTopic := "existingTopic"
err := engine.RegisterTopic(existingTopic, 0)
if err != nil {
t.Errorf("Unexpected error while registering a new topic: %v", err)
}
consumer2, err2 := engine.GetConsumer(existingTopic, mockHandler)
if err2 != nil {
t.Errorf("Expected error for existing topic %s: %v", existingTopic, err2)
}
if consumer2 == nil {
t.Error("Expected a consumer for existing topic, but got nil")
}
})
}
func TestProducerConsumer(t *testing.T) {
// Create a new instance of your Engine
engine := NewEngine(mockLogger)
topicName := "testTopic"
err := engine.RegisterTopic(topicName, 0)
if err != nil {
t.Errorf("Unexpected error while registering a new topic: %v", err)
}
sentMsg := "Go is awesome"
done := make(chan struct{})
// Mock handler function for the consumer
mockHandler := func(receivedMsg interface{}) {
defer close(done)
if receivedMsg != sentMsg {
t.Errorf("Expected message %v, got %v", sentMsg, receivedMsg)
}
}
consumer, err1 := engine.GetConsumer(topicName, mockHandler)
if err1 != nil {
t.Errorf("Unexpected error while getting a consumer for topic %s: %v", topicName, err1)
}
producer, err2 := engine.GetProducer(topicName)
if err2 != nil {
t.Errorf("Unexpected error while getting a producer for topic %s: %v", topicName, err2)
}
consumer.Run()
go func() {
err3 := producer.Publish(sentMsg)
if err3 != nil {
t.Errorf("Error publishing message1: %v", err3)
}
}()
select {
case <-done:
// The consumer has finished closing
case <-time.After(time.Second):
t.Error("Timed out waiting for consumer to close")
}
}