github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/terraform/util_test.go (about) 1 package terraform 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestSemaphore(t *testing.T) { 9 s := NewSemaphore(2) 10 timer := time.AfterFunc(time.Second, func() { 11 panic("deadlock") 12 }) 13 defer timer.Stop() 14 15 s.Acquire() 16 if !s.TryAcquire() { 17 t.Fatalf("should acquire") 18 } 19 if s.TryAcquire() { 20 t.Fatalf("should not acquire") 21 } 22 s.Release() 23 s.Release() 24 25 // This release should panic 26 defer func() { 27 r := recover() 28 if r == nil { 29 t.Fatalf("should panic") 30 } 31 }() 32 s.Release() 33 } 34 35 func TestStrSliceContains(t *testing.T) { 36 if strSliceContains(nil, "foo") { 37 t.Fatalf("Bad") 38 } 39 if strSliceContains([]string{}, "foo") { 40 t.Fatalf("Bad") 41 } 42 if strSliceContains([]string{"bar"}, "foo") { 43 t.Fatalf("Bad") 44 } 45 if !strSliceContains([]string{"bar", "foo"}, "foo") { 46 t.Fatalf("Bad") 47 } 48 }