Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(v2/rabbittest): add helper for creating client #29

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}