Repository navigation
Expand file tree
/
Copy pathprocess_test.go
More file actions
162 lines (134 loc) · 3.02 KB
/
Copy pathprocess_test.go
File metadata and controls
162 lines (134 loc) · 3.02 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
package neffos
import (
"fmt"
"sync"
"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(50 * time.Millisecond)
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 TestProcessDoneIdempotent(t *testing.T) {
procs := newProcesses()
p := procs.get("done-twice")
p.Start()
p.Done()
// Second Done must be a no-op rather than panicking with "close of closed channel".
p.Done()
if !p.isDone() {
t.Fatal("expected process to be done after Done()")
}
// Wait must return immediately since we're already done.
done := make(chan struct{})
go func() {
p.Wait()
close(done)
}()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("Wait did not return immediately after Done")
}
}
func TestProcessRestart(t *testing.T) {
procs := newProcesses()
p := procs.get("restart")
// First run.
p.Start()
if p.isDone() {
t.Fatal("expected process to be running right after Start")
}
p.Done()
if !p.isDone() {
t.Fatal("expected process to be done after Done")
}
p.Wait() // not running anymore, must return immediately.
// Second run: Start again must make Wait block until the new Done.
p.Start()
if p.isDone() {
t.Fatal("expected process to be running after the second Start")
}
waitReturned := make(chan struct{})
go func() {
p.Wait()
close(waitReturned)
}()
select {
case <-waitReturned:
t.Fatal("Wait returned before the second Done was called")
case <-time.After(50 * time.Millisecond):
}
p.Done()
select {
case <-waitReturned:
case <-time.After(time.Second):
t.Fatal("Wait did not return after the second Done")
}
if !p.isDone() {
t.Fatal("expected process to be done after the second Done")
}
}
func TestProcessWaitWithoutStartReturns(t *testing.T) {
procs := newProcesses()
p := procs.get("never-started")
if !p.isDone() {
t.Fatal("expected a never-started process to report isDone")
}
done := make(chan struct{})
go func() {
p.Wait()
close(done)
}()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("Wait blocked on a process that was never started")
}
}
func TestProcessesGetSamePointerConcurrently(t *testing.T) {
procs := newProcesses()
const name = "shared"
const goroutines = 50
var (
wg sync.WaitGroup
results = make([]*process, goroutines)
)
wg.Add(goroutines)
for i := range goroutines {
go func(i int) {
defer wg.Done()
results[i] = procs.get(name)
}(i)
}
wg.Wait()
first := results[0]
if first == nil {
t.Fatal("expected a non-nil process")
}
for i, p := range results {
if p != first {
t.Fatalf("goroutine %d got a different *process pointer than goroutine 0", i)
}
}
}