1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// Copyright 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package parallel_test
import (
"sort"
"sync"
"sync/atomic"
stdtesting "testing"
"time"
"github.com/juju/testing"
gc "gopkg.in/check.v1"
"github.com/juju/utils/v2/parallel"
)
type parallelSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(¶llelSuite{})
func (*parallelSuite) TestParallelMaxPar(c *gc.C) {
const (
totalDo = 10
maxConcurrentRunnersPar = 3
)
var mu sync.Mutex
maxConcurrentRunners := 0
nbRunners := 0
nbRuns := 0
parallelRunner := parallel.NewRun(maxConcurrentRunnersPar)
for i := 0; i < totalDo; i++ {
parallelRunner.Do(func() error {
mu.Lock()
nbRuns++
nbRunners++
if nbRunners > maxConcurrentRunners {
maxConcurrentRunners = nbRunners
}
mu.Unlock()
time.Sleep(time.Second / 10)
mu.Lock()
nbRunners--
mu.Unlock()
return nil
})
}
err := parallelRunner.Wait()
if nbRunners != 0 {
c.Errorf("%d functions still running", nbRunners)
}
if nbRuns != totalDo {
c.Errorf("all functions not executed; want %d got %d", totalDo, nbRuns)
}
c.Check(err, gc.IsNil)
if maxConcurrentRunners != maxConcurrentRunnersPar {
c.Errorf("wrong number of do's ran at once; want %d got %d", maxConcurrentRunnersPar, maxConcurrentRunners)
}
}
func nothing() error {
return nil
}
func BenchmarkRunSingle(b *stdtesting.B) {
for i := 0; i < b.N; i++ {
r := parallel.NewRun(1)
r.Do(nothing)
r.Wait()
}
}
func BenchmarkRun1000p100(b *stdtesting.B) {
for i := 0; i < b.N; i++ {
r := parallel.NewRun(100)
for j := 0; j < 1000; j++ {
r.Do(nothing)
}
r.Wait()
}
}
func (*parallelSuite) TestConcurrentDo(c *gc.C) {
r := parallel.NewRun(3)
var count int32
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
r.Do(func() error {
atomic.AddInt32(&count, 1)
return nil
})
wg.Done()
}()
}
wg.Wait()
err := r.Wait()
c.Assert(err, gc.IsNil)
c.Assert(count, gc.Equals, int32(100))
}
type intError int
func (intError) Error() string {
return "error"
}
func (*parallelSuite) TestParallelError(c *gc.C) {
const (
totalDo = 10
errDo = 5
)
parallelRun := parallel.NewRun(6)
for i := 0; i < totalDo; i++ {
i := i
if i >= errDo {
parallelRun.Do(func() error {
return intError(i)
})
} else {
parallelRun.Do(func() error {
return nil
})
}
}
err := parallelRun.Wait()
c.Check(err, gc.NotNil)
errs := err.(parallel.Errors)
c.Check(len(errs), gc.Equals, totalDo-errDo)
ints := make([]int, len(errs))
for i, err := range errs {
ints[i] = int(err.(intError))
}
sort.Ints(ints)
for i, n := range ints {
c.Check(n, gc.Equals, i+errDo)
}
}
func (*parallelSuite) TestZeroWorkerPanics(c *gc.C) {
defer func() {
r := recover()
c.Check(r, gc.Matches, "parameter max must be >= 1")
}()
parallel.NewRun(0)
}
|