Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,14 @@ func TestContainerCreationAndWaitForListeningPortLongEnough(t *testing.T) {
}

func TestContainerCreationTimesOut(t *testing.T) {
ctx := context.Background()

ctx, cancelfn := context.WithTimeout(context.Background(), 1*time.Second)
defer cancelfn()

// delayed-nginx will wait 2s before opening port
nginxC, err := Run(ctx, nginxDelayedImage,
WithExposedPorts(nginxDefaultPort),
WithWaitStrategy(wait.ForListeningPort(nginxDefaultPort).WithStartupTimeout(1*time.Second)),
WithWaitStrategy(wait.ForListeningPort(nginxDefaultPort)),
)
CleanupContainer(t, nginxC)

Expand Down
5 changes: 4 additions & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func ExampleRun() {

testFileContent := "Hello from file!"

ctx, cancelfn := context.WithTimeout(ctx, time.Second*5)
defer cancelfn()

ctr, err := testcontainers.Run(
ctx,
"nginx:alpine",
Expand All @@ -49,7 +52,7 @@ func ExampleRun() {
}),
testcontainers.WithExposedPorts("80/tcp"),
testcontainers.WithAfterReadyCommand(testcontainers.NewRawCommand([]string{"echo", "hello", "world"})),
testcontainers.WithWaitStrategy(wait.ForListeningPort("80/tcp").WithStartupTimeout(time.Second*5)),
testcontainers.WithWaitStrategy(wait.ForListeningPort("80/tcp")),
)
defer func() {
if err := testcontainers.TerminateContainer(ctr); err != nil {
Expand Down
14 changes: 10 additions & 4 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,18 @@ func GenericContainer(ctx context.Context, req GenericContainerRequest) (Contain
return c, fmt.Errorf("create container: %w", err)
}

if req.Started && !c.IsRunning() {
if err := c.Start(ctx); err != nil {
return c, fmt.Errorf("start container: %w", err)
// Context with Done for controlling timeouts and cancellations
select {
case <-ctx.Done():
return c, ctx.Err()
default:
if req.Started && !c.IsRunning() {
if err := c.Start(ctx); err != nil {
return c, fmt.Errorf("start container: %w", err)
}
}
return c, nil
}
return c, nil
}

// GenericProvider represents an abstraction for container and network providers
Expand Down
Loading