diff options
author | JW Wang <wangchun@google.com> | 2019-09-02 03:28:31 +0000 |
---|---|---|
committer | JW Wang <wangchun@google.com> | 2019-09-04 10:01:30 +0800 |
commit | 2d085672e31705dc332620f2303014d288396fcc (patch) | |
tree | d3b800780413fc7590253e4e61d755f018e7b4d2 | |
parent | 7baee5c357e89c2edbe3ea302f4f40bfe330fa8e (diff) |
Add a wrapper around SystemClock.uptimeMillis().
This bug is motivated by bug 140208026 where we want to replace
Thread.sleep() with TestLooper.moveTimeForward() in PackageWatchdogTest.java.
However, it turns out that PackageWatchdog uses SystemClock.uptimeMillis()
internally. The tests will fail if we don't forward PackageWatchdog's internal
clock accordingly.
We add a wrapper around SystemClock.uptimeMillis() so it is customizable
by the test case.
Bug: 140358475
Test: atest PackageWatchdogTest
Change-Id: Id26325a93dc4050c6468502347b0e7852ed1263f
-rw-r--r-- | services/core/java/com/android/server/PackageWatchdog.java | 21 | ||||
-rw-r--r-- | tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java | 2 |
2 files changed, 16 insertions, 7 deletions
diff --git a/services/core/java/com/android/server/PackageWatchdog.java b/services/core/java/com/android/server/PackageWatchdog.java index 73ec5614b2a3..2ab8e03ab2d3 100644 --- a/services/core/java/com/android/server/PackageWatchdog.java +++ b/services/core/java/com/android/server/PackageWatchdog.java @@ -29,7 +29,6 @@ import android.net.ConnectivityModuleConnector; import android.os.Environment; import android.os.Handler; import android.os.Looper; -import android.os.SystemClock; import android.provider.DeviceConfig; import android.text.TextUtils; import android.util.ArrayMap; @@ -135,6 +134,14 @@ public class PackageWatchdog { private final Runnable mSyncStateWithScheduledReason = this::syncStateWithScheduledReason; private final Runnable mSaveToFile = this::saveToFile; + @FunctionalInterface + @VisibleForTesting + interface SystemClock { + long uptimeMillis(); + } + + private final SystemClock mSystemClock; + private PackageWatchdog(Context context) { // Needs to be constructed inline this(context, new AtomicFile( @@ -142,7 +149,8 @@ public class PackageWatchdog { "package-watchdog.xml")), new Handler(Looper.myLooper()), BackgroundThread.getHandler(), new ExplicitHealthCheckController(context), - ConnectivityModuleConnector.getInstance()); + ConnectivityModuleConnector.getInstance(), + android.os.SystemClock::uptimeMillis); } /** @@ -151,13 +159,14 @@ public class PackageWatchdog { @VisibleForTesting PackageWatchdog(Context context, AtomicFile policyFile, Handler shortTaskHandler, Handler longTaskHandler, ExplicitHealthCheckController controller, - ConnectivityModuleConnector connectivityModuleConnector) { + ConnectivityModuleConnector connectivityModuleConnector, SystemClock clock) { mContext = context; mPolicyFile = policyFile; mShortTaskHandler = shortTaskHandler; mLongTaskHandler = longTaskHandler; mHealthCheckController = controller; mConnectivityModuleConnector = connectivityModuleConnector; + mSystemClock = clock; loadFromFile(); } @@ -579,7 +588,7 @@ public class PackageWatchdog { mUptimeAtLastStateSync = 0; } else { Slog.i(TAG, "Scheduling next state sync in " + durationMs + "ms"); - mUptimeAtLastStateSync = SystemClock.uptimeMillis(); + mUptimeAtLastStateSync = mSystemClock.uptimeMillis(); mShortTaskHandler.postDelayed(mSyncStateWithScheduledReason, durationMs); } } @@ -612,7 +621,7 @@ public class PackageWatchdog { @GuardedBy("mLock") private void pruneObserversLocked() { long elapsedMs = mUptimeAtLastStateSync == 0 - ? 0 : SystemClock.uptimeMillis() - mUptimeAtLastStateSync; + ? 0 : mSystemClock.uptimeMillis() - mUptimeAtLastStateSync; if (elapsedMs <= 0) { Slog.i(TAG, "Not pruning observers, elapsed time: " + elapsedMs + "ms"); return; @@ -1036,7 +1045,7 @@ public class PackageWatchdog { */ @GuardedBy("mLock") public boolean onFailureLocked() { - final long now = SystemClock.uptimeMillis(); + final long now = mSystemClock.uptimeMillis(); final long duration = now - mUptimeStartMs; if (duration > mTriggerFailureDurationMs) { // TODO(b/120598832): Reseting to 1 is not correct diff --git a/tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java b/tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java index c42201fa0d3e..47fefaeabc18 100644 --- a/tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java +++ b/tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java @@ -782,7 +782,7 @@ public class PackageWatchdogTest { Handler handler = new Handler(mTestLooper.getLooper()); PackageWatchdog watchdog = new PackageWatchdog(mSpyContext, policyFile, handler, handler, controller, - mConnectivityModuleConnector); + mConnectivityModuleConnector, android.os.SystemClock::uptimeMillis); // Verify controller is not automatically started assertFalse(controller.mIsEnabled); if (withPackagesReady) { |