Ensure @Before and @Test run on the same thread in AndroidJUnit4ClassRunner.#2479
Open
copybara-service[bot] wants to merge 1 commit intomainfrom
Open
Ensure @Before and @Test run on the same thread in AndroidJUnit4ClassRunner.#2479copybara-service[bot] wants to merge 1 commit intomainfrom
copybara-service[bot] wants to merge 1 commit intomainfrom
Conversation
d8021b5 to
eb77115
Compare
…Runner. Currently, AndroidJUnit4ClassRunner inherits standard JUnit 4 behavior where timeouts are applied only to the test method itself. If a test timeout is set (e.g., passed by TradeFed or runner arguments), FailOnTimeout executes the test method in a separate thread. Since setup (@before) and teardown (@after) are applied outside this timeout, they run on the caller thread, causing a thread mismatch. This prevents ThreadLocal variables initialized in setup from being accessible in the test. This CL modifies AndroidJUnit4ClassRunner to apply the timeout to the combined statement of setup, test, and teardown. This is achieved by: 1. Overriding withAfters to wrap the combined statement in FailOnTimeout. 2. Overriding withPotentialTimeout to be a no-op, preventing double timeout application. This ensures all lifecycle methods run on the same thread spawned by the timeout. NOTE: Because the timeout now wraps setup and teardown, any time spent in @before or @after will now count towards the test timeout. PiperOrigin-RevId: 895547043
eb77115 to
70e3c22
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Ensure @before and @test run on the same thread in AndroidJUnit4ClassRunner.
Currently, AndroidJUnit4ClassRunner inherits standard JUnit 4 behavior where
timeouts are applied only to the test method itself. If a test timeout is
set (e.g., passed by TradeFed or runner arguments), FailOnTimeout executes
the test method in a separate thread.
Since setup (@before) and teardown (@after) are applied outside this
timeout, they run on the caller thread, causing a thread mismatch. This
prevents ThreadLocal variables initialized in setup from being accessible in
the test.
This CL modifies AndroidJUnit4ClassRunner to apply the timeout to the
combined statement of setup, test, and teardown. This is achieved by:
timeout application.
This ensures all lifecycle methods run on the same thread spawned by the
timeout.
NOTE: Because the timeout now wraps setup and teardown, any time spent in
@before or @after will now count towards the test timeout.