Skip to content

Commit

Permalink
fix(redpanda): race condition on port check
Browse files Browse the repository at this point in the history
Use SkipInternalCheck to wait for ports to be bound externally only
before trying to get the mapped port. This fixes a race condition in the
setup of the container which was causing random test failures.
  • Loading branch information
stevenh committed Aug 12, 2024
1 parent 4e9cb66 commit 21cb525
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions modules/redpanda/redpanda.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
"--smp=1",
"--memory=1G",
},
WaitingFor: wait.ForAll(
// Wait for the ports to be exposed only as the container needs configuration
// before it will bind to the ports and be ready to serve requests.
wait.ForListeningPort(defaultKafkaAPIPort).SkipInternalCheck(),
wait.ForListeningPort(defaultAdminAPIPort).SkipInternalCheck(),
wait.ForListeningPort(defaultSchemaRegistryPort).SkipInternalCheck(),
),
},
Started: true,
}
Expand Down Expand Up @@ -119,7 +126,7 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom

// 4. Register extra kafka listeners if provided, network aliases will be
// set
if err := registerListeners(ctx, settings, req); err != nil {
if err := registerListeners(settings, req); err != nil {
return nil, fmt.Errorf("failed to register listeners: %w", err)
}

Expand Down Expand Up @@ -200,11 +207,13 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
return nil, fmt.Errorf("failed to copy redpanda.yaml into container: %w", err)
}

// 8. Wait until Redpanda is ready to serve requests
// 8. Wait until Redpanda is ready to serve requests.
err = wait.ForAll(
wait.ForListeningPort(defaultKafkaAPIPort),
wait.ForLog("Successfully started Redpanda!").WithPollInterval(100*time.Millisecond)).
WaitUntilReady(ctx, container)
wait.NewHostPortStrategy(defaultKafkaAPIPort),
wait.NewHostPortStrategy(defaultAdminAPIPort),
wait.NewHostPortStrategy(defaultSchemaRegistryPort),
wait.ForLog("Successfully started Redpanda!"),
).WaitUntilReady(ctx, container)
if err != nil {
return nil, fmt.Errorf("failed to wait for Redpanda readiness: %w", err)
}
Expand Down Expand Up @@ -299,7 +308,7 @@ func renderBootstrapConfig(settings options) ([]byte, error) {

// registerListeners validates that the provided listeners are valid and set network aliases for the provided addresses.
// The container must be attached to at least one network.
func registerListeners(ctx context.Context, settings options, req testcontainers.GenericContainerRequest) error {
func registerListeners(settings options, req testcontainers.GenericContainerRequest) error {
if len(settings.Listeners) == 0 {
return nil
}
Expand Down

0 comments on commit 21cb525

Please sign in to comment.