Skip to content

Commit 7a464b1

Browse files
committed
tests: extract deadlock checking test helper
1 parent 9cd8881 commit 7a464b1

2 files changed

Lines changed: 28 additions & 20 deletions

File tree

‎terraform/context_plan_test.go‎

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010
"sync"
1111
"testing"
12-
"time"
1312
)
1413

1514
func TestContext2Plan(t *testing.T) {
@@ -176,16 +175,11 @@ func TestContext2Plan_moduleCycle(t *testing.T) {
176175
}
177176

178177
func TestContext2Plan_moduleDeadlock(t *testing.T) {
179-
m := testModule(t, "plan-module-deadlock")
180-
p := testProvider("aws")
181-
p.DiffFn = testDiffFn
182-
timeout := make(chan bool, 1)
183-
done := make(chan bool, 1)
184-
go func() {
185-
time.Sleep(3 * time.Second)
186-
timeout <- true
187-
}()
188-
go func() {
178+
testCheckDeadlock(t, func() {
179+
m := testModule(t, "plan-module-deadlock")
180+
p := testProvider("aws")
181+
p.DiffFn = testDiffFn
182+
189183
ctx := testContext2(t, &ContextOpts{
190184
Module: m,
191185
Providers: map[string]ResourceProviderFactory{
@@ -194,7 +188,6 @@ func TestContext2Plan_moduleDeadlock(t *testing.T) {
194188
})
195189

196190
plan, err := ctx.Plan()
197-
done <- true
198191
if err != nil {
199192
t.Fatalf("err: %s", err)
200193
}
@@ -215,14 +208,7 @@ STATE:
215208
if actual != expected {
216209
t.Fatalf("expected:\n%sgot:\n%s", expected, actual)
217210
}
218-
}()
219-
220-
select {
221-
case <-timeout:
222-
t.Fatalf("timed out! probably deadlock")
223-
case <-done:
224-
// ok
225-
}
211+
})
226212
}
227213

228214
func TestContext2Plan_moduleInput(t *testing.T) {

‎terraform/context_test.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66
"testing"
7+
"time"
78
)
89

910
func testContext2(t *testing.T, opts *ContextOpts) *Context {
@@ -170,6 +171,27 @@ func resourceState(resourceType, resourceID string) *ResourceState {
170171
}
171172
}
172173

174+
// Test helper that gives a function 3 seconds to finish, assumes deadlock and
175+
// fails test if it does not.
176+
func testCheckDeadlock(t *testing.T, f func()) {
177+
timeout := make(chan bool, 1)
178+
done := make(chan bool, 1)
179+
go func() {
180+
time.Sleep(3 * time.Second)
181+
timeout <- true
182+
}()
183+
go func(f func(), done chan bool) {
184+
defer func() { done <- true }()
185+
f()
186+
}(f, done)
187+
select {
188+
case <-timeout:
189+
t.Fatalf("timed out! probably deadlock")
190+
case <-done:
191+
// ok
192+
}
193+
}
194+
173195
const testContextGraph = `
174196
root: root
175197
aws_instance.bar

0 commit comments

Comments
 (0)