@@ -180,7 +180,7 @@ func Test_StartTest_Success(t *testing.T) {
180
180
// Create our test client
181
181
testHTTPClient := newTestHTTPClient (t , server )
182
182
testClient , err := testapi .NewTestClient (server .URL ,
183
- testapi .WithPollInterval (1 * time .Second ), // short poll interval for faster tests
183
+ testapi .WithPollInterval (1 * time .Second ),
184
184
testapi .WithCustomHTTPClient (testHTTPClient ),
185
185
)
186
186
require .NoError (t , err )
@@ -1047,3 +1047,85 @@ func assertTestFinishedWithOutcomeErrorsAndWarnings(t *testing.T, result testapi
1047
1047
assert .Nil (t , result .GetWarnings ())
1048
1048
}
1049
1049
}
1050
+
1051
+ // TestJitter verifies times returned are 0.5-1.5 times the original value,
1052
+ // and invalid times are return unmodified.
1053
+ func TestJitter (t * testing.T ) {
1054
+ t .Parallel ()
1055
+ t .Run ("returns original duration for zero or negative input" , func (t * testing.T ) {
1056
+ t .Parallel ()
1057
+ assert .Equal (t , time .Duration (0 ), testapi .Jitter (0 ))
1058
+ assert .Equal (t , time .Duration (- 1 ), testapi .Jitter (- 1 ))
1059
+ })
1060
+
1061
+ t .Run ("returns duration within 0.5x to 1.5x of input" , func (t * testing.T ) {
1062
+ t .Parallel ()
1063
+ duration := 100 * time .Millisecond
1064
+ minDur := time .Duration (float64 (duration ) * 0.5 )
1065
+ maxDur := time .Duration (float64 (duration ) * 1.5 )
1066
+
1067
+ for range 100 {
1068
+ jittered := testapi .Jitter (duration )
1069
+ assert .GreaterOrEqual (t , jittered , minDur )
1070
+ assert .LessOrEqual (t , jittered , maxDur )
1071
+ }
1072
+ })
1073
+ }
1074
+
1075
+ // Test_Wait_CallsJitter ensures Jitter is called while polling for job completion.
1076
+ func Test_Wait_CallsJitter (t * testing.T ) {
1077
+ t .Parallel ()
1078
+ ctx := context .Background ()
1079
+
1080
+ testData := setupTestScenario (t )
1081
+
1082
+ params := testapi.StartTestParams {
1083
+ OrgID : testData .OrgID .String (),
1084
+ Subject : testData .TestSubjectCreate ,
1085
+ }
1086
+
1087
+ // Mock Jitter
1088
+ var jitterCalled bool
1089
+ jitterFunc := func (d time.Duration ) time.Duration {
1090
+ jitterCalled = true
1091
+ return d
1092
+ }
1093
+
1094
+ // Mock server handler
1095
+ handlerConfig := TestAPIHandlerConfig {
1096
+ OrgID : testData .OrgID ,
1097
+ JobID : testData .JobID ,
1098
+ TestID : testData .TestID ,
1099
+ APIVersion : testapi .DefaultAPIVersion ,
1100
+ PollCounter : testData .PollCounter ,
1101
+ JobPollResponses : []JobPollResponseConfig {
1102
+ {Status : testapi .Pending }, // First poll
1103
+ {ShouldRedirect : true }, // Second poll, redirects
1104
+ },
1105
+ FinalTestResult : FinalTestResultConfig {
1106
+ Outcome : testapi .Pass ,
1107
+ },
1108
+ }
1109
+ handler := newTestAPIMockHandler (t , handlerConfig )
1110
+ server , cleanup := startMockServer (t , handler )
1111
+ defer cleanup ()
1112
+
1113
+ // Act
1114
+ testHTTPClient := newTestHTTPClient (t , server )
1115
+ testClient , err := testapi .NewTestClient (server .URL ,
1116
+ testapi .WithPollInterval (1 * time .Second ),
1117
+ testapi .WithCustomHTTPClient (testHTTPClient ),
1118
+ testapi .WithJitterFunc (jitterFunc ),
1119
+ )
1120
+ require .NoError (t , err )
1121
+
1122
+ handle , err := testClient .StartTest (ctx , params )
1123
+ require .NoError (t , err )
1124
+ require .NotNil (t , handle )
1125
+
1126
+ err = handle .Wait (ctx )
1127
+ require .NoError (t , err )
1128
+
1129
+ // Assert
1130
+ assert .True (t , jitterCalled )
1131
+ }
0 commit comments