Add AmqpQueue.Purge() (#14)
This commit is contained in:
parent
95d4df61ad
commit
e662d9534b
|
|
@ -2,6 +2,7 @@ package rabbitmq_amqp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/Azure/go-amqp"
|
"github.com/Azure/go-amqp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -164,6 +165,12 @@ func (a *AmqpQueue) Delete(ctx context.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *AmqpQueue) Purge(ctx context.Context) (int, error) {
|
||||||
|
path := queuePurgePath(a.name)
|
||||||
|
response, err := a.management.Request(ctx, amqp.Null{}, path, commandDelete, []int{responseCode200})
|
||||||
|
return int(response["message_count"].(uint64)), err
|
||||||
|
}
|
||||||
|
|
||||||
func (a *AmqpQueue) Name(queueName string) IQueueSpecification {
|
func (a *AmqpQueue) Name(queueName string) IQueueSpecification {
|
||||||
a.name = queueName
|
a.name = queueName
|
||||||
return a
|
return a
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,9 @@ package rabbitmq_amqp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/Azure/go-amqp"
|
||||||
. "github.com/onsi/ginkgo/v2"
|
. "github.com/onsi/ginkgo/v2"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
|
|
@ -146,4 +149,40 @@ var _ = Describe("AMQP Queue test ", func() {
|
||||||
err = queueSpec.Delete(context.TODO())
|
err = queueSpec.Delete(context.TODO())
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("AMQP Purge Queue should succeed and return the number of messages purged", func() {
|
||||||
|
const queueName = "AMQP Purge Queue should succeed and return the number of messages purged"
|
||||||
|
queueSpec := management.Queue(queueName)
|
||||||
|
_, err := queueSpec.Declare(context.TODO())
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
publishMessages(queueName, 10)
|
||||||
|
purged, err := queueSpec.Purge(context.TODO())
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
Expect(purged).To(Equal(10))
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// TODO: This should be replaced with this library's publish function
|
||||||
|
// but for the time being, we need a way to publish messages or test purposes
|
||||||
|
func publishMessages(queueName string, count int) {
|
||||||
|
conn, err := amqp.Dial(context.TODO(), "amqp://guest:guest@localhost", nil)
|
||||||
|
if err != nil {
|
||||||
|
Fail(err.Error())
|
||||||
|
}
|
||||||
|
session, err := conn.NewSession(context.TODO(), nil)
|
||||||
|
if err != nil {
|
||||||
|
Fail(err.Error())
|
||||||
|
}
|
||||||
|
sender, err := session.NewSender(context.TODO(), queuePath(queueName), nil)
|
||||||
|
if err != nil {
|
||||||
|
Fail(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
err = sender.Send(context.TODO(), amqp.NewMessage([]byte("Message #"+strconv.Itoa(i))), nil)
|
||||||
|
if err != nil {
|
||||||
|
Fail(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,10 @@ import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/google/uuid"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -69,6 +70,10 @@ func queuePath(queueName string) string {
|
||||||
return "/" + queues + "/" + encodePathSegments(queueName)
|
return "/" + queues + "/" + encodePathSegments(queueName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func queuePurgePath(queueName string) string {
|
||||||
|
return "/" + queues + "/" + encodePathSegments(queueName) + "/messages"
|
||||||
|
}
|
||||||
|
|
||||||
func exchangePath(exchangeName string) string {
|
func exchangePath(exchangeName string) string {
|
||||||
return "/" + exchanges + "/" + encodePathSegments(exchangeName)
|
return "/" + exchanges + "/" + encodePathSegments(exchangeName)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ type IQueueSpecification interface {
|
||||||
MaxLengthBytes(length int64) IQueueSpecification
|
MaxLengthBytes(length int64) IQueueSpecification
|
||||||
DeadLetterExchange(dlx string) IQueueSpecification
|
DeadLetterExchange(dlx string) IQueueSpecification
|
||||||
DeadLetterRoutingKey(dlrk string) IQueueSpecification
|
DeadLetterRoutingKey(dlrk string) IQueueSpecification
|
||||||
|
Purge(ctx context.Context) (int, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IQueueInfo represents the information of a queue
|
// IQueueInfo represents the information of a queue
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue