removed ResetTrackedSessions, lock trackedSessions before access Signed-off-by: Maria Ntalla <mntalla@pivotal.io>
diff --git a/gexec/session.go b/gexec/session.go index d213b07..1dd5068 100644 --- a/gexec/session.go +++ b/gexec/session.go
@@ -215,13 +215,7 @@ } var trackedSessions = []*Session{} - -/* -Resets the sessions tracked by gexec after Run is called. -*/ -func ResetTrackedSessions() { - trackedSessions = []*Session{} -} +var trackedSessionsMutex = &sync.Mutex{} /* Kill sends a SIGKILL signal to all the processes started by Run, and waits for them to exit. @@ -230,6 +224,8 @@ If any of the processes already exited, KillAndWait returns silently. */ func KillAndWait(timeout ...interface{}) { + trackedSessionsMutex.Lock() + defer trackedSessionsMutex.Unlock() for _, session := range trackedSessions { session.Kill().Wait(timeout...) } @@ -242,6 +238,8 @@ If any of the processes already exited, TerminateAndWait returns silently. */ func TerminateAndWait(timeout ...interface{}) { + trackedSessionsMutex.Lock() + defer trackedSessionsMutex.Unlock() for _, session := range trackedSessions { session.Terminate().Wait(timeout...) } @@ -254,6 +252,8 @@ If any of the processes already exited, Kill returns silently. */ func Kill() { + trackedSessionsMutex.Lock() + defer trackedSessionsMutex.Unlock() for _, session := range trackedSessions { session.Kill() } @@ -266,6 +266,8 @@ If any of the processes already exited, Terminate returns silently. */ func Terminate() { + trackedSessionsMutex.Lock() + defer trackedSessionsMutex.Unlock() for _, session := range trackedSessions { session.Terminate() } @@ -278,6 +280,8 @@ If any of the processes already exited, Signal returns silently. */ func Signal(signal os.Signal) { + trackedSessionsMutex.Lock() + defer trackedSessionsMutex.Unlock() for _, session := range trackedSessions { session.Signal(signal) } @@ -290,6 +294,8 @@ If any of the processes already exited, Interrupt returns silently. */ func Interrupt() { + trackedSessionsMutex.Lock() + defer trackedSessionsMutex.Unlock() for _, session := range trackedSessions { session.Interrupt() }
diff --git a/gexec/session_test.go b/gexec/session_test.go index 173be8d..5a7af74 100644 --- a/gexec/session_test.go +++ b/gexec/session_test.go
@@ -128,7 +128,10 @@ }) Context("tracking sessions", func() { - BeforeEach(ResetTrackedSessions) + BeforeEach(func() { + KillAndWait() + }) + Describe("kill", func() { It("should kill all the started sessions, and not wait", func() { session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter) @@ -256,28 +259,6 @@ Eventually(session3).Should(Exit(128 + 2)) }) }) - - Describe("reset", func() { - It("should not track process before the reset", func() { - session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter) - Ω(err).ShouldNot(HaveOccurred()) - defer func() { session1.Kill().Wait() }() - - ResetTrackedSessions() - - session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter) - Ω(err).ShouldNot(HaveOccurred()) - - session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter) - Ω(err).ShouldNot(HaveOccurred()) - - KillAndWait() - - Ω(session1).ShouldNot(Exit(), "Should not have exited") - Ω(session2).Should(Exit(128+9), "Should have exited") - Ω(session3).Should(Exit(128+9), "Should have exited") - }) - }) }) Context("when the command exits", func() {