2024-11-21 17:34:08 +08:00
|
|
|
package rabbitmq_amqp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/Azure/go-amqp"
|
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var _ = Describe("AMQP publisher ", func() {
|
2025-01-30 18:29:44 +08:00
|
|
|
It("Send a message to a queue with a Message Target NewPublisher", func() {
|
|
|
|
|
qName := generateNameWithDateTime("Send a message to a queue with a Message Target NewPublisher")
|
2025-01-16 22:26:12 +08:00
|
|
|
connection, err := Dial(context.Background(), []string{"amqp://"}, nil)
|
2024-11-21 17:34:08 +08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(connection).NotTo(BeNil())
|
2025-01-30 18:29:44 +08:00
|
|
|
queueInfo, err := connection.Management().DeclareQueue(context.Background(), &QuorumQueueSpecification{
|
2024-11-21 17:34:08 +08:00
|
|
|
Name: qName,
|
|
|
|
|
})
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(queueInfo).NotTo(BeNil())
|
2025-01-30 18:29:44 +08:00
|
|
|
publisher, err := connection.NewPublisher(context.Background(), &QueueAddress{Queue: qName}, "test")
|
2024-11-21 17:34:08 +08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(publisher).NotTo(BeNil())
|
|
|
|
|
Expect(publisher).To(BeAssignableToTypeOf(&Publisher{}))
|
|
|
|
|
|
2025-01-16 22:26:12 +08:00
|
|
|
publishResult, err := publisher.Publish(context.Background(), amqp.NewMessage([]byte("hello")))
|
2024-11-21 17:34:08 +08:00
|
|
|
Expect(err).To(BeNil())
|
2025-01-16 22:26:12 +08:00
|
|
|
Expect(publishResult).NotTo(BeNil())
|
|
|
|
|
Expect(publishResult.Outcome).To(Equal(&amqp.StateAccepted{}))
|
|
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
nMessages, err := connection.Management().PurgeQueue(context.Background(), qName)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(nMessages).To(Equal(1))
|
|
|
|
|
Expect(publisher.Close(context.Background())).To(BeNil())
|
2025-01-16 22:26:12 +08:00
|
|
|
Expect(connection.Management().DeleteQueue(context.Background(), qName)).To(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
2025-01-30 18:29:44 +08:00
|
|
|
It("NewPublisher should fail to a not existing exchange", func() {
|
2025-01-16 22:26:12 +08:00
|
|
|
connection, err := Dial(context.Background(), []string{"amqp://"}, nil)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(connection).NotTo(BeNil())
|
|
|
|
|
exchangeName := "Nope"
|
2025-01-30 18:29:44 +08:00
|
|
|
publisher, err := connection.NewPublisher(context.Background(), &ExchangeAddress{Exchange: exchangeName}, "test")
|
2025-01-16 22:26:12 +08:00
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
|
Expect(publisher).To(BeNil())
|
|
|
|
|
Expect(connection.Close(context.Background())).To(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("publishResult should released to a not existing routing key", func() {
|
|
|
|
|
eName := generateNameWithDateTime("publishResult should released to a not existing routing key")
|
|
|
|
|
connection, err := Dial(context.Background(), []string{"amqp://"}, nil)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(connection).NotTo(BeNil())
|
2025-01-30 18:29:44 +08:00
|
|
|
exchange, err := connection.Management().DeclareExchange(context.Background(), &TopicExchangeSpecification{
|
2025-01-16 22:26:12 +08:00
|
|
|
Name: eName,
|
|
|
|
|
IsAutoDelete: false,
|
|
|
|
|
})
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(exchange).NotTo(BeNil())
|
|
|
|
|
routingKeyNope := "I don't exist"
|
|
|
|
|
Expect(err).To(BeNil())
|
2025-01-30 18:29:44 +08:00
|
|
|
publisher, err := connection.NewPublisher(context.Background(), &ExchangeAddress{
|
|
|
|
|
Exchange: eName,
|
|
|
|
|
Key: routingKeyNope,
|
|
|
|
|
}, "test")
|
2025-01-16 22:26:12 +08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(publisher).NotTo(BeNil())
|
|
|
|
|
publishResult, err := publisher.Publish(context.Background(), amqp.NewMessage([]byte("hello")))
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(publishResult).NotTo(BeNil())
|
|
|
|
|
Expect(publishResult.Outcome).To(Equal(&amqp.StateReleased{}))
|
|
|
|
|
Expect(connection.Management().DeleteExchange(context.Background(), eName)).To(BeNil())
|
|
|
|
|
Expect(connection.Close(context.Background())).To(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("Send a message to a deleted queue should fail", func() {
|
|
|
|
|
qName := generateNameWithDateTime("Send a message to a deleted queue should fail")
|
|
|
|
|
connection, err := Dial(context.Background(), []string{"amqp://"}, nil)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(connection).NotTo(BeNil())
|
2025-01-30 18:29:44 +08:00
|
|
|
_, err = connection.Management().DeclareQueue(context.Background(), &QuorumQueueSpecification{
|
2025-01-16 22:26:12 +08:00
|
|
|
Name: qName,
|
|
|
|
|
})
|
|
|
|
|
Expect(err).To(BeNil())
|
2025-01-30 18:29:44 +08:00
|
|
|
publisher, err := connection.NewPublisher(context.Background(), &QueueAddress{Queue: qName}, "test")
|
2025-01-16 22:26:12 +08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(publisher).NotTo(BeNil())
|
|
|
|
|
publishResult, err := publisher.Publish(context.Background(), amqp.NewMessage([]byte("hello")))
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(publishResult.Outcome).To(Equal(&amqp.StateAccepted{}))
|
|
|
|
|
err = connection.management.DeleteQueue(context.Background(), qName)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
publishResult, err = publisher.Publish(context.Background(), amqp.NewMessage([]byte("hello")))
|
|
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
|
Expect(connection.Close(context.Background()))
|
2024-11-21 17:34:08 +08:00
|
|
|
})
|
2025-01-30 18:29:44 +08:00
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
It("Multi Targets NewPublisher should fail with StateReleased when the destination does not exist", func() {
|
2025-01-30 18:29:44 +08:00
|
|
|
connection, err := Dial(context.Background(), []string{"amqp://"}, nil)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(connection).NotTo(BeNil())
|
|
|
|
|
publisher, err := connection.NewPublisher(context.Background(), nil, "test")
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(publisher).NotTo(BeNil())
|
2025-02-07 18:00:14 +08:00
|
|
|
qName := generateNameWithDateTime("Targets NewPublisher should fail when the destination does not exist")
|
2025-01-30 18:29:44 +08:00
|
|
|
msg := amqp.NewMessage([]byte("hello"))
|
|
|
|
|
Expect(MessageToAddressHelper(msg, &QueueAddress{Queue: qName})).To(BeNil())
|
|
|
|
|
publishResult, err := publisher.Publish(context.Background(), msg)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(publishResult).NotTo(BeNil())
|
|
|
|
|
Expect(publishResult.Outcome).To(Equal(&amqp.StateReleased{}))
|
2025-02-14 18:08:09 +08:00
|
|
|
msg, err = NewMessageToAddress([]byte("hello"), &QueueAddress{Queue: qName})
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
publishResult, err = publisher.Publish(context.Background(), msg)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(publishResult).NotTo(BeNil())
|
|
|
|
|
Expect(publishResult.Outcome).To(Equal(&amqp.StateReleased{}))
|
|
|
|
|
|
2025-01-30 18:29:44 +08:00
|
|
|
Expect(connection.Close(context.Background())).To(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
It("Multi Targets NewPublisher should success with StateReceived when the destination exists", func() {
|
2025-01-30 18:29:44 +08:00
|
|
|
connection, err := Dial(context.Background(), []string{"amqp://"}, nil)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(connection).NotTo(BeNil())
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
publisher, err := connection.NewPublisher(context.Background(), nil, "test")
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(publisher).NotTo(BeNil())
|
2025-02-07 18:00:14 +08:00
|
|
|
name := generateNameWithDateTime("Targets NewPublisher should success with StateReceived when the destination exists")
|
2025-01-30 18:29:44 +08:00
|
|
|
_, err = connection.Management().DeclareQueue(context.Background(), &QuorumQueueSpecification{
|
|
|
|
|
Name: name,
|
|
|
|
|
})
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
// as first message is sent to a queue, the outcome should be StateReceived
|
|
|
|
|
// since the message was accepted by the existing queue
|
|
|
|
|
msg := amqp.NewMessage([]byte("hello"))
|
|
|
|
|
Expect(MessageToAddressHelper(msg, &QueueAddress{Queue: name})).To(BeNil())
|
|
|
|
|
|
|
|
|
|
publishResult, err := publisher.Publish(context.Background(), msg)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(publishResult).NotTo(BeNil())
|
|
|
|
|
Expect(publishResult.Outcome).To(Equal(&amqp.StateAccepted{}))
|
|
|
|
|
|
|
|
|
|
_, err = connection.Management().DeclareExchange(context.Background(), &TopicExchangeSpecification{
|
|
|
|
|
Name: name,
|
|
|
|
|
IsAutoDelete: false,
|
|
|
|
|
})
|
|
|
|
|
msg = amqp.NewMessage([]byte("hello"))
|
|
|
|
|
Expect(MessageToAddressHelper(msg, &ExchangeAddress{Exchange: name})).To(BeNil())
|
|
|
|
|
// the status should be StateReleased since the exchange does not have any binding
|
|
|
|
|
publishResult, err = publisher.Publish(context.Background(), msg)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(publishResult).NotTo(BeNil())
|
|
|
|
|
Expect(publishResult.Outcome).To(Equal(&amqp.StateReleased{}))
|
|
|
|
|
|
|
|
|
|
// Create the binding between the exchange and the queue
|
|
|
|
|
_, err = connection.Management().Bind(context.Background(), &ExchangeToQueueBindingSpecification{
|
|
|
|
|
SourceExchange: name,
|
|
|
|
|
DestinationQueue: name,
|
|
|
|
|
BindingKey: "#",
|
|
|
|
|
})
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
// the status should be StateAccepted since the exchange has a binding
|
|
|
|
|
msg = amqp.NewMessage([]byte("hello"))
|
|
|
|
|
Expect(MessageToAddressHelper(msg, &ExchangeAddress{Exchange: name})).To(BeNil())
|
|
|
|
|
publishResult, err = publisher.Publish(context.Background(), msg)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(publishResult).NotTo(BeNil())
|
|
|
|
|
Expect(publishResult.Outcome).To(Equal(&amqp.StateAccepted{}))
|
|
|
|
|
Expect(connection.Management().DeleteQueue(context.Background(), name)).To(BeNil())
|
|
|
|
|
Expect(connection.Management().DeleteExchange(context.Background(), name)).To(BeNil())
|
|
|
|
|
Expect(connection.Close(context.Background())).To(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
It("Multi Targets NewPublisher should fail it TO is not set or not valid", func() {
|
2025-01-30 18:29:44 +08:00
|
|
|
connection, err := Dial(context.Background(), []string{"amqp://"}, nil)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(connection).NotTo(BeNil())
|
|
|
|
|
publisher, err := connection.NewPublisher(context.Background(), nil, "test")
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(publisher).NotTo(BeNil())
|
|
|
|
|
msg := amqp.NewMessage([]byte("hello"))
|
|
|
|
|
// the message should fail since the TO property is not set
|
|
|
|
|
publishResult, err := publisher.Publish(context.Background(), msg)
|
|
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
|
Expect(err.Error()).To(ContainSubstring("message properties TO is required"))
|
|
|
|
|
Expect(publishResult).To(BeNil())
|
|
|
|
|
|
|
|
|
|
invalid := "invalid"
|
|
|
|
|
// the message should fail since the TO property is not valid
|
|
|
|
|
msg.Properties = &amqp.MessageProperties{
|
|
|
|
|
To: &invalid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
publishResult, err = publisher.Publish(context.Background(), msg)
|
|
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
|
Expect(err.Error()).To(ContainSubstring("invalid destination address"))
|
|
|
|
|
Expect(publishResult).To(BeNil())
|
|
|
|
|
|
|
|
|
|
Expect(connection.Close(context.Background())).To(BeNil())
|
|
|
|
|
})
|
2024-11-21 17:34:08 +08:00
|
|
|
})
|