github.com/Hashicorp/terraform@v0.11.12-beta1/helper/mutexkv/mutexkv_test.go (about) 1 package mutexkv 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestMutexKVLock(t *testing.T) { 9 mkv := NewMutexKV() 10 11 mkv.Lock("foo") 12 13 doneCh := make(chan struct{}) 14 15 go func() { 16 mkv.Lock("foo") 17 close(doneCh) 18 }() 19 20 select { 21 case <-doneCh: 22 t.Fatal("Second lock was able to be taken. This shouldn't happen.") 23 case <-time.After(50 * time.Millisecond): 24 // pass 25 } 26 } 27 28 func TestMutexKVUnlock(t *testing.T) { 29 mkv := NewMutexKV() 30 31 mkv.Lock("foo") 32 mkv.Unlock("foo") 33 34 doneCh := make(chan struct{}) 35 36 go func() { 37 mkv.Lock("foo") 38 close(doneCh) 39 }() 40 41 select { 42 case <-doneCh: 43 // pass 44 case <-time.After(50 * time.Millisecond): 45 t.Fatal("Second lock blocked after unlock. This shouldn't happen.") 46 } 47 } 48 49 func TestMutexKVDifferentKeys(t *testing.T) { 50 mkv := NewMutexKV() 51 52 mkv.Lock("foo") 53 54 doneCh := make(chan struct{}) 55 56 go func() { 57 mkv.Lock("bar") 58 close(doneCh) 59 }() 60 61 select { 62 case <-doneCh: 63 // pass 64 case <-time.After(50 * time.Millisecond): 65 t.Fatal("Second lock on a different key blocked. This shouldn't happen.") 66 } 67 }