| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test HTTP 429 Too Many Requests retry logic' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | . "$TEST_DIRECTORY"/lib-httpd.sh |
| 8 | |
| 9 | start_httpd |
| 10 | |
| 11 | test_expect_success 'setup test repository' ' |
| 12 | test_commit initial && |
| 13 | git clone --bare . "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" && |
| 14 | git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" config http.receivepack true |
| 15 | ' |
| 16 | |
| 17 | # This test suite uses a special HTTP 429 endpoint at /http_429/ that simulates |
| 18 | # rate limiting. The endpoint format is: |
| 19 | # /http_429/<test-context>/<retry-after-value>/<repo-path> |
| 20 | # The http-429.sh script (in t/lib-httpd) returns a 429 response with the |
| 21 | # specified Retry-After header on the first request for each test context, |
| 22 | # then forwards subsequent requests to git-http-backend. Each test context |
| 23 | # is isolated, allowing multiple tests to run independently. |
| 24 | |
| 25 | test_expect_success 'HTTP 429 with retries disabled (maxRetries=0) fails immediately' ' |
| 26 | # Set maxRetries to 0 (disabled) |
| 27 | test_config http.maxRetries 0 && |
| 28 | test_config http.retryAfter 1 && |
| 29 | |
| 30 | # Should fail immediately without any retry attempt |
| 31 | test_must_fail git ls-remote "$HTTPD_URL/http_429/retries-disabled/1/repo.git" 2>err && |
| 32 | |
| 33 | # Verify no retry happened (no "waiting" message in stderr) |
| 34 | test_grep ! -i "waiting.*retry" err |
| 35 | ' |
| 36 | |
| 37 | test_expect_success 'HTTP 429 permanent should fail after max retries' ' |
| 38 | # Enable retries with a limit |
| 39 | test_config http.maxRetries 2 && |
| 40 | |
| 41 | # Git should retry but eventually fail when 429 persists |
| 42 | test_must_fail git ls-remote "$HTTPD_URL/http_429/permanent-fail/permanent/repo.git" 2>err |
| 43 | ' |
| 44 | |
| 45 | test_expect_success 'HTTP 429 with Retry-After is retried and succeeds' ' |
| 46 | # Enable retries |
| 47 | test_config http.maxRetries 3 && |
| 48 | |
| 49 | # Git should retry after receiving 429 and eventually succeed |
| 50 | git ls-remote "$HTTPD_URL/http_429/retry-succeeds/1/repo.git" >output 2>err && |
| 51 | test_grep "refs/heads/" output |
| 52 | ' |
| 53 | |
| 54 | test_expect_success 'HTTP 429 without Retry-After uses configured default' ' |
| 55 | # Enable retries and configure default delay |
| 56 | test_config http.maxRetries 3 && |
| 57 | test_config http.retryAfter 1 && |
| 58 | |
| 59 | # Git should retry using configured default and succeed |
| 60 | git ls-remote "$HTTPD_URL/http_429/no-retry-after-header/none/repo.git" >output 2>err && |
| 61 | test_grep "refs/heads/" output |
| 62 | ' |
| 63 | |
| 64 | test_expect_success 'HTTP 429 retry delays are respected' ' |
| 65 | # Enable retries |
| 66 | test_config http.maxRetries 3 && |
| 67 | |
| 68 | # Time the operation - it should take at least 2 seconds due to retry delay |
| 69 | start=$(test-tool date getnanos) && |
| 70 | git ls-remote "$HTTPD_URL/http_429/retry-delays-respected/2/repo.git" >output 2>err && |
| 71 | duration=$(test-tool date getnanos $start) && |
| 72 | |
| 73 | # Verify it took at least 2 seconds (allowing some tolerance) |
| 74 | duration_int=${duration%.*} && |
| 75 | test "$duration_int" -ge 1 && |
| 76 | test_grep "refs/heads/" output |
| 77 | ' |
| 78 | |
| 79 | test_expect_success 'HTTP 429 fails immediately if Retry-After exceeds http.maxRetryTime' ' |
| 80 | # Configure max retry time to 3 seconds (much less than requested 100) |
| 81 | test_config http.maxRetries 3 && |
| 82 | test_config http.maxRetryTime 3 && |
| 83 | |
| 84 | # Should fail immediately without waiting |
| 85 | start=$(test-tool date getnanos) && |
| 86 | test_must_fail git ls-remote "$HTTPD_URL/http_429/retry-after-exceeds-max-time/100/repo.git" 2>err && |
| 87 | duration=$(test-tool date getnanos $start) && |
| 88 | |
| 89 | # Should fail quickly (no 100 second wait) |
| 90 | duration_int=${duration%.*} && |
| 91 | test "$duration_int" -lt 99 && |
| 92 | test_grep "greater than http.maxRetryTime" err |
| 93 | ' |
| 94 | |
| 95 | test_expect_success 'HTTP 429 fails if configured http.retryAfter exceeds http.maxRetryTime' ' |
| 96 | # Test misconfiguration: retryAfter > maxRetryTime |
| 97 | # Configure retryAfter larger than maxRetryTime |
| 98 | test_config http.maxRetries 3 && |
| 99 | test_config http.retryAfter 100 && |
| 100 | test_config http.maxRetryTime 5 && |
| 101 | |
| 102 | # Should fail immediately with configuration error |
| 103 | start=$(test-tool date getnanos) && |
| 104 | test_must_fail git ls-remote "$HTTPD_URL/http_429/config-retry-after-exceeds-max-time/none/repo.git" 2>err && |
| 105 | duration=$(test-tool date getnanos $start) && |
| 106 | |
| 107 | # Should fail quickly (no 100 second wait) |
| 108 | duration_int=${duration%.*} && |
| 109 | test "$duration_int" -lt 99 && |
| 110 | test_grep "configured http.retryAfter.*exceeds.*http.maxRetryTime" err |
| 111 | ' |
| 112 | |
| 113 | test_expect_success 'HTTP 429 with Retry-After HTTP-date format' ' |
| 114 | # Test HTTP-date format (RFC 2822) in Retry-After header |
| 115 | raw=$(test-tool date timestamp now) && |
| 116 | now="${raw#* -> }" && |
| 117 | future_time=$((now + 2)) && |
| 118 | raw=$(test-tool date show:rfc2822 $future_time) && |
| 119 | future_date="${raw#* -> }" && |
| 120 | future_date_encoded=$(echo "$future_date" | sed "s/ /%20/g") && |
| 121 | |
| 122 | # Enable retries |
| 123 | test_config http.maxRetries 3 && |
| 124 | |
| 125 | # Git should parse the HTTP-date and retry after the delay |
| 126 | start=$(test-tool date getnanos) && |
| 127 | git ls-remote "$HTTPD_URL/http_429/http-date-format/$future_date_encoded/repo.git" >output 2>err && |
| 128 | duration=$(test-tool date getnanos $start) && |
| 129 | |
| 130 | # Should take at least 1 second (allowing tolerance for processing time) |
| 131 | duration_int=${duration%.*} && |
| 132 | test "$duration_int" -ge 1 && |
| 133 | test_grep "refs/heads/" output |
| 134 | ' |
| 135 | |
| 136 | test_expect_success 'HTTP 429 with HTTP-date exceeding maxRetryTime fails immediately' ' |
| 137 | raw=$(test-tool date timestamp now) && |
| 138 | now="${raw#* -> }" && |
| 139 | future_time=$((now + 200)) && |
| 140 | raw=$(test-tool date show:rfc2822 $future_time) && |
| 141 | future_date="${raw#* -> }" && |
| 142 | future_date_encoded=$(echo "$future_date" | sed "s/ /%20/g") && |
| 143 | |
| 144 | # Configure max retry time much less than the 200 second delay |
| 145 | test_config http.maxRetries 3 && |
| 146 | test_config http.maxRetryTime 10 && |
| 147 | |
| 148 | # Should fail immediately without waiting 200 seconds |
| 149 | start=$(test-tool date getnanos) && |
| 150 | test_must_fail git ls-remote "$HTTPD_URL/http_429/http-date-exceeds-max-time/$future_date_encoded/repo.git" 2>err && |
| 151 | duration=$(test-tool date getnanos $start) && |
| 152 | |
| 153 | # Should fail quickly (not wait 200 seconds) |
| 154 | duration_int=${duration%.*} && |
| 155 | test "$duration_int" -lt 199 && |
| 156 | test_grep "http.maxRetryTime" err |
| 157 | ' |
| 158 | |
| 159 | test_expect_success 'HTTP 429 with past HTTP-date should not wait' ' |
| 160 | raw=$(test-tool date timestamp now) && |
| 161 | now="${raw#* -> }" && |
| 162 | past_time=$((now - 10)) && |
| 163 | raw=$(test-tool date show:rfc2822 $past_time) && |
| 164 | past_date="${raw#* -> }" && |
| 165 | past_date_encoded=$(echo "$past_date" | sed "s/ /%20/g") && |
| 166 | |
| 167 | # Enable retries |
| 168 | test_config http.maxRetries 3 && |
| 169 | |
| 170 | # Git should retry immediately without waiting |
| 171 | start=$(test-tool date getnanos) && |
| 172 | git ls-remote "$HTTPD_URL/http_429/past-http-date/$past_date_encoded/repo.git" >output 2>err && |
| 173 | duration=$(test-tool date getnanos $start) && |
| 174 | |
| 175 | # Should complete quickly (no wait for a past-date Retry-After) |
| 176 | duration_int=${duration%.*} && |
| 177 | test "$duration_int" -lt 5 && |
| 178 | test_grep "refs/heads/" output |
| 179 | ' |
| 180 | |
| 181 | test_expect_success 'HTTP 429 with invalid Retry-After format uses configured default' ' |
| 182 | # Configure default retry-after |
| 183 | test_config http.maxRetries 3 && |
| 184 | test_config http.retryAfter 1 && |
| 185 | |
| 186 | # Should use configured default (1 second) since header is invalid |
| 187 | start=$(test-tool date getnanos) && |
| 188 | git ls-remote "$HTTPD_URL/http_429/invalid-retry-after-format/invalid/repo.git" >output 2>err && |
| 189 | duration=$(test-tool date getnanos $start) && |
| 190 | |
| 191 | # Should take at least 1 second (the configured default) |
| 192 | duration_int=${duration%.*} && |
| 193 | test "$duration_int" -ge 1 && |
| 194 | test_grep "refs/heads/" output && |
| 195 | test_grep "waiting.*retry" err |
| 196 | ' |
| 197 | |
| 198 | test_expect_success 'HTTP 429 will not be retried without config' ' |
| 199 | # Default config means http.maxRetries=0 (retries disabled) |
| 200 | # When 429 is received, it should fail immediately without retry |
| 201 | # Do NOT configure anything - use defaults (http.maxRetries defaults to 0) |
| 202 | |
| 203 | # Should fail immediately without retry |
| 204 | test_must_fail git ls-remote "$HTTPD_URL/http_429/no-retry-without-config/1/repo.git" 2>err && |
| 205 | |
| 206 | # Verify no retry happened (no "waiting" message) |
| 207 | test_grep ! -i "waiting.*retry" err && |
| 208 | |
| 209 | # Should get 429 error |
| 210 | test_grep "429" err |
| 211 | ' |
| 212 | |
| 213 | test_expect_success 'GIT_HTTP_RETRY_AFTER overrides http.retryAfter config' ' |
| 214 | # Configure retryAfter to 10 seconds |
| 215 | test_config http.maxRetries 3 && |
| 216 | test_config http.retryAfter 10 && |
| 217 | |
| 218 | # Override with environment variable to 1 second |
| 219 | start=$(test-tool date getnanos) && |
| 220 | GIT_HTTP_RETRY_AFTER=1 git ls-remote "$HTTPD_URL/http_429/env-retry-after-override/none/repo.git" >output 2>err && |
| 221 | duration=$(test-tool date getnanos $start) && |
| 222 | |
| 223 | # Should use env var (1 second), not config (10 seconds) |
| 224 | duration_int=${duration%.*} && |
| 225 | test "$duration_int" -ge 1 && |
| 226 | test "$duration_int" -lt 5 && |
| 227 | test_grep "refs/heads/" output && |
| 228 | test_grep "waiting.*retry" err |
| 229 | ' |
| 230 | |
| 231 | test_expect_success 'GIT_HTTP_MAX_RETRIES overrides http.maxRetries config' ' |
| 232 | # Configure maxRetries to 0 (disabled) |
| 233 | test_config http.maxRetries 0 && |
| 234 | test_config http.retryAfter 1 && |
| 235 | |
| 236 | # Override with environment variable to enable retries |
| 237 | GIT_HTTP_MAX_RETRIES=3 git ls-remote "$HTTPD_URL/http_429/env-max-retries-override/1/repo.git" >output 2>err && |
| 238 | |
| 239 | # Should retry (env var enables it despite config saying disabled) |
| 240 | test_grep "refs/heads/" output && |
| 241 | test_grep "waiting.*retry" err |
| 242 | ' |
| 243 | |
| 244 | test_expect_success 'GIT_HTTP_MAX_RETRY_TIME overrides http.maxRetryTime config' ' |
| 245 | # Configure maxRetryTime to 100 seconds (would accept 50 second delay) |
| 246 | test_config http.maxRetries 3 && |
| 247 | test_config http.maxRetryTime 100 && |
| 248 | |
| 249 | # Override with environment variable to 10 seconds (should reject 50 second delay) |
| 250 | start=$(test-tool date getnanos) && |
| 251 | test_must_fail env GIT_HTTP_MAX_RETRY_TIME=10 \ |
| 252 | git ls-remote "$HTTPD_URL/http_429/env-max-retry-time-override/50/repo.git" 2>err && |
| 253 | duration=$(test-tool date getnanos $start) && |
| 254 | |
| 255 | # Should fail quickly (not wait 50 seconds) because env var limits to 10 |
| 256 | duration_int=${duration%.*} && |
| 257 | test "$duration_int" -lt 49 && |
| 258 | test_grep "greater than http.maxRetryTime" err |
| 259 | ' |
| 260 | |
| 261 | test_expect_success 'verify normal repository access still works' ' |
| 262 | git ls-remote "$HTTPD_URL/smart/repo.git" >output && |
| 263 | test_grep "refs/heads/" output |
| 264 | ' |
| 265 | |
| 266 | test_done |