-
Notifications
You must be signed in to change notification settings - Fork 530
Add test sharding support for rust_test #3774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brianduff
wants to merge
3
commits into
bazelbuild:main
Choose a base branch
from
brianduff:push-tusnxzxumrtz
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| @REM Copyright 2024 The Bazel Authors. All rights reserved. | ||
| @REM | ||
| @REM Licensed under the Apache License, Version 2.0 (the "License"); | ||
| @REM you may not use this file except in compliance with the License. | ||
| @REM You may obtain a copy of the License at | ||
| @REM | ||
| @REM http://www.apache.org/licenses/LICENSE-2.0 | ||
| @REM | ||
| @REM Unless required by applicable law or agreed to in writing, software | ||
| @REM distributed under the License is distributed on an "AS IS" BASIS, | ||
| @REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| @REM See the License for the specific language governing permissions and | ||
| @REM limitations under the License. | ||
|
|
||
| @REM Wrapper script for rust_test that enables Bazel test sharding support. | ||
| @REM This script intercepts test execution, enumerates tests using libtest's | ||
| @REM --list flag, partitions them by shard index, and runs only the relevant subset. | ||
|
|
||
| @ECHO OFF | ||
| SETLOCAL EnableDelayedExpansion | ||
|
|
||
| SET "TEST_BINARY={{TEST_BINARY}}" | ||
|
|
||
| @REM If sharding is not enabled, run test binary directly | ||
| IF "%TEST_TOTAL_SHARDS%"=="" ( | ||
| "%TEST_BINARY%" %* | ||
| EXIT /B %ERRORLEVEL% | ||
| ) | ||
|
|
||
| @REM Touch status file to advertise sharding support to Bazel | ||
| IF NOT "%TEST_SHARD_STATUS_FILE%"=="" ( | ||
| ECHO.>"%TEST_SHARD_STATUS_FILE%" | ||
| ) | ||
|
|
||
| @REM Create a temporary file for test list | ||
| SET "TEMP_LIST=%TEMP%\rust_test_list_%RANDOM%.txt" | ||
|
|
||
| @REM Enumerate all tests using libtest's --list flag | ||
| "%TEST_BINARY%" --list --format terse 2>NUL | FINDSTR /R ": test$" > "%TEMP_LIST%" | ||
|
|
||
| @REM Check if any tests were found | ||
| FOR %%A IN ("%TEMP_LIST%") DO IF %%~zA==0 ( | ||
| DEL "%TEMP_LIST%" 2>NUL | ||
| EXIT /B 0 | ||
| ) | ||
|
|
||
| @REM Filter tests for this shard and build argument list | ||
| SET "INDEX=0" | ||
| SET "SHARD_TESTS=" | ||
|
|
||
| FOR /F "usebackq delims=" %%T IN ("%TEMP_LIST%") DO ( | ||
| SET "TEST_LINE=%%T" | ||
| @REM Strip ": test" suffix | ||
| SET "TEST_NAME=!TEST_LINE:: test=!" | ||
|
|
||
| @REM Calculate index % TEST_TOTAL_SHARDS | ||
| SET /A "MOD=INDEX %% TEST_TOTAL_SHARDS" | ||
|
|
||
| IF !MOD! EQU %TEST_SHARD_INDEX% ( | ||
| SET "SHARD_TESTS=!SHARD_TESTS! "!TEST_NAME!"" | ||
| ) | ||
|
|
||
| SET /A "INDEX+=1" | ||
| ) | ||
|
|
||
| DEL "%TEMP_LIST%" 2>NUL | ||
|
|
||
| @REM If no tests for this shard, exit successfully | ||
| IF "%SHARD_TESTS%"=="" EXIT /B 0 | ||
|
|
||
| @REM Run the filtered tests with --exact to match exact test names | ||
| "%TEST_BINARY%" %SHARD_TESTS% --exact %* | ||
| EXIT /B %ERRORLEVEL% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #!/usr/bin/env bash | ||
| # Copyright 2024 The Bazel Authors. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # Wrapper script for rust_test that enables Bazel test sharding support. | ||
| # This script intercepts test execution, enumerates tests using libtest's | ||
| # --list flag, partitions them by shard index, and runs only the relevant subset. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| TEST_BINARY="{{TEST_BINARY}}" | ||
|
|
||
| # If sharding is not enabled, run test binary directly | ||
| if [[ -z "${TEST_TOTAL_SHARDS:-}" ]]; then | ||
| exec "./${TEST_BINARY}" "$@" | ||
| fi | ||
|
|
||
| # Touch status file to advertise sharding support to Bazel | ||
| if [[ -n "${TEST_SHARD_STATUS_FILE:-}" ]]; then | ||
| touch "${TEST_SHARD_STATUS_FILE}" | ||
| fi | ||
|
|
||
| # Enumerate all tests using libtest's --list flag | ||
| # Output format: "test_name: test" - we need to strip the ": test" suffix | ||
| test_list=$("./${TEST_BINARY}" --list --format terse 2>/dev/null | grep ': test$' | sed 's/: test$//' || true) | ||
|
|
||
| # If no tests found, exit successfully | ||
| if [[ -z "$test_list" ]]; then | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Filter tests for this shard | ||
| # test_index % TEST_TOTAL_SHARDS == TEST_SHARD_INDEX | ||
| shard_tests=() | ||
| index=0 | ||
| while IFS= read -r test_name; do | ||
| if (( index % TEST_TOTAL_SHARDS == TEST_SHARD_INDEX )); then | ||
| shard_tests+=("$test_name") | ||
| fi | ||
| ((index++)) || true | ||
| done <<< "$test_list" | ||
|
|
||
| # If no tests for this shard, exit successfully | ||
| if [[ ${#shard_tests[@]} -eq 0 ]]; then | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Run the filtered tests with --exact to match exact test names | ||
| exec "./${TEST_BINARY}" "${shard_tests[@]}" --exact "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| load(":test_sharding.bzl", "test_sharding_test_suite") | ||
|
|
||
| ############################ UNIT TESTS ############################# | ||
| test_sharding_test_suite(name = "test_sharding_test_suite") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright 2024 The Bazel Authors. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Test file with multiple tests for verifying sharding support. | ||
| //! | ||
| //! These tests are intentionally trivial - their purpose is to provide multiple | ||
| //! enumerable test functions that can be partitioned across shards. The BUILD | ||
| //! file runs this with `shard_count = 3`, so these 6 tests should be split | ||
| //! ~2 per shard. The sharding wrapper script enumerates tests via `--list`, | ||
| //! assigns each to a shard based on `index % shard_count`, and runs only the | ||
| //! subset for the current shard. | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| #[test] | ||
| fn test_1() {} | ||
|
|
||
| #[test] | ||
| fn test_2() {} | ||
|
|
||
| #[test] | ||
| fn test_3() {} | ||
|
|
||
| #[test] | ||
| fn test_4() {} | ||
|
|
||
| #[test] | ||
| fn test_5() {} | ||
|
|
||
| #[test] | ||
| fn test_6() {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """Tests for rust_test sharding support.""" | ||
|
|
||
| load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") | ||
| load("//rust:defs.bzl", "rust_test") | ||
|
|
||
| def _sharding_enabled_test(ctx): | ||
| """Test that sharding wrapper is generated when experimental_enable_sharding is True.""" | ||
| env = analysistest.begin(ctx) | ||
| tut = analysistest.target_under_test(env) | ||
|
|
||
| # Get the executable from DefaultInfo | ||
| default_info = tut[DefaultInfo] | ||
| executable = default_info.files_to_run.executable | ||
|
|
||
| # When sharding is enabled, the executable should be a wrapper script | ||
| asserts.true( | ||
| env, | ||
| executable.basename.endswith("_sharding_wrapper.sh") or | ||
| executable.basename.endswith("_sharding_wrapper.bat"), | ||
| "Expected sharding wrapper script, got: " + executable.basename, | ||
| ) | ||
|
|
||
| return analysistest.end(env) | ||
|
|
||
| sharding_enabled_test = analysistest.make(_sharding_enabled_test) | ||
|
|
||
| def _sharding_disabled_test(ctx): | ||
| """Test that no wrapper is generated when experimental_enable_sharding is False.""" | ||
| env = analysistest.begin(ctx) | ||
| tut = analysistest.target_under_test(env) | ||
|
|
||
| # Get the executable from DefaultInfo | ||
| default_info = tut[DefaultInfo] | ||
| executable = default_info.files_to_run.executable | ||
|
|
||
| # When sharding is disabled, the executable should be the test binary directly | ||
| asserts.false( | ||
| env, | ||
| executable.basename.endswith("_sharding_wrapper.sh") or | ||
| executable.basename.endswith("_sharding_wrapper.bat"), | ||
| "Expected test binary, not wrapper script: " + executable.basename, | ||
| ) | ||
|
|
||
| return analysistest.end(env) | ||
|
|
||
| sharding_disabled_test = analysistest.make(_sharding_disabled_test) | ||
|
|
||
| def _test_sharding_targets(): | ||
| """Create test targets for sharding tests.""" | ||
|
|
||
| # Test with sharding enabled | ||
| rust_test( | ||
| name = "sharded_test_enabled", | ||
| srcs = ["sharded_test.rs"], | ||
| edition = "2021", | ||
| experimental_enable_sharding = True, | ||
| ) | ||
|
|
||
| sharding_enabled_test( | ||
| name = "sharding_enabled_test", | ||
| target_under_test = ":sharded_test_enabled", | ||
| ) | ||
|
|
||
| # Test with sharding disabled (default) | ||
| rust_test( | ||
| name = "sharded_test_disabled", | ||
| srcs = ["sharded_test.rs"], | ||
| edition = "2021", | ||
| experimental_enable_sharding = False, | ||
| ) | ||
|
|
||
| sharding_disabled_test( | ||
| name = "sharding_disabled_test", | ||
| target_under_test = ":sharded_test_disabled", | ||
| ) | ||
|
|
||
| # Integration test: actually run a sharded test | ||
| rust_test( | ||
| name = "sharded_integration_test", | ||
| srcs = ["sharded_test.rs"], | ||
| edition = "2021", | ||
| experimental_enable_sharding = True, | ||
| shard_count = 3, | ||
| ) | ||
|
|
||
| def test_sharding_test_suite(name): | ||
| """Entry-point macro called from the BUILD file. | ||
|
|
||
| Args: | ||
| name: Name of the macro. | ||
| """ | ||
|
|
||
| _test_sharding_targets() | ||
|
|
||
| native.test_suite( | ||
| name = name, | ||
| tests = [ | ||
| ":sharding_enabled_test", | ||
| ":sharding_disabled_test", | ||
| ":sharded_integration_test", | ||
| ], | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do something like edit
rustc_compile_actionto return a dict of provider types to providers, and then do a key lookup, rather than sniffing an ordered list.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
much better - done.