github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/worker/runtime/timeout_lock_test.go (about)

     1  package runtime_test
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/pf-qiu/concourse/v6/worker/runtime"
     8  	"github.com/stretchr/testify/require"
     9  	"github.com/stretchr/testify/suite"
    10  )
    11  
    12  type TimeoutLockSuite struct {
    13  	suite.Suite
    14  	*require.Assertions
    15  
    16  	ctx context.Context
    17  }
    18  
    19  func (s *TimeoutLockSuite) SetupTest() {
    20  	s.ctx = context.TODO()
    21  }
    22  
    23  // When disabled, can always successfully acquire & release lock
    24  //
    25  func (s *TimeoutLockSuite) TestLockWhenDisabled() {
    26  	lock := runtime.NewTimeoutLimitLock(time.Millisecond*1, false)
    27  
    28  	err := lock.Acquire(s.ctx)
    29  	s.NoError(err)
    30  
    31  	err = lock.Acquire(s.ctx)
    32  	s.NoError(err)
    33  
    34  	lock.Release()
    35  	lock.Release()
    36  
    37  }
    38  
    39  // When enabled, can successfully acquire & release lock
    40  //
    41  func (s *TimeoutLockSuite) TestLockWithTimeout() {
    42  	lock := runtime.NewTimeoutLimitLock(time.Millisecond*1, true)
    43  
    44  	err := lock.Acquire(s.ctx)
    45  	s.NoError(err)
    46  	defer lock.Release()
    47  
    48  }
    49  
    50  // When enabled, acquiring fails if it timeout elapses
    51  //
    52  func (s *TimeoutLockSuite) TestLockWithTimeoutFailure() {
    53  	lock := runtime.NewTimeoutLimitLock(time.Millisecond*1, true)
    54  
    55  	err := lock.Acquire(s.ctx)
    56  	s.NoError(err)
    57  	defer lock.Release()
    58  
    59  	err = lock.Acquire(s.ctx)
    60  	s.EqualError(err, "context deadline exceeded")
    61  
    62  }