Skip to content

Commit 09baa88

Browse files
committed
feat(rabbitmq): Clean up tests
1 parent 8a25dd0 commit 09baa88

File tree

4 files changed

+102
-13
lines changed

4 files changed

+102
-13
lines changed

compose.rabbitmq.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ services:
2323
rabbitmq:
2424
condition: service_started
2525
rabbitmq:
26+
# Make sure the docker image version listed here matches the one used in `internal/rabbitmq/task_test.go`.
2627
image: rabbitmq:3.11.10-management-alpine
2728
ports:
2829
- 5672:5672

internal/elasticsearch/task_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package elasticsearch_test
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"net/http"
@@ -203,15 +204,17 @@ type ElasticsearchClient struct {
203204
}
204205

205206
func (r *ElasticsearchClient) Teardown() error {
207+
var err error
208+
206209
if r.container != nil {
207-
if err := testcontainers.TerminateContainer(r.container); err != nil {
208-
return fmt.Errorf("failed to terminate container: %w", err)
210+
if err1 := testcontainers.TerminateContainer(r.container); err1 != nil {
211+
err = fmt.Errorf("failed to terminate container: %w", err1)
209212
}
210213
}
211214

212215
if r.err != nil {
213-
return r.err
216+
err = errors.Join(err, r.err)
214217
}
215218

216-
return nil
219+
return err
217220
}

internal/rabbitmq/task_integration_test.go renamed to internal/rabbitmq/task_test.go

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
package rabbitmq_test
22

33
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"sync"
48
"testing"
59

610
"github.com/streadway/amqp"
711
"github.com/testcontainers/testcontainers-go"
812
"github.com/testcontainers/testcontainers-go/modules/rabbitmq"
913

1014
"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"
1216
)
1317

18+
// dockerImage must match the docker image listed in `compose.rabbitmq.yml`.
19+
const dockerImage = "rabbitmq:3.11.10-management-alpine"
20+
1421
func TestTask_Created_Integration(t *testing.T) {
1522
t.Parallel()
1623

1724
ctx := t.Context()
1825

19-
rmqContainer, err := rabbitmq.Run(ctx, "rabbitmq:3.12-management-alpine")
26+
rmqContainer, err := rabbitmq.Run(ctx, dockerImage)
2027
if err != nil {
2128
t.Fatalf("failed to start rabbitmq container: %v", err)
2229
}
@@ -63,7 +70,7 @@ func TestTask_Created_Integration(t *testing.T) {
6370
}
6471

6572
// Create task publisher
66-
taskPub := rabbitmqTask.NewTask(channel)
73+
taskPub := rabbitmqtask.NewTask(channel)
6774

6875
// Test Created method
6976
task := internal.Task{
@@ -121,7 +128,7 @@ func TestTask_Updated_Integration(t *testing.T) {
121128
t.Fatalf("failed to declare exchange: %v", err)
122129
}
123130

124-
taskPub := rabbitmqTask.NewTask(channel)
131+
taskPub := rabbitmqtask.NewTask(channel)
125132

126133
task := internal.Task{
127134
ID: "test-456",
@@ -177,10 +184,85 @@ func TestTask_Deleted_Integration(t *testing.T) {
177184
t.Fatalf("failed to declare exchange: %v", err)
178185
}
179186

180-
taskPub := rabbitmqTask.NewTask(channel)
187+
taskPub := rabbitmqtask.NewTask(channel)
181188

182189
err = taskPub.Deleted(ctx, "test-789")
183190
if err != nil {
184191
t.Fatalf("Failed to publish deleted event: %v", err)
185192
}
186193
}
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+
}

internal/redis/task_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package redis_test
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"os"
89
"strings"
@@ -278,15 +279,17 @@ type RedisClient struct {
278279
}
279280

280281
func (r *RedisClient) Teardown() error {
282+
var err error
283+
281284
if r.container != nil {
282-
if err := testcontainers.TerminateContainer(r.container); err != nil {
283-
return fmt.Errorf("failed to terminate container: %w", err)
285+
if err1 := testcontainers.TerminateContainer(r.container); err1 != nil {
286+
err = fmt.Errorf("failed to terminate container: %w", err1)
284287
}
285288
}
286289

287290
if r.err != nil {
288-
return r.err
291+
err = errors.Join(err, r.err)
289292
}
290293

291-
return nil
294+
return err
292295
}

0 commit comments

Comments
 (0)