From 7a464b1156d53e5e3bbacad3543e18d492671215 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Mon, 10 Aug 2015 15:03:02 -0500 Subject: [PATCH] tests: extract deadlock checking test helper --- terraform/context_plan_test.go | 26 ++++++-------------------- terraform/context_test.go | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 20 deletions(-) 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