|
1 | 1 | package rabbitmq_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "sync" |
4 | 8 | "testing" |
5 | 9 |
|
6 | 10 | "github.com/streadway/amqp" |
7 | 11 | "github.com/testcontainers/testcontainers-go" |
8 | 12 | "github.com/testcontainers/testcontainers-go/modules/rabbitmq" |
9 | 13 |
|
10 | 14 | "github.com/MarioCarrion/todo-api-microservice-example/internal" |
11 | | - rabbitmqTask "github.com/MarioCarrion/todo-api-microservice-example/internal/rabbitmq" |
| 15 | + rabbitmqtask "github.com/MarioCarrion/todo-api-microservice-example/internal/rabbitmq" |
12 | 16 | ) |
13 | 17 |
|
| 18 | +// dockerImage must match the docker image listed in `compose.rabbitmq.yml`. |
| 19 | +const dockerImage = "rabbitmq:3.11.10-management-alpine" |
| 20 | + |
14 | 21 | func TestTask_Created_Integration(t *testing.T) { |
15 | 22 | t.Parallel() |
16 | 23 |
|
17 | 24 | ctx := t.Context() |
18 | 25 |
|
19 | | - rmqContainer, err := rabbitmq.Run(ctx, "rabbitmq:3.12-management-alpine") |
| 26 | + rmqContainer, err := rabbitmq.Run(ctx, dockerImage) |
20 | 27 | if err != nil { |
21 | 28 | t.Fatalf("failed to start rabbitmq container: %v", err) |
22 | 29 | } |
@@ -63,7 +70,7 @@ func TestTask_Created_Integration(t *testing.T) { |
63 | 70 | } |
64 | 71 |
|
65 | 72 | // Create task publisher |
66 | | - taskPub := rabbitmqTask.NewTask(channel) |
| 73 | + taskPub := rabbitmqtask.NewTask(channel) |
67 | 74 |
|
68 | 75 | // Test Created method |
69 | 76 | task := internal.Task{ |
@@ -121,7 +128,7 @@ func TestTask_Updated_Integration(t *testing.T) { |
121 | 128 | t.Fatalf("failed to declare exchange: %v", err) |
122 | 129 | } |
123 | 130 |
|
124 | | - taskPub := rabbitmqTask.NewTask(channel) |
| 131 | + taskPub := rabbitmqtask.NewTask(channel) |
125 | 132 |
|
126 | 133 | task := internal.Task{ |
127 | 134 | ID: "test-456", |
@@ -177,10 +184,85 @@ func TestTask_Deleted_Integration(t *testing.T) { |
177 | 184 | t.Fatalf("failed to declare exchange: %v", err) |
178 | 185 | } |
179 | 186 |
|
180 | | - taskPub := rabbitmqTask.NewTask(channel) |
| 187 | + taskPub := rabbitmqtask.NewTask(channel) |
181 | 188 |
|
182 | 189 | err = taskPub.Deleted(ctx, "test-789") |
183 | 190 | if err != nil { |
184 | 191 | t.Fatalf("Failed to publish deleted event: %v", err) |
185 | 192 | } |
186 | 193 | } |
| 194 | + |
| 195 | +//- |
| 196 | + |
| 197 | +var setupClient = sync.OnceValue(func() RabbitMQClient { //nolint: gochecknoglobals |
| 198 | + var res RabbitMQClient |
| 199 | + |
| 200 | + ctx := context.Background() |
| 201 | + |
| 202 | + rmqContainer, err := rabbitmq.Run(ctx, dockerImage) |
| 203 | + if err != nil { |
| 204 | + res.err = fmt.Errorf("failed to start rabbitmq container: %w", err) |
| 205 | + |
| 206 | + return res |
| 207 | + } |
| 208 | + |
| 209 | + res.container = rmqContainer |
| 210 | + |
| 211 | + connStr, err := rmqContainer.AmqpURL(ctx) |
| 212 | + if err != nil { |
| 213 | + res.err = fmt.Errorf("failed to get connection string: %w", err) |
| 214 | + |
| 215 | + return res |
| 216 | + } |
| 217 | + |
| 218 | + //- RabbitMQ connection |
| 219 | + conn, err := amqp.Dial(connStr) |
| 220 | + if err != nil { |
| 221 | + res.err = fmt.Errorf("failed to connect to rabbitmq: %w", err) |
| 222 | + |
| 223 | + return res |
| 224 | + } |
| 225 | + |
| 226 | + res.connection = conn |
| 227 | + |
| 228 | + //- RabbitMQ Channel |
| 229 | + |
| 230 | + channel, err := conn.Channel() |
| 231 | + if err != nil { |
| 232 | + res.err = fmt.Errorf("failed to open channel: %w", err) |
| 233 | + |
| 234 | + return res |
| 235 | + } |
| 236 | + |
| 237 | + res.channel = channel |
| 238 | + |
| 239 | + return res |
| 240 | + |
| 241 | +}) |
| 242 | + |
| 243 | +type RabbitMQClient struct { |
| 244 | + container *rabbitmq.RabbitMQContainer |
| 245 | + channel *amqp.Channel |
| 246 | + connection *amqp.Connection |
| 247 | + err error |
| 248 | +} |
| 249 | + |
| 250 | +func (r *RabbitMQClient) Teardown() error { |
| 251 | + var err error |
| 252 | + |
| 253 | + if r.channel != nil { |
| 254 | + err = r.channel.Close() |
| 255 | + } |
| 256 | + |
| 257 | + if r.connection != nil { |
| 258 | + err = errors.Join(err, fmt.Errorf("failed to close connection: %w", r.connection.Close())) |
| 259 | + } |
| 260 | + |
| 261 | + if r.container != nil { |
| 262 | + if err1 := testcontainers.TerminateContainer(r.container); err1 != nil { |
| 263 | + err = errors.Join(err, fmt.Errorf("failed to terminate container: %w", err1)) |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + return err |
| 268 | +} |
0 commit comments