Skip to content

Commit dc7e447

Browse files
committed
refactor: refactor Redis test setup for improved reusability and cleanup
- Refactor `TestWithRedis` to use a new `setupRedisContainer` function - Remove redundant container setup code from `TestRedisDefaultFlow` - Add endpoint retrieval and error handling in `setupRedisContainer` function - Ensure container cleanup in `TestRedisDefaultFlow` using defer Signed-off-by: appleboy <[email protected]>
1 parent 77c7a0d commit dc7e447

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

redis_test.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ func (m mockMessage) Bytes() []byte {
3838
return []byte(m.Message)
3939
}
4040

41-
func TestWithRedis(t *testing.T) {
42-
ctx := context.Background()
41+
func setupRedisContainer(ctx context.Context, t *testing.T) (testcontainers.Container, string) {
4342
req := testcontainers.ContainerRequest{
4443
Image: "redis:6",
4544
ExposedPorts: []string{"6379/tcp"},
@@ -49,25 +48,24 @@ func TestWithRedis(t *testing.T) {
4948
ContainerRequest: req,
5049
Started: true,
5150
})
52-
testcontainers.CleanupContainer(t, redisC)
5351
require.NoError(t, err)
52+
53+
endpoint, err := redisC.Endpoint(ctx, "")
54+
require.NoError(t, err)
55+
56+
return redisC, endpoint
5457
}
5558

56-
func TestRedisDefaultFlow(t *testing.T) {
59+
func TestWithRedis(t *testing.T) {
5760
ctx := context.Background()
58-
req := testcontainers.ContainerRequest{
59-
Image: "redis:6",
60-
ExposedPorts: []string{"6379/tcp"},
61-
WaitingFor: wait.ForLog("Ready to accept connections"),
62-
}
63-
redisC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
64-
ContainerRequest: req,
65-
Started: true,
66-
})
67-
require.NoError(t, err)
61+
redisC, _ := setupRedisContainer(ctx, t)
62+
testcontainers.CleanupContainer(t, redisC)
63+
}
6864

69-
endpoint, err := redisC.Endpoint(ctx, "")
70-
require.NoError(t, err)
65+
func TestRedisDefaultFlow(t *testing.T) {
66+
ctx := context.Background()
67+
redisC, endpoint := setupRedisContainer(ctx, t)
68+
defer testcontainers.CleanupContainer(t, redisC)
7169

7270
m := &mockMessage{
7371
Message: "foo",

0 commit comments

Comments
 (0)