Skip to content

Commit

Permalink
Merge pull request #15 from syncromatics/feature/rabbitmq-timeout
Browse files Browse the repository at this point in the history
fix: added timeout in rabbitmq setup
  • Loading branch information
francisminu authored Aug 20, 2021
2 parents 1bb6a8f + 3ac6964 commit e512f10
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
12 changes: 8 additions & 4 deletions testing/docker/rabbitmq_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ var (
rabbitMqImage = "rabbitmq:3.7.7-management"
)

// SetupRabbitMQ sets up a RabbitMQ broker
func SetupRabbitMQ(testName string) (string, error) {
return SetupRabbitMQWithTimeOut(testName, 30*time.Second)
}

// SetupRabbitMQ sets up a RabbitMQ broker
func SetupRabbitMQWithTimeOut(testName string, timeOut time.Duration) (string, error) {
os.Setenv("DOCKER_API_VERSION", "1.35")
cli, err := client.NewEnvClient()
if err != nil {
Expand Down Expand Up @@ -97,7 +101,7 @@ rabbitmq_node_is_healthy.
return "", err
}

url, err := waitForRabbitMQToBeReady(cli, create.ID, amqpPort)
url, err := waitForRabbitMQToBeReady(cli, create.ID, amqpPort, timeOut)
if err != nil {
return "", err
}
Expand All @@ -122,8 +126,8 @@ func removeRabbitMQContainer(client *client.Client, testName string) {
client.ContainerRemove(context.Background(), containerName, types.ContainerRemoveOptions{Force: true})
}

func waitForRabbitMQToBeReady(client *client.Client, id string, amqpPort int) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
func waitForRabbitMQToBeReady(client *client.Client, id string, amqpPort int, timeOut time.Duration) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeOut)

for {
select {
Expand Down
27 changes: 27 additions & 0 deletions testing/docker/rabbitmq_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package docker_test

import (
"fmt"
"time"

"github.com/syncromatics/go-kit/v2/testing/docker"
)
Expand Down Expand Up @@ -30,3 +31,29 @@ func ExampleSetupRabbitMQ() {
docker.TeardownRabbitMQ("test")
// Output: amqpURL is set: true
}

func ExampleSetupRabbitMQWithTimeOut() {
timeOut := 30 * time.Second
amqpURL, err := docker.SetupRabbitMQWithTimeOut("test", timeOut)
if err != nil {
panic(err)
}

fmt.Printf("amqpURL is set: %t\n", amqpURL != "")
/*
conn, err := amqp.Dial(amqpURL)
if err != nil {
panic(err)
}
// snip
err = conn.Close()
if err != nil {
panic(err)
}
*/

docker.TeardownRabbitMQ("test")
// Output: amqpURL is set: true
}

0 comments on commit e512f10

Please sign in to comment.