aboutsummaryrefslogtreecommitdiff
path: root/weed/mq/offset/subscriber_test.go
blob: 1ab97dadc047d0a723605b3540472ac3ecf76f29 (plain)
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
package offset

import (
	"testing"

	"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
)

func TestOffsetSubscriber_CreateSubscription(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	subscriber := NewOffsetSubscriber(registry)
	partition := createTestPartition()

	// Assign some offsets first
	registry.AssignOffsets("test-namespace", "test-topic", partition, 10)

	// Test EXACT_OFFSET subscription
	sub, err := subscriber.CreateSubscription("test-sub-1", "test-namespace", "test-topic", partition, schema_pb.OffsetType_EXACT_OFFSET, 5)
	if err != nil {
		t.Fatalf("Failed to create EXACT_OFFSET subscription: %v", err)
	}

	if sub.StartOffset != 5 {
		t.Errorf("Expected start offset 5, got %d", sub.StartOffset)
	}
	if sub.CurrentOffset != 5 {
		t.Errorf("Expected current offset 5, got %d", sub.CurrentOffset)
	}

	// Test RESET_TO_LATEST subscription
	sub2, err := subscriber.CreateSubscription("test-sub-2", "test-namespace", "test-topic", partition, schema_pb.OffsetType_RESET_TO_LATEST, 0)
	if err != nil {
		t.Fatalf("Failed to create RESET_TO_LATEST subscription: %v", err)
	}

	if sub2.StartOffset != 10 { // Should be at high water mark
		t.Errorf("Expected start offset 10, got %d", sub2.StartOffset)
	}
}

func TestOffsetSubscriber_InvalidSubscription(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	subscriber := NewOffsetSubscriber(registry)
	partition := createTestPartition()

	// Assign some offsets
	registry.AssignOffsets("test-namespace", "test-topic", partition, 5)

	// Test invalid offset (beyond high water mark)
	_, err := subscriber.CreateSubscription("invalid-sub", "test-namespace", "test-topic", partition, schema_pb.OffsetType_EXACT_OFFSET, 10)
	if err == nil {
		t.Error("Expected error for offset beyond high water mark")
	}

	// Test negative offset
	_, err = subscriber.CreateSubscription("invalid-sub-2", "test-namespace", "test-topic", partition, schema_pb.OffsetType_EXACT_OFFSET, -1)
	if err == nil {
		t.Error("Expected error for negative offset")
	}
}

func TestOffsetSubscriber_DuplicateSubscription(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	subscriber := NewOffsetSubscriber(registry)
	partition := createTestPartition()

	// Create first subscription
	_, err := subscriber.CreateSubscription("duplicate-sub", "test-namespace", "test-topic", partition, schema_pb.OffsetType_RESET_TO_EARLIEST, 0)
	if err != nil {
		t.Fatalf("Failed to create first subscription: %v", err)
	}

	// Try to create duplicate
	_, err = subscriber.CreateSubscription("duplicate-sub", "test-namespace", "test-topic", partition, schema_pb.OffsetType_RESET_TO_EARLIEST, 0)
	if err == nil {
		t.Error("Expected error for duplicate subscription ID")
	}
}

func TestOffsetSubscription_SeekToOffset(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	subscriber := NewOffsetSubscriber(registry)
	partition := createTestPartition()

	// Assign offsets
	registry.AssignOffsets("test-namespace", "test-topic", partition, 20)

	// Create subscription
	sub, err := subscriber.CreateSubscription("seek-test", "test-namespace", "test-topic", partition, schema_pb.OffsetType_RESET_TO_EARLIEST, 0)
	if err != nil {
		t.Fatalf("Failed to create subscription: %v", err)
	}

	// Test valid seek
	err = sub.SeekToOffset(10)
	if err != nil {
		t.Fatalf("Failed to seek to offset 10: %v", err)
	}

	if sub.CurrentOffset != 10 {
		t.Errorf("Expected current offset 10, got %d", sub.CurrentOffset)
	}

	// Test invalid seek (beyond high water mark)
	err = sub.SeekToOffset(25)
	if err == nil {
		t.Error("Expected error for seek beyond high water mark")
	}

	// Test negative seek
	err = sub.SeekToOffset(-1)
	if err == nil {
		t.Error("Expected error for negative seek offset")
	}
}

func TestOffsetSubscription_AdvanceOffset(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	subscriber := NewOffsetSubscriber(registry)
	partition := createTestPartition()

	// Create subscription
	sub, err := subscriber.CreateSubscription("advance-test", "test-namespace", "test-topic", partition, schema_pb.OffsetType_RESET_TO_EARLIEST, 0)
	if err != nil {
		t.Fatalf("Failed to create subscription: %v", err)
	}

	// Test single advance
	initialOffset := sub.GetNextOffset()
	sub.AdvanceOffset()

	if sub.GetNextOffset() != initialOffset+1 {
		t.Errorf("Expected offset %d, got %d", initialOffset+1, sub.GetNextOffset())
	}

	// Test batch advance
	sub.AdvanceOffsetBy(5)

	if sub.GetNextOffset() != initialOffset+6 {
		t.Errorf("Expected offset %d, got %d", initialOffset+6, sub.GetNextOffset())
	}
}

