-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
process_test.go
82 lines (66 loc) · 1.44 KB
/
process_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package neffos
import (
"fmt"
"sync/atomic"
"testing"
"time"
"golang.org/x/sync/errgroup"
)
func TestProcessWaitDone(t *testing.T) {
var testProcessName = "default"
procs := newProcesses()
p := procs.get(testProcessName)
p.Start()
worker := func() error {
defer p.Done()
if p.isDone() {
return fmt.Errorf("%s process should be running", testProcessName)
}
time.Sleep(1 * time.Second)
return nil
}
g := new(errgroup.Group)
g.Go(worker)
p.Wait()
if !p.isDone() {
t.Fatalf("%s process should be finished", testProcessName)
}
if err := g.Wait(); err != nil {
t.Fatal(err)
}
}
func TestProcessSingalFinished(t *testing.T) {
var testProcessName = "default"
procs := newProcesses()
p := procs.get(testProcessName)
var count uint32
worker := func() error {
p.Start()
defer p.Done()
tc := time.NewTicker(time.Second)
defer tc.Stop()
for {
select {
case <-tc.C:
atomic.AddUint32(&count, 1)
case <-p.Finished():
return nil
}
}
}
g := new(errgroup.Group)
g.Go(worker)
var sleepSecs uint32 = 2
time.Sleep(time.Duration(sleepSecs) * time.Second)
p.Signal()
p.Wait()
if !procs.get(testProcessName).isDone() {
t.Fatalf("%s process should be stopped", testProcessName)
}
if err := g.Wait(); err != nil {
t.Fatal(err)
}
if counts := atomic.LoadUint32(&count); counts != sleepSecs {
t.Fatalf("%s process should tik-tok for %d seconds but: %d", testProcessName, sleepSecs, counts)
}
}