github.com/opentofu/opentofu@v1.7.1/internal/tofu/util_test.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package tofu
     7  
     8  import (
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestSemaphore(t *testing.T) {
    14  	s := NewSemaphore(2)
    15  	timer := time.AfterFunc(time.Second, func() {
    16  		panic("deadlock")
    17  	})
    18  	defer timer.Stop()
    19  
    20  	s.Acquire()
    21  	if !s.TryAcquire() {
    22  		t.Fatalf("should acquire")
    23  	}
    24  	if s.TryAcquire() {
    25  		t.Fatalf("should not acquire")
    26  	}
    27  	s.Release()
    28  	s.Release()
    29  
    30  	// This release should panic
    31  	defer func() {
    32  		r := recover()
    33  		if r == nil {
    34  			t.Fatalf("should panic")
    35  		}
    36  	}()
    37  	s.Release()
    38  }