-
Notifications
You must be signed in to change notification settings - Fork 3
/
parallel_test.go
62 lines (56 loc) · 1.19 KB
/
parallel_test.go
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
package goproc
import (
"strconv"
"sync/atomic"
"testing"
"time"
"github.com/zeebo/assert"
)
func TestParallel(t *testing.T) {
proc := NewWithCleanup()
proc.AddCommand(&Cmd{
Program: `echo`,
Parameters: []string{`1`},
MaxRestart: 4,
})
proc.AddCommand(&Cmd{
Program: `echo`,
Parameters: []string{`2`},
MaxRestart: 4,
})
proc.StartAllParallel().Wait()
}
func TestParallelMultiple(t *testing.T) {
proc := NewWithCleanup()
var total uint32
firstCmdId := proc.AddCommand(&Cmd{
Program: `echo`,
Parameters: []string{`-1`},
MaxRestart: 4,
OnProcessCompleted: func(cmd *Cmd, durationMs int64) {
atomic.AddUint32(&total, 1)
time.Sleep(100 * time.Millisecond)
},
})
wg := proc.StartAllParallel()
const n = 10
for z := range n {
go func() {
cmdId := proc.AddCommand(&Cmd{
Program: `echo`,
Parameters: []string{strconv.Itoa(z)},
MaxRestart: 4,
OnProcessCompleted: func(cmd *Cmd, durationMs int64) {
atomic.AddUint32(&total, 1)
},
})
proc.Start(cmdId)
}()
}
time.Sleep(500 * time.Millisecond)
wg.Wait()
assert.Equal(t, uint32(5+n*5), total)
// call again once
proc.Start(firstCmdId)
assert.Equal(t, uint32(5+n*5+5), total)
}