diff --git a/redis.go b/redis.go index 449fdd5..1ea0ff6 100644 --- a/redis.go +++ b/redis.go @@ -23,10 +23,12 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize ExposedPorts: []string{redisServicePort}, WaitingFor: newRedisWaitStrategy(), } + genericContainerRequest := testcontainers.GenericContainerRequest{ ContainerRequest: containerRequest, Started: true, } + for _, opt := range opts { if err := opt.Customize(&genericContainerRequest); err != nil { return nil, fmt.Errorf("failed to apply option: %w", err) @@ -47,6 +49,7 @@ func (c RedisContainer) ServicePort(ctx context.Context) (int, error) { if err != nil { return 0, err } + return port.Int(), nil } diff --git a/redis_test.go b/redis_test.go index e42a378..5c01706 100644 --- a/redis_test.go +++ b/redis_test.go @@ -14,6 +14,7 @@ func TestSet(t *testing.T) { container, err := RunContainer(ctx) require.NoError(t, err) + t.Cleanup(func() { err := container.Terminate(ctx) require.NoErrorf(t, err, "failed to terminate Redis container") @@ -21,6 +22,7 @@ func TestSet(t *testing.T) { host, err := container.Host(ctx) require.NoErrorf(t, err, "failed to fetch Redis host") + port, err := container.ServicePort(ctx) require.NoErrorf(t, err, "failed to fetch Redis port") diff --git a/wait.go b/wait.go index 2bf2482..e3d7d3c 100644 --- a/wait.go +++ b/wait.go @@ -34,10 +34,12 @@ func (s redisWaitStrategy) WaitUntilReady(ctx context.Context, target wait.Strat if err != nil { return fmt.Errorf("failed to fetch host: %w", err) } + port, err := target.MappedPort(ctx, redisServicePort) if err != nil { return fmt.Errorf("failed to fetch port: %w", err) } + return s.pollUntilReady(ctx, host, port.Int()) } @@ -46,11 +48,13 @@ func (s redisWaitStrategy) pollUntilReady(ctx context.Context, host string, port select { case <-ctx.Done(): return fmt.Errorf("timed out while waiting for Redis to start: %w", ctx.Err()) + case <-time.After(defaultPollInterval): isReady, err := s.isReady(ctx, host, port) if err != nil { return err } + if isReady { return nil } @@ -68,5 +72,6 @@ func (s redisWaitStrategy) isReady(ctx context.Context, host string, port int) ( if err != nil { return false, nil } + return true, nil }