go.temporal.io/server@v1.23.0/common/quotas/priority_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 priorityReservationSuite struct { 38 suite.Suite 39 *require.Assertions 40 41 controller *gomock.Controller 42 decidingReservation *MockReservation 43 otherReservation *MockReservation 44 } 45 ) 46 47 func TestPriorityReservationSuite(t *testing.T) { 48 s := new(priorityReservationSuite) 49 suite.Run(t, s) 50 } 51 52 func (s *priorityReservationSuite) SetupSuite() { 53 54 } 55 56 func (s *priorityReservationSuite) TearDownSuite() { 57 58 } 59 60 func (s *priorityReservationSuite) SetupTest() { 61 s.Assertions = require.New(s.T()) 62 63 s.controller = gomock.NewController(s.T()) 64 s.decidingReservation = NewMockReservation(s.controller) 65 s.otherReservation = NewMockReservation(s.controller) 66 } 67 68 func (s *priorityReservationSuite) TearDownTest() { 69 s.controller.Finish() 70 } 71 72 func (s *priorityReservationSuite) Test_NotOK_OK() { 73 s.decidingReservation.EXPECT().OK().Return(false) 74 reservation := NewPriorityReservation(s.decidingReservation, []Reservation{s.otherReservation}) 75 76 result := reservation.OK() 77 s.False(result) 78 } 79 80 func (s *priorityReservationSuite) Test_OK_OK() { 81 s.decidingReservation.EXPECT().OK().Return(true) 82 reservation := NewPriorityReservation(s.decidingReservation, []Reservation{s.otherReservation}) 83 84 result := reservation.OK() 85 s.True(result) 86 } 87 88 func (s *priorityReservationSuite) Test_CancelAt() { 89 now := time.Now() 90 s.decidingReservation.EXPECT().CancelAt(now) 91 s.otherReservation.EXPECT().CancelAt(now) 92 reservation := NewPriorityReservation(s.decidingReservation, []Reservation{s.otherReservation}) 93 94 reservation.CancelAt(now) 95 } 96 97 func (s *priorityReservationSuite) Test_DelayFrom() { 98 now := time.Now() 99 decidingReservationDelay := time.Second 100 otherReservationDelay := time.Minute 101 s.decidingReservation.EXPECT().DelayFrom(now).Return(decidingReservationDelay) 102 s.otherReservation.EXPECT().DelayFrom(now).Return(otherReservationDelay).Times(0) 103 reservation := NewPriorityReservation(s.decidingReservation, []Reservation{s.otherReservation}) 104 105 result := reservation.DelayFrom(now) 106 s.Equal(decidingReservationDelay, result) 107 }