Skip to content

Commit

Permalink
added err to connection.Start (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmorelli92 authored Oct 2, 2023
1 parent 3e07514 commit f356a80
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 13 deletions.
14 changes: 11 additions & 3 deletions bunnify/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,19 @@ func NewConnection(opts ...func(*connectionOption)) *Connection {
}

// Start establishes the connection towards the AMQP server.
func (c *Connection) Start() {
// Only returns errors when the uri is not valid (retry won't do a thing)
func (c *Connection) Start() error {
var err error
var conn *amqp.Connection
ticker := time.NewTicker(c.options.reconnectInterval)

uri, err := amqp.ParseURI(c.options.uri)
if err != nil {
return err
}

for {
conn, err = amqp.Dial(c.options.uri)
conn, err = amqp.Dial(uri.String())
if err == nil {
break
}
Expand All @@ -84,9 +90,11 @@ func (c *Connection) Start() {
<-conn.NotifyClose(make(chan *amqp.Error))
if !c.connectionClosedBySystem {
notifyConnectionLost(c.options.notificationChannel)
c.Start()
_ = c.Start()
}
}()

return nil
}

// Closes connection with towards the AMQP server
Expand Down
21 changes: 19 additions & 2 deletions tests/consumer_invalid_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,30 @@ import (
"go.uber.org/goleak"
)

func TestConnectionReturnErrorWhenNotValidURI(t *testing.T) {
// Setup
connection := bunnify.NewConnection(bunnify.WithURI("13123"))

// Exercise
err := connection.Start()

// Assert
if err == nil {
t.Fatal(err)
}

goleak.VerifyNone(t)
}

func TestConsumerShouldReturnErrorWhenNoHandlersSpecified(t *testing.T) {
// Setup
connection := bunnify.NewConnection()
connection.Start()
consumer := connection.NewConsumer("queueName")
if err := connection.Start(); err != nil {
t.Fatal(err)
}

// Exercise
consumer := connection.NewConsumer("queueName")
err := consumer.Consume()

// Assert
Expand Down
18 changes: 14 additions & 4 deletions tests/consumer_publish_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import (
func TestConsumerPublisherMetrics(t *testing.T) {
t.Run("ACK event", func(t *testing.T) {
connection := bunnify.NewConnection()
connection.Start()
if err := connection.Start(); err != nil {
t.Fatal(err)
}

publisher := connection.NewPublisher()

queueName := uuid.NewString()
Expand Down Expand Up @@ -75,7 +78,10 @@ func TestConsumerPublisherMetrics(t *testing.T) {

t.Run("NACK event", func(t *testing.T) {
connection := bunnify.NewConnection()
connection.Start()
if err := connection.Start(); err != nil {
t.Fatal(err)
}

publisher := connection.NewPublisher()

queueName := uuid.NewString()
Expand Down Expand Up @@ -133,7 +139,9 @@ func TestConsumerPublisherMetrics(t *testing.T) {
}

connection := bunnify.NewConnection()
connection.Start()
if err := connection.Start(); err != nil {
t.Fatal(err)
}

queueName := uuid.NewString()
exchangeName := uuid.NewString()
Expand All @@ -157,7 +165,9 @@ func TestConsumerPublisherMetrics(t *testing.T) {
}

connection = bunnify.NewConnection()
connection.Start()
if err := connection.Start(); err != nil {
t.Fatal(err)
}

// Register again but with other routing key
// The existing binding on the AMQP instance still exists
Expand Down
4 changes: 3 additions & 1 deletion tests/consumer_publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func TestConsumerPublisher(t *testing.T) {
bunnify.WithReconnectInterval(1*time.Second),
bunnify.WithNotificationChannel(notificationChannel))

connection.Start()
if err := connection.Start(); err != nil {
t.Fatal(err)
}

var consumedEvent bunnify.ConsumableEvent[orderCreated]
eventHandler := func(ctx context.Context, event bunnify.ConsumableEvent[orderCreated]) error {
Expand Down
4 changes: 3 additions & 1 deletion tests/consumer_publish_tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func TestConsumerPublisherTracing(t *testing.T) {
routingKey := uuid.NewString()

connection := bunnify.NewConnection()
connection.Start()
if err := connection.Start(); err != nil {
t.Fatal(err)
}

// Exercise consuming
var actualTraceID trace.TraceID
Expand Down
4 changes: 3 additions & 1 deletion tests/dead_letter_receives_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ func TestDeadLetterReceivesEvent(t *testing.T) {

// Exercise
connection := bunnify.NewConnection()
connection.Start()
if err := connection.Start(); err != nil {
t.Fatal(err)
}

consumer := connection.NewConsumer(
queueName,
Expand Down
4 changes: 3 additions & 1 deletion tests/go_routines_not_leaked_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func TestGoRoutinesAreNotLeaked(t *testing.T) {
// Setup
ticker := time.NewTicker(2 * time.Second)
connection := bunnify.NewConnection()
connection.Start()
if err := connection.Start(); err != nil {
t.Fatal(err)
}

// Exercise
for i := 0; i < 100; i++ {
Expand Down

0 comments on commit f356a80

Please sign in to comment.