-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_lock_test.go
96 lines (76 loc) · 1.69 KB
/
context_lock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package hclconfig
import (
"fmt"
"sync"
"testing"
"time"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
)
func TestContextLockDoesNotAllowConcurrentAccesstoContext(t *testing.T) {
a := &hcl.EvalContext{Variables: map[string]cty.Value{}}
w := sync.WaitGroup{}
w.Add(2)
go func() {
// get a lock but never unlock it
getContextLock(a)
for i := 0; i < 100; i++ {
a.Variables[fmt.Sprintf("%d", i)] = cty.StringVal("bar")
}
w.Done()
}()
go func() {
unlock := getContextLock(a)
defer unlock()
for i := 0; i < 100; i++ {
a.Variables[fmt.Sprintf("%d", i)] = cty.StringVal("bar")
}
w.Done()
}()
done := make(chan struct{})
go func() {
w.Wait()
<-done
}()
to := time.NewTimer(100 * time.Millisecond)
select {
case <-to.C:
t.Log("timed out waiting for wait group, test passed")
case <-done:
t.Fatal("should not have completed")
}
}
func TestContextLockAllowsConcurrentAccesstoDifferentContexts(t *testing.T) {
a := &hcl.EvalContext{Variables: map[string]cty.Value{}}
b := &hcl.EvalContext{Variables: map[string]cty.Value{}}
w := sync.WaitGroup{}
w.Add(2)
go func() {
unlock := getContextLock(a)
defer unlock()
for i := 0; i < 100; i++ {
a.Variables[fmt.Sprintf("%d", i)] = cty.StringVal("bar")
}
w.Done()
}()
go func() {
unlock := getContextLock(b)
defer unlock()
for i := 0; i < 100; i++ {
b.Variables[fmt.Sprintf("%d", i)] = cty.StringVal("bar")
}
w.Done()
}()
done := make(chan struct{})
go func() {
w.Wait()
done <- struct{}{}
}()
to := time.NewTimer(100 * time.Millisecond)
select {
case <-to.C:
t.Fatal("timed out waiting for wait group")
case <-done:
t.Log("wait group completed, test passed")
}
}