2024-09-10 17:26:46 +08:00
|
|
|
package rabbitmq_amqp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var _ = Describe("AMQP Connection Test", func() {
|
2024-09-18 00:59:33 +08:00
|
|
|
It("AMQP SASLTypeAnonymous Connection should succeed", func() {
|
2024-09-10 17:26:46 +08:00
|
|
|
amqpConnection := NewAmqpConnection()
|
|
|
|
|
Expect(amqpConnection).NotTo(BeNil())
|
|
|
|
|
Expect(amqpConnection).To(BeAssignableToTypeOf(&AmqpConnection{}))
|
|
|
|
|
|
|
|
|
|
connectionSettings := NewConnectionSettings()
|
|
|
|
|
Expect(connectionSettings).NotTo(BeNil())
|
2024-09-11 20:42:05 +08:00
|
|
|
connectionSettings.SaslMechanism(SaslMechanism{Type: Anonymous})
|
2024-09-10 17:26:46 +08:00
|
|
|
Expect(connectionSettings).To(BeAssignableToTypeOf(&ConnectionSettings{}))
|
2024-09-18 00:59:33 +08:00
|
|
|
|
|
|
|
|
err := amqpConnection.Open(context.Background(), connectionSettings)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
err = amqpConnection.Close(context.Background())
|
2024-09-10 17:26:46 +08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
2024-09-18 00:59:33 +08:00
|
|
|
It("AMQP SASLTypePlain Connection should succeed", func() {
|
2024-09-11 20:42:05 +08:00
|
|
|
amqpConnection := NewAmqpConnection()
|
|
|
|
|
Expect(amqpConnection).NotTo(BeNil())
|
|
|
|
|
Expect(amqpConnection).To(BeAssignableToTypeOf(&AmqpConnection{}))
|
|
|
|
|
|
|
|
|
|
connectionSettings := NewConnectionSettings()
|
|
|
|
|
Expect(connectionSettings).NotTo(BeNil())
|
|
|
|
|
Expect(connectionSettings).To(BeAssignableToTypeOf(&ConnectionSettings{}))
|
|
|
|
|
connectionSettings.SaslMechanism(SaslMechanism{Type: Plain})
|
2024-09-18 00:59:33 +08:00
|
|
|
|
|
|
|
|
err := amqpConnection.Open(context.Background(), connectionSettings)
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
err = amqpConnection.Close(context.Background())
|
2024-09-11 20:42:05 +08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
2024-09-10 17:26:46 +08:00
|
|
|
It("AMQP Connection should fail due of wrong port", func() {
|
|
|
|
|
amqpConnection := NewAmqpConnection()
|
|
|
|
|
Expect(amqpConnection).NotTo(BeNil())
|
|
|
|
|
Expect(amqpConnection).To(BeAssignableToTypeOf(&AmqpConnection{}))
|
|
|
|
|
connectionSettings := NewConnectionSettings()
|
|
|
|
|
Expect(connectionSettings).NotTo(BeNil())
|
|
|
|
|
Expect(connectionSettings).To(BeAssignableToTypeOf(&ConnectionSettings{}))
|
|
|
|
|
connectionSettings.Host("localhost").Port(1234)
|
2024-09-18 00:59:33 +08:00
|
|
|
|
|
|
|
|
err := amqpConnection.Open(context.Background(), connectionSettings)
|
2024-09-10 17:26:46 +08:00
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("AMQP Connection should fail due of wrong host", func() {
|
|
|
|
|
amqpConnection := NewAmqpConnection()
|
|
|
|
|
Expect(amqpConnection).NotTo(BeNil())
|
|
|
|
|
Expect(amqpConnection).To(BeAssignableToTypeOf(&AmqpConnection{}))
|
|
|
|
|
|
|
|
|
|
connectionSettings := NewConnectionSettings()
|
|
|
|
|
Expect(connectionSettings).NotTo(BeNil())
|
|
|
|
|
Expect(connectionSettings).To(BeAssignableToTypeOf(&ConnectionSettings{}))
|
|
|
|
|
connectionSettings.Host("wronghost").Port(5672)
|
2024-09-18 00:59:33 +08:00
|
|
|
|
|
|
|
|
err := amqpConnection.Open(context.Background(), connectionSettings)
|
2024-09-10 17:26:46 +08:00
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
2024-09-18 00:59:33 +08:00
|
|
|
It("AMQP Connection should fail due to context cancellation", func() {
|
2024-09-10 17:26:46 +08:00
|
|
|
amqpConnection := NewAmqpConnection()
|
|
|
|
|
Expect(amqpConnection).NotTo(BeNil())
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
|
|
|
|
|
cancel()
|
|
|
|
|
err := amqpConnection.Open(ctx, NewConnectionSettings())
|
|
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
|
})
|
|
|
|
|
|
2024-09-18 00:59:33 +08:00
|
|
|
It("AMQP Connection should receive events", func() {
|
2024-09-10 17:26:46 +08:00
|
|
|
amqpConnection := NewAmqpConnection()
|
|
|
|
|
Expect(amqpConnection).NotTo(BeNil())
|
|
|
|
|
ch := make(chan *StatusChanged, 1)
|
|
|
|
|
amqpConnection.NotifyStatusChange(ch)
|
2024-09-18 00:59:33 +08:00
|
|
|
err := amqpConnection.Open(context.Background(), NewConnectionSettings())
|
2024-09-10 17:26:46 +08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
recv := <-ch
|
|
|
|
|
Expect(recv).NotTo(BeNil())
|
|
|
|
|
Expect(recv.From).To(Equal(Closed))
|
|
|
|
|
Expect(recv.To).To(Equal(Open))
|
|
|
|
|
|
|
|
|
|
err = amqpConnection.Close(context.Background())
|
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
recv = <-ch
|
|
|
|
|
Expect(recv).NotTo(BeNil())
|
|
|
|
|
|
|
|
|
|
Expect(recv.From).To(Equal(Open))
|
|
|
|
|
Expect(recv.To).To(Equal(Closed))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//It("AMQP TLS Connection should success with SASLTypeAnonymous ", func() {
|
|
|
|
|
// amqpConnection := NewAmqpConnection()
|
|
|
|
|
// Expect(amqpConnection).NotTo(BeNil())
|
|
|
|
|
// Expect(amqpConnection).To(BeAssignableToTypeOf(&AmqpConnection{}))
|
|
|
|
|
//
|
|
|
|
|
// connectionSettings := NewConnectionSettings().
|
|
|
|
|
// UseSsl(true).Port(5671).TlsConfig(&tls.Config{
|
|
|
|
|
// //ServerName: "localhost",
|
|
|
|
|
// InsecureSkipVerify: true,
|
|
|
|
|
// })
|
|
|
|
|
// Expect(connectionSettings).NotTo(BeNil())
|
|
|
|
|
// Expect(connectionSettings).To(BeAssignableToTypeOf(&ConnectionSettings{}))
|
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())
|
|
|
|
|
//})
|
|
|
|
|
})
|