Skip to content

Commit

Permalink
chore(v2/rabbittest): add helper for creating client
Browse files Browse the repository at this point in the history
  • Loading branch information
dmksnnk committed Aug 13, 2024
1 parent 7913103 commit 95b11b6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
27 changes: 8 additions & 19 deletions v2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"net"
"os"
"testing"
"time"

Expand All @@ -22,17 +21,12 @@ func TestMain(m *testing.M) {
}

func TestReconnectsOnClosedChannel(t *testing.T) {
rmqURL := os.Getenv("RABBITMQ_URL")
if rmqURL == "" {
t.Fatal("please provide rabbitMQ URL via RABBITMQ_URL environment variable")
}

t.Run("publishing", func(t *testing.T) {
exchange, queue, key := t.Name(), t.Name(), t.Name()
setup(t, exchange, queue, key)

client := gorabbit.New(
rmqURL,
client := rabbittest.NewClient(
t,
gorabbit.WithOnChannelClosed(func(err error) {
t.Logf("channel closed: %s", err)
}),
Expand Down Expand Up @@ -60,8 +54,8 @@ func TestReconnectsOnClosedChannel(t *testing.T) {
exchange, queue, key := t.Name(), t.Name(), t.Name()
channel := setup(t, exchange, queue, key)

client := gorabbit.New(
rmqURL,
client := rabbittest.NewClient(
t,
gorabbit.WithOnChannelClosed(func(err error) {
t.Logf("channel closed: %s", err)
}),
Expand Down Expand Up @@ -104,8 +98,8 @@ func TestReconnectsOnClosedChannel(t *testing.T) {
exchange, queue, key := t.Name(), t.Name(), t.Name()
setup(t, exchange, queue, key)

client := gorabbit.New(
rmqURL,
client := rabbittest.NewClient(
t,
gorabbit.WithOnChannelClosed(func(err error) {
t.Logf("channel closed: %s", err)
}),
Expand Down Expand Up @@ -330,17 +324,12 @@ func causeException(t *testing.T, channel *amqp.Channel) {
func breakableClient(t *testing.T, signal chan struct{}) *gorabbit.Client {
t.Helper()

rmqURL := os.Getenv("RABBITMQ_URL")
if rmqURL == "" {
t.Fatal("please provide rabbitMQ URL via RABBITMQ_URL environment variable")
}

bo := backoff.NewExponentialBackOff()
bo.MaxElapsedTime = 2 * time.Second
bo.InitialInterval = 10 * time.Millisecond

client := gorabbit.New(
rmqURL,
client := rabbittest.NewClient(
t,
gorabbit.WithBackoff(bo),
gorabbit.WithOnChannelClosed(func(err error) {
t.Logf("channel closed: %s", err)
Expand Down
29 changes: 29 additions & 0 deletions v2/rabbittest/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package rabbittest

import (
"os"
"testing"

"github.com/heureka/gorabbit/v2"
)

// NewClient creates a new RabbitMQ client.
// Closes the client on test cleanup.
func NewClient(t *testing.T, ops ...gorabbit.Option) *gorabbit.Client {
t.Helper()

url := os.Getenv("RABBITMQ_URL")
if url == "" {
t.Fatal("please provide rabbitMQ URL via RABBITMQ_URL environment variable")
}

client := gorabbit.New(url, ops...)

t.Cleanup(func() {
if err := client.Close(); err != nil {
t.Fatalf("close client: %s", err)
}
})

return client
}

0 comments on commit 95b11b6

Please sign in to comment.