@@ -23,6 +23,7 @@ import (
2323	"github.com/google/go-cmp/cmp" 
2424	v1 "k8s.io/api/core/v1" 
2525	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 
26+ 	"k8s.io/klog/v2/ktesting" 
2627	clocktesting "k8s.io/utils/clock/testing" 
2728	"k8s.io/utils/pointer" 
2829)
@@ -466,3 +467,46 @@ func TestGetRemainingBackoffTime(t *testing.T) {
466467		})
467468	}
468469}
470+ 
471+ func  TestGetRemainingBackoffTimePerIndex (t  * testing.T ) {
472+ 	defaultTestTime  :=  metav1 .NewTime (time .Date (2009 , time .November , 10 , 23 , 0 , 0 , 0 , time .UTC ))
473+ 	testCases  :=  map [string ]struct  {
474+ 		currentTime     time.Time 
475+ 		maxBackoff      time.Duration 
476+ 		defaultBackoff  time.Duration 
477+ 		lastFailedPod   * v1.Pod 
478+ 		wantDuration    time.Duration 
479+ 	}{
480+ 		"no failures" : {
481+ 			lastFailedPod :  nil ,
482+ 			defaultBackoff : 5  *  time .Second ,
483+ 			maxBackoff :     700  *  time .Second ,
484+ 			wantDuration :   0  *  time .Second ,
485+ 		},
486+ 		"two prev failures; current time and failure time are same" : {
487+ 			lastFailedPod :  buildPod ().phase (v1 .PodFailed ).indexFailureCount ("2" ).customDeletionTimestamp (defaultTestTime .Time ).Pod ,
488+ 			currentTime :    defaultTestTime .Time ,
489+ 			defaultBackoff : 5  *  time .Second ,
490+ 			maxBackoff :     700  *  time .Second ,
491+ 			wantDuration :   20  *  time .Second ,
492+ 		},
493+ 		"one prev failure counted and one ignored; current time and failure time are same" : {
494+ 			lastFailedPod :  buildPod ().phase (v1 .PodFailed ).indexFailureCount ("1" ).indexIgnoredFailureCount ("1" ).customDeletionTimestamp (defaultTestTime .Time ).Pod ,
495+ 			currentTime :    defaultTestTime .Time ,
496+ 			defaultBackoff : 5  *  time .Second ,
497+ 			maxBackoff :     700  *  time .Second ,
498+ 			wantDuration :   20  *  time .Second ,
499+ 		},
500+ 	}
501+ 
502+ 	for  name , tc  :=  range  testCases  {
503+ 		t .Run (name , func (t  * testing.T ) {
504+ 			logger , _  :=  ktesting .NewTestContext (t )
505+ 			fakeClock  :=  clocktesting .NewFakeClock (tc .currentTime .Truncate (time .Second ))
506+ 			d  :=  getRemainingTimePerIndex (logger , fakeClock , tc .defaultBackoff , tc .maxBackoff , tc .lastFailedPod )
507+ 			if  d .Seconds () !=  tc .wantDuration .Seconds () {
508+ 				t .Errorf ("Expected value of duration %v; got %v" , tc .wantDuration , d )
509+ 			}
510+ 		})
511+ 	}
512+ }
0 commit comments