@@ -714,7 +714,7 @@ func TestEntraidTokenManager_durationToRenewal(t *testing.T) {
714
714
715
715
// return time to lower bound, if the returned time will be after the lower bound
716
716
result = tm .durationToRenewal ()
717
- assert .InDelta (t , time .Until (tm .token .expiresOn .Add (- 1 * tm .lowerBoundDuration )), result , float64 (time .Second ))
717
+ assert .InEpsilon (t , time .Until (tm .token .expiresOn .Add (- 1 * tm .lowerBoundDuration )), result , float64 (time .Second ))
718
718
})
719
719
720
720
})
@@ -997,11 +997,21 @@ func TestEntraidTokenManager_Streaming(t *testing.T) {
997
997
mock .AssertExpectationsForObjects (t , idp , listener )
998
998
})
999
999
1000
- t .Run ("Start and Listen with retriable error - max retries" , func (t * testing.T ) {
1000
+ t .Run ("Start and Listen with retriable error - max retries and max delay " , func (t * testing.T ) {
1001
1001
idp := & mockIdentityProvider {}
1002
1002
listener := & mockTokenListener {}
1003
+ maxAttempts := 3
1004
+ maxDelayMs := 500
1005
+ initialDelayMs := 100
1003
1006
tokenManager , err := NewTokenManager (idp ,
1004
- TokenManagerOptions {},
1007
+ TokenManagerOptions {
1008
+ RetryOptions : RetryOptions {
1009
+ MaxAttempts : maxAttempts ,
1010
+ MaxDelayMs : maxDelayMs ,
1011
+ InitialDelayMs : initialDelayMs ,
1012
+ BackoffMultiplier : 10 ,
1013
+ },
1014
+ },
1005
1015
)
1006
1016
assert .NoError (t , err )
1007
1017
assert .NotNil (t , tokenManager )
@@ -1028,11 +1038,19 @@ func TestEntraidTokenManager_Streaming(t *testing.T) {
1028
1038
response := idpResponse .(* authResult )
1029
1039
response .authResult = res
1030
1040
}).Return (idpResponse , nil )
1031
-
1032
- listener .On ("OnTokenNext" , mock .AnythingOfType ("*entraid.Token" )).Return ()
1041
+ var start , end time.Time
1042
+ var elapsed time.Duration
1043
+
1044
+ _ = listener .
1045
+ On ("OnTokenNext" , mock .AnythingOfType ("*entraid.Token" )).
1046
+ Run (func (_ mock.Arguments ) {
1047
+ start = time .Now ()
1048
+ }).Return ()
1033
1049
maxAttemptsReached := make (chan struct {})
1034
1050
listener .On ("OnTokenError" , mock .Anything ).Run (func (args mock.Arguments ) {
1035
1051
err := args .Get (0 ).(error )
1052
+ end = time .Now ()
1053
+ elapsed = end .Sub (start )
1036
1054
assert .NotNil (t , err )
1037
1055
assert .ErrorContains (t , err , "max attempts reached" )
1038
1056
close (maxAttemptsReached )
@@ -1042,29 +1060,34 @@ func TestEntraidTokenManager_Streaming(t *testing.T) {
1042
1060
assert .NotNil (t , cancel )
1043
1061
assert .NoError (t , err )
1044
1062
assert .NotNil (t , tm .listener )
1045
-
1046
- noErrCall .Unset ()
1047
- returnErr := newMockError (true )
1048
- idp .On ("RequestToken" ).Return (nil , returnErr )
1049
-
1050
1063
toRenewal := tm .durationToRenewal ()
1051
1064
assert .NotEqual (t , time .Duration (0 ), toRenewal )
1052
1065
assert .NotEqual (t , expiresIn , toRenewal )
1053
1066
assert .True (t , expiresIn > toRenewal )
1054
1067
1068
+ noErrCall .Unset ()
1069
+ returnErr := newMockError (true )
1070
+
1071
+ idp .On ("RequestToken" ).Return (nil , returnErr )
1072
+
1055
1073
select {
1056
- case <- time .After (toRenewal + time .Duration (tm . retryOptions . MaxAttempts * tm . retryOptions . MaxDelayMs )* time .Millisecond ):
1057
- assert .Fail (t , "Timeout - max retries not reached " )
1074
+ case <- time .After (toRenewal + time .Duration (maxAttempts * maxDelayMs )* time .Millisecond ):
1075
+ assert .Fail (t , "Timeout - max retries not reached" )
1058
1076
case <- maxAttemptsReached :
1059
1077
}
1060
1078
1061
- // maxAttempts + the initial one
1079
+ // initialRenewal window, maxAttempts - 1 * max delay + the initial one which was lower than max delay
1080
+ allDelaysShouldBe := toRenewal
1081
+ allDelaysShouldBe += time .Duration (initialDelayMs ) * time .Millisecond
1082
+ allDelaysShouldBe += time .Duration (maxAttempts - 1 ) * time .Duration (maxDelayMs ) * time .Millisecond
1083
+
1084
+ assert .InEpsilon (t , elapsed , allDelaysShouldBe , float64 (10 * time .Millisecond ))
1085
+
1062
1086
idp .AssertNumberOfCalls (t , "RequestToken" , tm .retryOptions .MaxAttempts + 1 )
1063
1087
listener .AssertNumberOfCalls (t , "OnTokenNext" , 1 )
1064
1088
listener .AssertNumberOfCalls (t , "OnTokenError" , 1 )
1065
1089
mock .AssertExpectationsForObjects (t , idp , listener )
1066
1090
})
1067
-
1068
1091
t .Run ("Start and Listen and close during retries" , func (t * testing.T ) {
1069
1092
idp := & mockIdentityProvider {}
1070
1093
listener := & mockTokenListener {}
@@ -1124,7 +1147,7 @@ func TestEntraidTokenManager_Streaming(t *testing.T) {
1124
1147
assert .NotEqual (t , expiresIn , toRenewal )
1125
1148
assert .True (t , expiresIn > toRenewal )
1126
1149
1127
- <- time .After (toRenewal + 50 * time .Millisecond )
1150
+ <- time .After (toRenewal + 500 * time .Millisecond )
1128
1151
assert .Nil (t , cancel ())
1129
1152
1130
1153
select {
0 commit comments