Skip to content

Commit 5fc29f4

Browse files
authored
Add support for querying queue info (#13)
Request `GET /queues/:queue` and return the details or ErrDoesNotExist
1 parent e662d95 commit 5fc29f4

File tree

6 files changed

+73
-9
lines changed

6 files changed

+73
-9
lines changed

rabbitmq_amqp/amqp_exchange_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rabbitmq_amqp
22

33
import (
44
"context"
5+
56
. "github.com/onsi/ginkgo/v2"
67
. "github.com/onsi/gomega"
78
)

rabbitmq_amqp/amqp_management.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"github.com/Azure/go-amqp"
8-
"github.com/google/uuid"
97
"strconv"
108
"time"
9+
10+
"github.com/Azure/go-amqp"
11+
"github.com/google/uuid"
1112
)
1213

1314
var ErrPreconditionFailed = errors.New("precondition Failed")
15+
var ErrDoesNotExist = errors.New("does not exist")
1416

1517
type AmqpManagement struct {
1618
session *amqp.Session
@@ -185,20 +187,33 @@ func (a *AmqpManagement) request(ctx context.Context, id string, body any, path
185187
return msg.Value.(map[string]any), nil
186188
}
187189

188-
i, _ := strconv.Atoi(*msg.Properties.Subject)
190+
responseCode, _ := strconv.Atoi(*msg.Properties.Subject)
189191

190-
err = a.validateResponseCode(i, expectedResponseCodes)
192+
err = a.validateResponseCode(responseCode, expectedResponseCodes)
191193
if err != nil {
192194
return nil, err
193195
}
194196

197+
if responseCode == responseCode404 {
198+
return nil, ErrDoesNotExist
199+
}
200+
195201
return make(map[string]any), nil
196202
}
197203

198204
func (a *AmqpManagement) Queue(queueName string) IQueueSpecification {
199205
return newAmqpQueue(a, queueName)
200206
}
201207

208+
func (a *AmqpManagement) QueueInfo(ctx context.Context, queueName string) (IQueueInfo, error) {
209+
path := queuePath(queueName)
210+
result, err := a.Request(ctx, amqp.Null{}, path, commandGet, []int{responseCode200, responseCode404})
211+
if err != nil {
212+
return nil, err
213+
}
214+
return newAmqpQueueInfo(result), nil
215+
}
216+
202217
func (a *AmqpManagement) QueueClientName() IQueueSpecification {
203218
return newAmqpQueue(a, "")
204219
}

rabbitmq_amqp/amqp_management_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package rabbitmq_amqp
22

33
import (
44
"context"
5+
"time"
6+
7+
"github.com/Azure/go-amqp"
58
. "github.com/onsi/ginkgo/v2"
69
. "github.com/onsi/gomega"
7-
"time"
810
)
911

1012
var _ = Describe("Management tests", func() {
@@ -68,4 +70,22 @@ var _ = Describe("Management tests", func() {
6870
Expect(management.Close(context.Background())).To(BeNil())
6971
amqpConnection.Close(context.Background())
7072
})
73+
74+
It("GET on non-existing queue returns ErrDoesNotExist", func() {
75+
amqpConnection := NewAmqpConnection()
76+
Expect(amqpConnection).NotTo(BeNil())
77+
Expect(amqpConnection).To(BeAssignableToTypeOf(&AmqpConnection{}))
78+
79+
connectionSettings := NewConnectionSettings()
80+
Expect(connectionSettings).NotTo(BeNil())
81+
Expect(connectionSettings).To(BeAssignableToTypeOf(&ConnectionSettings{}))
82+
err := amqpConnection.Open(context.Background(), connectionSettings)
83+
Expect(err).To(BeNil())
84+
85+
management := amqpConnection.Management()
86+
path := "/queues/i-do-not-exist"
87+
result, err := management.Request(context.Background(), amqp.Null{}, path, commandGet, []int{responseCode200, responseCode404})
88+
Expect(err).To(Equal(ErrDoesNotExist))
89+
Expect(result).To(BeNil())
90+
})
7191
})

rabbitmq_amqp/amqp_queue_test.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var _ = Describe("AMQP Queue test ", func() {
2929
Expect(connection.Close(context.Background())).To(BeNil())
3030
})
3131

