From 749242a21a2d026d61dfd3883fb6e34b56785661 Mon Sep 17 00:00:00 2001 From: JW Wang Date: Thu, 9 Jul 2020 09:49:35 +0800 Subject: [PATCH] Rewrite #restartSystemServer ProcessInfo#getStartTime is not reliable. My local test shows it is incremented by one after polling a few times even if we don't restart system server at all. Let's use pid to detect whether system server is restarted or not. Now #restartSystemServer fails correctly when `am restart` is not called at all or is called without root privilege. Bug: 160754072 Test: StagedInstallInternalTest Change-Id: Iebcf1583f38c17fbc8f2af42bdc61ae000b39354 --- .../host/StagedInstallInternalTest.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/StagedInstallTest/src/com/android/tests/stagedinstallinternal/host/StagedInstallInternalTest.java b/tests/StagedInstallTest/src/com/android/tests/stagedinstallinternal/host/StagedInstallInternalTest.java index 530f885e62bf2..e6ba8015e5b51 100644 --- a/tests/StagedInstallTest/src/com/android/tests/stagedinstallinternal/host/StagedInstallInternalTest.java +++ b/tests/StagedInstallTest/src/com/android/tests/stagedinstallinternal/host/StagedInstallInternalTest.java @@ -19,6 +19,7 @@ package com.android.tests.stagedinstallinternal.host; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import com.android.ddmlib.Log; import com.android.tests.rollback.host.AbandonSessionsRule; @@ -83,7 +84,7 @@ public class StagedInstallInternalTest extends BaseHostJUnit4Test { private void restartSystemServer() throws Exception { // Restart the system server - long oldStartTime = getDevice().getProcessByName("system_server").getStartTime(); + ProcessInfo oldPs = getDevice().getProcessByName("system_server"); getDevice().enableAdbRoot(); // Need root to restart system server assertThat(getDevice().executeShellCommand("am restart")).contains("Restart the system"); @@ -91,18 +92,16 @@ public class StagedInstallInternalTest extends BaseHostJUnit4Test { // Wait for new system server process to start long start = System.currentTimeMillis(); - long newStartTime = oldStartTime; while (System.currentTimeMillis() < start + SYSTEM_SERVER_TIMEOUT_MS) { ProcessInfo newPs = getDevice().getProcessByName("system_server"); if (newPs != null) { - newStartTime = newPs.getStartTime(); - if (newStartTime != oldStartTime) { - break; + if (newPs.getPid() != oldPs.getPid()) { + getDevice().waitForDeviceAvailable(); + return; } } Thread.sleep(500); } - assertThat(newStartTime).isNotEqualTo(oldStartTime); - getDevice().waitForDeviceAvailable(); + fail("Timed out in restarting system server"); } }