github.com/hs0210/hashicorp-terraform@v0.11.12-beta1/terraform/util_test.go (about) 1 package terraform 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 "time" 8 ) 9 10 func TestSemaphore(t *testing.T) { 11 s := NewSemaphore(2) 12 timer := time.AfterFunc(time.Second, func() { 13 panic("deadlock") 14 }) 15 defer timer.Stop() 16 17 s.Acquire() 18 if !s.TryAcquire() { 19 t.Fatalf("should acquire") 20 } 21 if s.TryAcquire() { 22 t.Fatalf("should not acquire") 23 } 24 s.Release() 25 s.Release() 26 27 // This release should panic 28 defer func() { 29 r := recover() 30 if r == nil { 31 t.Fatalf("should panic") 32 } 33 }() 34 s.Release() 35 } 36 37 func TestStrSliceContains(t *testing.T) { 38 if strSliceContains(nil, "foo") { 39 t.Fatalf("Bad") 40 } 41 if strSliceContains([]string{}, "foo") { 42 t.Fatalf("Bad") 43 } 44 if strSliceContains([]string{"bar"}, "foo") { 45 t.Fatalf("Bad") 46 } 47 if !strSliceContains([]string{"bar", "foo"}, "foo") { 48 t.Fatalf("Bad") 49 } 50 } 51 52 func TestUniqueStrings(t *testing.T) { 53 cases := []struct { 54 Input []string 55 Expected []string 56 }{ 57 { 58 []string{}, 59 []string{}, 60 }, 61 { 62 []string{"x"}, 63 []string{"x"}, 64 }, 65 { 66 []string{"a", "b", "c"}, 67 []string{"a", "b", "c"}, 68 }, 69 { 70 []string{"a", "a", "a"}, 71 []string{"a"}, 72 }, 73 { 74 []string{"a", "b", "a", "b", "a", "a"}, 75 []string{"a", "b"}, 76 }, 77 { 78 []string{"c", "b", "a", "c", "b"}, 79 []string{"a", "b", "c"}, 80 }, 81 } 82 83 for i, tc := range cases { 84 t.Run(fmt.Sprintf("unique-%d", i), func(t *testing.T) { 85 actual := uniqueStrings(tc.Input) 86 if !reflect.DeepEqual(tc.Expected, actual) { 87 t.Fatalf("Expected: %q\nGot: %q", tc.Expected, actual) 88 } 89 }) 90 } 91 }