jeudi 22 janvier 2015

Testing Goroutine That My Code Does Not Wait For

I have a function that is executed concurrently. It's task is to run a command line method, logging an error if there is one. My main thread of execution does not wait for this method to finish. It simply returns optimistically.


How do I test my function? Assuming that I give it a cmdStr, such as {"sleep", "1"}, that works but doesn't return instantly, how can I get my test to wait for this function to finish?


I want to ensure that the actual program that runs this does not have to wait for it to finish.



func runCmd(cmdStr []string, errChan chan error) {
cmd := exec.Command(cmdStr...)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Start()
if err != nil {
errChan <- err
return
}
// Command started successfully, so our caller can continue.
errChan <- nil
err = cmd.Wait()
if err != nil {
log.Println(err)
}
}

Aucun commentaire:

Enregistrer un commentaire