-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathamqp_queue_test.go
More file actions
314 lines (271 loc) · 11.7 KB
/
amqp_queue_test.go
File metadata and controls
314 lines (271 loc) · 11.7 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
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
310
311
312
313
314
package rabbitmqamqp
import (
"context"
"strconv"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("AMQP Queue test ", func() {
var connection *AmqpConnection
var management *AmqpManagement
BeforeEach(func() {
conn, err := Dial(context.TODO(), "amqp://", nil)
Expect(err).To(BeNil())
connection = conn
management = connection.Management()
})
AfterEach(func() {
Expect(connection.Close(context.Background())).To(BeNil())
})
It("AMQP Queue Declare With Response and Get/Delete should succeed", func() {
var queueName = generateName("AMQP Queue Declare With Response and Delete should succeed")
queueInfo, err := management.DeclareQueue(context.TODO(), &QuorumQueueSpecification{
Name: queueName,
})
Expect(err).To(BeNil())
Expect(queueInfo).NotTo(BeNil())
Expect(queueInfo.Name()).To(Equal(queueName))
Expect(queueInfo.IsDurable()).To(BeTrue())
Expect(queueInfo.IsAutoDelete()).To(BeFalse())
Expect(queueInfo.IsExclusive()).To(BeFalse())
Expect(queueInfo.Type()).To(Equal(Quorum))
// validate GET (query queue info)
queueInfoReceived, err := management.QueueInfo(context.TODO(), queueName)
Expect(err).To(BeNil())
Expect(queueInfoReceived).To(Equal(queueInfo))
err = management.DeleteQueue(context.TODO(), queueName)
Expect(err).To(BeNil())
})
It("AMQP Queue Declare With Parameters and Get/Delete should succeed", func() {
var queueName = generateName("AMQP Queue Declare With Parameters and Delete should succeed")
queueInfo, err := management.DeclareQueue(context.TODO(), &ClassicQueueSpecification{
Name: queueName,
IsAutoDelete: true,
IsExclusive: true,
AutoExpire: 1000,
MessageTTL: 1000,
OverflowStrategy: &DropHeadOverflowStrategy{},
SingleActiveConsumer: true,
DeadLetterExchange: "dead-letter-exchange",
DeadLetterRoutingKey: "dead-letter-routing-key",
MaxLength: 9_000,
MaxLengthBytes: CapacityGB(1),
MaxPriority: 2,
LeaderLocator: &BalancedLeaderLocator{},
Arguments: map[string]any{
"foo": "bar",
},
})
Expect(err).To(BeNil())
Expect(queueInfo).NotTo(BeNil())
Expect(queueInfo.Name()).To(Equal(queueName))
Expect(queueInfo.IsDurable()).To(BeTrue())
Expect(queueInfo.IsAutoDelete()).To(BeTrue())
Expect(queueInfo.IsExclusive()).To(BeTrue())
Expect(queueInfo.Type()).To(Equal(Classic))
Expect(queueInfo.messageCount).To(BeZero())
Expect(queueInfo.consumerCount).To(BeZero())
Expect(queueInfo.Leader()).To(ContainSubstring("rabbit"))
Expect(len(queueInfo.Members())).To(BeNumerically(">", 0))
Expect(queueInfo.Arguments()).To(HaveKeyWithValue("x-dead-letter-exchange", "dead-letter-exchange"))
Expect(queueInfo.Arguments()).To(HaveKeyWithValue("x-dead-letter-routing-key", "dead-letter-routing-key"))
Expect(queueInfo.Arguments()).To(HaveKeyWithValue("x-max-length-bytes", int64(1000000000)))
Expect(queueInfo.Arguments()).To(HaveKeyWithValue("x-max-length", int64(9000)))
Expect(queueInfo.Arguments()).To(HaveKeyWithValue("x-message-ttl", int64(1000)))
Expect(queueInfo.Arguments()).To(HaveKeyWithValue("x-single-active-consumer", true))
Expect(queueInfo.Arguments()).To(HaveKeyWithValue("x-overflow", "drop-head"))
Expect(queueInfo.Arguments()).To(HaveKeyWithValue("x-expires", int64(1000)))
Expect(queueInfo.Arguments()).To(HaveKeyWithValue("x-max-priority", int64(2)))
Expect(queueInfo.Arguments()).To(HaveKeyWithValue("x-queue-leader-locator", "random"))
Expect(queueInfo.Arguments()).To(HaveKeyWithValue("foo", "bar"))
// validate GET (query queue info)
queueInfoReceived, err := management.QueueInfo(context.TODO(), queueName)
Expect(err).To(BeNil())
Expect(queueInfoReceived).To(Equal(queueInfo))
err = management.DeleteQueue(context.TODO(), queueName)
Expect(err).To(BeNil())
})
It("AMQP Declare Quorum Queue and Get/Delete should succeed", func() {
var queueName = generateName("AMQP Declare Quorum Queue and Delete should succeed")
// Quorum queue will ignore Exclusive and AutoDelete settings
// since they are not supported by quorum queues
queueInfo, err := management.DeclareQueue(context.TODO(), &QuorumQueueSpecification{
Name: queueName,
})
Expect(err).To(BeNil())
Expect(queueInfo).NotTo(BeNil())
Expect(queueInfo.Name()).To(Equal(queueName))
Expect(queueInfo.IsDurable()).To(BeTrue())
Expect(queueInfo.IsAutoDelete()).To(BeFalse())
Expect(queueInfo.IsExclusive()).To(BeFalse())
Expect(queueInfo.Type()).To(Equal(Quorum))
// validate GET (query queue info)
queueInfoReceived, err := management.QueueInfo(context.TODO(), queueName)
Expect(err).To(BeNil())
Expect(queueInfoReceived).To(Equal(queueInfo))
err = management.DeleteQueue(context.TODO(), queueName)
Expect(err).To(BeNil())
})
It("AMQP Declare Stream Queue and Get/Delete should succeed", func() {
const queueName = "AMQP Declare Stream Queue and Delete should succeed"
// Stream queue will ignore Exclusive and AutoDelete settings
// since they are not supported by quorum queues
queueInfo, err := management.DeclareQueue(context.TODO(), &ClassicQueueSpecification{
Name: queueName,
IsAutoDelete: false,
IsExclusive: false})
Expect(err).To(BeNil())
Expect(queueInfo).NotTo(BeNil())
Expect(queueInfo.Name()).To(Equal(queueName))
Expect(queueInfo.IsDurable()).To(BeTrue())
Expect(queueInfo.IsAutoDelete()).To(BeFalse())
Expect(queueInfo.IsExclusive()).To(BeFalse())
Expect(queueInfo.Type()).To(Equal(Classic))
// validate GET (query queue info)
queueInfoReceived, err := management.QueueInfo(context.TODO(), queueName)
Expect(err).To(BeNil())
Expect(queueInfoReceived).To(Equal(queueInfo))
err = management.DeleteQueue(context.TODO(), queueName)
Expect(err).To(BeNil())
})
It("AMQP Classic should return queue info ", func() {
const queueName = "AMQP Classic should return queue info"
qName := generateName(queueName)
queueInfo, err := management.DeclareQueue(context.TODO(), &ClassicQueueSpecification{
Name: qName,
MaxPriority: 32,
MaxLengthBytes: CapacityGB(1),
MaxLength: CapacityKB(1024),
IsAutoDelete: false,
})
Expect(err).To(BeNil())
Expect(queueInfo).NotTo(BeNil())
Expect(queueInfo.Name()).To(Equal(qName))
Expect(queueInfo.IsDurable()).To(BeTrue())
Expect(queueInfo.IsAutoDelete()).To(BeFalse())
Expect(queueInfo.IsExclusive()).To(BeFalse())
Expect(queueInfo.Type()).To(Equal(Classic))
qInfoReceived, err := management.QueueInfo(context.TODO(), qName)
Expect(err).To(BeNil())
Expect(qInfoReceived).To(Equal(queueInfo))
Expect(management.DeleteQueue(context.TODO(), qName))
})
It("AMQP Declare Queue should fail with Precondition fail", func() {
// The first queue is declared as Classic, and it should succeed
// The second queue is declared as Quorum, and it should fail since it is already declared as Classic
// queueName := generateName("AMQP Declare Queue should fail with Precondition fail")
queueName := "ab"
_, err := management.DeclareQueue(context.TODO(), &ClassicQueueSpecification{
Name: queueName,
})
Expect(err).To(BeNil())
_, err = management.DeclareQueue(context.TODO(), &QuorumQueueSpecification{
Name: queueName,
})
Expect(err).NotTo(BeNil())
Expect(err).To(Equal(ErrPreconditionFailed))
err = management.DeleteQueue(context.TODO(), queueName)
Expect(err).To(BeNil())
})
It("AMQP Declare Queue should fail during validation", func() {
queueName := generateName("AMQP Declare Queue should fail during validation")
_, err := management.DeclareQueue(context.TODO(), &QuorumQueueSpecification{
Name: queueName,
MaxLengthBytes: -1,
})
Expect(err).NotTo(BeNil())
Expect(err).To(HaveOccurred())
})
It("AMQP Declare Queue should fail if queue specification is nil", func() {
_, err := management.DeclareQueue(context.TODO(), nil)
Expect(err).NotTo(BeNil())
Expect(err).To(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("queue specification cannot be nil"))
})
It("AMQP Declare Queue should create client name queue", func() {
queueInfo, err := management.DeclareQueue(context.TODO(), &AutoGeneratedQueueSpecification{
IsAutoDelete: true,
IsExclusive: false,
})
Expect(err).To(BeNil())
Expect(queueInfo).NotTo(BeNil())
Expect(queueInfo.Name()).To(ContainSubstring("client.gen-"))
err = management.DeleteQueue(context.TODO(), queueInfo.Name())
Expect(err).To(BeNil())
})
It("AMQP Purge Queue should succeed and return the number of messages purged", func() {
queueName := generateName("AMQP Purge Queue should succeed and return the number of messages purged")
queueInfo, err := management.DeclareQueue(context.TODO(), &QuorumQueueSpecification{
Name: queueName,
})
Expect(err).To(BeNil())
publishMessages(queueName, 10)
purged, err := management.PurgeQueue(context.TODO(), queueInfo.Name())
Expect(err).To(BeNil())
Expect(purged).To(Equal(10))
err = management.DeleteQueue(context.TODO(), queueName)
Expect(err).To(BeNil())
})
It("AMQP GET on non-existing queue should return ErrDoesNotExist", func() {
const queueName = "This queue does not exist"
result, err := management.QueueInfo(context.TODO(), queueName)
Expect(err).To(Equal(ErrDoesNotExist))
Expect(result).To(BeNil())
})
It("should fail if declare a JMS queue in the open source RabbitMQ", func() {
queueName := generateName("should fail if declare a JMS queue in the open source RabbitMQ")
_, err := management.DeclareQueue(context.TODO(), &JMSQueueSpecification{
Name: queueName,
})
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("JMSQueueSpecification is only supported on Tanzu RabbitMQ"))
})
It("should fail if declare a Delayed queue in the open source RabbitMQ", func() {
queueName := generateName("should fail if declare a Delayed queue in the open source RabbitMQ")
_, err := management.DeclareQueue(context.TODO(), &DelayedQueueSpecification{
Name: queueName,
})
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("DelayedQueueSpecification is only supported on Tanzu RabbitMQ"))
})
// default
It("AMQP Declare Queue with DefaultQueueSpecification should succeed", func() {
queueName := generateName("AMQP Declare Queue with DefaultQueueSpecification should succeed")
queueInfo, err := management.DeclareQueue(context.TODO(), &DefaultQueueSpecification{
Name: queueName,
DeadLetterExchange: "dead-letter-exchange",
DeadLetterRoutingKey: "dead-letter-routing-key",
})
Expect(err).To(BeNil())
Expect(queueInfo).NotTo(BeNil())
Expect(queueInfo.Name()).To(Equal(queueName))
Expect(queueInfo.IsDurable()).To(BeTrue())
Expect(queueInfo.IsAutoDelete()).To(BeFalse())
Expect(queueInfo.IsExclusive()).To(BeFalse())
Expect(queueInfo.arguments["x-dead-letter-exchange"]).To(Equal("dead-letter-exchange"))
Expect(queueInfo.arguments["x-dead-letter-routing-key"]).To(Equal("dead-letter-routing-key"))
// the default value for queue type is classic
Expect(queueInfo.Type()).To(Equal(Classic))
err = management.DeleteQueue(context.TODO(), queueName)
Expect(err).To(BeNil())
})
})
func publishMessages(queueName string, count int, args ...string) {
conn, err := Dial(context.TODO(), "amqp://guest:guest@localhost", nil)
Expect(err).To(BeNil())
publisher, err := conn.NewPublisher(context.TODO(), &QueueAddress{Queue: queueName}, nil)
Expect(err).To(BeNil())
Expect(publisher).NotTo(BeNil())
for i := 0; i < count; i++ {
body := "Message #" + strconv.Itoa(i)
if len(args) > 0 {
body = args[0]
}
publishResult, err := publisher.Publish(context.TODO(), NewMessage([]byte(body)))
Expect(err).To(BeNil())
Expect(publishResult).NotTo(BeNil())
Expect(publishResult.Outcome).To(Equal(&StateAccepted{}))
}
err = conn.Close(context.TODO())
Expect(err).To(BeNil())
}