From bbc5f305e2de978a510a35ab1445d62af41be8cb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 3 Jun 2013 10:49:23 -0700 Subject: [PATCH] packer: RemoteCmd.Wait --- packer/communicator.go | 8 ++++++++ packer/communicator_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/packer/communicator.go b/packer/communicator.go index d1b6f3a66..936a55eda 100644 --- a/packer/communicator.go +++ b/packer/communicator.go @@ -2,6 +2,7 @@ package packer import ( "io" + "time" ) // RemoteCmd represents a remote command being prepared or run. @@ -42,3 +43,10 @@ type Communicator interface { Upload(string, io.Reader) error Download(string, io.Writer) error } + +// Wait waits for the remote command to complete. +func (r *RemoteCmd) Wait() { + for !r.Exited { + time.Sleep(50 * time.Millisecond) + } +} diff --git a/packer/communicator_test.go b/packer/communicator_test.go index d7c610c15..2c4e9e33b 100644 --- a/packer/communicator_test.go +++ b/packer/communicator_test.go @@ -1 +1,26 @@ package packer + +import ( + "testing" + "time" +) + +func TestRemoteCmd_Wait(t *testing.T) { + var cmd RemoteCmd + + result := make(chan bool) + go func() { + cmd.Wait() + result <- true + }() + + cmd.ExitStatus = 42 + cmd.Exited = true + + select { + case <-result: + // Success + case <-time.After(500 * time.Millisecond): + t.Fatal("never got exit notification") + } +}