-
Notifications
You must be signed in to change notification settings - Fork 148
/
child_test.go
113 lines (89 loc) · 1.84 KB
/
child_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
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
package tableflip
import (
"os"
"testing"
)
func TestChildExit(t *testing.T) {
env, procs := testEnv()
child, err := startChild(env, nil)
if err != nil {
t.Fatal(err)
}
proc := <-procs
proc.exit(nil)
if err := <-child.result; err != nil {
t.Error("Wait returns non-nil error:", err)
}
}
func TestChildKill(t *testing.T) {
env, procs := testEnv()
child, err := startChild(env, nil)
if err != nil {
t.Fatal(err)
}
proc := <-procs
go child.Kill()
if sig := proc.recvSignal(nil); sig != os.Kill {
t.Errorf("Received %v instead of os.Kill", sig)
}
proc.exit(nil)
}
func TestChildNotReady(t *testing.T) {
env, procs := testEnv()
child, err := startChild(env, nil)
if err != nil {
t.Fatal(err)
}
proc := <-procs
proc.exit(nil)
<-child.result
<-child.exited
select {
case <-child.ready:
t.Error("Child signals readiness without pipe being closed")
default:
}
}
func TestChildReady(t *testing.T) {
env, procs := testEnv()
child, err := startChild(env, nil)
if err != nil {
t.Fatal(err)
}
proc := <-procs
if _, _, err := proc.notify(); err != nil {
t.Fatal("Can't notify:", err)
}
<-child.ready
proc.exit(nil)
}
func TestChildPassedFds(t *testing.T) {
env, procs := testEnv()
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
in := map[fileName]*file{
{"r"}: newFile(r.Fd(), fileName{"r"}),
{"w"}: newFile(w.Fd(), fileName{"w"}),
}
if _, err := startChild(env, in); err != nil {
t.Fatal(err)
}
proc := <-procs
out, _, err := proc.notify()
if err != nil {
t.Fatal("Notify failed:", err)
}
if len(out) != len(in) {
t.Errorf("Expected %d files, got %d", len(in), len(out))
}
for name, inFd := range in {
if outFd, ok := out[name]; !ok {
t.Error(name, "is missing")
} else if outFd.Fd() != inFd.Fd() {
t.Error(name, "fd mismatch:", outFd.Fd(), inFd.Fd())
}
}
proc.exit(nil)
}