Skip to content

Commit

Permalink
avoid race conditions by waiting one polling interval before checking…
Browse files Browse the repository at this point in the history
… ports are bound (testcontainers#1697)
  • Loading branch information
gflarity authored Sep 29, 2023
1 parent 1980c84 commit f76cdc8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 4 additions & 2 deletions wait/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ func (ws *HTTPStrategy) WaitUntilReady(ctx context.Context, target StrategyTarge

var mappedPort nat.Port
if ws.Port == "" {
ports, err := target.Ports(ctx)
for err != nil {
var err error
var ports nat.PortMap
// we wait one polling interval before we grab the ports otherwise they might not be bound yet on startup
for err != nil || ports == nil {
select {
case <-ctx.Done():
return fmt.Errorf("%w: %w", ctx.Err(), err)
Expand Down
9 changes: 6 additions & 3 deletions wait/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,8 @@ func TestHttpStrategyFailsWhileGettingPortDueToNoExposedPorts(t *testing.T) {
},
StateImpl: func(_ context.Context) (*types.ContainerState, error) {
return &types.ContainerState{
Status: "running",
Status: "running",
Running: true,
}, nil
},
PortsImpl: func(ctx context.Context) (nat.PortMap, error) {
Expand Down Expand Up @@ -602,7 +603,8 @@ func TestHttpStrategyFailsWhileGettingPortDueToOnlyUDPPorts(t *testing.T) {
},
StateImpl: func(_ context.Context) (*types.ContainerState, error) {
return &types.ContainerState{
Status: "running",
Running: true,
Status: "running",
}, nil
},
PortsImpl: func(ctx context.Context) (nat.PortMap, error) {
Expand Down Expand Up @@ -649,7 +651,8 @@ func TestHttpStrategyFailsWhileGettingPortDueToExposedPortNoBindings(t *testing.
},
StateImpl: func(_ context.Context) (*types.ContainerState, error) {
return &types.ContainerState{
Status: "running",
Running: true,
Status: "running",
}, nil
},
PortsImpl: func(ctx context.Context) (nat.PortMap, error) {
Expand Down

0 comments on commit f76cdc8

Please sign in to comment.