@@ -16,6 +16,7 @@ import (
1616 "os/signal"
1717 "strconv"
1818 "strings"
19+ "sync"
1920 "syscall"
2021 "text/tabwriter"
2122 "time"
@@ -140,6 +141,17 @@ func main() {
140141 r , err := runner .New (ctx , serviceSpecs )
141142 must (err )
142143
144+ stopAll := sync .OnceValues (func () (map [string ]* os.ProcessState , error ) {
145+ states , err := r .StopAll ()
146+ cancelFunc ()
147+ return states , err
148+ })
149+ mustStopAll := func () map [string ]* os.ProcessState {
150+ states , err := stopAll ()
151+ must (err )
152+ return states
153+ }
154+
143155 servicesErrCh := make (chan error , len (unversionedSpecs ))
144156
145157 go func () {
@@ -168,10 +180,12 @@ func main() {
168180
169181 criticalPath , err := r .StartAll (servicesErrCh )
170182 if errors .Is (err , context .Canceled ) {
171- _ , err := r .StopAll ()
172- must (err )
183+ mustStopAll ()
173184 return
174185 }
186+ if err != nil {
187+ mustStopAll ()
188+ }
175189 must (err )
176190
177191 // API is NewWriter(output io.Writer, minwidth, tabwidth, padding int, padchar byte, flags uint) *Writer
@@ -261,8 +275,7 @@ func main() {
261275 select {
262276 case <- ctx .Done ():
263277 log .Println ("Shutting down services." )
264- _ , err := r .StopAll ()
265- must (err )
278+ mustStopAll ()
266279 log .Println ("Cleaning up." )
267280 return
268281 case ibazelCmd := <- interactiveCh :
@@ -302,20 +315,21 @@ func main() {
302315 if testErr != nil {
303316 log .Printf ("Encountered error during test run: %s\n " , testErr )
304317 if isOneShot {
318+ mustStopAll ()
305319 os .Exit (1 )
306320 }
307321 }
308322 case serviceErr := <- servicesErrCh :
309323 log .Print (serviceErr )
310324 if isOneShot {
325+ mustStopAll ()
311326 log .Fatal ("Service exited uncleanly, marking test as failed.\n \n " )
312327 }
313328 }
314329
315330 if isOneShot {
316331 buf .WriteString ("Target\t User Time\t System Time\n " )
317- states , err := r .StopAll ()
318- must (err )
332+ states := mustStopAll ()
319333 for label , state := range states {
320334 buf .WriteString (fmt .Sprintf ("%s\t %s\t %s\n " ,
321335 label , state .UserTime (), state .SystemTime ()))
@@ -637,4 +651,3 @@ func buildTestEnv(ports svclib.Ports) ([]string, error) {
637651
638652 return baseEnv , nil
639653}
640-
0 commit comments