Skip to content

Commit f356a80

Browse files
authored
added err to connection.Start (#20)
1 parent 3e07514 commit f356a80

7 files changed

+56
-13
lines changed

bunnify/connection.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,19 @@ func NewConnection(opts ...func(*connectionOption)) *Connection {
6262
}
6363

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

71+
uri, err := amqp.ParseURI(c.options.uri)
72+
if err != nil {
73+
return err
74+
}
75+
7076
for {
71-
conn, err = amqp.Dial(c.options.uri)
77+
conn, err = amqp.Dial(uri.String())
7278
if err == nil {
7379
break
7480
}
@@ -84,9 +90,11 @@ func (c *Connection) Start() {
8490
<-conn.NotifyClose(make(chan *amqp.Error))
8591
if !c.connectionClosedBySystem {
8692
notifyConnectionLost(c.options.notificationChannel)
87-
c.Start()
93+
_ = c.Start()
8894
}
8995
}()
96+
97+
return nil
9098
}
9199

92100
// Closes connection with towards the AMQP server

tests/consumer_invalid_options_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,30 @@ import (
77
"go.uber.org/goleak"
88
)
99

10+
func TestConnectionReturnErrorWhenNotValidURI(t *testing.T) {
11+
// Setup
12+
connection := bunnify.NewConnection(bunnify.WithURI("13123"))
13+
14+
// Exercise
15+
err := connection.Start()
16+
17+
// Assert
18+
if err == nil {
19+
t.Fatal(err)
20+
}
21+
22+
goleak.VerifyNone(t)
23+
}
24+
1025
func TestConsumerShouldReturnErrorWhenNoHandlersSpecified(t *testing.T) {
1126
// Setup
1227
connection := bunnify.NewConnection()
13-
connection.Start()
14-
consumer := connection.NewConsumer("queueName")
28+
if err := connection.Start(); err != nil {
29+
t.Fatal(err)
30+
}
1531

1632
// Exercise
33+
consumer := connection.NewConsumer("queueName")
1734
err := consumer.Consume()
1835

1936
// Assert

tests/consumer_publish_metrics_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import (
1717
func TestConsumerPublisherMetrics(t *testing.T) {
1818
t.Run("ACK event", func(t *testing.T) {
1919
connection := bunnify.NewConnection()
20-
connection.Start()
20+
if err := connection.Start(); err != nil {
21+
t.Fatal(err)
22+
}
23+
2124
publisher := connection.NewPublisher()
2225

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

7679
t.Run("NACK event", func(t *testing.T) {
7780
connection := bunnify.NewConnection()
78-
connection.Start()
81+
if err := connection.Start(); err != nil {
82+
t.Fatal(err)
83+
}
84+
7985
publisher := connection.NewPublisher()
8086

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

135141
connection := bunnify.NewConnection()
136-
connection.Start()
142+
if err := connection.Start(); err != nil {
143+
t.Fatal(err)
144+
}
137145

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

159167
connection = bunnify.NewConnection()
160-
connection.Start()
168+
if err := connection.Start(); err != nil {
169+
t.Fatal(err)
170+
}
161171

162172
// Register again but with other routing key
163173
// The existing binding on the AMQP instance still exists

tests/consumer_publish_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ func TestConsumerPublisher(t *testing.T) {
4040
bunnify.WithReconnectInterval(1*time.Second),
4141
bunnify.WithNotificationChannel(notificationChannel))
4242

43-
connection.Start()
43+
if err := connection.Start(); err != nil {
44+
t.Fatal(err)
45+
}
4446

4547
var consumedEvent bunnify.ConsumableEvent[orderCreated]
4648
eventHandler := func(ctx context.Context, event bunnify.ConsumableEvent[orderCreated]) error {

tests/consumer_publish_tracer_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ func TestConsumerPublisherTracing(t *testing.T) {
2525
routingKey := uuid.NewString()
2626

2727
connection := bunnify.NewConnection()
28-
connection.Start()
28+
if err := connection.Start(); err != nil {
29+
t.Fatal(err)
30+
}
2931

3032
// Exercise consuming
3133
var actualTraceID trace.TraceID

tests/dead_letter_receives_event_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ func TestDeadLetterReceivesEvent(t *testing.T) {
4444

4545
// Exercise
4646
connection := bunnify.NewConnection()
47-
connection.Start()
47+
if err := connection.Start(); err != nil {
48+
t.Fatal(err)
49+
}
4850

4951
consumer := connection.NewConsumer(
5052
queueName,

tests/go_routines_not_leaked_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ func TestGoRoutinesAreNotLeaked(t *testing.T) {
1515
// Setup
1616
ticker := time.NewTicker(2 * time.Second)
1717
connection := bunnify.NewConnection()
18-
connection.Start()
18+
if err := connection.Start(); err != nil {
19+
t.Fatal(err)
20+
}
1921

2022
// Exercise
2123
for i := 0; i < 100; i++ {

0 commit comments

Comments
 (0)