packagerabbitmqimport("context""fmt""github.com/testcontainers/testcontainers-go""github.com/testcontainers/testcontainers-go/wait")// rabbitmqContainer represents the mongodb container type used in the moduletyperabbitmqContainerstruct{testcontainers.Containerendpointstring}funcstartContainer(ctxcontext.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,})iferr!=nil{returnnil,err}host,err:=container.Host(ctx)iferr!=nil{returnnil,err}mappedPort,err:=container.MappedPort(ctx,"5672")iferr!=nil{returnnil,err}return&rabbitmqContainer{Container:container,endpoint:fmt.Sprintf("%s:%s",host,mappedPort.Port()),},nil}
packagerabbitmqimport("context""fmt""testing"amqp"github.com/rabbitmq/amqp091-go")funcTestRabbitMQ(t*testing.T){ctx:=context.Background()container,err:=startContainer(ctx)iferr!=nil{t.Fatal(err)}// Clean up the container after the test is completet.Cleanup(func(){iferr:=container.Terminate(ctx);err!=nil{t.Fatalf("failed to terminate container: %s",err)}})// perform assertionsamqpConnection,err:=amqp.Dial(fmt.Sprintf("amqp://guest:guest@%s",container.endpoint))iferr!=nil{t.Fatal(fmt.Errorf("error creating amqp client: %w",err))}err=amqpConnection.Close()iferr!=nil{t.Fatal(fmt.Errorf("error closing amqp connection: %s",err))}}