-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue_test.go
More file actions
344 lines (292 loc) · 8.28 KB
/
queue_test.go
File metadata and controls
344 lines (292 loc) · 8.28 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
package sqliteq
import (
"fmt"
"os"
"testing"
)
func TestSQLiteQueue(t *testing.T) {
// Create a temporary database file
dbPath := "test_queue.db"
// Cleanup after test
defer os.Remove(dbPath)
queues := New(dbPath)
// Create a new queue with default settings (removeOnComplete = true)
q, err := queues.NewQueue("test_queue")
if err != nil {
t.Fatalf("Failed to create queue: %v", err)
}
defer queues.Close()
// Test enqueue
t.Run("Enqueue", func(t *testing.T) {
success := q.Enqueue([]byte("test item 1"))
if !success {
t.Error("Enqueue failed")
}
success = q.Enqueue([]byte("42"))
if !success {
t.Error("Enqueue failed")
}
success = q.Enqueue([]byte("some complex data"))
if !success {
t.Error("Enqueue failed")
}
if q.Len() != 3 {
t.Errorf("Expected queue length 3, got %d", q.Len())
}
})
// Test values
t.Run("Values", func(t *testing.T) {
values := q.Values()
if len(values) != 3 {
t.Errorf("Expected 3 values, got %d", len(values))
}
})
// Test dequeue
t.Run("Dequeue", func(t *testing.T) {
data, success := q.Dequeue()
if !success {
t.Error("Dequeue failed")
}
// The first item should be "test item 1" as bytes
byteData, ok := data.([]byte)
if !ok {
t.Errorf("Expected []byte, got %T", data)
}
if string(byteData) != "test item 1" {
t.Errorf("Expected 'test item 1', got '%s'", string(byteData))
}
if q.Len() != 2 {
t.Errorf("Expected queue length 2, got %d", q.Len())
}
// Verify the item is completely removed from the database
var count int
row := q.client.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE status = 'completed' OR status = 'processing'", q.tableName))
error := row.Scan(&count)
if error != nil {
t.Errorf("Error checking items in database: %v", error)
}
if count != 0 {
t.Errorf("Expected 0 items with completed/processing status, got %d", count)
}
})
// Test dequeue with ack ID
t.Run("DequeueWithAckId", func(t *testing.T) {
item, success, ackID := q.DequeueWithAckId()
if !success {
t.Error("DequeueWithAckId failed")
}
if ackID == "" {
t.Error("Expected non-empty ack ID")
}
// The next item should be the string "42" as bytes
byteData, ok := item.([]byte)
if !ok {
t.Errorf("Expected []byte, got %T", item)
}
if string(byteData) != "42" {
t.Errorf("Expected '42', got '%s'", string(byteData))
}
// Test acknowledge
ackSuccess := q.Acknowledge(ackID)
if !ackSuccess {
t.Error("Acknowledge failed")
}
// Test invalid ack ID
ackSuccess = q.Acknowledge("invalid-ack-id")
if ackSuccess {
t.Error("Acknowledge with invalid ID should fail")
}
})
// Test purge
t.Run("Purge", func(t *testing.T) {
q.Purge()
if q.Len() != 0 {
t.Errorf("Expected queue length 0 after purge, got %d", q.Len())
}
})
// Test empty queue
t.Run("EmptyQueue", func(t *testing.T) {
item, success := q.Dequeue()
if success {
t.Errorf("Dequeue on empty queue should fail, got %v", item)
}
item, success, ackID := q.DequeueWithAckId()
if success {
t.Errorf("DequeueWithAckId on empty queue should fail, got %v, %s", item, ackID)
}
})
}
// Test removeOnComplete option behavior
func TestRemoveOnCompleteOption(t *testing.T) {
// Test with removeOnComplete = false
t.Run("KeepCompletedItems", func(t *testing.T) {
// Create a temporary database file
dbPath := "test_keep_completed.db"
defer os.Remove(dbPath)
// Create a queue with removeOnComplete = false
queues := New(dbPath)
q, err := queues.NewQueue("test_queue", WithRemoveOnComplete(false))
if err != nil {
t.Fatalf("Failed to create queue: %v", err)
}
defer queues.Close()
// Enqueue an item
q.Enqueue("test item")
// Dequeue with ack ID
_, success, ackID := q.DequeueWithAckId()
if !success {
t.Error("DequeueWithAckId failed")
}
// Acknowledge the item
if !q.Acknowledge(ackID) {
t.Error("Acknowledge failed")
}
// Since removeOnComplete is false, the item should still be in the database
// but marked as completed, so the queue length should be 0
if q.Len() != 0 {
t.Errorf("Expected queue length 0, got %d", q.Len())
}
// Verify the item is still in the database by checking directly
var count int
row := q.client.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE status = 'completed'", q.tableName))
error := row.Scan(&count)
if error != nil {
t.Errorf("Error checking completed items: %v", error)
}
if count != 1 {
t.Errorf("Expected 1 completed item in database, got %d", count)
}
})
// Test with removeOnComplete = true (default)
t.Run("RemoveCompletedItems", func(t *testing.T) {
// Create a temporary database file
dbPath := "test_remove_completed.db"
defer os.Remove(dbPath)
queues := New(dbPath)
// Create a queue with default removeOnComplete = true
q, err := queues.NewQueue("test_queue")
if err != nil {
t.Fatalf("Failed to create queue: %v", err)
}
defer queues.Close()
// Enqueue an item
q.Enqueue("test item")
// Test direct dequeue (no ack ID)
_, success := q.Dequeue()
if !success {
t.Error("Dequeue failed")
}
// Since dequeueInternal with withAckId=false now deletes directly,
// the item should be removed from the database immediately
var count int
row := q.client.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s", q.tableName))
error := row.Scan(&count)
if error != nil {
t.Errorf("Error checking items in database: %v", error)
}
if count != 0 {
t.Errorf("Expected 0 items in database after Dequeue, got %d", count)
}
// Try with DequeueWithAckId and Acknowledge process
q.Enqueue([]byte("test item 2"))
_, success, ackID := q.DequeueWithAckId()
if !success {
t.Error("DequeueWithAckId failed")
}
// Check that the item is still in the database with processing status
row = q.client.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE status = 'processing'", q.tableName))
error = row.Scan(&count)
if error != nil {
t.Errorf("Error checking processing items: %v", error)
}
if count != 1 {
t.Errorf("Expected 1 processing item in database, got %d", count)
}
// Acknowledge the item
if !q.Acknowledge(ackID) {
t.Error("Acknowledge failed")
}
// Since removeOnComplete is true, the item should be removed from the database
row = q.client.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s", q.tableName))
error = row.Scan(&count)
if error != nil {
t.Errorf("Error checking items in database: %v", error)
}
if count != 0 {
t.Errorf("Expected 0 items in database after Acknowledge, got %d", count)
}
})
}
// Test concurrent operations
func TestConcurrentOperations(t *testing.T) {
// Create a temporary database file
dbPath := "test_concurrent.db"
// Cleanup after test
defer os.Remove(dbPath)
// Create a new queue
queues := New(dbPath)
q, err := queues.NewQueue("test_queue")
if err != nil {
t.Fatalf("Failed to create queue: %v", err)
}
defer queues.Close()
// Enqueue items concurrently
numItems := 100
done := make(chan bool)
// Producer goroutine
go func() {
for i := 0; i < numItems; i++ {
itemData := []byte(fmt.Sprintf("item-%d", i))
if !q.Enqueue(itemData) {
t.Errorf("Failed to enqueue item %d", i)
}
}
done <- true
}()
// Consumer goroutine
processed := 0
ackIDs := make([]string, 0, numItems)
go func() {
for processed < numItems {
_, success, ackID := q.DequeueWithAckId()
if success {
ackIDs = append(ackIDs, ackID)
processed++
}
}
done <- true
}()
// Wait for producer and consumer to finish
<-done
<-done
// Verify all items were processed
if processed != numItems {
t.Errorf("Expected %d processed items, got %d", numItems, processed)
}
// Acknowledge all items
for _, ackID := range ackIDs {
if !q.Acknowledge(ackID) {
t.Errorf("Failed to acknowledge item with ID %s", ackID)
}
}
// Verify queue is empty
if q.Len() != 0 {
t.Errorf("Expected empty queue, got length %d", q.Len())
}
}
func TestQuoteIdent(t *testing.T) {
tt := []struct{ input, want string }{
{"foo", `"foo"`},
{"spaces are here", `"spaces are here"`},
{`"quoted"`, `"""quoted"""`},
{``, `""`}, // belive it or not this is a valid table name
}
for _, tc := range tt {
t.Run(tc.input, func(t *testing.T) {
got := quoteIdent(tc.input)
if got != tc.want {
t.Errorf("Unexpected quotes tabled (want %q, got %q)", tc.want, got)
}
})
}
}