From 572f1a318596a37e632b4352052d053e7710d2f3 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Wed, 25 Jan 2017 23:09:07 +0900 Subject: [PATCH] Attempt to make waitForIdleHandler reliable. The current implementation of IdleableHandlerThread is based on the assumption that MessageQueue#isIdle will return true iff the message loop has finished processing its messages. Unfortunately, this is incorrect: isIdle returns true iff are no more messages waiting in the queue; thus, it will also return true while it is processing the last message before going idle. Instead of using idle handlers, take the simpler approach of posting a runnable to the message loop and waiting for it to be processed. Test: see other CLs in this patch series Bug: 31479480 Change-Id: Iae75781f067b762c8653a488a5e4d5ee0c789e01 --- .../server/ConnectivityServiceTest.java | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/tests/net/java/com/android/server/ConnectivityServiceTest.java b/tests/net/java/com/android/server/ConnectivityServiceTest.java index 2d7a68fe88..db7552d070 100644 --- a/tests/net/java/com/android/server/ConnectivityServiceTest.java +++ b/tests/net/java/com/android/server/ConnectivityServiceTest.java @@ -155,25 +155,13 @@ public class ConnectivityServiceTest extends AndroidTestCase { /** * Block until the given handler becomes idle, or until timeoutMs has passed. */ - private static void waitForIdleHandler(HandlerThread handler, int timeoutMs) { + private static void waitForIdleHandler(HandlerThread handlerThread, int timeoutMs) { final ConditionVariable cv = new ConditionVariable(); - final MessageQueue queue = handler.getLooper().getQueue(); - final IdleHandler idleHandler = () -> { - synchronized (queue) { - cv.open(); - return false; // Remove the idleHandler. - } - }; - synchronized (queue) { - if (queue.isIdle()) { - return; - } - queue.addIdleHandler(idleHandler); - } + final Handler handler = new Handler(handlerThread.getLooper()); + handler.post(() -> cv.open()); if (!cv.block(timeoutMs)) { - fail("HandlerThread " + handler.getName() + + fail("HandlerThread " + handlerThread.getName() + " did not become idle after " + timeoutMs + " ms"); - queue.removeIdleHandler(idleHandler); } }