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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
package integration
import (
"context"
"testing"
"time"
)
// Unit tests for new FetchRecords functionality
// TestSeaweedMQHandler_MapSeaweedToKafkaOffsets tests offset mapping logic
func TestSeaweedMQHandler_MapSeaweedToKafkaOffsets(t *testing.T) {
// Note: This test is now obsolete since the ledger system has been removed
// SMQ now uses native offsets directly, so no mapping is needed
t.Skip("Test obsolete: ledger system removed, SMQ uses native offsets")
}
// TestSeaweedMQHandler_MapSeaweedToKafkaOffsets_EmptyRecords tests empty record handling
func TestSeaweedMQHandler_MapSeaweedToKafkaOffsets_EmptyRecords(t *testing.T) {
// Note: This test is now obsolete since the ledger system has been removed
t.Skip("Test obsolete: ledger system removed, SMQ uses native offsets")
}
// TestSeaweedMQHandler_ConvertSeaweedToKafkaRecordBatch tests record batch conversion
func TestSeaweedMQHandler_ConvertSeaweedToKafkaRecordBatch(t *testing.T) {
handler := &SeaweedMQHandler{}
// Create sample records
seaweedRecords := []*SeaweedRecord{
{
Key: []byte("batch-key1"),
Value: []byte("batch-value1"),
Timestamp: 1000000000,
Offset: 0,
},
{
Key: []byte("batch-key2"),
Value: []byte("batch-value2"),
Timestamp: 1000000001,
Offset: 1,
},
}
fetchOffset := int64(0)
maxBytes := int32(1024)
// Test conversion
batchData, err := handler.convertSeaweedToKafkaRecordBatch(seaweedRecords, fetchOffset, maxBytes)
if err != nil {
t.Fatalf("Failed to convert to record batch: %v", err)
}
if len(batchData) == 0 {
t.Errorf("Record batch should not be empty")
}
// Basic validation of record batch structure
if len(batchData) < 61 { // Minimum Kafka record batch header size
t.Errorf("Record batch too small: got %d bytes", len(batchData))
}
// Verify magic byte (should be 2 for version 2)
magicByte := batchData[16] // Magic byte is at offset 16
if magicByte != 2 {
t.Errorf("Invalid magic byte: got %d, want 2", magicByte)
}
t.Logf("Successfully converted %d records to %d byte batch", len(seaweedRecords), len(batchData))
}
// TestSeaweedMQHandler_ConvertSeaweedToKafkaRecordBatch_EmptyRecords tests empty batch handling
func TestSeaweedMQHandler_ConvertSeaweedToKafkaRecordBatch_EmptyRecords(t *testing.T) {
handler := &SeaweedMQHandler{}
batchData, err := handler.convertSeaweedToKafkaRecordBatch([]*SeaweedRecord{}, 0, 1024)
if err != nil {
t.Errorf("Converting empty records should not fail: %v", err)
}
if len(batchData) != 0 {
t.Errorf("Empty record batch should be empty, got %d bytes", len(batchData))
}
}
// TestSeaweedMQHandler_ConvertSingleSeaweedRecord tests individual record conversion
func TestSeaweedMQHandler_ConvertSingleSeaweedRecord(t *testing.T) {
handler := &SeaweedMQHandler{}
testCases := []struct {
name string
record *SeaweedRecord
index int64
base int64
}{
{
name: "Record with key and value",
record: &SeaweedRecord{
Key: []byte("test-key"),
Value: []byte("test-value"),
Timestamp: 1000000000,
Offset: 5,
},
index: 0,
base: 5,
},
{
name: "Record with null key",
record: &SeaweedRecord{
Key: nil,
Value: []byte("test-value-no-key"),
Timestamp: 1000000001,
Offset: 6,
},
index: 1,
base: 5,
},
{
name: "Record with empty value",
record: &SeaweedRecord{
Key: []byte("test-key-empty-value"),
Value: []byte{},
Timestamp: 1000000002,
Offset: 7,
},
index: 2,
base: 5,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
recordData := handler.convertSingleSeaweedRecord(tc.record, tc.index, tc.base)
if len(recordData) == 0 {
t.Errorf("Record data should not be empty")
}
// Basic validation - should have at least attributes, timestamp delta, offset delta, key length, value length, headers count
if len(recordData) < 6 {
t.Errorf("Record data too small: got %d bytes", len(recordData))
}
// Verify record structure
pos := 0
// Attributes (1 byte)
if recordData[pos] != 0 {
t.Errorf("Expected attributes to be 0, got %d", recordData[pos])
}
pos++
// Timestamp delta (1 byte simplified)
pos++
// Offset delta (1 byte simplified)
if recordData[pos] != byte(tc.index) {
t.Errorf("Expected offset delta %d, got %d", tc.index, recordData[pos])
}
pos++
t.Logf("Successfully converted single record: %d bytes", len(recordData))
})
}
}
// Integration tests
// TestSeaweedMQHandler_Creation tests handler creation and shutdown
func TestSeaweedMQHandler_Creation(t *testing.T) {
// Skip if no real broker available
t.Skip("Integration test requires real SeaweedMQ Broker - run manually with broker available")
handler, err := NewSeaweedMQBrokerHandler("localhost:9333", "default", "localhost")
if err != nil {
t.Fatalf("Failed to create SeaweedMQ handler: %v", err)
}
defer handler.Close()
// Test basic operations
topics := handler.ListTopics()
if topics == nil {
t.Errorf("ListTopics returned nil")
}
t.Logf("SeaweedMQ handler created successfully, found %d existing topics", len(topics))
}
// TestSeaweedMQHandler_TopicLifecycle tests topic creation and deletion
func TestSeaweedMQHandler_TopicLifecycle(t *testing.T) {
t.Skip("Integration test requires real SeaweedMQ Broker - run manually with broker available")
handler, err := NewSeaweedMQBrokerHandler("localhost:9333", "default", "localhost")
if err != nil {
t.Fatalf("Failed to create SeaweedMQ handler: %v", err)
}
defer handler.Close()
topicName := "lifecycle-test-topic"
// Initially should not exist
if handler.TopicExists(topicName) {
t.Errorf("Topic %s should not exist initially", topicName)
}
// Create the topic
err = handler.CreateTopic(topicName, 1)
if err != nil {
t.Fatalf("Failed to create topic: %v", err)
}
// Now should exist
if !handler.TopicExists(topicName) {
t.Errorf("Topic %s should exist after creation", topicName)
}
// Get topic info
info, exists := handler.GetTopicInfo(topicName)
if !exists {
t.Errorf("Topic info should exist")
}
if info.Name != topicName {
t.Errorf("Topic name mismatch: got %s, want %s", info.Name, topicName)
}
if info.Partitions != 1 {
t.Errorf("Partition count mismatch: got %d, want 1", info.Partitions)
}
// Try to create again (should fail)
err = handler.CreateTopic(topicName, 1)
if err == nil {
t.Errorf("Creating existing topic should fail")
}
// Delete the topic
err = handler.DeleteTopic(topicName)
if err != nil {
t.Fatalf("Failed to delete topic: %v", err)
}
// Should no longer exist
if handler.TopicExists(topicName) {
t.Errorf("Topic %s should not exist after deletion", topicName)
}
t.Logf("Topic lifecycle test completed successfully")
}
// TestSeaweedMQHandler_ProduceRecord tests message production
func TestSeaweedMQHandler_ProduceRecord(t *testing.T) {
t.Skip("Integration test requires real SeaweedMQ Broker - run manually with broker available")
handler, err := NewSeaweedMQBrokerHandler("localhost:9333", "default", "localhost")
if err != nil {
t.Fatalf("Failed to create SeaweedMQ handler: %v", err)
}
defer handler.Close()
topicName := "produce-test-topic"
// Create topic
err = handler.CreateTopic(topicName, 1)
if err != nil {
t.Fatalf("Failed to create topic: %v", err)
}
defer handler.DeleteTopic(topicName)
// Produce a record
key := []byte("produce-key")
value := []byte("produce-value")
offset, err := handler.ProduceRecord(context.Background(), topicName, 0, key, value)
if err != nil {
t.Fatalf("Failed to produce record: %v", err)
}
if offset < 0 {
t.Errorf("Invalid offset: %d", offset)
}
// Check high water mark from broker (ledgers removed - broker handles offset management)
hwm, err := handler.GetLatestOffset(topicName, 0)
if err != nil {
t.Errorf("Failed to get high water mark: %v", err)
}
if hwm != offset+1 {
t.Errorf("High water mark mismatch: got %d, want %d", hwm, offset+1)
}
t.Logf("Produced record at offset %d, HWM: %d", offset, hwm)
}
// TestSeaweedMQHandler_MultiplePartitions tests multiple partition handling
func TestSeaweedMQHandler_MultiplePartitions(t *testing.T) {
t.Skip("Integration test requires real SeaweedMQ Broker - run manually with broker available")
handler, err := NewSeaweedMQBrokerHandler("localhost:9333", "default", "localhost")
if err != nil {
t.Fatalf("Failed to create SeaweedMQ handler: %v", err)
}
defer handler.Close()
topicName := "multi-partition-test-topic"
numPartitions := int32(3)
// Create topic with multiple partitions
err = handler.CreateTopic(topicName, numPartitions)
if err != nil {
t.Fatalf("Failed to create topic: %v", err)
}
defer handler.DeleteTopic(topicName)
// Produce to different partitions
for partitionID := int32(0); partitionID < numPartitions; partitionID++ {
key := []byte("partition-key")
value := []byte("partition-value")
offset, err := handler.ProduceRecord(context.Background(), topicName, partitionID, key, value)
if err != nil {
t.Fatalf("Failed to produce to partition %d: %v", partitionID, err)
}
// Verify offset from broker (ledgers removed - broker handles offset management)
hwm, err := handler.GetLatestOffset(topicName, partitionID)
if err != nil {
t.Errorf("Failed to get high water mark for partition %d: %v", partitionID, err)
} else if hwm <= offset {
t.Errorf("High water mark should be greater than produced offset for partition %d: hwm=%d, offset=%d", partitionID, hwm, offset)
}
t.Logf("Partition %d: produced at offset %d", partitionID, offset)
}
t.Logf("Multi-partition test completed successfully")
}
// TestSeaweedMQHandler_FetchRecords tests record fetching with real SeaweedMQ data
func TestSeaweedMQHandler_FetchRecords(t *testing.T) {
t.Skip("Integration test requires real SeaweedMQ Broker - run manually with broker available")
handler, err := NewSeaweedMQBrokerHandler("localhost:9333", "default", "localhost")
if err != nil {
t.Fatalf("Failed to create SeaweedMQ handler: %v", err)
}
defer handler.Close()
topicName := "fetch-test-topic"
// Create topic
err = handler.CreateTopic(topicName, 1)
if err != nil {
t.Fatalf("Failed to create topic: %v", err)
}
defer handler.DeleteTopic(topicName)
// Produce some test records with known data
testRecords := []struct {
key string
value string
}{
{"fetch-key-1", "fetch-value-1"},
{"fetch-key-2", "fetch-value-2"},
{"fetch-key-3", "fetch-value-3"},
}
var producedOffsets []int64
for i, record := range testRecords {
offset, err := handler.ProduceRecord(context.Background(), topicName, 0, []byte(record.key), []byte(record.value))
if err != nil {
t.Fatalf("Failed to produce record %d: %v", i, err)
}
producedOffsets = append(producedOffsets, offset)
t.Logf("Produced record %d at offset %d: key=%s, value=%s", i, offset, record.key, record.value)
}
// Wait a bit for records to be available in SeaweedMQ
time.Sleep(500 * time.Millisecond)
// Test fetching from beginning
fetchedBatch, err := handler.FetchRecords(topicName, 0, 0, 2048)
if err != nil {
t.Fatalf("Failed to fetch records: %v", err)
}
if len(fetchedBatch) == 0 {
t.Errorf("No record data fetched - this indicates the FetchRecords implementation is not working properly")
} else {
t.Logf("Successfully fetched %d bytes of real record batch data", len(fetchedBatch))
// Basic validation of Kafka record batch format
if len(fetchedBatch) >= 61 { // Minimum Kafka record batch size
// Check magic byte (at offset 16)
magicByte := fetchedBatch[16]
if magicByte == 2 {
t.Logf("✓ Valid Kafka record batch format detected (magic byte = 2)")
} else {
t.Errorf("Invalid Kafka record batch magic byte: got %d, want 2", magicByte)
}
} else {
t.Errorf("Fetched batch too small to be valid Kafka record batch: %d bytes", len(fetchedBatch))
}
}
// Test fetching from specific offset
if len(producedOffsets) > 1 {
partialBatch, err := handler.FetchRecords(topicName, 0, producedOffsets[1], 1024)
if err != nil {
t.Fatalf("Failed to fetch from specific offset: %v", err)
}
t.Logf("Fetched %d bytes starting from offset %d", len(partialBatch), producedOffsets[1])
}
// Test fetching beyond high water mark (ledgers removed - use broker offset management)
hwm, err := handler.GetLatestOffset(topicName, 0)
if err != nil {
t.Fatalf("Failed to get high water mark: %v", err)
}
emptyBatch, err := handler.FetchRecords(topicName, 0, hwm, 1024)
if err != nil {
t.Fatalf("Failed to fetch from HWM: %v", err)
}
if len(emptyBatch) != 0 {
t.Errorf("Should get empty batch beyond HWM, got %d bytes", len(emptyBatch))
}
t.Logf("✓ Real data fetch test completed successfully - FetchRecords is now working with actual SeaweedMQ data!")
}
// TestSeaweedMQHandler_FetchRecords_ErrorHandling tests error cases for fetching
func TestSeaweedMQHandler_FetchRecords_ErrorHandling(t *testing.T) {
t.Skip("Integration test requires real SeaweedMQ Broker - run manually with broker available")
handler, err := NewSeaweedMQBrokerHandler("localhost:9333", "default", "localhost")
if err != nil {
t.Fatalf("Failed to create SeaweedMQ handler: %v", err)
}
defer handler.Close()
// Test fetching from non-existent topic
_, err = handler.FetchRecords("non-existent-topic", 0, 0, 1024)
if err == nil {
t.Errorf("Fetching from non-existent topic should fail")
}
// Create topic for partition tests
topicName := "fetch-error-test-topic"
err = handler.CreateTopic(topicName, 1)
if err != nil {
t.Fatalf("Failed to create topic: %v", err)
}
defer handler.DeleteTopic(topicName)
// Test fetching from non-existent partition (partition 1 when only 0 exists)
batch, err := handler.FetchRecords(topicName, 1, 0, 1024)
// This may or may not fail depending on implementation, but should return empty batch
if err != nil {
t.Logf("Expected behavior: fetching from non-existent partition failed: %v", err)
} else if len(batch) > 0 {
t.Errorf("Fetching from non-existent partition should return empty batch, got %d bytes", len(batch))
}
// Test with very small maxBytes
_, err = handler.ProduceRecord(context.Background(), topicName, 0, []byte("key"), []byte("value"))
if err != nil {
t.Fatalf("Failed to produce test record: %v", err)
}
time.Sleep(100 * time.Millisecond)
smallBatch, err := handler.FetchRecords(topicName, 0, 0, 1) // Very small maxBytes
if err != nil {
t.Errorf("Fetching with small maxBytes should not fail: %v", err)
}
t.Logf("Fetch with maxBytes=1 returned %d bytes", len(smallBatch))
t.Logf("Error handling test completed successfully")
}
// TestSeaweedMQHandler_ErrorHandling tests error conditions
func TestSeaweedMQHandler_ErrorHandling(t *testing.T) {
t.Skip("Integration test requires real SeaweedMQ Broker - run manually with broker available")
handler, err := NewSeaweedMQBrokerHandler("localhost:9333", "default", "localhost")
if err != nil {
t.Fatalf("Failed to create SeaweedMQ handler: %v", err)
}
defer handler.Close()
// Try to produce to non-existent topic
_, err = handler.ProduceRecord(context.Background(), "non-existent-topic", 0, []byte("key"), []byte("value"))
if err == nil {
t.Errorf("Producing to non-existent topic should fail")
}
// Try to fetch from non-existent topic
_, err = handler.FetchRecords("non-existent-topic", 0, 0, 1024)
if err == nil {
t.Errorf("Fetching from non-existent topic should fail")
}
// Try to delete non-existent topic
err = handler.DeleteTopic("non-existent-topic")
if err == nil {
t.Errorf("Deleting non-existent topic should fail")
}
t.Logf("Error handling test completed successfully")
}
|