Skip to content

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
* add new type ParallelContainerRequest
* update docs for parallel request
  • Loading branch information
Vladimir Stepanov committed Jun 19, 2022
1 parent d2efcb4 commit 31f4317
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 40 deletions.
84 changes: 50 additions & 34 deletions docs/features/creating_container.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
```
7 changes: 4 additions & 3 deletions parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Expand All @@ -40,7 +40,7 @@ func TestParallelContainers(t *testing.T) {
},
{
name: "running two containers (one error)",
reqs: []GenericContainerRequest{
reqs: ParallelContainerRequest{
{
ContainerRequest: ContainerRequest{

Expand Down

0 comments on commit 31f4317

Please sign in to comment.