func TestOffsetSubscription_GetLag(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	subscriber := NewOffsetSubscriber(registry)
	partition := createTestPartition()

	// Assign offsets
	registry.AssignOffsets("test-namespace", "test-topic", partition, 15)

	// Create subscription at offset 5
	sub, err := subscriber.CreateSubscription("lag-test", "test-namespace", "test-topic", partition, schema_pb.OffsetType_EXACT_OFFSET, 5)
	if err != nil {
		t.Fatalf("Failed to create subscription: %v", err)
	}

	// Check initial lag
	lag, err := sub.GetLag()
	if err != nil {
		t.Fatalf("Failed to get lag: %v", err)
	}

	expectedLag := int64(15 - 5) // hwm - current
	if lag != expectedLag {
		t.Errorf("Expected lag %d, got %d", expectedLag, lag)
	}

	// Advance and check lag again
	sub.AdvanceOffsetBy(3)

	lag, err = sub.GetLag()
	if err != nil {
		t.Fatalf("Failed to get lag after advance: %v", err)
	}

	expectedLag = int64(15 - 8) // hwm - current
	if lag != expectedLag {
		t.Errorf("Expected lag %d after advance, got %d", expectedLag, lag)
	}
}

func TestOffsetSubscription_IsAtEnd(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	subscriber := NewOffsetSubscriber(registry)
	partition := createTestPartition()

	// Assign offsets
	registry.AssignOffsets("test-namespace", "test-topic", partition, 10)

	// Create subscription at end
	sub, err := subscriber.CreateSubscription("end-test", "test-namespace", "test-topic", partition, schema_pb.OffsetType_RESET_TO_LATEST, 0)
	if err != nil {
		t.Fatalf("Failed to create subscription: %v", err)
	}

	// Should be at end
	atEnd, err := sub.IsAtEnd()
	if err != nil {
		t.Fatalf("Failed to check if at end: %v", err)
	}

	if !atEnd {
		t.Error("Expected subscription to be at end")
	}

	// Seek to middle and check again
	sub.SeekToOffset(5)

	atEnd, err = sub.IsAtEnd()
	if err != nil {
		t.Fatalf("Failed to check if at end after seek: %v", err)
	}

	if atEnd {
		t.Error("Expected subscription not to be at end after seek")
	}
}

func TestOffsetSubscription_GetOffsetRange(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	subscriber := NewOffsetSubscriber(registry)
	partition := createTestPartition()

	// Assign offsets
	registry.AssignOffsets("test-namespace", "test-topic", partition, 20)

	// Create subscription
	sub, err := subscriber.CreateSubscription("range-test", "test-namespace", "test-topic", partition, schema_pb.OffsetType_EXACT_OFFSET, 5)
	if err != nil {
		t.Fatalf("Failed to create subscription: %v", err)
	}

	// Test normal range
	offsetRange, err := sub.GetOffsetRange(10)
	if err != nil {
		t.Fatalf("Failed to get offset range: %v", err)
	}

	if offsetRange.StartOffset != 5 {
		t.Errorf("Expected start offset 5, got %d", offsetRange.StartOffset)
	}
	if offsetRange.EndOffset != 14 {
		t.Errorf("Expected end offset 14, got %d", offsetRange.EndOffset)
	}
	if offsetRange.Count != 10 {
		t.Errorf("Expected count 10, got %d", offsetRange.Count)
	}

	// Test range that exceeds high water mark
	sub.SeekToOffset(15)
	offsetRange, err = sub.GetOffsetRange(10)
	if err != nil {
		t.Fatalf("Failed to get offset range near end: %v", err)
	}

	if offsetRange.StartOffset != 15 {
		t.Errorf("Expected start offset 15, got %d", offsetRange.StartOffset)
	}
	if offsetRange.EndOffset != 19 { // Should be capped at hwm-1
		t.Errorf("Expected end offset 19, got %d", offsetRange.EndOffset)
	}
	if offsetRange.Count != 5 {
		t.Errorf("Expected count 5, got %d", offsetRange.Count)
	}
}

func TestOffsetSubscription_EmptyRange(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	subscriber := NewOffsetSubscriber(registry)
	partition := createTestPartition()

	// Assign offsets
	registry.AssignOffsets("test-namespace", "test-topic", partition, 10)

	// Create subscription at end
	sub, err := subscriber.CreateSubscription("empty-range-test", "test-namespace", "test-topic", partition, schema_pb.OffsetType_RESET_TO_LATEST, 0)
	if err != nil {
		t.Fatalf("Failed to create subscription: %v", err)
	}

	// Request range when at end
	offsetRange, err := sub.GetOffsetRange(5)
	if err != nil {
		t.Fatalf("Failed to get offset range at end: %v", err)
	}

	if offsetRange.Count != 0 {
		t.Errorf("Expected empty range (count 0), got count %d", offsetRange.Count)
	}

	if offsetRange.StartOffset != 10 {
		t.Errorf("Expected start offset 10, got %d", offsetRange.StartOffset)
	}

	if offsetRange.EndOffset != 9 { // Empty range: end < start
		t.Errorf("Expected end offset 9 (empty range), got %d", offsetRange.EndOffset)
	}
}

