Skip to content

Commit ef82cbe

Browse files
authored
Terraform: use reference to MutexKV in Config (#141)
We don't want to have a copy of the `sync.Locker` in the `Config` since lock value shold not be copied. Usually `Config` used as a singleton in different Terraform provider resources and all of them should use a single `sync.Locker` instead of different copies. This commit changes `MutexKV` field of the `Config` to be a reference instead of a copy and updates `MutexKV` tests to check that `MutexKV` is used in a proper way.
1 parent 6f54843 commit ef82cbe

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

terraform/auth/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type Config struct {
5858
TerraformVersion string
5959
SDKVersion string
6060

61-
mutexkv.MutexKV
61+
*mutexkv.MutexKV
6262
}
6363

6464
// LoadAndValidate performs the authentication and initial configuration

terraform/mutexkv/mutexkv_test.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import (
66
)
77

88
func TestMutexKVLock(t *testing.T) {
9-
mkv := NewMutexKV()
9+
s := struct {
10+
mkv *MutexKV
11+
}{
12+
mkv: NewMutexKV(),
13+
}
1014

11-
mkv.Lock("foo")
15+
s.mkv.Lock("foo")
1216

1317
doneCh := make(chan struct{})
1418

1519
go func() {
16-
mkv.Lock("foo")
20+
s.mkv.Lock("foo")
1721
close(doneCh)
1822
}()
1923

@@ -26,15 +30,19 @@ func TestMutexKVLock(t *testing.T) {
2630
}
2731

2832
func TestMutexKVUnlock(t *testing.T) {
29-
mkv := NewMutexKV()
33+
s := struct {
34+
mkv *MutexKV
35+
}{
36+
mkv: NewMutexKV(),
37+
}
3038

31-
mkv.Lock("foo")
32-
mkv.Unlock("foo")
39+
s.mkv.Lock("foo")
40+
s.mkv.Unlock("foo")
3341

3442
doneCh := make(chan struct{})
3543

3644
go func() {
37-
mkv.Lock("foo")
45+
s.mkv.Lock("foo")
3846
close(doneCh)
3947
}()
4048

@@ -47,14 +55,18 @@ func TestMutexKVUnlock(t *testing.T) {
4755
}
4856

4957
func TestMutexKVDifferentKeys(t *testing.T) {
50-
mkv := NewMutexKV()
58+
s := struct {
59+
mkv *MutexKV
60+
}{
61+
mkv: NewMutexKV(),
62+
}
5163

52-
mkv.Lock("foo")
64+
s.mkv.Lock("foo")
5365

5466
doneCh := make(chan struct{})
5567

5668
go func() {
57-
mkv.Lock("bar")
69+
s.mkv.Lock("bar")
5870
close(doneCh)
5971
}()
6072

0 commit comments

Comments
 (0)