From 97ba15277154142acca63479bbf38ac5f2b401a2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 28 May 2013 18:02:14 -0700 Subject: [PATCH] packer: tests for RemoteCommand.Wait --- packer/communicator_test.go | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 packer/communicator_test.go diff --git a/packer/communicator_test.go b/packer/communicator_test.go new file mode 100644 index 000000000..bba6addbd --- /dev/null +++ b/packer/communicator_test.go @@ -0,0 +1,50 @@ +package packer + +import ( + "testing" + "time" +) + +func TestRemoteCommand_WaitBlocks(t *testing.T) { + t.Parallel() + + rc := &RemoteCommand{} + + complete := make(chan bool) + + // Make a goroutine that never exits. Since this is just in a test, + // this should be okay. + go func() { + rc.Wait() + complete <- true + }() + + select { + case <-complete: + t.Fatal("It never should've completed") + case <-time.After(500 * time.Millisecond): + // All is well + } +} + +func TestRemoteCommand_WaitCompletes(t *testing.T) { + t.Parallel() + + rc := &RemoteCommand{} + + complete := make(chan bool) + go func() { + rc.Wait() + complete <- true + }() + + // Flag that it completed + rc.Exited = true + + select { + case <-complete: + // All is well + case <-time.After(1 * time.Second): + t.Fatal("Timeout waiting for command completion.") + } +}