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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
package integration
import (
"context"
"fmt"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/mq/agent"
"github.com/seaweedfs/seaweedfs/weed/mq/client/pub_client"
"github.com/seaweedfs/seaweedfs/weed/mq/client/sub_client"
"github.com/seaweedfs/seaweedfs/weed/mq/schema"
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
// TestEnvironment holds the configuration for the test environment
type TestEnvironment struct {
Masters []string
Brokers []string
Filers []string
TestTimeout time.Duration
CleanupFuncs []func()
mutex sync.Mutex
}
// IntegrationTestSuite provides the base test framework
type IntegrationTestSuite struct {
env *TestEnvironment
agents map[string]*agent.MessageQueueAgent
publishers map[string]*pub_client.TopicPublisher
subscribers map[string]*sub_client.TopicSubscriber
subCancels map[string]context.CancelFunc
cleanupOnce sync.Once
t *testing.T
}
// NewIntegrationTestSuite creates a new test suite instance
func NewIntegrationTestSuite(t *testing.T) *IntegrationTestSuite {
env := &TestEnvironment{
Masters: getEnvList("SEAWEED_MASTERS", []string{"localhost:19333"}),
Brokers: getEnvList("SEAWEED_BROKERS", []string{"localhost:17777"}),
Filers: getEnvList("SEAWEED_FILERS", []string{"localhost:18888"}),
TestTimeout: getEnvDuration("GO_TEST_TIMEOUT", 30*time.Minute),
}
return &IntegrationTestSuite{
env: env,
agents: make(map[string]*agent.MessageQueueAgent),
publishers: make(map[string]*pub_client.TopicPublisher),
subscribers: make(map[string]*sub_client.TopicSubscriber),
subCancels: make(map[string]context.CancelFunc),
t: t,
}
}
// Setup initializes the test environment
func (its *IntegrationTestSuite) Setup() error {
// Wait for cluster to be ready
if err := its.waitForClusterReady(); err != nil {
return fmt.Errorf("cluster not ready: %v", err)
}
// Register cleanup
its.t.Cleanup(its.Cleanup)
return nil
}
// Cleanup performs cleanup operations
func (its *IntegrationTestSuite) Cleanup() {
its.cleanupOnce.Do(func() {
// Close all subscribers first (they use context cancellation)
for name := range its.subscribers {
if cancel, ok := its.subCancels[name]; ok && cancel != nil {
cancel()
its.t.Logf("Cancelled subscriber context: %s", name)
}
its.t.Logf("Cleaned up subscriber: %s", name)
}
// Wait a moment for gRPC connections to close gracefully
time.Sleep(1 * time.Second)
// Close all publishers
for name, publisher := range its.publishers {
if publisher != nil {
// Add timeout to prevent deadlock during shutdown
done := make(chan bool, 1)
go func(p *pub_client.TopicPublisher, n string) {
p.Shutdown()
done <- true
}(publisher, name)
select {
case <-done:
its.t.Logf("Cleaned up publisher: %s", name)
case <-time.After(5 * time.Second):
its.t.Logf("Publisher shutdown timed out: %s", name)
}
}
}
// Execute additional cleanup functions
its.env.mutex.Lock()
for _, cleanup := range its.env.CleanupFuncs {
cleanup()
}
its.env.mutex.Unlock()
})
}
// CreatePublisher creates a new topic publisher
func (its *IntegrationTestSuite) CreatePublisher(config *PublisherTestConfig) (*pub_client.TopicPublisher, error) {
publisherConfig := &pub_client.PublisherConfiguration{
Topic: topic.NewTopic(config.Namespace, config.TopicName),
PartitionCount: config.PartitionCount,
Brokers: its.env.Brokers,
PublisherName: config.PublisherName,
RecordType: config.RecordType,
}
publisher, err := pub_client.NewTopicPublisher(publisherConfig)
if err != nil {
return nil, fmt.Errorf("failed to create publisher: %v", err)
}
its.publishers[config.PublisherName] = publisher
return publisher, nil
}
// CreateSubscriber creates a new topic subscriber
func (its *IntegrationTestSuite) CreateSubscriber(config *SubscriberTestConfig) (*sub_client.TopicSubscriber, error) {
subscriberConfig := &sub_client.SubscriberConfiguration{
ConsumerGroup: config.ConsumerGroup,
ConsumerGroupInstanceId: config.ConsumerInstanceId,
GrpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials()),
MaxPartitionCount: config.MaxPartitionCount,
SlidingWindowSize: config.SlidingWindowSize,
}
contentConfig := &sub_client.ContentConfiguration{
Topic: topic.NewTopic(config.Namespace, config.TopicName),
Filter: config.Filter,
PartitionOffsets: config.PartitionOffsets,
OffsetType: config.OffsetType,
OffsetTsNs: config.OffsetTsNs,
}
offsetChan := make(chan sub_client.KeyedOffset, 1024)
ctx, cancel := context.WithCancel(context.Background())
subscriber := sub_client.NewTopicSubscriber(
ctx,
its.env.Brokers,
subscriberConfig,
contentConfig,
offsetChan,
)
its.subscribers[config.ConsumerInstanceId] = subscriber
its.subCancels[config.ConsumerInstanceId] = cancel
return subscriber, nil
}
// CreateAgent creates a new message queue agent
func (its *IntegrationTestSuite) CreateAgent(name string) (*agent.MessageQueueAgent, error) {
var brokerAddresses []pb.ServerAddress
for _, broker := range its.env.Brokers {
brokerAddresses = append(brokerAddresses, pb.ServerAddress(broker))
}
agentOptions := &agent.MessageQueueAgentOptions{
SeedBrokers: brokerAddresses,
}
mqAgent := agent.NewMessageQueueAgent(
agentOptions,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
its.agents[name] = mqAgent
return mqAgent, nil
}
// PublisherTestConfig holds configuration for creating test publishers
type PublisherTestConfig struct {
Namespace string
TopicName string
PartitionCount int32
PublisherName string
RecordType *schema_pb.RecordType
}
// SubscriberTestConfig holds configuration for creating test subscribers
type SubscriberTestConfig struct {
Namespace string
TopicName string
ConsumerGroup string
ConsumerInstanceId string
MaxPartitionCount int32
SlidingWindowSize int32
Filter string
PartitionOffsets []*schema_pb.PartitionOffset
OffsetType schema_pb.OffsetType
OffsetTsNs int64
}
// TestMessage represents a test message with metadata
type TestMessage struct {
ID string
Content []byte
Timestamp time.Time
Key []byte
}
// MessageCollector collects received messages for verification
type MessageCollector struct {
messages []TestMessage
mutex sync.RWMutex
waitCh chan struct{}
expected int
closed bool // protect against closing waitCh multiple times
}
// NewMessageCollector creates a new message collector
func NewMessageCollector(expectedCount int) *MessageCollector {
return &MessageCollector{
messages: make([]TestMessage, 0),
waitCh: make(chan struct{}),
expected: expectedCount,
}
}
// AddMessage adds a received message to the collector
func (mc *MessageCollector) AddMessage(msg TestMessage) {
mc.mutex.Lock()
defer mc.mutex.Unlock()
mc.messages = append(mc.messages, msg)
if len(mc.messages) >= mc.expected && !mc.closed {
close(mc.waitCh)
mc.closed = true
}
}
// WaitForMessages waits for the expected number of messages or timeout
func (mc *MessageCollector) WaitForMessages(timeout time.Duration) []TestMessage {
select {
case <-mc.waitCh:
case <-time.After(timeout):
}
mc.mutex.RLock()
defer mc.mutex.RUnlock()
result := make([]TestMessage, len(mc.messages))
copy(result, mc.messages)
return result
}
// GetMessages returns all collected messages
func (mc *MessageCollector) GetMessages() []TestMessage {
mc.mutex.RLock()
defer mc.mutex.RUnlock()
result := make([]TestMessage, len(mc.messages))
copy(result, mc.messages)
return result
}
// CreateTestSchema creates a simple test schema
func CreateTestSchema() *schema_pb.RecordType {
return schema.RecordTypeBegin().
WithField("id", schema.TypeString).
WithField("timestamp", schema.TypeInt64).
WithField("content", schema.TypeString).
WithField("sequence", schema.TypeInt32).
RecordTypeEnd()
}
// CreateComplexTestSchema creates a complex test schema with nested structures
func CreateComplexTestSchema() *schema_pb.RecordType {
addressType := schema.RecordTypeBegin().
WithField("street", schema.TypeString).
WithField("city", schema.TypeString).
WithField("zipcode", schema.TypeString).
RecordTypeEnd()
return schema.RecordTypeBegin().
WithField("user_id", schema.TypeString).
WithField("name", schema.TypeString).
WithField("age", schema.TypeInt32).
WithField("emails", schema.ListOf(schema.TypeString)).
WithRecordField("address", addressType).
WithField("created_at", schema.TypeInt64).
RecordTypeEnd()
}
// Helper functions
func getEnvList(key string, defaultValue []string) []string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return strings.Split(value, ",")
}
func getEnvDuration(key string, defaultValue time.Duration) time.Duration {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
duration, err := time.ParseDuration(value)
if err != nil {
return defaultValue
}
return duration
}
func (its *IntegrationTestSuite) waitForClusterReady() error {
maxRetries := 30
retryInterval := 2 * time.Second
for i := 0; i < maxRetries; i++ {
if its.isClusterReady() {
return nil
}
its.t.Logf("Waiting for cluster to be ready... attempt %d/%d", i+1, maxRetries)
time.Sleep(retryInterval)
}
return fmt.Errorf("cluster not ready after %d attempts", maxRetries)
}
func (its *IntegrationTestSuite) isClusterReady() bool {
// Check if at least one broker is accessible
for _, broker := range its.env.Brokers {
if its.isBrokerReady(broker) {
return true
}
}
return false
}
func (its *IntegrationTestSuite) isBrokerReady(broker string) bool {
// Simple connection test
conn, err := grpc.NewClient(broker, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return false
}
defer conn.Close()
// TODO: Add actual health check call here
return true
}
// AssertMessagesReceived verifies that expected messages were received
func (its *IntegrationTestSuite) AssertMessagesReceived(t *testing.T, collector *MessageCollector, expectedCount int, timeout time.Duration) {
messages := collector.WaitForMessages(timeout)
require.Len(t, messages, expectedCount, "Expected %d messages, got %d", expectedCount, len(messages))
}
// AssertMessageOrdering verifies that messages are received in the expected order
func (its *IntegrationTestSuite) AssertMessageOrdering(t *testing.T, messages []TestMessage) {
for i := 1; i < len(messages); i++ {
require.True(t, messages[i].Timestamp.After(messages[i-1].Timestamp) || messages[i].Timestamp.Equal(messages[i-1].Timestamp),
"Messages not in chronological order: message %d timestamp %v should be >= message %d timestamp %v",
i, messages[i].Timestamp, i-1, messages[i-1].Timestamp)
}
}
|