Repository navigation
Expand file tree
/
Copy pathqueue_test.go
More file actions
227 lines (192 loc) · 5.54 KB
/
Copy pathqueue_test.go
File metadata and controls
227 lines (192 loc) · 5.54 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
package s3queue_test
import (
"context"
"fmt"
"os"
"sync"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3queue "github.com/devesh-shetty/s3-queue"
)
const testBucket = "s3-queue-test"
// newTestClient creates an S3 client pointing at a local MinIO instance.
// Set S3_ENDPOINT to override the default "http://localhost:9000".
func newTestClient(t *testing.T) *s3.Client {
t.Helper()
endpoint := os.Getenv("S3_ENDPOINT")
if endpoint == "" {
endpoint = "http://localhost:9000"
}
cfg, err := config.LoadDefaultConfig(context.Background(),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
"minioadmin", "minioadmin", "",
)),
config.WithRegion("us-east-1"),
)
if err != nil {
t.Fatalf("loading AWS config: %v", err)
}
client := s3.NewFromConfig(cfg, func(o *s3.Options) {
o.BaseEndpoint = aws.String(endpoint)
o.UsePathStyle = true
})
// Ensure the test bucket exists.
_, err = client.CreateBucket(context.Background(), &s3.CreateBucketInput{
Bucket: aws.String(testBucket),
})
// Ignore "bucket already exists" errors.
if err != nil {
// BucketAlreadyOwnedByYou / BucketAlreadyExists are fine.
t.Logf("create bucket (may already exist): %v", err)
}
return client
}
// uniquePrefix returns a test-unique prefix to isolate concurrent test runs.
func uniquePrefix(t *testing.T) string {
return fmt.Sprintf("test/%s/%d", t.Name(), time.Now().UnixNano())
}
func TestEnqueueAndList(t *testing.T) {
client := newTestClient(t)
ctx := context.Background()
q := s3queue.New(client, testBucket, uniquePrefix(t))
// Enqueue three messages with a small delay so timestamps differ.
ids := []string{"job-001", "job-002", "job-003"}
for _, id := range ids {
if err := q.Enqueue(ctx, id, []byte("payload-"+id)); err != nil {
t.Fatalf("enqueue %s: %v", id, err)
}
time.Sleep(10 * time.Millisecond) // ensure distinct timestamps
}
// List should return all three in FIFO order.
messages, err := q.List(ctx)
if err != nil {
t.Fatalf("list: %v", err)
}
if len(messages) != 3 {
t.Fatalf("expected 3 messages, got %d", len(messages))
}
for i, msg := range messages {
if msg.ID != ids[i] {
t.Errorf("message %d: expected ID %s, got %s", i, ids[i], msg.ID)
}
}
}
func TestClaimPreventsDoubleClaim(t *testing.T) {
client := newTestClient(t)
ctx := context.Background()
q := s3queue.New(client, testBucket, uniquePrefix(t))
// Enqueue one message.
if err := q.Enqueue(ctx, "job-001", []byte("payload")); err != nil {
t.Fatalf("enqueue: %v", err)
}
messages, err := q.List(ctx)
if err != nil {
t.Fatalf("list: %v", err)
}
if len(messages) == 0 {
t.Fatal("expected at least 1 message")
}
key := messages[0].Key
// First claim should succeed.
if err := q.Claim(ctx, key, "worker-A"); err != nil {
t.Fatalf("first claim should succeed: %v", err)
}
// Second claim should fail with ErrAlreadyClaimed.
err = q.Claim(ctx, key, "worker-B")
if err != s3queue.ErrAlreadyClaimed {
t.Fatalf("expected ErrAlreadyClaimed, got: %v", err)
}
}
func TestDequeue(t *testing.T) {
client := newTestClient(t)
ctx := context.Background()
q := s3queue.New(client, testBucket, uniquePrefix(t))
// Enqueue three messages.
for i := 1; i <= 3; i++ {
id := fmt.Sprintf("job-%03d", i)
if err := q.Enqueue(ctx, id, []byte("payload-"+id)); err != nil {
t.Fatalf("enqueue %s: %v", id, err)
}
time.Sleep(10 * time.Millisecond)
}
// Two concurrent consumers race to dequeue.
var mu sync.Mutex
claimed := make(map[string]string) // messageID -> workerID
var wg sync.WaitGroup
for _, workerID := range []string{"worker-1", "worker-2"} {
wg.Add(1)
go func(wid string) {
defer wg.Done()
for {
msg, err := q.Dequeue(ctx, wid)
if err != nil {
t.Errorf("[%s] dequeue error: %v", wid, err)
return
}
if msg == nil {
return // queue is empty
}
mu.Lock()
if _, exists := claimed[msg.ID]; exists {
t.Errorf("message %s was claimed by two workers", msg.ID)
}
claimed[msg.ID] = wid
mu.Unlock()
// Ack the message.
if err := q.Ack(ctx, msg.Key); err != nil {
t.Errorf("[%s] ack error: %v", wid, err)
}
}
}(workerID)
}
wg.Wait()
// All three messages should have been processed exactly once.
if len(claimed) != 3 {
t.Fatalf("expected 3 claimed messages, got %d: %v", len(claimed), claimed)
}
}
func TestAck(t *testing.T) {
client := newTestClient(t)
ctx := context.Background()
prefix := uniquePrefix(t)
q := s3queue.New(client, testBucket, prefix)
// Enqueue and dequeue a message.
if err := q.Enqueue(ctx, "job-001", []byte("payload")); err != nil {
t.Fatalf("enqueue: %v", err)
}
msg, err := q.Dequeue(ctx, "worker-1")
if err != nil {
t.Fatalf("dequeue: %v", err)
}
if msg == nil {
t.Fatal("expected a message, got nil")
}
// Ack the message.
if err := q.Ack(ctx, msg.Key); err != nil {
t.Fatalf("ack: %v", err)
}
// Pending should be empty now.
pending, err := q.List(ctx)
if err != nil {
t.Fatalf("list after ack: %v", err)
}
if len(pending) != 0 {
t.Errorf("expected 0 pending messages, got %d", len(pending))
}
// Done prefix should have the message.
donePrefix := fmt.Sprintf("%s/done/", prefix)
resp, err := client.ListObjectsV2(ctx, &s3.ListObjectsV2Input{
Bucket: aws.String(testBucket),
Prefix: aws.String(donePrefix),
})
if err != nil {
t.Fatalf("listing done: %v", err)
}
if len(resp.Contents) != 1 {
t.Fatalf("expected 1 done marker, got %d", len(resp.Contents))
}
}