Skip to content

Commit

Permalink
fix: 🐛 fix testcontainer error
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihadeli committed Aug 30, 2023
1 parent 5dc6886 commit 99346f1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ func (g *eventstoredbTestContainers) CreatingContainerOptions(

// Clean up the container after the test is complete
t.Cleanup(func() {
_ = dbContainer.Terminate(ctx)
if err := dbContainer.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

option := &config.EventStoreDbOptions{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func (g *gormTestContainers) CreatingContainerOptions(

// Clean up the container after the test is complete
t.Cleanup(func() {
_ = dbContainer.Terminate(ctx)
if err := dbContainer.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

gormOptions := &gormPostgres.GormOptions{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ func (g *mongoTestContainers) CreatingContainerOptions(
g.container = dbContainer

// Clean up the container after the test is complete
t.Cleanup(func() { _ = dbContainer.Terminate(ctx) })
t.Cleanup(func() {
if err := dbContainer.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})
option := &mongodb.MongoDbOptions{
User: g.defaultOptions.UserName,
Password: g.defaultOptions.Password,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ func (g *postgresPgxTestContainers) CreatingContainerOptions(

// Clean up the container after the test is complete
t.Cleanup(func() {
_ = dbContainer.Terminate(ctx)
if err := dbContainer.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

gormOptions := &postgres.PostgresPgxOptions{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package rabbitmq
import (
"context"
"fmt"
"net"
"strconv"
"testing"
"time"

"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
Expand Down Expand Up @@ -69,6 +72,7 @@ func (g *rabbitmqTestContainers) CreatingContainerOptions(
return nil, err
}
g.defaultOptions.HostPort = hostPort.Int()
t.Logf("rabbitmq host port is: %d", hostPort.Int())

// https://github.com/michaelklishin/rabbit-hole/issues/74
// get a free random host port for rabbitmq UI `Http Port`
Expand All @@ -84,11 +88,15 @@ func (g *rabbitmqTestContainers) CreatingContainerOptions(
return nil, err
}

raw_connect(host, hostPort.Int())

g.container = dbContainer

// Clean up the container after the test is complete
t.Cleanup(func() {
_ = dbContainer.Terminate(ctx)
if err := dbContainer.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

option := &config.RabbitmqOptions{
Expand All @@ -105,6 +113,18 @@ func (g *rabbitmqTestContainers) CreatingContainerOptions(
return option, nil
}

func raw_connect(host string, port int) {
timeout := time.Second
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, strconv.Itoa(port)), timeout)
if err != nil {
fmt.Println("RabbitMQ connecting error:", err)
}
if conn != nil {
defer conn.Close()
fmt.Println("Opened rabbitmq connection.", net.JoinHostPort(host, strconv.Itoa(port)))
}
}

func (g *rabbitmqTestContainers) Start(
ctx context.Context,
t *testing.T,
Expand Down Expand Up @@ -165,7 +185,6 @@ func (g *rabbitmqTestContainers) getRunOptions(
ExposedPorts: g.defaultOptions.Ports,
WaitingFor: wait.ForListeningPort(nat.Port(g.defaultOptions.Ports[0])),
Hostname: g.defaultOptions.Host,
SkipReaper: true,
Env: map[string]string{
"RABBITMQ_DEFAULT_USER": g.defaultOptions.UserName,
"RABBITMQ_DEFAULT_PASS": g.defaultOptions.Password,
Expand Down

0 comments on commit 99346f1

Please sign in to comment.