-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique_queue_test.go
146 lines (123 loc) · 2.98 KB
/
unique_queue_test.go
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
package gopq_test
import (
"context"
"os"
"testing"
"time"
"github.com/mattdeak/gopq"
_ "github.com/mattn/go-sqlite3"
)
func TestNewUniqueQueue(t *testing.T) {
tempFile := tempFilePath(t)
defer os.Remove(tempFile)
q, err := gopq.NewUniqueQueue(tempFile)
if err != nil {
t.Fatalf("NewUniqueQueue() error = %v", err)
}
if q == nil {
t.Fatal("NewUniqueQueue() returned nil")
}
}
func TestUniqueQueue_Enqueue(t *testing.T) {
q := setupTestUniqueQueue(t)
err := q.Enqueue([]byte("test item"))
if err != nil {
t.Fatalf("Enqueue() error = %v", err)
}
count, err := q.Len()
if err != nil {
t.Fatalf("Len() error = %v", err)
}
if count != 1 {
t.Errorf("Expected queue length 1, got %d", count)
}
// Test uniqueness
err = q.Enqueue([]byte("test item"))
if err != nil {
t.Fatalf("Enqueue() error = %v", err)
}
count, err = q.Len()
if err != nil {
t.Fatalf("Len() error = %v", err)
}
if count != 1 {
t.Errorf("Expected queue length 1 after duplicate enqueue, got %d", count)
}
}
func TestUniqueQueue_TryDequeue(t *testing.T) {
q := setupTestUniqueQueue(t)
// Enqueue an item
err := q.Enqueue([]byte("test item"))
if err != nil {
t.Fatalf("Enqueue() error = %v", err)
}
// Dequeue the item
msg, err := q.TryDequeue()
if err != nil {
t.Fatalf("TryDequeue() error = %v", err)
}
if string(msg.Item) != "test item" {
t.Errorf("Expected 'test item', got '%s'", string(msg.Item))
}
// Try to dequeue from empty queue
_, err = q.TryDequeue()
if err == nil {
t.Error("Expected error when dequeuing from empty queue, got nil")
}
}
func TestUniqueQueue_DequeueCtx(t *testing.T) {
q := setupTestUniqueQueue(t)
// Test dequeue with timeout
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
_, err := q.DequeueCtx(ctx)
if err == nil {
t.Fatal("Expected error, got nil")
}
// Enqueue and dequeue
err = q.Enqueue([]byte("test item"))
if err != nil {
t.Fatalf("Enqueue() error = %v", err)
}
msg, err := q.DequeueCtx(context.Background())
if err != nil {
t.Fatalf("DequeueCtx() error = %v", err)
}
if string(msg.Item) != "test item" {
t.Errorf("Expected 'test item', got '%s'", string(msg.Item))
}
}
func TestUniqueQueue_Len(t *testing.T) {
q := setupTestUniqueQueue(t)
count, err := q.Len()
if err != nil {
t.Fatalf("Len() error = %v", err)
}
if count != 0 {
t.Errorf("Expected empty queue, got length %d", count)
}
err = q.Enqueue([]byte("item1"))
if err != nil {
t.Fatalf("Enqueue() error = %v", err)
}
err = q.Enqueue([]byte("item2"))
if err != nil {
t.Fatalf("Enqueue() error = %v", err)
}
count, err = q.Len()
if err != nil {
t.Fatalf("Len() error = %v", err)
}
if count != 2 {
t.Errorf("Expected queue length 2, got %d", count)
}
}
func setupTestUniqueQueue(t *testing.T) *gopq.Queue {
tempFile := tempFilePath(t)
t.Cleanup(func() { os.Remove(tempFile) })
q, err := gopq.NewUniqueQueue(tempFile)
if err != nil {
t.Fatalf("Failed to create test queue: %v", err)
}
return q
}