diff options
| author | 2024-09-13 10:31:09 +0000 | |
|---|---|---|
| committer | 2024-09-13 10:34:35 +0000 | |
| commit | 3f04af20f6f7034d5a329f23e018a7a89feb50fc (patch) | |
| tree | 454fe8817583ad7a4d753c5279c8d225b819455a | |
| parent | ca420c5491490ab44bc1d11be6ba9bd6b50799f8 (diff) | |
Extend IpcDataCache constructors to take lambda for bypass.
This change also assure existing lambda for cache query is not held as variable.
Bug: 364947162
Change-Id: I83cd06e9d3f504a2a90228337f6430ad897f4e76
Test: atest FrameworksCoreTests:IpcDataCacheTest
Flag: android.multiuser.caching_development_improvements
| -rw-r--r-- | core/java/android/os/IpcDataCache.java | 58 | ||||
| -rw-r--r-- | core/tests/coretests/src/android/os/IpcDataCacheTest.java | 42 |
2 files changed, 95 insertions, 5 deletions
diff --git a/core/java/android/os/IpcDataCache.java b/core/java/android/os/IpcDataCache.java index bf44d65c4002..0776cf405cfe 100644 --- a/core/java/android/os/IpcDataCache.java +++ b/core/java/android/os/IpcDataCache.java @@ -16,6 +16,7 @@ package android.os; +import android.annotation.FlaggedApi; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.StringDef; @@ -551,7 +552,7 @@ public class IpcDataCache<Query, Result> extends PropertyInvalidatedCache<Query, } /** - * An interface suitable for a lambda expression instead of a QueryHandler. + * An interface suitable for a lambda expression instead of a QueryHandler applying remote call. * @hide */ public interface RemoteCall<Query, Result> { @@ -559,6 +560,14 @@ public class IpcDataCache<Query, Result> extends PropertyInvalidatedCache<Query, } /** + * An interface suitable for a lambda expression instead of a QueryHandler bypassing the cache. + * @hide + */ + public interface BypassCall<Query> { + Boolean apply(Query query); + } + + /** * This is a query handler that is created with a lambda expression that is invoked * every time the handler is called. The handler is specifically meant for services * hosted by system_server; the handler automatically rethrows RemoteException as a @@ -580,11 +589,54 @@ public class IpcDataCache<Query, Result> extends PropertyInvalidatedCache<Query, } } + /** * Create a cache using a config and a lambda expression. + * @param config The configuration for the cache. + * @param remoteCall The lambda expression that will be invoked to fetch the data. * @hide */ - public IpcDataCache(@NonNull Config config, @NonNull RemoteCall<Query, Result> computer) { - this(config, new SystemServerCallHandler<>(computer)); + public IpcDataCache(@NonNull Config config, @NonNull RemoteCall<Query, Result> remoteCall) { + this(config, android.multiuser.Flags.cachingDevelopmentImprovements() ? + new QueryHandler<Query, Result>() { + @Override + public Result apply(Query query) { + try { + return remoteCall.apply(query); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + } : new SystemServerCallHandler<>(remoteCall)); + } + + + /** + * Create a cache using a config and a lambda expression. + * @param config The configuration for the cache. + * @param remoteCall The lambda expression that will be invoked to fetch the data. + * @param bypass The lambda expression that will be invoked to determine if the cache should be + * bypassed. + * @hide + */ + @FlaggedApi(android.multiuser.Flags.FLAG_CACHING_DEVELOPMENT_IMPROVEMENTS) + public IpcDataCache(@NonNull Config config, + @NonNull RemoteCall<Query, Result> remoteCall, + @NonNull BypassCall<Query> bypass) { + this(config, new QueryHandler<Query, Result>() { + @Override + public Result apply(Query query) { + try { + return remoteCall.apply(query); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + @Override + public boolean shouldBypassCache(Query query) { + return bypass.apply(query); + } + }); } } diff --git a/core/tests/coretests/src/android/os/IpcDataCacheTest.java b/core/tests/coretests/src/android/os/IpcDataCacheTest.java index b03fd6485786..64f77b309829 100644 --- a/core/tests/coretests/src/android/os/IpcDataCacheTest.java +++ b/core/tests/coretests/src/android/os/IpcDataCacheTest.java @@ -18,7 +18,9 @@ package android.os; import static org.junit.Assert.assertEquals; +import android.multiuser.Flags; import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.annotations.RequiresFlagsEnabled; import android.platform.test.ravenwood.RavenwoodRule; import androidx.test.filters.SmallTest; @@ -151,8 +153,6 @@ public class IpcDataCacheTest { tester.verify(9); } - // This test is disabled pending an sepolicy change that allows any app to set the - // test property. @Test public void testRemoteCall() { @@ -193,6 +193,44 @@ public class IpcDataCacheTest { } @Test + @RequiresFlagsEnabled(Flags.FLAG_CACHING_DEVELOPMENT_IMPROVEMENTS) + public void testRemoteCallBypass() { + + // A stand-in for the binder. The test verifies that calls are passed through to + // this class properly. + ServerProxy tester = new ServerProxy(); + + // Create a cache that uses simple arithmetic to computer its values. + IpcDataCache.Config config = new IpcDataCache.Config(4, MODULE, API, "testCache3"); + IpcDataCache<Integer, Boolean> testCache = + new IpcDataCache<>(config, (x) -> tester.query(x), (x) -> x % 9 == 0); + + IpcDataCache.setTestMode(true); + testCache.testPropertyName(); + + tester.verify(0); + assertEquals(tester.value(3), testCache.query(3)); + tester.verify(1); + assertEquals(tester.value(3), testCache.query(3)); + tester.verify(2); + testCache.invalidateCache(); + assertEquals(tester.value(3), testCache.query(3)); + tester.verify(3); + assertEquals(tester.value(5), testCache.query(5)); + tester.verify(4); + assertEquals(tester.value(5), testCache.query(5)); + tester.verify(4); + assertEquals(tester.value(3), testCache.query(3)); + tester.verify(4); + assertEquals(tester.value(9), testCache.query(9)); + tester.verify(5); + assertEquals(tester.value(3), testCache.query(3)); + tester.verify(5); + assertEquals(tester.value(5), testCache.query(5)); + tester.verify(5); + } + + @Test public void testDisableCache() { // A stand-in for the binder. The test verifies that calls are passed through to |