robolectric / robolectric

Android Unit Testing Framework
http://robolectric.org
Other
5.81k stars 1.36k forks source link

Create TimeoutRule which can be used with Robolectric in place of org.junit.rules.Timeout #9037

Closed fknives closed 4 weeks ago

fknives commented 1 month ago

Overview

Close #3829

org.junit.rules.Timeout JUnit rule is not compatible with Robolectric because it starts a separate execution Thread. Robolectric's @Test(timeout = ) uses TimeLimitedStatement which evaluates the statement on the same thread & moves the timeout & interruption into a separate thread.

Proposed Changes

Introduces a Timeout Test rule which uses the same TimeLimitedStatement to be used in place of org.junit.rules.Timeout

utzcoz commented 1 month ago

@fknives Thanks for your contribution. Please check GitHub Action for commit message validation job and make your PR pass the CI.

utzcoz commented 1 month ago

Run # Check that the commit has a body
  # Check that the commit has a body
  commit_body="$(git log -1 --pretty=format:'%b' | grep -v 'PiperOrigin-RevId')"
  if [ -z "$commit_body" ]; then
    echo "Error: the commit should have a descriptive body"
    exit 1
  fi

  while read -r line; do
    if [ "${#line}" -gt 120 ] && [[ ! "$line" =~ (https?://|www\.) ]]; then
      echo "Error: the following line of the commit body is too long (max: 120 characters):"
      echo "> $line"
      exit 1
    fi
  done <<< $commit_body
  shell: /usr/bin/bash -e {0}
Error: Process completed with exit code 1.
fknives commented 1 month ago

@utzcoz I didn't realize, I've added a body to the commit message, sorry about that

utzcoz commented 1 month ago

@fknives Please check Robolectric code style CI job, and ensure your code are formatted locally.

fknives commented 1 month ago

@utzcoz Adjusted the docs and applied google-java-format. Hopefully everything is right now 🤞 , thank you for your patience

utzcoz commented 1 month ago
Running google-java-format-diff against 'origin/master'
From https://github.com/robolectric/robolectric
 * branch                master     -> FETCH_HEAD
Please run google-java-format on the changes in this pull request
--- junit/src/main/java/org/robolectric/internal/TimeLimitedStatement.java  (before formatting)
+++ junit/src/main/java/org/robolectric/internal/TimeLimitedStatement.java  (after formatting)
@@ -5,8 +5,8 @@
 import org.junit.runners.model.TestTimedOutException;

 /**
- * Similar to JUnit's {@link org.junit.internal.runners.statements.FailOnTimeout}, but runs the
- * test on the current thread (with a timer on a new thread) rather than the other way around.
+ * Similar to JUnit's {@link org.junit.internal.runners.statements.FailOnTimeout}, but runs the test
+ * on the current thread (with a timer on a new thread) rather than the other way around.
  */
 public class TimeLimitedStatement extends Statement {

There still exists some format issues. You can check https://github.com/robolectric/robolectric/wiki/Robolectric's-code-style to fix it.

fknives commented 1 month ago

@utzcoz Right, the TimeLimitedStatement wasn't formatter properly either, ohh my

utzcoz commented 4 weeks ago

@fknives Please rebase your PR based on latest master branch as I merged some commits to make CI green.

utzcoz commented 4 weeks ago

@fknives Thanks for your contribution, and congrats to your first contribution for Robolectric. I think it will be released with Robolectric 4.13, and you can use Robolectric 4.13-snapshot now to test it.