mutex for h.Process mutation to prevent data races

This commit is contained in:
Naitik Shah 2013-08-19 15:37:41 -07:00
parent 3bfe2f78ad
commit 0b607a2494

View File

@ -48,6 +48,7 @@ type harness struct {
ExeName string // The temp binary from the build. ExeName string // The temp binary from the build.
Addr []string // The addresses for the http servers. Addr []string // The addresses for the http servers.
Process []*os.Process // The server commands, oldest to newest. Process []*os.Process // The server commands, oldest to newest.
ProcessMutex sync.Mutex // The mutex to guard Process manipulation.
RequestWaitGroup sync.WaitGroup // The wait group for the HTTP requests. RequestWaitGroup sync.WaitGroup // The wait group for the HTTP requests.
newProcess chan bool // A bool is sent on restart. newProcess chan bool // A bool is sent on restart.
} }
@ -121,7 +122,9 @@ func (h *harness) Start() {
if err != nil { if err != nil {
h.T.Fatalf("Could not find process with pid: %d", res.Pid) h.T.Fatalf("Could not find process with pid: %d", res.Pid)
} }
h.ProcessMutex.Lock()
h.Process = append(h.Process, process) h.Process = append(h.Process, process)
h.ProcessMutex.Unlock()
h.newProcess <- true h.newProcess <- true
} }
}() }()
@ -129,7 +132,9 @@ func (h *harness) Start() {
if err != nil { if err != nil {
h.T.Fatalf("Failed to start command: %s", err) h.T.Fatalf("Failed to start command: %s", err)
} }
h.ProcessMutex.Lock()
h.Process = append(h.Process, cmd.Process) h.Process = append(h.Process, cmd.Process)
h.ProcessMutex.Unlock()
<-h.newProcess <-h.newProcess
time.Sleep(processWait) time.Sleep(processWait)
} }
@ -154,6 +159,8 @@ func (h *harness) Stop() {
// Returns the most recent server process. // Returns the most recent server process.
func (h *harness) MostRecentProcess() *os.Process { func (h *harness) MostRecentProcess() *os.Process {
h.ProcessMutex.Lock()
defer h.ProcessMutex.Unlock()
l := len(h.Process) l := len(h.Process)
if l == 0 { if l == 0 {
h.T.Fatalf("Most recent command requested before command was created.") h.T.Fatalf("Most recent command requested before command was created.")