Skip to content

Commit

Permalink
refactoring: rename all methods and errors working with parallel cont…
Browse files Browse the repository at this point in the history
…ainers
  • Loading branch information
Vladimir Stepanov committed Jun 16, 2022
1 parent 33f0243 commit 0d5b2ab
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
6 changes: 3 additions & 3 deletions docs/features/creating_container.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestIntegrationNginxLatestReturn(t *testing.T) {

# Parallel running

`testcontainers.GenericParallelContainers` - defines the containers that should be run in parallel mode.
`testcontainers.ParallelContainers` - defines the containers that should be run in parallel mode.

```go
ctx := context.Background()
Expand Down Expand Up @@ -112,10 +112,10 @@ requests := []GenericContainerRequest{
},
}

res, err := GenericParallelContainers(ctx, requests, GenericParallelOptions{})
res, err := ParallelContainers(ctx, requests, ParallelContainersOptions{})

if err != nil {
e, ok := err.(GenericParallelErrors)
e, ok := err.(ParallelContainersError)
if !ok {
log.Fatalf("unknown error: %v", err)
}
Expand Down
33 changes: 16 additions & 17 deletions parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@ const (
defaultWorkersCount = 8
)

// GenericParallelOptions represents additional options for running
// ParallelContainersOptions represents additional options for parallel running
// * WorkersCount - count of parallel workers. if field empty(zero), default value will be 'defaultWorkersCount'
type GenericParallelOptions struct {
type ParallelContainersOptions struct {
WorkersCount int
}

// GenericContainerRequestError represents error from parallel request
type GenericContainerRequestError struct {
// ParallelContainersRequestError represents error from parallel request
type ParallelContainersRequestError struct {
Request GenericContainerRequest
Error error
}

type GenericParallelErrors struct {
Errors []GenericContainerRequestError
type ParallelContainersError struct {
Errors []ParallelContainersRequestError
}

func (gpe GenericParallelErrors) Error() string {
func (gpe ParallelContainersError) Error() string {
return fmt.Sprintf("%v", gpe.Errors)
}

func genericContainerRunner(
func parallelContainersRunner(
ctx context.Context,
requests <-chan GenericContainerRequest,
errors chan<- GenericContainerRequestError,
errors chan<- ParallelContainersRequestError,
containers chan<- Container,
wg *sync.WaitGroup) {
for req := range requests {
c, err := GenericContainer(ctx, req)
if err != nil {
errors <- GenericContainerRequestError{
errors <- ParallelContainersRequestError{
Request: req,
Error: err,
}
Expand All @@ -50,8 +50,8 @@ func genericContainerRunner(
wg.Done()
}

// GenericParallelContainers creates a generic containers with parameters and run it in parallel mode
func GenericParallelContainers(ctx context.Context, reqs []GenericContainerRequest, opt GenericParallelOptions) ([]Container, error) {
// ParallelContainers creates a generic containers with parameters and run it in parallel mode
func ParallelContainers(ctx context.Context, reqs []GenericContainerRequest, opt ParallelContainersOptions) ([]Container, error) {
if opt.WorkersCount == 0 {
opt.WorkersCount = defaultWorkersCount
}
Expand All @@ -62,19 +62,19 @@ func GenericParallelContainers(ctx context.Context, reqs []GenericContainerReque
}

tasksChan := make(chan GenericContainerRequest, tasksChanSize)
errsChan := make(chan GenericContainerRequestError)
errsChan := make(chan ParallelContainersRequestError)
resChan := make(chan Container)
waitRes := make(chan struct{})

containers := make([]Container, 0)
errors := make([]GenericContainerRequestError, 0)
errors := make([]ParallelContainersRequestError, 0)

wg := sync.WaitGroup{}
wg.Add(tasksChanSize)

// run workers
for i := 0; i < tasksChanSize; i++ {
go genericContainerRunner(ctx, tasksChan, errsChan, resChan, &wg)
go parallelContainersRunner(ctx, tasksChan, errsChan, resChan, &wg)
}

go func() {
Expand Down Expand Up @@ -113,9 +113,8 @@ func GenericParallelContainers(ctx context.Context, reqs []GenericContainerReque
<-waitRes

if len(errors) == 0 {
return containers, GenericParallelErrors{Errors: errors}
return containers, ParallelContainersError{Errors: errors}
}

return containers, nil

}
6 changes: 3 additions & 3 deletions parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestGenericParallelContainers(t *testing.T) {
func TestParallelContainers(t *testing.T) {
tests := []struct {
name string
reqs []GenericContainerRequest
Expand Down Expand Up @@ -69,10 +69,10 @@ func TestGenericParallelContainers(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
res, err := GenericParallelContainers(context.Background(), tc.reqs, GenericParallelOptions{})
res, err := ParallelContainers(context.Background(), tc.reqs, ParallelContainersOptions{})

if err != nil && tc.expErrors > 0 {
e, _ := err.(GenericParallelErrors)
e, _ := err.(ParallelContainersError)

if len(e.Errors) != tc.expErrors {
t.Fatalf("expected erorrs: %d, got: %d\n", tc.expErrors, len(e.Errors))
Expand Down

0 comments on commit 0d5b2ab

Please sign in to comment.