2024-09-10 17:26:46 +08:00
|
|
|
package rabbitmq_amqp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-11-21 17:34:08 +08:00
|
|
|
"github.com/Azure/go-amqp"
|
2024-09-10 17:26:46 +08:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
var _ = Describe("AMQP connection Test", func() {
|
|
|
|
|
It("AMQP SASLTypeAnonymous connection should succeed", func() {
|
2024-09-10 17:26:46 +08:00
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
connection, err := Dial(context.Background(), []string{"amqp://"}, &AmqpConnOptions{
|
2024-11-21 17:34:08 +08:00
|
|
|
SASLType: amqp.SASLTypeAnonymous()})
|
2024-09-18 00:59:33 +08:00
|
|
|
Expect(err).To(BeNil())
|
2024-11-21 17:34:08 +08:00
|
|
|
err = connection.Close(context.Background())
|
2024-09-10 17:26:46 +08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
It("AMQP SASLTypePlain connection should succeed", func() {
|
2024-09-11 20:42:05 +08:00
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
connection, err := Dial(context.Background(), []string{"amqp://"}, &AmqpConnOptions{
|
2024-11-21 17:34:08 +08:00
|
|
|
SASLType: amqp.SASLTypePlain("guest", "guest")})
|
2024-09-18 00:59:33 +08:00
|
|
|
|
|
|
|
|
Expect(err).To(BeNil())
|
2024-11-21 17:34:08 +08:00
|
|
|
err = connection.Close(context.Background())
|
2024-09-11 20:42:05 +08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
It("AMQP connection connect to the one correct uri and fails the others", func() {
|
2025-01-16 22:26:12 +08:00
|
|
|
conn, err := Dial(context.Background(), []string{"amqp://localhost:1234", "amqp://nohost:555", "amqp://"}, nil)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(conn.Close(context.Background()))
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
It("AMQP connection should fail due of wrong Port", func() {
|
2025-01-16 22:26:12 +08:00
|
|
|
_, err := Dial(context.Background(), []string{"amqp://localhost:1234"}, nil)
|
2024-09-10 17:26:46 +08:00
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
It("AMQP connection should fail due of wrong Host", func() {
|
2025-01-16 22:26:12 +08:00
|
|
|
_, err := Dial(context.Background(), []string{"amqp://wrong_host:5672"}, nil)
|
|
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
It("AMQP connection should fails with all the wrong uris", func() {
|
2025-01-16 22:26:12 +08:00
|
|
|
_, err := Dial(context.Background(), []string{"amqp://localhost:1234", "amqp://nohost:555", "amqp://nono"}, nil)
|
2024-09-10 17:26:46 +08:00
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
It("AMQP connection should fail due to context cancellation", func() {
|
2024-09-10 17:26:46 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
|
|
|
|
|
cancel()
|
2025-01-16 22:26:12 +08:00
|
|
|
_, err := Dial(ctx, []string{"amqp://"}, nil)
|
2024-09-10 17:26:46 +08:00
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
It("AMQP connection should receive events", func() {
|
2025-01-16 22:26:12 +08:00
|
|
|
ch := make(chan *StateChanged, 1)
|
|
|
|
|
connection, err := Dial(context.Background(), []string{"amqp://"}, nil)
|
2024-09-10 17:26:46 +08:00
|
|
|
Expect(err).To(BeNil())
|
2024-11-21 17:34:08 +08:00
|
|
|
connection.NotifyStatusChange(ch)
|
|
|
|
|
err = connection.Close(context.Background())
|
2024-09-10 17:26:46 +08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
recv := <-ch
|
|
|
|
|
Expect(recv).NotTo(BeNil())
|
2025-01-16 22:26:12 +08:00
|
|
|
Expect(recv.From).To(Equal(&StateOpen{}))
|
|
|
|
|
Expect(recv.To).To(Equal(&StateClosed{}))
|
2024-09-10 17:26:46 +08:00
|
|
|
})
|
|
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
//It("AMQP TLS connection should success with SASLTypeAnonymous ", func() {
|
2024-09-10 17:26:46 +08:00
|
|
|
// amqpConnection := NewAmqpConnection()
|
|
|
|
|
// Expect(amqpConnection).NotTo(BeNil())
|
|
|
|
|
// Expect(amqpConnection).To(BeAssignableToTypeOf(&AmqpConnection{}))
|
|
|
|
|
//
|
2024-11-21 17:34:08 +08:00
|
|
|
// connectionSettings := NewConnUrlHelper().
|
2024-09-10 17:26:46 +08:00
|
|
|
// UseSsl(true).Port(5671).TlsConfig(&tls.Config{
|
|
|
|
|
// //ServerName: "localhost",
|
|
|
|
|
// InsecureSkipVerify: true,
|
|
|
|
|
// })
|
|
|
|
|
// Expect(connectionSettings).NotTo(BeNil())
|
2024-11-21 17:34:08 +08:00
|
|
|
// Expect(connectionSettings).To(BeAssignableToTypeOf(&ConnUrlHelper{}))
|
2024-09-18 00:59:33 +08:00
|
|
|
// err := amqpConnection.Open(context.Background(), connectionSettings)
|
2024-09-10 17:26:46 +08:00
|
|
|
// Expect(err).To(BeNil())
|
|
|
|
|
//})
|
|
|
|
|
})
|