summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Shai Barack <shayba@google.com> 2025-01-28 14:31:50 -0800
committer Shai Barack <shayba@google.com> 2025-01-28 14:31:50 -0800
commit94fc9dda370cb3fb65cc45580b405ea484f9b220 (patch)
treeaa6812d0ae09ce17473bf29038a82b2283955632
parent0af50eb34063f95c8d820da0739aab1b03644935 (diff)
TestLooperManager methods that read/mutate the queue should ensure that LooperHolder was already popped
Bug: 385010271 Change-Id: I632ac5cdf77248550a394a1d79a4f0f72a2053d8 Flag: android.os.message_queue_testability
-rw-r--r--core/java/android/os/TestLooperManager.java31
1 files changed, 21 insertions, 10 deletions
diff --git a/core/java/android/os/TestLooperManager.java b/core/java/android/os/TestLooperManager.java
index d451109554fa..ddfa3799706e 100644
--- a/core/java/android/os/TestLooperManager.java
+++ b/core/java/android/os/TestLooperManager.java
@@ -84,17 +84,8 @@ public class TestLooperManager {
* interactions with it have completed.
*/
public Message next() {
- // Wait for the looper block to come up, to make sure we don't accidentally get
- // the message for the block.
- while (!mLooperIsMyLooper && !mLooperBlocked) {
- synchronized (this) {
- try {
- wait();
- } catch (InterruptedException e) {
- }
- }
- }
checkReleased();
+ waitForLooperHolder();
return mQueue.next();
}
@@ -110,6 +101,7 @@ public class TestLooperManager {
@Nullable
public Message poll() {
checkReleased();
+ waitForLooperHolder();
return mQueue.pollForTest();
}
@@ -124,6 +116,7 @@ public class TestLooperManager {
@Nullable
public Long peekWhen() {
checkReleased();
+ waitForLooperHolder();
return mQueue.peekWhenForTest();
}
@@ -133,6 +126,7 @@ public class TestLooperManager {
@FlaggedApi(Flags.FLAG_MESSAGE_QUEUE_TESTABILITY)
public boolean isBlockedOnSyncBarrier() {
checkReleased();
+ waitForLooperHolder();
return mQueue.isBlockedOnSyncBarrier();
}
@@ -221,6 +215,23 @@ public class TestLooperManager {
}
}
+ /**
+ * Waits until the Looper is blocked by the LooperHolder, if one was posted.
+ *
+ * After this method returns, it's guaranteed that the LooperHolder Message
+ * is not in the underlying queue.
+ */
+ private void waitForLooperHolder() {
+ while (!mLooperIsMyLooper && !mLooperBlocked) {
+ synchronized (this) {
+ try {
+ wait();
+ } catch (InterruptedException e) {
+ }
+ }
+ }
+ }
+
private class LooperHolder implements Runnable {
@Override
public void run() {