github.com/spinnaker/spin@v1.30.0/cmd/orca-tasks/task_watcher_test.go (about) 1 // Copyright (c) 2023 OpsMx, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package orca_tasks 16 17 import ( 18 "testing" 19 "time" 20 ) 21 22 func Test_calculateSleepTime(t *testing.T) { 23 type args struct { 24 now int64 25 lastTryTime int64 26 attempts int64 27 } 28 tests := []struct { 29 name string 30 args args 31 want time.Duration 32 }{ 33 { 34 "attempt 1, lots of time remaining", 35 args{100, 100000, 1}, 36 time.Duration(1) * time.Second, 37 }, 38 { 39 "attempt 2, lots of time remaining", 40 args{100, 100000, 2}, 41 time.Duration(4) * time.Second, 42 }, 43 { 44 "attempt 100, lots of time remaining", 45 args{100, 100000, 100}, 46 time.Duration(20) * time.Second, 47 }, 48 { 49 "attempt 10, 5 seconds remaining", 50 args{0, 5, 10}, 51 time.Duration(6) * time.Second, 52 }, 53 } 54 for _, tt := range tests { 55 t.Run(tt.name, func(t *testing.T) { 56 if got := calculateSleepTime(tt.args.now, tt.args.lastTryTime, tt.args.attempts); got != tt.want { 57 t.Errorf("calculateSleepTime() = %v, want %v", got, tt.want) 58 } 59 }) 60 } 61 }