2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-09-20 07:16:53 -05:00
|
|
|
package tofu
|
2014-09-17 12:53:24 -05:00
|
|
|
|
2014-10-16 12:04:07 -05:00
|
|
|
import (
|
|
|
|
"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()
|
|
|
|
}
|