github.com/angryronald/go-kit@v0.0.0-20240505173814-ff2bd9c79dbf/test/docker/rabbitmq/rabbitmq.sample.go (about) 1 package rabbitmq 2 3 // uncomment this to try run rabbitmq 4 // package main 5 6 // import ( 7 // "fmt" 8 // "log" 9 // "time" 10 // 11 12 // "github.com/ory/dockertest" 13 // // "github.com/streadway/amqp" 14 // amqp "github.com/rabbitmq/amqp091-go" 15 // ) 16 17 // func main() { 18 // // Create a new pool of Docker containers 19 // pool, err := dockertest.NewPool("") 20 // if err != nil { 21 // log.Fatalf("Could not connect to Docker: %s", err) 22 // } 23 24 // // Set up options for RabbitMQ container 25 // options := &dockertest.RunOptions{ 26 // Repository: "rabbitmq", 27 // Tag: "3-management", 28 // Env: []string{ 29 // "RABBITMQ_DEFAULT_USER=user", 30 // "RABBITMQ_DEFAULT_PASS=password", 31 // }, 32 // } 33 34 // // Run RabbitMQ container 35 // resource, err := pool.RunWithOptions(options) 36 // if err != nil { 37 // log.Fatalf("Could not start RabbitMQ container: %s", err) 38 // } 39 // defer func() { 40 // // Clean up and remove the RabbitMQ container when done 41 // if err := pool.Purge(resource); err != nil { 42 // log.Fatalf("Could not purge RabbitMQ container: %s", err) 43 // } 44 // }() 45 // fmt.Printf("RabbitMQ container started: ID=%s\n", resource.Container.ID) 46 47 // // Sleep for some time to allow RabbitMQ to run 48 // time.Sleep(10 * time.Second) // Adjust the sleep duration as needed 49 50 // // Wait for RabbitMQ to be ready 51 // if err := pool.Retry(func() error { 52 // conn, err := amqp.Dial(fmt.Sprintf( 53 // "amqp://user:password@%s:%s/", 54 // resource.GetBoundIP("5672/tcp"), 55 // resource.GetPort("5672/tcp"), 56 // )) 57 // if err != nil { 58 // return err 59 // } 60 // defer conn.Close() 61 // return nil 62 // }); err != nil { 63 // log.Fatalf("Could not connect to RabbitMQ: %s", err) 64 // } 65 66 // // At this point, RabbitMQ should be running and ready to use. 67 68 // // You can use the streadway/amqp library to interact with RabbitMQ. 69 // // For example, you can declare a queue and publish a message: 70 // conn, err := amqp.Dial(fmt.Sprintf( 71 // "amqp://user:password@%s:%s/", 72 // resource.GetBoundIP("5672/tcp"), 73 // resource.GetPort("5672/tcp"), 74 // )) 75 // if err != nil { 76 // log.Fatalf("Could not connect to RabbitMQ: %s", err) 77 // } 78 // defer conn.Close() 79 80 // ch, err := conn.Channel() 81 // if err != nil { 82 // log.Fatalf("Could not open a channel: %s", err) 83 // } 84 // defer ch.Close() 85 86 // queueName := "test-queue" 87 // _, err = ch.QueueDeclare( 88 // queueName, 89 // false, // durable 90 // false, // delete when unused 91 // false, // exclusive 92 // false, // no-wait 93 // nil, // arguments 94 // ) 95 // if err != nil { 96 // log.Fatalf("Could not declare the queue: %s", err) 97 // } 98 99 // messageBody := "Hello, RabbitMQ!" 100 // err = ch.Publish( 101 // "", // exchange 102 // queueName, // routing key 103 // false, // mandatory 104 // false, // immediate 105 // amqp.Publishing{ 106 // ContentType: "text/plain", 107 // Body: []byte(messageBody), 108 // }) 109 // if err != nil { 110 // log.Fatalf("Could not publish a message: %s", err) 111 // } 112 113 // fmt.Printf("Published message: %s\n", messageBody) 114 115 // // You can now consume messages, perform integration tests, and clean up resources as needed. 116 // // Remember to handle errors appropriately in a real-world scenario. 117 // }