diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2023-06-15 04:26:12 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2023-06-15 04:26:12 +0000 |
commit | 826da4696051d7e56167f28cb15a26d9c820fafa (patch) | |
tree | 5283fdc7f9a5ab9bef1ee1646329c7e4636c17b0 | |
parent | 352163b4d00485c720cf16dfea52bda2c7cb048d (diff) | |
parent | 1c0fc4f662d7dbcb545c38a10e724643a54faf5d (diff) | |
download | platform_testing-826da4696051d7e56167f28cb15a26d9c820fafa.tar.gz |
Merge "Added a common polling method for device-side tests" into udc-dev
-rw-r--r-- | libraries/sts-common-util/device-side/src/com/android/sts/common/SystemUtil.java | 81 |
1 files changed, 76 insertions, 5 deletions
diff --git a/libraries/sts-common-util/device-side/src/com/android/sts/common/SystemUtil.java b/libraries/sts-common-util/device-side/src/com/android/sts/common/SystemUtil.java index c6504e067..a218f5890 100644 --- a/libraries/sts-common-util/device-side/src/com/android/sts/common/SystemUtil.java +++ b/libraries/sts-common-util/device-side/src/com/android/sts/common/SystemUtil.java @@ -18,27 +18,39 @@ package com.android.sts.common; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assume.assumeThat; +import static org.junit.Assume.assumeTrue; import android.app.Instrumentation; +import android.os.Handler; +import android.os.HandlerThread; import com.android.compatibility.common.util.UserSettings; import java.util.Optional; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.function.BooleanSupplier; public class SystemUtil { + private static final String TAG = "SystemUtil"; + public static final long DEFAULT_MAX_POLL_TIME_MS = 30_000L; + public static final long DEFAULT_POLL_TIME_MS = 100L; /** * Set the value of a device setting and set it back to old value upon closing. * * @param instrumentation {@link Instrumentation} instance, obtained from a test running in - * instrumentation framework + * instrumentation framework * @param namespace "system", "secure", or "global" * @param key setting key to set * @param value setting value to set to * @return AutoCloseable that resets the setting back to existing value upon closing. */ - public static AutoCloseable withSetting(Instrumentation instrumentation, final String namespace, - final String key, String value) { + public static AutoCloseable withSetting( + Instrumentation instrumentation, + final String namespace, + final String key, + String value) { UserSettings userSettings = new UserSettings(UserSettings.Namespace.of(namespace)); String getSettingRes = userSettings.get(key); final Optional<String> oldSetting = Optional.ofNullable(getSettingRes); @@ -46,8 +58,10 @@ public class SystemUtil { String getSettingCurrent = userSettings.get(key); Optional<String> currSetting = Optional.ofNullable(getSettingCurrent); - assumeThat(String.format("Could not set %s:%s to %s", namespace, key, value), - currSetting.isPresent() ? currSetting.get().trim() : null, equalTo(value)); + assumeThat( + String.format("Could not set %s:%s to %s", namespace, key, value), + currSetting.isPresent() ? currSetting.get().trim() : null, + equalTo(value)); return new AutoCloseable() { @Override @@ -69,4 +83,61 @@ public class SystemUtil { } }; } + + /** + * Poll on a condition supplied by the user. + * + * @param waitCondition returns true when the polling condition is met, false otherwise. + * @return boolean value of {@code waitCondition}. + * @throws IllegalArgumentException when {@code pollingTime} is not a positive ineteger and is + * not less than {@code maxPollingTime}. + * @throws InterruptedException if the current thread is interrupted. + */ + public static boolean poll(BooleanSupplier waitCondition) + throws IllegalArgumentException, InterruptedException { + return poll(waitCondition, DEFAULT_POLL_TIME_MS, DEFAULT_MAX_POLL_TIME_MS); + } + + /** + * Poll on a condition supplied by the user. + * + * @param waitCondition returns true when the polling condition is met, false otherwise. + * @param pollingTime wait between successive calls to fetch value of {@code waitCondition} in + * milliseconds + * @param maxPollingTime maximum waiting time before return. + * @return boolean value of {@code waitCondition}. + * @throws IllegalArgumentException when {@code pollingTime} is not a positive ineteger and is + * not less than {@code maxPollingTime}. + * @throws InterruptedException if the current thread is interrupted. + */ + public static boolean poll(BooleanSupplier waitCondition, long pollingTime, long maxPollingTime) + throws IllegalArgumentException, InterruptedException { + // The value of pollingTime should be a positive integer + if (pollingTime <= 0) { + throw new IllegalArgumentException("pollingTime should be a positive integer"); + } + + // The value of pollingTime should be less than maxPollingTime + if (pollingTime >= maxPollingTime) { + throw new IllegalArgumentException("pollingTime should be less than maxPollingTime"); + } + + // Use handlerThread to run task in a separate thread. + final HandlerThread handlerThread = new HandlerThread(TAG); + handlerThread.start(); + Handler handler = new Handler(handlerThread.getLooper()); + final Semaphore semaphore = new Semaphore(0); + final long startTime = System.currentTimeMillis(); + do { + // Check for the status. + if (waitCondition.getAsBoolean()) { + return true; + } + + // Wait before checking status again. + handler.postDelayed(() -> semaphore.release(), pollingTime); + assumeTrue(semaphore.tryAcquire(maxPollingTime, TimeUnit.MILLISECONDS)); + } while (System.currentTimeMillis() - startTime <= maxPollingTime); + return waitCondition.getAsBoolean(); + } } |