@@ -2,8 +2,11 @@ package middleware
22
33import (
44 "context"
5+ "errors"
56 "log"
7+ "sync/atomic"
68 "testing"
9+ "time"
710
811 amqp "github.com/rabbitmq/amqp091-go"
912 "github.com/stretchr/testify/assert"
@@ -13,32 +16,66 @@ import (
1316
1417func TestAckDelivery (t * testing.T ) {
1518 tests := []struct {
16- handler amqprpc.HandlerFunc
17- name string
19+ handler amqprpc.HandlerFunc
20+ name string
21+ ackReturn error
22+ didSendOnChannel bool
1823 }{
1924 {
20- name : "handler doesn't ack" ,
21- handler : func (_ context.Context , _ * amqprpc.ResponseWriter , _ amqp.Delivery ) {},
25+ name : "handler doesn't ack" ,
26+ handler : func (_ context.Context , _ * amqprpc.ResponseWriter , _ amqp.Delivery ) {},
27+ ackReturn : nil ,
2228 },
2329 {
2430 name : "handler does ack" ,
2531 handler : func (_ context.Context , _ * amqprpc.ResponseWriter , d amqp.Delivery ) {
2632 _ = d .Ack (false )
2733 },
34+ ackReturn : nil ,
35+ },
36+ {
37+ name : "handler fails to ack" ,
38+ handler : func (_ context.Context , _ * amqprpc.ResponseWriter , _ amqp.Delivery ) {},
39+ ackReturn : errors .New ("issue in the multiplexer" ), //nolint:err113 // Just a test
40+ didSendOnChannel : true ,
2841 },
2942 }
3043 for _ , tt := range tests {
3144 t .Run (tt .name , func (t * testing.T ) {
32- acknowledger := & amqprpc.MockAcknowledger {}
45+ acknowledger := & amqprpc.MockAcknowledger {
46+ OnAckFn : func () error {
47+ return tt .ackReturn
48+ },
49+ }
50+
51+ didSendOnCh := atomic.Bool {}
52+ didSendOnCh .Store (false )
53+
54+ // We setup a channel to ensure we don't proceed until we started
55+ // the go routine that will listen to the signal.
56+ isListening := make (chan struct {})
57+
58+ ch := make (chan struct {})
59+ go func () {
60+ close (isListening )
61+ <- ch
62+ didSendOnCh .Store (true )
63+ }()
64+
65+ // Block until ready.
66+ <- isListening
3367
34- handler := AckDelivery (log .Printf )(tt .handler )
68+ handler := AckDelivery (OnAckErrorSendOnChannel ( log .Printf , ch ) )(tt .handler )
3569
3670 rw := amqprpc .NewResponseWriter (& amqp.Publishing {})
37- d := amqp.Delivery {Acknowledger : acknowledger }
71+ d := amqp.Delivery {Acknowledger : acknowledger , CorrelationId : "id-1234" }
3872
3973 handler (context .Background (), rw , d )
4074
4175 assert .Equal (t , 1 , acknowledger .Acks )
76+ assert .Eventually (t , func () bool {
77+ return didSendOnCh .Load () == tt .didSendOnChannel
78+ }, 2 * time .Second , 100 * time .Millisecond )
4279 })
4380 }
4481}
0 commit comments