diff options
author | Devendra Singhi <devendra.singhi@ittiam.com> | 2023-06-15 14:41:09 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-06-15 14:41:09 +0000 |
commit | 87b5a07da82b9030fcf044b518434200bd5151f7 (patch) | |
tree | 3a67f19f0fea5065a5e74040452f4d482572237d | |
parent | 45fa358bbef581477498c8e2c1f9e5d4f5e2564f (diff) | |
parent | 68ca639269b3c72c473d33cf1c1020484262a9c8 (diff) | |
download | platform_testing-87b5a07da82b9030fcf044b518434200bd5151f7.tar.gz |
Added a common polling method for device-side tests am: 4d186de97e am: 031e97a436 am: f4bb50b0cf am: 9072f1657c am: 68ca639269
Original change: https://googleplex-android-review.googlesource.com/c/platform/platform_testing/+/23163618
Change-Id: I8de4d597d52e908ffb885a33d3aadefa9b4d4159
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | libraries/sts-common-util/device-side/src/com/android/sts/common/SystemUtil.java | 86 |
1 files changed, 79 insertions, 7 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 b8e458fbf..65e5c8fcb 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,36 +18,49 @@ 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.SettingsUtils; -import java.io.IOException; 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) { String getSettingRes = SettingsUtils.get(namespace, key); final Optional<String> oldSetting = Optional.ofNullable(getSettingRes); SettingsUtils.set(namespace, key, value); String getSettingCurrent = SettingsUtils.get(namespace, 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 @@ -61,10 +74,69 @@ public class SystemUtil { String.format("could not reset '%s' back to '%s'", key, oldValue); String getSettingCurrent = SettingsUtils.get(namespace, key); Optional<String> currSetting = Optional.ofNullable(getSettingCurrent); - assumeThat(failMsg, currSetting.isPresent() ? currSetting.get().trim() : null, + assumeThat( + failMsg, + currSetting.isPresent() ? currSetting.get().trim() : null, equalTo(oldValue)); } } }; } + + /** + * 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(); + } } |