2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2014-09-17 12:53:24 -05:00
|
|
|
package terraform
|
|
|
|
|
2014-10-16 12:04:07 -05:00
|
|
|
import (
|
2017-04-08 13:24:00 -05:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2014-10-16 12:04:07 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSemaphore(t *testing.T) {
|
|
|
|
s := NewSemaphore(2)
|
|
|
|
timer := time.AfterFunc(time.Second, func() {
|
|
|
|
panic("deadlock")
|
|
|
|
})
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
s.Acquire()
|
|
|
|
if !s.TryAcquire() {
|
|
|
|
t.Fatalf("should acquire")
|
|
|
|
}
|
|
|
|
if s.TryAcquire() {
|
|
|
|
t.Fatalf("should not acquire")
|
|
|
|
}
|
|
|
|
s.Release()
|
|
|
|
s.Release()
|
|
|
|
|
|
|
|
// This release should panic
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
if r == nil {
|
|
|
|
t.Fatalf("should panic")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
s.Release()
|
|
|
|
}
|
2014-09-17 12:53:24 -05:00
|
|
|
|
|
|
|
func TestStrSliceContains(t *testing.T) {
|
|
|
|
if strSliceContains(nil, "foo") {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if strSliceContains([]string{}, "foo") {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if strSliceContains([]string{"bar"}, "foo") {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if !strSliceContains([]string{"bar", "foo"}, "foo") {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
}
|
2016-12-05 11:24:57 -06:00
|
|
|
|
2017-04-08 13:24:00 -05:00
|
|
|
func TestUniqueStrings(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input []string
|
|
|
|
Expected []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]string{},
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"x"},
|
|
|
|
[]string{"x"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"a", "b", "c"},
|
|
|
|
[]string{"a", "b", "c"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"a", "a", "a"},
|
|
|
|
[]string{"a"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"a", "b", "a", "b", "a", "a"},
|
|
|
|
[]string{"a", "b"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"c", "b", "a", "c", "b"},
|
|
|
|
[]string{"a", "b", "c"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
t.Run(fmt.Sprintf("unique-%d", i), func(t *testing.T) {
|
|
|
|
actual := uniqueStrings(tc.Input)
|
|
|
|
if !reflect.DeepEqual(tc.Expected, actual) {
|
|
|
|
t.Fatalf("Expected: %q\nGot: %q", tc.Expected, actual)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|