github.com/etecs-ru/gnomock@v0.13.2/parallel.go (about) 1 package gnomock 2 3 import "golang.org/x/sync/errgroup" 4 5 // InParallel begins parallel preset execution setup. Use Start to add more 6 // presets with their configuration to parallel execution, and Go() in the end 7 // to kick-off everything 8 func InParallel() *Parallel { 9 return &Parallel{} 10 } 11 12 type configuredPreset struct { 13 Preset 14 15 opts []Option 16 } 17 18 // Parallel is a builder object that configures parallel preset execution 19 type Parallel struct { 20 presets []configuredPreset 21 } 22 23 // Start adds the provided preset with its configuration to the parallel 24 // execution kicked-off by Go(), together with other added presets 25 func (b *Parallel) Start(p Preset, opts ...Option) *Parallel { 26 b.presets = append(b.presets, configuredPreset{p, opts}) 27 28 return b 29 } 30 31 // Go kicks-off parallel preset execution. Returned containers are in the same 32 // order as they were added with Start. An error is returned if any of the 33 // containers failed to start and become available. Even if Go() returns an 34 // errors, there still might be containers created in the process, and it is 35 // callers responsibility to Stop them 36 func (b *Parallel) Go() ([]*Container, error) { 37 var g errgroup.Group 38 39 containers := make([]*Container, len(b.presets)) 40 41 for i, preset := range b.presets { 42 containerIndex := i 43 p := preset 44 45 g.Go(func() error { 46 c, err := Start(p.Preset, p.opts...) 47 containers[containerIndex] = c 48 49 return err 50 }) 51 } 52 53 return containers, g.Wait() 54 }