-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer_test.go
309 lines (291 loc) · 9.61 KB
/
consumer_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
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
package dynamomq_test
import (
"context"
"errors"
"fmt"
"log"
"os"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/vvatanabe/dynamomq"
"github.com/vvatanabe/dynamomq/internal/mock"
"github.com/vvatanabe/dynamomq/internal/test"
)
func TestConsumerStartConsumingShouldReturnErrConsumerClosed(t *testing.T) {
t.Parallel()
queue, dlq, store := prepareQueueAndStore(1, 1)
client := NewClientForConsumerTest(queue, dlq, store, ClientForConsumerTestConfig{
SimulateReceiveMessageError: true,
})
processor := &CountProcessor[test.MessageData]{
SimulateProcessError: false,
}
consumer := dynamomq.NewConsumer[test.MessageData](client, processor)
_ = consumer.Shutdown(context.Background())
if err := consumer.StartConsuming(); !errors.Is(err, dynamomq.ErrConsumerClosed) {
t.Errorf("StartConsuming() error = %v, want = %v", err, dynamomq.ErrConsumerClosed)
return
}
}
func TestConsumerStartConsumingShouldReturnNoTemporaryError(t *testing.T) {
t.Parallel()
queue, dlq, store := prepareQueueAndStore(1, 1)
client := NewClientForConsumerTest(queue, dlq, store, ClientForConsumerTestConfig{
SimulateReceiveMessageNoTemporaryError: true,
})
processor := &CountProcessor[test.MessageData]{
SimulateProcessError: false,
}
consumer := dynamomq.NewConsumer[test.MessageData](client, processor)
if err := consumer.StartConsuming(); !errors.Is(err, test.ErrTest) {
t.Errorf("StartConsuming() error = %v, want = %v", err, test.ErrTest)
return
}
}
func TestConsumerStartConsuming(t *testing.T) {
t.Parallel()
type testCase struct {
Name string
ClientForConsumerTestConfig ClientForConsumerTestConfig
WantMessageProcessError bool
MessageSize int
MessageReceiveCount int
MaximumReceives int
QueueType dynamomq.QueueType
ExpectedCountSize int
ExpectedStoreSize int
ExpectedDLQSize int
}
tests := []testCase{
{
Name: "should succeed message process",
MessageSize: 10,
QueueType: dynamomq.QueueTypeStandard,
ExpectedCountSize: 10,
},
{
Name: "should retried message process when maximum receives is 0",
WantMessageProcessError: true,
MaximumReceives: 0,
MessageSize: 10,
QueueType: dynamomq.QueueTypeStandard,
ExpectedStoreSize: 10,
},
{
Name: "should retried message process when receive count < maximum receives",
WantMessageProcessError: true,
MaximumReceives: 2,
MessageSize: 10,
QueueType: dynamomq.QueueTypeStandard,
ExpectedStoreSize: 10,
},
{
Name: "should moved message to DLQ",
WantMessageProcessError: true,
MessageReceiveCount: 1,
MaximumReceives: 1,
QueueType: dynamomq.QueueTypeStandard,
MessageSize: 10,
ExpectedStoreSize: 10,
ExpectedDLQSize: 10,
},
{
Name: "should delete message when when receive count < maximum receives",
WantMessageProcessError: true,
MessageReceiveCount: 1,
MaximumReceives: 1,
QueueType: dynamomq.QueueTypeDLQ,
MessageSize: 10,
},
{
Name: "should not return error when receive message is temporary",
ClientForConsumerTestConfig: ClientForConsumerTestConfig{
SimulateReceiveMessageError: true,
},
QueueType: dynamomq.QueueTypeStandard,
MessageSize: 10,
ExpectedStoreSize: 10,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.Name, func(t *testing.T) {
t.Parallel()
queue, dlq, store := prepareQueueAndStore(tt.MessageSize, tt.MessageReceiveCount)
client := NewClientForConsumerTest(queue, dlq, store, tt.ClientForConsumerTestConfig)
processor := &CountProcessor[test.MessageData]{
SimulateProcessError: tt.WantMessageProcessError,
}
consumer := dynamomq.NewConsumer[test.MessageData](client, processor,
dynamomq.WithPollingInterval(0),
dynamomq.WithConcurrency(3),
dynamomq.WithMaximumReceives(tt.MaximumReceives),
dynamomq.WithVisibilityTimeout(30),
dynamomq.WithRetryInterval(1),
dynamomq.WithQueueType(tt.QueueType),
dynamomq.WithErrorLog(log.New(os.Stderr, "", 0)),
dynamomq.WithOnShutdown([]func(){}))
go func() {
if err := consumer.StartConsuming(); err != nil {
t.Errorf("StartConsuming() error = %v", err)
return
}
}()
time.Sleep(time.Second)
if processor.Count.Load() != int32(tt.ExpectedCountSize) {
t.Errorf("StartConsuming() count = %v, want %v", processor.Count.Load(), tt.ExpectedCountSize)
}
var storeSize int
store.Range(func(key, value any) bool {
storeSize++
return true
})
if storeSize != tt.ExpectedStoreSize {
t.Errorf("StartConsuming() storeSize = %v, want %v", storeSize, tt.ExpectedStoreSize)
}
if len(dlq) != tt.ExpectedDLQSize {
t.Errorf("StartConsuming() dlqSize = %v, want %v", len(dlq), tt.ExpectedDLQSize)
}
})
}
}
func TestConsumerShutdown(t *testing.T) {
t.Parallel()
type testCase struct {
Name string
ProcessorSleep time.Duration
MessageSize int
ExpectedCountSize int
WantShutdownError bool
}
tests := []testCase{
{
Name: "should succeed a graceful shutdown",
MessageSize: 100,
ExpectedCountSize: 100,
},
{
Name: "should timeout during a graceful shutdown",
ProcessorSleep: 3 * time.Second,
MessageSize: 100,
WantShutdownError: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.Name, func(t *testing.T) {
t.Parallel()
queue, dlq, store := prepareQueueAndStore(tt.MessageSize, 0)
client := NewClientForConsumerTest(queue, dlq, store, ClientForConsumerTestConfig{})
processor := &CountProcessor[test.MessageData]{
SimulateProcessError: false,
Sleep: tt.ProcessorSleep,
}
consumer := dynamomq.NewConsumer[test.MessageData](client, processor,
dynamomq.WithPollingInterval(0),
dynamomq.WithConcurrency(3),
dynamomq.WithMaximumReceives(1),
dynamomq.WithQueueType(dynamomq.QueueTypeStandard),
dynamomq.WithErrorLog(log.New(os.Stderr, "", 0)),
dynamomq.WithOnShutdown([]func(){
func() {},
}))
go func() {
if err := consumer.StartConsuming(); err != nil {
t.Errorf("StartConsuming() error = %v", err)
return
}
}()
time.Sleep(time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err := consumer.Shutdown(ctx)
if tt.WantShutdownError {
if err == nil {
t.Error("Shutdown() error is nil")
}
return
}
if err != nil {
t.Errorf("Shutdown() error = %v", err)
return
}
if processor.Count.Load() != int32(tt.ExpectedCountSize) {
t.Errorf("StartConsuming() count = %v, want %v", processor.Count.Load(), tt.ExpectedCountSize)
}
})
}
}
func prepareQueueAndStore(size, receiveCount int) (queue, dlq chan *dynamomq.Message[test.MessageData], store *sync.Map) {
var storeMap sync.Map
queue = make(chan *dynamomq.Message[test.MessageData], size)
for i := 1; i <= size; i++ {
id := fmt.Sprintf("A-%d", i)
msg := dynamomq.NewMessage(id, test.NewMessageData(id), test.DefaultTestDate)
msg.ReceiveCount = receiveCount
storeMap.Store(id, msg)
queue <- msg
}
return queue, make(chan *dynamomq.Message[test.MessageData], size), &storeMap
}
type CountProcessor[T any] struct {
Count atomic.Int32
SimulateProcessError bool
Sleep time.Duration
}
func (p *CountProcessor[T]) Process(_ *dynamomq.Message[T]) error {
if p.SimulateProcessError {
return test.ErrTest
}
time.Sleep(p.Sleep)
p.Count.Add(1)
return nil
}
type ClientForConsumerTestConfig struct {
SimulateReceiveMessageError bool
SimulateReceiveMessageNoTemporaryError bool
SimulateDeleteMessageError bool
SimulateMessageAsVisibleError bool
SimulateMoveMessageToDLQError bool
}
func NewClientForConsumerTest(queue, dlq chan *dynamomq.Message[test.MessageData], store *sync.Map,
cfg ClientForConsumerTestConfig) dynamomq.Client[test.MessageData] {
return &mock.Client[test.MessageData]{
ReceiveMessageFunc: func(ctx context.Context, params *dynamomq.ReceiveMessageInput) (*dynamomq.ReceiveMessageOutput[test.MessageData], error) {
if cfg.SimulateReceiveMessageNoTemporaryError {
return nil, test.ErrTest
}
if cfg.SimulateReceiveMessageError {
return nil, &dynamomq.DynamoDBAPIError{Cause: test.ErrTest}
}
message := <-queue
return &dynamomq.ReceiveMessageOutput[test.MessageData]{ReceivedMessage: message}, nil
},
DeleteMessageFunc: func(ctx context.Context, params *dynamomq.DeleteMessageInput) (*dynamomq.DeleteMessageOutput, error) {
if cfg.SimulateDeleteMessageError {
return nil, test.ErrTest
}
store.Delete(params.ID)
return &dynamomq.DeleteMessageOutput{}, nil
},
ChangeMessageVisibilityFunc: func(ctx context.Context, params *dynamomq.ChangeMessageVisibilityInput) (*dynamomq.ChangeMessageVisibilityOutput[test.MessageData], error) {
if cfg.SimulateMessageAsVisibleError {
return nil, test.ErrTest
}
return &dynamomq.ChangeMessageVisibilityOutput[test.MessageData]{}, nil
},
MoveMessageToDLQFunc: func(ctx context.Context, params *dynamomq.MoveMessageToDLQInput) (*dynamomq.MoveMessageToDLQOutput[test.MessageData], error) {
if cfg.SimulateMoveMessageToDLQError {
return nil, test.ErrTest
}
v, _ := store.Load(params.ID)
msg, _ := v.(*dynamomq.Message[test.MessageData])
dlq <- msg
return &dynamomq.MoveMessageToDLQOutput[test.MessageData]{
MovedMessage: msg,
}, nil
},
}
}