diff --git a/docs/features/creating_container.md b/docs/features/creating_container.md index 762312451e..c1882e9d3f 100644 --- a/docs/features/creating_container.md +++ b/docs/features/creating_container.md @@ -87,42 +87,58 @@ func TestIntegrationNginxLatestReturn(t *testing.T) { `testcontainers.ParallelContainers` - defines the containers that should be run in parallel mode. ```go -ctx := context.Background() - -requests := []GenericContainerRequest{ - { - ContainerRequest: ContainerRequest{ - - Image: "nginx", - ExposedPorts: []string{ - "10080/tcp", - }, - }, - Started: true, - }, - { - ContainerRequest: ContainerRequest{ - - Image: "nginx", - ExposedPorts: []string{ - "10081/tcp", - }, - }, - Started: true, - }, -} +package main -res, err := ParallelContainers(ctx, requests, ParallelContainersOptions{}) +import ( + "context" + "fmt" + "log" -if err != nil { - e, ok := err.(ParallelContainersError) - if !ok { - log.Fatalf("unknown error: %v", err) - } - ... -} + "github.com/testcontainers/testcontainers-go" +) -for _, c := range res { - defer c.Terminate(ctx) +func main() { + ctx := context.Background() + + requests := testcontainers.ParallelContainerRequest{ + { + ContainerRequest: testcontainers.ContainerRequest{ + + Image: "nginx", + ExposedPorts: []string{ + "10080/tcp", + }, + }, + Started: true, + }, + { + ContainerRequest: testcontainers.ContainerRequest{ + + Image: "nginx", + ExposedPorts: []string{ + "10081/tcp", + }, + }, + Started: true, + }, + } + + res, err := testcontainers.ParallelContainers(ctx, requests, testcontainers.ParallelContainersOptions{}) + + if err != nil { + e, ok := err.(testcontainers.ParallelContainersError) + if !ok { + log.Fatalf("unknown error: %v", err) + } + + for _, pe := range e.Errors { + fmt.Println(pe.Request, pe.Error) + } + return + } + + for _, c := range res { + defer c.Terminate(ctx) + } } ``` \ No newline at end of file diff --git a/parallel.go b/parallel.go index c319fbf3f5..f6d371dbb3 100644 --- a/parallel.go +++ b/parallel.go @@ -10,10 +10,11 @@ const ( defaultWorkersCount = 8 ) +type ParallelContainerRequest []GenericContainerRequest + // ParallelContainersOptions represents additional options for parallel running -// * WorkersCount - count of parallel workers. if field empty(zero), default value will be 'defaultWorkersCount' type ParallelContainersOptions struct { - WorkersCount int + WorkersCount int // count of parallel workers. If field empty(zero), default value will be 'defaultWorkersCount' } // ParallelContainersRequestError represents error from parallel request @@ -51,7 +52,7 @@ func parallelContainersRunner( } // ParallelContainers creates a generic containers with parameters and run it in parallel mode -func ParallelContainers(ctx context.Context, reqs []GenericContainerRequest, opt ParallelContainersOptions) ([]Container, error) { +func ParallelContainers(ctx context.Context, reqs ParallelContainerRequest, opt ParallelContainersOptions) ([]Container, error) { if opt.WorkersCount == 0 { opt.WorkersCount = defaultWorkersCount } diff --git a/parallel_test.go b/parallel_test.go index 72ce0ffc32..390bd5892d 100644 --- a/parallel_test.go +++ b/parallel_test.go @@ -8,13 +8,13 @@ import ( func TestParallelContainers(t *testing.T) { tests := []struct { name string - reqs []GenericContainerRequest + reqs ParallelContainerRequest resLen int expErrors int }{ { name: "running two containers (success)", - reqs: []GenericContainerRequest{ + reqs: ParallelContainerRequest{ { ContainerRequest: ContainerRequest{ @@ -40,7 +40,7 @@ func TestParallelContainers(t *testing.T) { }, { name: "running two containers (one error)", - reqs: []GenericContainerRequest{ + reqs: ParallelContainerRequest{ { ContainerRequest: ContainerRequest{