diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index 289779511..61864fb5d 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -9,7 +9,6 @@ import ( "strings" "sync" "testing" - "time" ) func TestContext2Plan(t *testing.T) { @@ -176,16 +175,11 @@ func TestContext2Plan_moduleCycle(t *testing.T) { } func TestContext2Plan_moduleDeadlock(t *testing.T) { - m := testModule(t, "plan-module-deadlock") - p := testProvider("aws") - p.DiffFn = testDiffFn - timeout := make(chan bool, 1) - done := make(chan bool, 1) - go func() { - time.Sleep(3 * time.Second) - timeout <- true - }() - go func() { + testCheckDeadlock(t, func() { + m := testModule(t, "plan-module-deadlock") + p := testProvider("aws") + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ Module: m, Providers: map[string]ResourceProviderFactory{ @@ -194,7 +188,6 @@ func TestContext2Plan_moduleDeadlock(t *testing.T) { }) plan, err := ctx.Plan() - done <- true if err != nil { t.Fatalf("err: %s", err) } @@ -215,14 +208,7 @@ STATE: if actual != expected { t.Fatalf("expected:\n%sgot:\n%s", expected, actual) } - }() - - select { - case <-timeout: - t.Fatalf("timed out! probably deadlock") - case <-done: - // ok - } + }) } func TestContext2Plan_moduleInput(t *testing.T) { diff --git a/terraform/context_test.go b/terraform/context_test.go index 8bcbf1b2b..65f840505 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" "testing" + "time" ) func testContext2(t *testing.T, opts *ContextOpts) *Context { @@ -170,6 +171,27 @@ func resourceState(resourceType, resourceID string) *ResourceState { } } +// Test helper that gives a function 3 seconds to finish, assumes deadlock and +// fails test if it does not. +func testCheckDeadlock(t *testing.T, f func()) { + timeout := make(chan bool, 1) + done := make(chan bool, 1) + go func() { + time.Sleep(3 * time.Second) + timeout <- true + }() + go func(f func(), done chan bool) { + defer func() { done <- true }() + f() + }(f, done) + select { + case <-timeout: + t.Fatalf("timed out! probably deadlock") + case <-done: + // ok + } +} + const testContextGraph = ` root: root aws_instance.bar