32-
It("AMQP Queue Declare With Response and Delete should succeed", func() {
32+
It("AMQP Queue Declare With Response and Get/Delete should succeed", func() {
3333
const queueName = "AMQP Queue Declare With Response and Delete should succeed"
3434
queueSpec := management.Queue(queueName)
3535
queueInfo, err := queueSpec.Declare(context.TODO())
@@ -40,11 +40,16 @@ var _ = Describe("AMQP Queue test ", func() {
4040
Expect(queueInfo.IsAutoDelete()).To(BeFalse())
4141
Expect(queueInfo.IsExclusive()).To(BeFalse())
4242
Expect(queueInfo.Type()).To(Equal(Classic))
43+
44+
// validate GET (query queue info)
45+
queueInfoReceived, err := management.QueueInfo(context.TODO(), queueName)
46+
Expect(queueInfoReceived).To(Equal(queueInfo))
47+
4348
err = queueSpec.Delete(context.TODO())
4449
Expect(err).To(BeNil())
4550
})
4651

47-
It("AMQP Queue Declare With Parameters and Delete should succeed", func() {
52+
It("AMQP Queue Declare With Parameters and Get/Delete should succeed", func() {
4853
const queueName = "AMQP Queue Declare With Parameters and Delete should succeed"
4954
queueSpec := management.Queue(queueName).Exclusive(true).
5055
AutoDelete(true).
@@ -67,11 +72,15 @@ var _ = Describe("AMQP Queue test ", func() {
6772
Expect(queueInfo.GetArguments()).To(HaveKeyWithValue("x-dead-letter-routing-key", "dead-letter-routing-key"))
6873
Expect(queueInfo.GetArguments()).To(HaveKeyWithValue("max-length-bytes", int64(1000000000)))
6974

75+
// validate GET (query queue info)
76+
queueInfoReceived, err := management.QueueInfo(context.TODO(), queueName)
77+
Expect(queueInfoReceived).To(Equal(queueInfo))
78+
7079
err = queueSpec.Delete(context.TODO())
7180
Expect(err).To(BeNil())
7281
})
7382

74-
It("AMQP Declare Quorum Queue and Delete should succeed", func() {
83+
It("AMQP Declare Quorum Queue and Get/Delete should succeed", func() {
7584
const queueName = "AMQP Declare Quorum Queue and Delete should succeed"
7685
// Quorum queue will ignore Exclusive and AutoDelete settings
7786
// since they are not supported by quorum queues
@@ -86,11 +95,16 @@ var _ = Describe("AMQP Queue test ", func() {
8695
Expect(queueInfo.IsAutoDelete()).To(BeFalse())
8796
Expect(queueInfo.IsExclusive()).To(BeFalse())
8897
Expect(queueInfo.Type()).To(Equal(Quorum))
98+
99+
// validate GET (query queue info)
100+
queueInfoReceived, err := management.QueueInfo(context.TODO(), queueName)
101+
Expect(queueInfoReceived).To(Equal(queueInfo))
102+
89103
err = queueSpec.Delete(context.TODO())
90104
Expect(err).To(BeNil())
91105
})
92106

93-
It("AMQP Declare Stream Queue and Delete should succeed", func() {
107+
It("AMQP Declare Stream Queue and Get/Delete should succeed", func() {
94108
const queueName = "AMQP Declare Stream Queue and Delete should succeed"
95109
// Stream queue will ignore Exclusive and AutoDelete settings
96110
// since they are not supported by quorum queues
@@ -105,6 +119,11 @@ var _ = Describe("AMQP Queue test ", func() {
105119
Expect(queueInfo.IsAutoDelete()).To(BeFalse())
106120
Expect(queueInfo.IsExclusive()).To(BeFalse())
107121
Expect(queueInfo.Type()).To(Equal(Stream))
122+
123+
// validate GET (query queue info)
124+
queueInfoReceived, err := management.QueueInfo(context.TODO(), queueName)
125+
Expect(queueInfoReceived).To(Equal(queueInfo))
126+
108127
err = queueSpec.Delete(context.TODO())
109128
Expect(err).To(BeNil())
110129
})
@@ -160,6 +179,13 @@ var _ = Describe("AMQP Queue test ", func() {
160179
Expect(err).To(BeNil())
161180
Expect(purged).To(Equal(10))
162181
})
182+
183+
It("AMQP GET on non-existing queue should return ErrDoesNotExist", func() {
184+
const queueName = "This queue does not exist"
185+
result, err := management.QueueInfo(context.TODO(), queueName)
186+
Expect(err).To(Equal(ErrDoesNotExist))
187+
Expect(result).To(BeNil())
188+
})
163189
})
164190

165191
// TODO: This should be replaced with this library's publish function

rabbitmq_amqp/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
responseCode200 = 200
1515
responseCode201 = 201
1616
responseCode204 = 204
17+
responseCode404 = 404
1718
responseCode409 = 409
1819
commandPut = "PUT"
1920
commandGet = "GET"

rabbitmq_amqp/management.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type IManagement interface {
88
Open(ctx context.Context, connection IConnection) error
99
Close(ctx context.Context) error
1010
Queue(queueName string) IQueueSpecification
11+
QueueInfo(ctx context.Context, queueName string) (IQueueInfo, error)
1112
Exchange(exchangeName string) IExchangeSpecification
1213
Binding() IBindingSpecification
1314
QueueClientName() IQueueSpecification

0 commit comments

Comments
 (0)