Retry rate-limited Robolectric downloads with a Maven Central mirror (#3620)

This commit is contained in:
MicroCool authored and GitHub committed 2026-12-03 07:26:11 -05:00
1 parent affb0da37f
commit cec2e4ed70
4 files changed
+115 -3

No files matched your search

+21 -3
View File
@@ -128,14 +128,32 @@ jobs:
run: |
set -o pipefail
mkdir -p app/build
log=app/build/unit-tests.log
attempt_log=app/build/unit-tests-attempt.log
: > "$log"
mirror=()
central='https://(repo1\.maven\.org|repo\.maven\.apache\.org)/maven2/org/robolectric/'
gradle_error='((Caused by: )?org\.gradle\.internal\.resource\.transport\.http\.HttpErrorStatusCodeException: |>[[:space:]]+)?'
java_error='((Caused by: )?java\.io\.IOException: )?'
# A Maven 429 fails every test class that needs the missing jar. The jars an attempt did get stay for the next.
for wait in 60 180 0; do
status=0
./gradlew testReleaseUnitTest --console=plain --info --stacktrace -Drobolectric.logging=stderr -PskipRust=true -PunitTestsOnly=true -PndkVersion="$NDK_VERSION" 2>&1 | tee app/build/unit-tests.log || status=$?
if [ "$status" = 0 ] || [ "$wait" = 0 ] || ! grep -Eq '^[[:space:]]*(Caused by: org\.gradle\.internal\.resource\.transport\.http\.HttpErrorStatusCodeException: Could not GET .*https://(repo\.maven\.apache\.org|repo1\.maven\.org)/.*Received status code 429 from server|Caused by: java\.io\.IOException: Server returned HTTP response code: 429 for URL: https://(repo\.maven\.apache\.org|repo1\.maven\.org)/)' app/build/unit-tests.log; then
./gradlew testReleaseUnitTest --console=plain --info --stacktrace -Drobolectric.logging=stderr -PskipRust=true -PunitTestsOnly=true -PndkVersion="$NDK_VERSION" "${mirror[@]}" 2>&1 | tee "$attempt_log" || status=$?
cat "$attempt_log" >> "$log"
if [ "$status" = 0 ] || [ "$wait" = 0 ]; then
exit "$status"
fi
echo "::warning::Maven Central answered 429; running the unit tests again in ${wait}s"
if [ "${#mirror[@]}" = 0 ] && grep -qE \
-e "^[[:space:]]*${gradle_error}Could not (GET|HEAD) '?${central}[^'[:space:]]+'?\. Received status code 429([[:space:]]|$)" \
-e "^[[:space:]]*${java_error}Server returned HTTP response code: 429 for URL: ${central}[^[:space:]]+" "$attempt_log"; then
mirror=(-ProbolectricMavenMirror=https://maven-central.storage-download.googleapis.com/maven2)
echo "::notice::Maven Central rate limited Robolectric. Retrying unit tests with the Google Cloud mirror." | tee -a "$log"
continue
fi
if ! grep -Eq '^[[:space:]]*(Caused by: org\.gradle\.internal\.resource\.transport\.http\.HttpErrorStatusCodeException: Could not GET .*https://(repo\.maven\.apache\.org|repo1\.maven\.org)/.*Received status code 429 from server|Caused by: java\.io\.IOException: Server returned HTTP response code: 429 for URL: https://(repo\.maven\.apache\.org|repo1\.maven\.org)/)' "$attempt_log"; then
exit "$status"
fi
echo "::warning::Maven Central answered 429; running the unit tests again in ${wait}s" | tee -a "$log"
sleep "$wait"
done
+3
View File
@@ -242,6 +242,9 @@ android {
doFirst { jvmTemp.mkdirs() }
def logTarget = providers.systemProperty('robolectric.logging').orNull
if (logTarget != null) systemProperty 'robolectric.logging', logTarget
// Robolectric resolves Android SDK jars inside the test JVM independently of Gradle.
def robolectricMirror = providers.gradleProperty('robolectricMavenMirror').orNull
if (robolectricMirror) systemProperty 'robolectric.dependency.repo.url', robolectricMirror
}
}
+12
View File
@@ -8,6 +8,18 @@ pluginManagement {
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
def robolectricMirror = providers.gradleProperty('robolectricMavenMirror').orNull
if (robolectricMirror) {
exclusiveContent {
forRepository {
maven {
name = 'robolectricMirror'
url = uri(robolectricMirror)
}
}
filter { includeGroup 'org.robolectric' }
}
}
google()
mavenCentral()
maven { url 'https://jitpack.io' }
+79
View File
@@ -75,6 +75,85 @@ class CiChecksTest(unittest.TestCase):
self.assertIn("outputs.doNotCacheIf('CI executes the test suite on every run') { true }", gradle)
self.assertIn('outputs.upToDateWhen { false }', gradle)
def test_unit_tests_switch_mirrors_only_for_robolectric_rate_limits(self):
steps = yaml.safe_load((ROOT / '.github/workflows/unit-tests.yml').read_text())['jobs']['unit-tests']['steps']
script = next(step['run'] for step in steps if step.get('id') == 'unit-tests')
central = 'https://repo.maven.apache.org/maven2/'
native = f'{central}org/robolectric/nativeruntime-dist-compat/1.0.18/nativeruntime-dist-compat-1.0.18.jar'
sdk = 'https://repo1.maven.org/maven2/org/robolectric/android-all-instrumented/sdk.jar.sha512'
gradle_error = 'org.gradle.internal.resource.transport.http.HttpErrorStatusCodeException'
get_error = f"Could not GET '{native}'. Received status code 429 from server: Too Many Requests"
mirror = '-ProbolectricMavenMirror=https://maven-central.storage-download.googleapis.com/maven2'
# These two resolvers report the status and URL in opposite orders.
cases = [
('ok', 0, 0, 0, 1),
(f'Could not GET {native}. Received status code 429', 1, 0, 0, 2),
(f'Server returned HTTP response code: 429 for URL: {sdk}', 1, 9, 9, 2),
(f' Caused by: {gradle_error}: {get_error}', 1, 0, 0, 2),
(f' Caused by: java.io.IOException: Server returned HTTP response code: 429 for URL: {sdk}', 1, 0, 0, 2),
(f'Failed to get resource: GET. [HTTP HTTP/1.1 429 Too Many Requests: {native})]\n'
'java.lang.AssertionError: expected a visible window', 7, 0, 7, 1),
('AssertionError: expected 429', 7, 0, 7, 1),
(f'AssertionError: expected status code 429 for URL: {sdk}', 7, 0, 7, 1),
(f'Caused by: java.lang.AssertionError: {get_error}', 7, 0, 7, 1),
(f'Could not GET {central}other/group/lib.jar. Received status code 429', 8, 0, 8, 1),
]
for message, first, retry, expected, attempts in cases:
with self.subTest(message=message), tempfile.TemporaryDirectory() as directory:
root = Path(directory)
gradle = root / 'gradlew'
gradle.write_text('#!/bin/sh\nprintf "%s\\n" "$*" >> calls\n'
'case "$*" in *-ProbolectricMavenMirror=*) exit "$RETRY";; esac\n'
'echo "$MESSAGE"\nexit "$FIRST"\n')
gradle.chmod(0o755)
sleep = root / 'sleep'
sleep.write_text('#!/bin/sh\necho "sleep $*" >> calls\n')
sleep.chmod(0o755)
env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'],
NDK_VERSION='test', MESSAGE=message, FIRST=str(first), RETRY=str(retry))
result = subprocess.run(['bash', '-e', '-c', script], cwd=root, env=env, capture_output=True, timeout=15)
self.assertEqual(result.returncode, expected, result.stdout)
calls = (root / 'calls').read_text().splitlines()
self.assertEqual(len(calls), attempts)
self.assertNotIn(mirror, calls[0])
if attempts == 2:
self.assertEqual(calls[1].split(), [*calls[0].split(), mirror])
def test_unit_tests_back_off_on_maven_central_rate_limits(self):
steps = yaml.safe_load((ROOT / '.github/workflows/unit-tests.yml').read_text())['jobs']['unit-tests']['steps']
script = next(step['run'] for step in steps if step.get('id') == 'unit-tests')
rate_limit = (
"Caused by: org.gradle.internal.resource.transport.http.HttpErrorStatusCodeException: "
"Could not GET 'https://repo.maven.apache.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar'. "
"Received status code 429 from server: Too Many Requests"
)
for exit_codes, expected_status, expected_events in (
([7, 0], 0, ['gradle', 'sleep 60', 'gradle']),
([7, 9, 23, 0], 23, ['gradle', 'sleep 60', 'gradle', 'sleep 180', 'gradle']),
):
with self.subTest(exit_codes=exit_codes), tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / 'statuses').write_text(''.join(f'{code}\n' for code in exit_codes))
gradle = root / 'gradlew'
gradle.write_text('#!/bin/sh\n'
'echo gradle >> events\n'
'echo "$*" >> gradle-args\n'
'attempt=$(wc -l < gradle-args)\n'
'status=$(sed -n "${attempt}p" statuses)\n'
'if [ "$status" != 0 ]; then echo "$MAVEN_ERROR"; fi\n'
'exit "${status:-99}"\n')
gradle.chmod(0o755)
sleep = root / 'sleep'
sleep.write_text('#!/bin/sh\necho "sleep $*" >> events\n')
sleep.chmod(0o755)
env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'],
NDK_VERSION='test', MAVEN_ERROR=rate_limit)
result = subprocess.run(['bash', '-e', '-c', script], cwd=root, env=env,
capture_output=True, text=True, timeout=15)
self.assertEqual(result.returncode, expected_status, result.stdout + result.stderr)
self.assertEqual((root / 'events').read_text().splitlines(), expected_events)
self.assertNotIn('-ProbolectricMavenMirror=', (root / 'gradle-args').read_text())
def test_unit_tests_do_not_hold_up_the_apk(self):
build = yaml.safe_load((ROOT / '.github/workflows/build.yml').read_text())
for step in build['jobs']['build']['steps']: