tests: extract deadlock checking test helper

This commit is contained in:
Paul Hinze 2015-08-10 15:03:02 -05:00
parent 9cd88810f4
commit 7a464b1156
2 changed files with 28 additions and 20 deletions

View File

@ -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) {
testCheckDeadlock(t, func() {
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() {
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) {

View File

@ -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