go.temporal.io/server@v1.23.0/common/timer/validation_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 timer
    26  
    27  import (
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/stretchr/testify/assert"
    32  	"google.golang.org/protobuf/types/known/durationpb"
    33  )
    34  
    35  func TestValidateAndCapTimer(t *testing.T) {
    36  	t.Parallel()
    37  
    38  	testCases := []struct {
    39  		name                  string
    40  		timerDuration         *durationpb.Duration
    41  		expectedErr           error
    42  		expectedTimerDuration *durationpb.Duration
    43  	}{
    44  		{
    45  			name:                  "nil timer duration",
    46  			timerDuration:         nil,
    47  			expectedErr:           nil,
    48  			expectedTimerDuration: nil,
    49  		},
    50  		{
    51  			name:                  "negative timer duration",
    52  			timerDuration:         durationpb.New(-time.Minute),
    53  			expectedErr:           errNegativeDuration,
    54  			expectedTimerDuration: nil,
    55  		},
    56  		{
    57  			name:                  "zero timer duration",
    58  			timerDuration:         durationpb.New(0),
    59  			expectedErr:           nil,
    60  			expectedTimerDuration: durationpb.New(0),
    61  		},
    62  		{
    63  			name:          "cap timer duration",
    64  			timerDuration: durationpb.New(200 * 365 * 24 * time.Hour),
    65  
    66  			expectedErr:           nil,
    67  			expectedTimerDuration: durationpb.New(MaxAllowedTimer),
    68  		},
    69  		{
    70  			name:                  "valid timer duration",
    71  			timerDuration:         durationpb.New(time.Hour),
    72  			expectedErr:           nil,
    73  			expectedTimerDuration: durationpb.New(time.Hour),
    74  		},
    75  	}
    76  
    77  	for _, tc := range testCases {
    78  		tc := tc
    79  		t.Run(tc.name, func(t *testing.T) {
    80  			t.Parallel()
    81  
    82  			actualErr := ValidateAndCapTimer(tc.timerDuration)
    83  
    84  			assert.Equal(t, tc.expectedErr, actualErr)
    85  			if tc.expectedErr == nil {
    86  				assert.Equal(t, tc.expectedTimerDuration, tc.timerDuration)
    87  			}
    88  		})
    89  	}
    90  }