@@ -2,6 +2,8 @@ package redisqueue
22
33import (
44 "context"
5+ "fmt"
6+ "log/slog"
57 "os"
68 "testing"
79 "time"
@@ -249,6 +251,67 @@ func TestRun(t *testing.T) {
249251 c .Run (ctx )
250252 })
251253
254+ t .Run ("reclaims pending messages maximum MaxDeliveryCount" , func (tt * testing.T ) {
255+ ctx := context .Background ()
256+ var maxDeliveryCount int64 = 3
257+ var visibilityTimeout time.Duration = 5 * time .Millisecond
258+ var reclaimInterval time.Duration = 1 * time .Millisecond
259+
260+ // create a consumer
261+ c , err := NewConsumerWithOptions (ctx , & ConsumerOptions {
262+ VisibilityTimeout : visibilityTimeout ,
263+ BlockingTimeout : 10 * time .Millisecond ,
264+ ReclaimInterval : 1 * time .Millisecond ,
265+ BufferSize : 100 ,
266+ Concurrency : 10 ,
267+ MaxDeliveryCount : maxDeliveryCount ,
268+ })
269+ require .NoError (tt , err )
270+
271+ // create a producer
272+ p , err := NewProducer ()
273+ require .NoError (tt , err )
274+
275+ // create consumer group
276+ c .redis .XGroupDestroy (ctx , tt .Name (), c .options .GroupName )
277+ c .redis .XGroupCreateMkStream (ctx , tt .Name (), c .options .GroupName , "$" )
278+
279+ // enqueue a message
280+ msg := & Message {
281+ Stream : tt .Name (),
282+ Values : map [string ]interface {}{"test" : "value" },
283+ }
284+ err = p .Enqueue (ctx , msg )
285+ require .NoError (tt , err )
286+
287+ var deliveryCount int64 = 0
288+
289+ // register a handler that will assert the message and then shut down
290+ // the consumer
291+ c .Register (tt .Name (), func (m * Message ) error {
292+ slog .Info ("message received" , "id" , m .ID , "delivery count" , deliveryCount )
293+
294+ deliveryCount ++
295+ assert .Equal (tt , msg .ID , m .ID )
296+ return fmt .Errorf ("dummy error" )
297+ })
298+
299+ // // watch for consumer errors
300+ // go func() {
301+ // <-c.Errors
302+ // }()
303+
304+ // run the consumer
305+ go c .Run (ctx )
306+
307+ // wait for more than VisibilityTimeout + (ReclaimInterval*number_higher_than_max_dlivery_count) to ensure that
308+ // the message was reclaimed more than MaxDeliveryCount
309+
310+ time .Sleep (visibilityTimeout + (reclaimInterval * 10 ) + 1 * time .Millisecond )
311+ c .Shutdown ()
312+ assert .Equal (tt , maxDeliveryCount , deliveryCount )
313+ })
314+
252315 t .Run ("doesn't reclaim if there is no VisibilityTimeout set" , func (tt * testing.T ) {
253316 ctx := context .Background ()
254317
@@ -263,8 +326,8 @@ func TestRun(t *testing.T) {
263326
264327 // create a producer
265328 p , err := NewProducerWithOptions (ctx , & ProducerOptions {
266- StreamMaxLength : 2 ,
267- ApproximateMaxLength : false ,
329+ StreamMaxLength : 2 ,
330+ UseApproximate : false ,
268331 })
269332 require .NoError (tt , err )
270333
@@ -345,8 +408,8 @@ func TestRun(t *testing.T) {
345408
346409 // create a producer
347410 p , err := NewProducerWithOptions (ctx , & ProducerOptions {
348- StreamMaxLength : 1 ,
349- ApproximateMaxLength : false ,
411+ StreamMaxLength : 1 ,
412+ UseApproximate : false ,
350413 })
351414 require .NoError (tt , err )
352415
0 commit comments