Skip to content

Commit 675144f

Browse files
authored
Merge pull request moby#26514 from AkihiroSuda/fix-pkg-integration-race
[test] fix races in pkg/integration/cmd
2 parents 04c189f + e17f77e commit 675144f

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

pkg/integration/cmd/command.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"runtime"
1010
"strings"
11+
"sync"
1112
"syscall"
1213
"time"
1314

@@ -49,15 +50,32 @@ func ProcessExitCode(err error) (exitCode int) {
4950
return
5051
}
5152

53+
type lockedBuffer struct {
54+
m sync.RWMutex
55+
buf bytes.Buffer
56+
}
57+
58+
func (buf *lockedBuffer) Write(b []byte) (int, error) {
59+
buf.m.Lock()
60+
defer buf.m.Unlock()
61+
return buf.buf.Write(b)
62+
}
63+
64+
func (buf *lockedBuffer) String() string {
65+
buf.m.RLock()
66+
defer buf.m.RUnlock()
67+
return buf.buf.String()
68+
}
69+
5270
// Result stores the result of running a command
5371
type Result struct {
5472
Cmd *exec.Cmd
5573
ExitCode int
5674
Error error
5775
// Timeout is true if the command was killed because it ran for too long
5876
Timeout bool
59-
outBuffer *bytes.Buffer
60-
errBuffer *bytes.Buffer
77+
outBuffer *lockedBuffer
78+
errBuffer *lockedBuffer
6179
}
6280

6381
// Assert compares the Result against the Expected struct, and fails the test if
@@ -255,8 +273,8 @@ func buildCmd(cmd Cmd) *Result {
255273
default:
256274
execCmd = exec.Command(cmd.Command[0], cmd.Command[1:]...)
257275
}
258-
outBuffer := new(bytes.Buffer)
259-
errBuffer := new(bytes.Buffer)
276+
outBuffer := new(lockedBuffer)
277+
errBuffer := new(lockedBuffer)
260278

261279
execCmd.Stdin = cmd.Stdin
262280
execCmd.Dir = cmd.Dir

0 commit comments

Comments
 (0)