func TestOffsetSeeker_ValidateOffsetRange(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	seeker := NewOffsetSeeker(registry)
	partition := createTestPartition()

	// Assign offsets
	registry.AssignOffsets("test-namespace", "test-topic", partition, 15)

	// Test valid range
	err := seeker.ValidateOffsetRange("test-namespace", "test-topic", partition, 5, 10)
	if err != nil {
		t.Errorf("Valid range should not return error: %v", err)
	}

	// Test invalid ranges
	testCases := []struct {
		name        string
		startOffset int64
		endOffset   int64
		expectError bool
	}{
		{"negative start", -1, 5, true},
		{"end before start", 10, 5, true},
		{"start beyond hwm", 20, 25, true},
		{"valid range", 0, 14, false},
		{"single offset", 5, 5, false},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			err := seeker.ValidateOffsetRange("test-namespace", "test-topic", partition, tc.startOffset, tc.endOffset)
			if tc.expectError && err == nil {
				t.Error("Expected error but got none")
			}
			if !tc.expectError && err != nil {
				t.Errorf("Expected no error but got: %v", err)
			}
		})
	}
}

func TestOffsetSeeker_GetAvailableOffsetRange(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	seeker := NewOffsetSeeker(registry)
	partition := createTestPartition()

	// Test empty partition
	offsetRange, err := seeker.GetAvailableOffsetRange("test-namespace", "test-topic", partition)
	if err != nil {
		t.Fatalf("Failed to get available range for empty partition: %v", err)
	}

	if offsetRange.Count != 0 {
		t.Errorf("Expected empty range for empty partition, got count %d", offsetRange.Count)
	}

	// Assign offsets and test again
	registry.AssignOffsets("test-namespace", "test-topic", partition, 25)

	offsetRange, err = seeker.GetAvailableOffsetRange("test-namespace", "test-topic", partition)
	if err != nil {
		t.Fatalf("Failed to get available range: %v", err)
	}

	if offsetRange.StartOffset != 0 {
		t.Errorf("Expected start offset 0, got %d", offsetRange.StartOffset)
	}
	if offsetRange.EndOffset != 24 {
		t.Errorf("Expected end offset 24, got %d", offsetRange.EndOffset)
	}
	if offsetRange.Count != 25 {
		t.Errorf("Expected count 25, got %d", offsetRange.Count)
	}
}

func TestOffsetSubscriber_CloseSubscription(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	subscriber := NewOffsetSubscriber(registry)
	partition := createTestPartition()

	// Create subscription
	sub, err := subscriber.CreateSubscription("close-test", "test-namespace", "test-topic", partition, schema_pb.OffsetType_RESET_TO_EARLIEST, 0)
	if err != nil {
		t.Fatalf("Failed to create subscription: %v", err)
	}

	// Verify subscription exists
	_, err = subscriber.GetSubscription("close-test")
	if err != nil {
		t.Fatalf("Subscription should exist: %v", err)
	}

	// Close subscription
	err = subscriber.CloseSubscription("close-test")
	if err != nil {
		t.Fatalf("Failed to close subscription: %v", err)
	}

	// Verify subscription is gone
	_, err = subscriber.GetSubscription("close-test")
	if err == nil {
		t.Error("Subscription should not exist after close")
	}

	// Verify subscription is marked inactive
	if sub.IsActive {
		t.Error("Subscription should be marked inactive after close")
	}
}

func TestOffsetSubscription_InactiveOperations(t *testing.T) {
	storage := NewInMemoryOffsetStorage()
	registry := NewPartitionOffsetRegistry(storage)
	subscriber := NewOffsetSubscriber(registry)
	partition := createTestPartition()

	// Create and close subscription
	sub, err := subscriber.CreateSubscription("inactive-test", "test-namespace", "test-topic", partition, schema_pb.OffsetType_RESET_TO_EARLIEST, 0)
	if err != nil {
		t.Fatalf("Failed to create subscription: %v", err)
	}

	subscriber.CloseSubscription("inactive-test")

	// Test operations on inactive subscription
	err = sub.SeekToOffset(5)
	if err == nil {
		t.Error("Expected error for seek on inactive subscription")
	}

	_, err = sub.GetLag()
	if err == nil {
		t.Error("Expected error for GetLag on inactive subscription")
	}

	_, err = sub.IsAtEnd()
	if err == nil {
		t.Error("Expected error for IsAtEnd on inactive subscription")
	}

	_, err = sub.GetOffsetRange(10)
	if err == nil {
		t.Error("Expected error for GetOffsetRange on inactive subscription")
	}
}