-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdistributed_test.go
More file actions
462 lines (355 loc) · 15.9 KB
/
Copy pathdistributed_test.go
File metadata and controls
462 lines (355 loc) · 15.9 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
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
package varmq
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/goptics/varmq/mocks"
)
func setupDistributedQueue() (*distributedQueue[string], *mocks.MockDistributedQueue) {
mockQueue := mocks.NewMockDistributedQueue()
queue := NewDistributedQueue[string](mockQueue).(*distributedQueue[string])
return queue, mockQueue
}
func setupDistributedPriorityQueue() (*distributedPriorityQueue[string], *mocks.MockDistributedPriorityQueue) {
mockQueue := mocks.NewMockDistributedPriorityQueue()
queue := NewDistributedPriorityQueue[string](mockQueue).(*distributedPriorityQueue[string])
return queue, mockQueue
}
// Tests for DistributedQueue
func TestDistributedQueue(t *testing.T) {
t.Run("Add method success", func(t *testing.T) {
queue, mockQueue := setupDistributedQueue()
// Test adding a job
ok := queue.Add("test-data")
assert.True(t, ok, "Job should be added successfully")
assert.Equal(t, 1, queue.Len(), "Queue should have one pending job")
assert.Equal(t, 1, mockQueue.Len(), "Mock queue should have one item")
})
t.Run("Add method with job configs", func(t *testing.T) {
queue, mockQueue := setupDistributedQueue()
// Test adding a job with custom ID
ok := queue.Add("test-data", WithJobId("custom-id"))
assert.True(t, ok, "Job should be added successfully with custom ID")
assert.Equal(t, 1, queue.Len(), "Queue should have one pending job")
assert.Equal(t, 1, mockQueue.Len(), "Mock queue should have one item")
})
t.Run("Add method failure when queue is closed", func(t *testing.T) {
queue, mockQueue := setupDistributedQueue()
// Close the internal queue to simulate failure
err := mockQueue.Close()
assert.NoError(t, err, "Mock queue should close successfully")
// Attempt to add a job to closed queue
ok := queue.Add("test-data")
assert.False(t, ok, "Job should not be added to closed queue")
assert.Equal(t, 0, queue.Len(), "Queue should have no pending jobs")
assert.Equal(t, 0, mockQueue.Len(), "Mock queue should be empty")
})
t.Run("Add multiple jobs", func(t *testing.T) {
queue, mockQueue := setupDistributedQueue()
// Add multiple jobs
for i := range 5 {
ok := queue.Add("test-data-" + string(rune(i)))
assert.True(t, ok, "Job %d should be added successfully", i)
}
assert.Equal(t, 5, queue.Len(), "Queue should have five pending jobs")
assert.Equal(t, 5, mockQueue.Len(), "Mock queue should have five items")
})
t.Run("Subscription functionality", func(t *testing.T) {
queue, mockQueue := setupDistributedQueue()
// Track subscription calls
var subscriptionCalls []string
mockQueue.Subscribe(func(action string) {
subscriptionCalls = append(subscriptionCalls, action)
})
// Add a job and verify subscription is called
ok := queue.Add("test-data")
assert.True(t, ok, "Job should be added successfully")
assert.Equal(t, 1, len(subscriptionCalls), "Should have one subscription call")
assert.Equal(t, "enqueued", subscriptionCalls[0], "Subscription should be called with 'enqueued' action")
})
t.Run("Multiple subscribers", func(t *testing.T) {
queue, mockQueue := setupDistributedQueue()
// Track subscription calls from multiple subscribers
var subscriber1Calls []string
var subscriber2Calls []string
mockQueue.Subscribe(func(action string) {
subscriber1Calls = append(subscriber1Calls, action)
})
mockQueue.Subscribe(func(action string) {
subscriber2Calls = append(subscriber2Calls, action)
})
// Add a job and verify both subscribers are called
ok := queue.Add("test-data")
assert.True(t, ok, "Job should be added successfully")
assert.Equal(t, 1, len(subscriber1Calls), "Subscriber 1 should have one call")
assert.Equal(t, 1, len(subscriber2Calls), "Subscriber 2 should have one call")
assert.Equal(t, "enqueued", subscriber1Calls[0], "Subscriber 1 should receive 'enqueued' action")
assert.Equal(t, "enqueued", subscriber2Calls[0], "Subscriber 2 should receive 'enqueued' action")
})
t.Run("NumPending method", func(t *testing.T) {
queue, mockQueue := setupDistributedQueue()
// Initially should be empty
assert.Equal(t, 0, queue.Len(), "Queue should initially be empty")
// Add some jobs
queue.Add("job1")
queue.Add("job2")
assert.Equal(t, 2, queue.Len(), "Queue should have two pending jobs")
assert.Equal(t, 2, mockQueue.Len(), "Mock queue should have two items")
})
}
// Tests for DistributedPriorityQueue
func TestDistributedPriorityQueue(t *testing.T) {
t.Run("Add method success", func(t *testing.T) {
queue, mockQueue := setupDistributedPriorityQueue()
// Test adding a job with priority
ok := queue.Add("test-data", 5)
assert.True(t, ok, "Job should be added successfully")
assert.Equal(t, 1, queue.Len(), "Queue should have one pending job")
assert.Equal(t, 1, mockQueue.Len(), "Mock queue should have one item")
})
t.Run("Add method with job configs", func(t *testing.T) {
queue, mockQueue := setupDistributedPriorityQueue()
// Test adding a job with custom ID and priority
ok := queue.Add("test-data", 3, WithJobId("custom-id"))
assert.True(t, ok, "Job should be added successfully with custom ID and priority")
assert.Equal(t, 1, queue.Len(), "Queue should have one pending job")
assert.Equal(t, 1, mockQueue.Len(), "Mock queue should have one item")
})
t.Run("Add method failure when queue is closed", func(t *testing.T) {
queue, mockQueue := setupDistributedPriorityQueue()
// Close the internal queue to simulate failure
err := mockQueue.Close()
assert.NoError(t, err, "Mock queue should close successfully")
// Attempt to add a job to closed queue
ok := queue.Add("test-data", 1)
assert.False(t, ok, "Job should not be added to closed queue")
assert.Equal(t, 0, queue.Len(), "Queue should have no pending jobs")
assert.Equal(t, 0, mockQueue.Len(), "Mock queue should be empty")
})
t.Run("Add multiple jobs with different priorities", func(t *testing.T) {
queue, mockQueue := setupDistributedPriorityQueue()
// Add jobs with different priorities
priorities := []int{5, 1, 3, 2, 4}
for i, priority := range priorities {
ok := queue.Add("test-data-"+string(rune(i)), priority)
assert.True(t, ok, "Job %d should be added successfully", i)
}
assert.Equal(t, 5, queue.Len(), "Queue should have five pending jobs")
assert.Equal(t, 5, mockQueue.Len(), "Mock queue should have five items")
})
t.Run("Priority ordering", func(t *testing.T) {
queue, mockQueue := setupDistributedPriorityQueue()
// Add jobs with different priorities (lower number = higher priority)
queue.Add("low-priority", 10)
queue.Add("high-priority", 1)
queue.Add("medium-priority", 5)
assert.Equal(t, 3, queue.Len(), "Queue should have three pending jobs")
// Dequeue and verify priority ordering (highest priority first)
item1, ok1 := mockQueue.Dequeue()
assert.True(t, ok1, "Should dequeue first item")
item2, ok2 := mockQueue.Dequeue()
assert.True(t, ok2, "Should dequeue second item")
item3, ok3 := mockQueue.Dequeue()
assert.True(t, ok3, "Should dequeue third item")
// Note: We can't directly check the content since items are serialized jobs
// But we can verify all items were dequeued
assert.NotNil(t, item1, "First item should not be nil")
assert.NotNil(t, item2, "Second item should not be nil")
assert.NotNil(t, item3, "Third item should not be nil")
})
t.Run("Subscription functionality", func(t *testing.T) {
queue, mockQueue := setupDistributedPriorityQueue()
// Track subscription calls
var subscriptionCalls []string
mockQueue.Subscribe(func(action string) {
subscriptionCalls = append(subscriptionCalls, action)
})
// Add a job and verify subscription is called
ok := queue.Add("test-data", 1)
assert.True(t, ok, "Job should be added successfully")
assert.Equal(t, 1, len(subscriptionCalls), "Should have one subscription call")
assert.Equal(t, "enqueued", subscriptionCalls[0], "Subscription should be called with 'enqueued' action")
})
t.Run("Multiple subscribers", func(t *testing.T) {
queue, mockQueue := setupDistributedPriorityQueue()
// Track subscription calls from multiple subscribers
var subscriber1Calls []string
var subscriber2Calls []string
mockQueue.Subscribe(func(action string) {
subscriber1Calls = append(subscriber1Calls, action)
})
mockQueue.Subscribe(func(action string) {
subscriber2Calls = append(subscriber2Calls, action)
})
// Add a job and verify both subscribers are called
ok := queue.Add("test-data", 2)
assert.True(t, ok, "Job should be added successfully")
assert.Equal(t, 1, len(subscriber1Calls), "Subscriber 1 should have one call")
assert.Equal(t, 1, len(subscriber2Calls), "Subscriber 2 should have one call")
assert.Equal(t, "enqueued", subscriber1Calls[0], "Subscriber 1 should receive 'enqueued' action")
assert.Equal(t, "enqueued", subscriber2Calls[0], "Subscriber 2 should receive 'enqueued' action")
})
t.Run("NumPending method", func(t *testing.T) {
queue, mockQueue := setupDistributedPriorityQueue()
// Initially should be empty
assert.Equal(t, 0, queue.Len(), "Queue should initially be empty")
// Add some jobs with priorities
queue.Add("job1", 1)
queue.Add("job2", 2)
assert.Equal(t, 2, queue.Len(), "Queue should have two pending jobs")
assert.Equal(t, 2, mockQueue.Len(), "Mock queue should have two items")
})
}
func TestDistributedQueueFailures(t *testing.T) {
t.Run("Enqueue failure", func(t *testing.T) {
queue, mockQueue := setupDistributedQueue()
mockQueue.ShouldFailEnqueue = true
// Track subscriptions to ensure none are called on failure
var subscriptionCalls []string
mockQueue.Subscribe(func(action string) {
subscriptionCalls = append(subscriptionCalls, action)
})
ok := queue.Add("test-data")
assert.False(t, ok, "Enqueue should fail when configured")
assert.Equal(t, 0, queue.Len(), "Queue should be empty")
assert.Equal(t, 0, len(subscriptionCalls), "No subscription calls should be made")
})
t.Run("Acknowledge failure", func(t *testing.T) {
_, mockQueue := setupDistributedQueue()
mockQueue.ShouldFailAcknowledge = true
ok := mockQueue.Acknowledge("ack-id")
assert.False(t, ok, "Acknowledge should fail when configured")
})
}
func TestDistributedPriorityQueueFailures(t *testing.T) {
t.Run("Enqueue failure", func(t *testing.T) {
queue, mockQueue := setupDistributedPriorityQueue()
mockQueue.ShouldFailEnqueue = true
// Track subscriptions to ensure none are called on failure
var subscriptionCalls []string
mockQueue.Subscribe(func(action string) {
subscriptionCalls = append(subscriptionCalls, action)
})
ok := queue.Add("test-data", 1)
assert.False(t, ok, "Enqueue should fail when configured")
assert.Equal(t, 0, queue.Len(), "Queue should be empty")
assert.Equal(t, 0, len(subscriptionCalls), "No subscription calls should be made")
})
t.Run("Acknowledge failure", func(t *testing.T) {
_, mockQueue := setupDistributedPriorityQueue()
mockQueue.ShouldFailAcknowledge = true
ok := mockQueue.Acknowledge("ack-id")
assert.False(t, ok, "Acknowledge should fail when configured")
})
}
// Additional edge case tests
func TestDistributedQueueEdgeCases(t *testing.T) {
t.Run("Add with empty data", func(t *testing.T) {
queue, _ := setupDistributedQueue()
// Test adding empty string
ok := queue.Add("")
assert.True(t, ok, "Empty string should be added successfully")
})
t.Run("Add with nil-like data", func(t *testing.T) {
// Test with pointer type that can be nil
mockQueue := mocks.NewMockDistributedQueue()
queue := NewDistributedQueue[*string](mockQueue)
var nilString *string
ok := queue.Add(nilString)
assert.True(t, ok, "Nil pointer should be added successfully")
})
t.Run("Subscription with failed enqueue", func(t *testing.T) {
queue, mockQueue := setupDistributedQueue()
// Close the queue first
mockQueue.Close()
// Track subscription calls
var subscriptionCalls []string
mockQueue.Subscribe(func(action string) {
subscriptionCalls = append(subscriptionCalls, action)
})
// Attempt to add a job to closed queue
ok := queue.Add("test-data")
assert.False(t, ok, "Job should not be added to closed queue")
assert.Equal(t, 0, len(subscriptionCalls), "No subscription calls should be made for failed enqueue")
})
}
func TestDistributedPriorityQueueEdgeCases(t *testing.T) {
t.Run("Add with zero priority", func(t *testing.T) {
queue, mockQueue := setupDistributedPriorityQueue()
// Test adding with zero priority
ok := queue.Add("test-data", 0)
assert.True(t, ok, "Job with zero priority should be added successfully")
assert.Equal(t, 1, mockQueue.Len(), "Mock queue should have one item")
})
t.Run("Add with negative priority", func(t *testing.T) {
queue, mockQueue := setupDistributedPriorityQueue()
// Test adding with negative priority (should have highest priority)
ok := queue.Add("test-data", -5)
assert.True(t, ok, "Job with negative priority should be added successfully")
assert.Equal(t, 1, mockQueue.Len(), "Mock queue should have one item")
})
t.Run("Add with very high priority", func(t *testing.T) {
queue, mockQueue := setupDistributedPriorityQueue()
// Test adding with very high priority number (should have lowest priority)
ok := queue.Add("test-data", 1000000)
assert.True(t, ok, "Job with very high priority number should be added successfully")
assert.Equal(t, 1, mockQueue.Len(), "Mock queue should have one item")
})
t.Run("Subscription with failed enqueue", func(t *testing.T) {
queue, mockQueue := setupDistributedPriorityQueue()
// Close the queue first
mockQueue.Close()
// Track subscription calls
var subscriptionCalls []string
mockQueue.Subscribe(func(action string) {
subscriptionCalls = append(subscriptionCalls, action)
})
// Attempt to add a job to closed queue
ok := queue.Add("test-data", 1)
assert.False(t, ok, "Job should not be added to closed queue")
assert.Equal(t, 0, len(subscriptionCalls), "No subscription calls should be made for failed enqueue")
})
}
func TestDistributedQueueCapacity(t *testing.T) {
t.Run("Add returns false when at capacity", func(t *testing.T) {
mockQueue := mocks.NewMockDistributedQueue()
queue := NewDistributedQueue[string](mockQueue, WithQueueCapacity(2))
ok1 := queue.Add("item-1")
assert.True(t, ok1, "First item should be added successfully")
ok2 := queue.Add("item-2")
assert.True(t, ok2, "Second item should be added successfully")
ok3 := queue.Add("item-3")
assert.False(t, ok3, "Third item should fail when queue is at capacity")
assert.Equal(t, 2, queue.Len(), "Queue should have exactly 2 pending items")
})
t.Run("IsFull returns correct state", func(t *testing.T) {
mockQueue := mocks.NewMockDistributedQueue()
queue := NewDistributedQueue[string](mockQueue, WithQueueCapacity(2))
assert.False(t, queue.IsFull(), "Empty queue should not be full")
queue.Add("item-1")
assert.False(t, queue.IsFull(), "Queue with 1/2 items should not be full")
queue.Add("item-2")
assert.True(t, queue.IsFull(), "Queue at capacity should be full")
})
}
func TestDistributedPriorityQueueCapacity(t *testing.T) {
t.Run("Add returns false when at capacity", func(t *testing.T) {
mockQueue := mocks.NewMockDistributedPriorityQueue()
queue := NewDistributedPriorityQueue[string](mockQueue, WithQueueCapacity(2))
ok1 := queue.Add("high", 1)
assert.True(t, ok1, "First item should be added successfully")
ok2 := queue.Add("medium", 5)
assert.True(t, ok2, "Second item should be added successfully")
ok3 := queue.Add("low", 10)
assert.False(t, ok3, "Third item should fail when queue is at capacity")
assert.Equal(t, 2, queue.Len(), "Queue should have exactly 2 pending items")
})
t.Run("IsFull returns correct state", func(t *testing.T) {
mockQueue := mocks.NewMockDistributedPriorityQueue()
queue := NewDistributedPriorityQueue[string](mockQueue, WithQueueCapacity(2))
assert.False(t, queue.IsFull(), "Empty queue should not be full")
queue.Add("item-1", 1)
assert.False(t, queue.IsFull(), "Queue with 1/2 items should not be full")
queue.Add("item-2", 2)
assert.True(t, queue.IsFull(), "Queue at capacity should be full")
})
}