Skip to content

Commit ad79e1f

Browse files
committed
Fix spurious Upgrader test failures
TestUpgraderOnOS is susceptible to a race: if the child process exits too quickly after calling Ready(), the parent may see the exit before the readiness notification. Wait for the parent to exit before exiting the test.
1 parent 062b0ef commit ad79e1f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

upgrader_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,23 @@ func childProcess(upg *Upgrader) error {
121121
file.Close()
122122
}
123123

124+
rExit, err := upg.Fds.File("rExit")
125+
if err != nil {
126+
return err
127+
}
128+
129+
// Ready closes all inherited but unused files.
124130
if err := upg.Ready(); err != nil {
125131
return fmt.Errorf("can't signal ready: %s", err)
126132
}
127133

134+
// Block until the parent is done with us. Returning an
135+
// error here won't make the parent fail, so don't bother.
136+
if rExit != nil {
137+
var b [1]byte
138+
rExit.Read(b[:])
139+
}
140+
128141
return nil
129142
}
130143

@@ -159,6 +172,11 @@ func TestUpgraderOnOS(t *testing.T) {
159172

160173
addPipe("wState", wState)
161174

175+
rExit, wExit := pipe()
176+
defer wExit.Close()
177+
178+
addPipe("rExit", rExit)
179+
162180
var readers []*os.File
163181
defer func() {
164182
for _, r := range readers {
@@ -188,6 +206,9 @@ func TestUpgraderOnOS(t *testing.T) {
188206
}
189207
}
190208

209+
// Tell child it's OK to exit now.
210+
wExit.Close()
211+
191212
// Close copies of write pipes, so that
192213
// reads below return EOF.
193214
u.Stop()

0 commit comments

Comments
 (0)