-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconsumer_test.go
168 lines (143 loc) · 4.37 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
package tackle
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var options = Options{
URL: "amqp://guest:guest@rabbitmq:5672",
RemoteExchange: "test.remote-exchange",
Service: "test.service",
RoutingKey: "test-routing-key",
}
func TestConsumerCanBeStartedAndStopped(t *testing.T) {
amqpConsumer := NewConsumer()
go func() {
err := amqpConsumer.Start(&options, func(Delivery) error { return nil })
if err != nil {
t.Error(err)
}
}()
assert.Eventually(t, func() bool { return amqpConsumer.State == StateListening },
time.Second, 100*time.Millisecond)
amqpConsumer.Stop()
assert.Eventually(t, func() bool { return amqpConsumer.State == StateNotListening },
time.Second, 100*time.Millisecond)
}
func TestConsumerCannotBeStartedTwice(t *testing.T) {
consumer := NewConsumer()
go func() {
err := consumer.Start(&options, func(Delivery) error { return nil })
if err != nil {
t.Error(err)
}
}()
defer consumer.Stop()
assert.Eventually(t, func() bool { return consumer.State == StateListening }, time.Second, 100*time.Millisecond)
err := consumer.Start(&options, func(delivery Delivery) error { return nil })
assert.NotNil(t, err)
}
func TestProcessorRanOnceAndPublishWork(t *testing.T) {
counter := &struct {
count int
}{}
consumer := NewConsumer()
go func() {
err := consumer.Start(&options, func(delivery Delivery) error {
counter.count++
return nil
})
assert.Nil(t, err)
}()
assert.Eventually(t, func() bool { return consumer.State == StateListening }, time.Second, 100*time.Millisecond)
params := PublishParams{
Body: []byte("{'test': 'message' }"),
Headers: nil,
AmqpURL: options.URL,
RoutingKey: options.RoutingKey,
Exchange: options.RemoteExchange,
}
err := PublishMessage(¶ms)
assert.Nil(t, err)
assert.Eventually(t, func() bool { return counter.count == 1 }, time.Second, 100*time.Millisecond)
consumer.Stop()
}
func TestConsumerRetry(t *testing.T) {
receivedMessagesCount := 0
timestamps := []time.Time{}
//
// Phase 1: Set up consumer that fails every time and collects
// information about the received messages
//
consumer := NewConsumer()
deadHookExecuted := false
options := Options{
URL: "amqp://guest:guest@rabbitmq:5672",
RemoteExchange: "test.remote-exchange",
Service: "test.service",
RoutingKey: "test-routing-key",
RetryDelay: 1,
RetryLimit: 5,
OnDeadFunc: func(d Delivery) {
deadHookExecuted = true
},
}
go consumer.Start(&options, func(d Delivery) error {
receivedMessagesCount++
timestamps = append(timestamps, time.Now())
return fmt.Errorf("not able to handle it")
})
defer consumer.Stop()
//
// Phase 2: Purge the dead queue and make sure that we have a clean slate.
//
purgeDeadQueueInTests(t, consumer)
//
// Phase 3: Publish the message
//
params := PublishParams{
Body: []byte("hello"),
Headers: nil,
AmqpURL: options.URL,
RoutingKey: options.RoutingKey,
Exchange: options.RemoteExchange,
}
err := PublishMessage(¶ms)
assert.Nil(t, err)
//
// Phase 4: Assert that retry logic is working as advertised
//
t.Run("the message gets retried at least 5 times", func(t *testing.T) {
assert.Eventually(t, func() bool {
return receivedMessagesCount > 5
}, 10*time.Second, 1*time.Second)
})
t.Run("the interval between retries is around 1 second", func(t *testing.T) {
for i := 1; i < 5; i++ {
duration := timestamps[i].Sub(timestamps[i-1])
assert.InDelta(t, duration.Milliseconds(), 1000, 100)
}
})
t.Run("the message ends up in the dead queue", func(t *testing.T) {
deadQueue, err := consumer.channel.QueueInspect(options.GetDeadQueueName())
assert.Nil(t, err)
assert.Equal(t, 1, deadQueue.Messages)
assert.True(t, deadHookExecuted)
})
t.Run("the message in the dead queue is the same one as oiginally sent", func(t *testing.T) {
delivery, ok, err := consumer.channel.Get(options.GetDeadQueueName(), true)
assert.Nil(t, err)
assert.True(t, ok)
assert.Equal(t, string(delivery.Body), "hello")
})
}
func purgeDeadQueueInTests(t *testing.T, consumer *Consumer) {
consumer.retryWithConstantWait("purge the dead queue", 5, 1*time.Second, func() error {
if consumer.channel == nil {
return fmt.Errorf("not yet ready for purging")
}
_, err := consumer.channel.QueuePurge(options.GetDeadQueueName(), false)
return err
})
}