Skip to content

RabbitMQ

package rabbitmq

import (
    "context"
    "fmt"

    "github.com/testcontainers/testcontainers-go"
    "github.com/testcontainers/testcontainers-go/wait"
)

// rabbitmqContainer represents the mongodb container type used in the module
type rabbitmqContainer struct {
    testcontainers.Container
    endpoint string
}

func startContainer(ctx context.Context) (*rabbitmqContainer, error) {
    req := testcontainers.ContainerRequest{
        Image:        "rabbitmq:3",
        ExposedPorts: []string{"5672/tcp"},
        WaitingFor:   wait.ForLog("started TCP listener on [::]:5672"),
    }

    container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
        ContainerRequest: req,
        Started:          true,
    })
    if err != nil {
        return nil, err
    }

    host, err := container.Host(ctx)
    if err != nil {
        return nil, err
    }

    mappedPort, err := container.MappedPort(ctx, "5672")
    if err != nil {
        return nil, err
    }

    return &rabbitmqContainer{
        Container: container,
        endpoint:  fmt.Sprintf("%s:%s", host, mappedPort.Port()),
    }, nil
}
package rabbitmq

import (
    "context"
    "fmt"
    "testing"

    amqp "github.com/rabbitmq/amqp091-go"
)

func TestRabbitMQ(t *testing.T) {
    ctx := context.Background()

    container, err := startContainer(ctx)
    if err != nil {
        t.Fatal(err)
    }

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

    // perform assertions

    amqpConnection, err := amqp.Dial(fmt.Sprintf("amqp://guest:guest@%s", container.endpoint))

    if err != nil {
        t.Fatal(fmt.Errorf("error creating amqp client: %w", err))
    }

    err = amqpConnection.Close()
    if err != nil {
        t.Fatal(fmt.Errorf("error closing amqp connection: %s", err))
    }
}