Skip to content

Commit ea443fc

Browse files
Clarify IdlingResource.ResourceCallback usage.
Adds javadoc to IdlingResource.ResourceCallback.onTransitionToIdle() to warn that it should be called before isIdleNow() returns true to avoid race conditions. If the order is inverted a race can cause consistency error to be thrown: ``` java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Resource MyIdlingResource isIdleNow() is returning true, but a message indicating that the resource has transitioned from busy to idle was never sent. at androidx.test.espresso.Espresso.onIdle(Espresso.java:356) at androidx.test.espresso.Espresso.onIdle(Espresso.java:376) ``` If the `IdlingResource` changes its `isIdleNow()` to true on a thread other than the main thread, Espresso may observe that change from the main thread and subsequently complete a consistency check to verify that it did not observe a call to `onTransitionToIdle()` before the other thread has proceeded to call it. PiperOrigin-RevId: 908820178
1 parent 1144878 commit ea443fc

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

espresso/idling_resource/java/androidx/test/espresso/IdlingResource.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ public interface IdlingResource {
6060

6161
/** Registered by an {@link IdlingResource} to notify Espresso of a transition to idle. */
6262
public interface ResourceCallback {
63-
/** Called when the resource goes from busy to idle. */
63+
/**
64+
* Called when the resource goes from busy to idle.
65+
*
66+
* <p>If the idle state is updated on a thread other than the main thread, this method should be
67+
* called <b>before</b> the state is updated to return true. Failing to do so can cause a race
68+
* condition where Espresso throws a consistency error due to observing {@link #isIdleNow()}
69+
* returning true before the callback is invoked.
70+
*/
6471
public void onTransitionToIdle();
6572
}
6673
}

0 commit comments

Comments
 (0)