Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 749242a2 authored by JW Wang's avatar JW Wang
Browse files

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
parent 3ac333f2
Loading
Loading
Loading
Loading
+6 −7
Original line number Diff line number Diff line
@@ -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");
    }
}