go.temporal.io/server@v1.23.0/common/quotas/multi_reservation_impl_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package quotas
    26  
    27  import (
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/golang/mock/gomock"
    32  	"github.com/stretchr/testify/require"
    33  	"github.com/stretchr/testify/suite"
    34  )
    35  
    36  type (
    37  	multiReservationSuite struct {
    38  		suite.Suite
    39  		*require.Assertions
    40  
    41  		controller        *gomock.Controller
    42  		firstReservation  *MockReservation
    43  		secondReservation *MockReservation
    44  	}
    45  )
    46  
    47  func TestMultiReservationSuite(t *testing.T) {
    48  	s := new(multiReservationSuite)
    49  	suite.Run(t, s)
    50  }
    51  
    52  func (s *multiReservationSuite) SetupSuite() {
    53  
    54  }
    55  
    56  func (s *multiReservationSuite) TearDownSuite() {
    57  
    58  }
    59  
    60  func (s *multiReservationSuite) SetupTest() {
    61  	s.Assertions = require.New(s.T())
    62  
    63  	s.controller = gomock.NewController(s.T())
    64  	s.firstReservation = NewMockReservation(s.controller)
    65  	s.secondReservation = NewMockReservation(s.controller)
    66  }
    67  
    68  func (s *multiReservationSuite) TearDownTest() {
    69  	s.controller.Finish()
    70  }
    71  
    72  func (s *multiReservationSuite) Test_NotOK_OK() {
    73  	reservation := NewMultiReservation(false, nil)
    74  
    75  	result := reservation.OK()
    76  	s.False(result)
    77  }
    78  
    79  func (s *multiReservationSuite) Test_NotOK_CancelAt() {
    80  	now := time.Now()
    81  	reservation := NewMultiReservation(false, nil)
    82  
    83  	reservation.CancelAt(now)
    84  }
    85  
    86  func (s *multiReservationSuite) Test_NotOK_DelayFrom() {
    87  	now := time.Now()
    88  	reservation := NewMultiReservation(false, nil)
    89  
    90  	result := reservation.DelayFrom(now)
    91  	s.Equal(InfDuration, result)
    92  }
    93  
    94  func (s *multiReservationSuite) Test_OK_OK() {
    95  	reservation := NewMultiReservation(true, []Reservation{s.firstReservation, s.secondReservation})
    96  
    97  	result := reservation.OK()
    98  	s.True(result)
    99  }
   100  
   101  func (s *multiReservationSuite) Test_OK_CancelAt() {
   102  	now := time.Now()
   103  	s.firstReservation.EXPECT().CancelAt(now)
   104  	s.secondReservation.EXPECT().CancelAt(now)
   105  	reservation := NewMultiReservation(true, []Reservation{s.firstReservation, s.secondReservation})
   106  
   107  	reservation.CancelAt(now)
   108  }
   109  
   110  func (s *multiReservationSuite) Test_OK_DelayFrom() {
   111  	now := time.Now()
   112  	firstReservationDelay := time.Second
   113  	secondReservationDelay := time.Minute
   114  	s.firstReservation.EXPECT().DelayFrom(now).Return(firstReservationDelay).AnyTimes()
   115  	s.secondReservation.EXPECT().DelayFrom(now).Return(secondReservationDelay).AnyTimes()
   116  	reservation := NewMultiReservation(true, []Reservation{s.firstReservation, s.secondReservation})
   117  
   118  	result := reservation.DelayFrom(now)
   119  	s.Equal(secondReservationDelay, result)
   120  }