Skip to content

Commit

Permalink
Defines a build flag for the EntityHandle storage stack code.
Browse files Browse the repository at this point in the history
Serves as an example for how to add a new build flag for a feature.

PiperOrigin-RevId: 344932610
  • Loading branch information
csilvestrini authored and arcs-c3po committed Dec 1, 2020
1 parent 46755db commit 899fd52
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 7 deletions.
1 change: 1 addition & 0 deletions java/arcs/android/storage/service/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ arcs_kt_android_library(
"//java/arcs/core/storage/database",
"//java/arcs/core/storage/driver:driver_providers",
"//java/arcs/core/util",
"//java/arcs/flags",
"//third_party/java/androidx/annotation",
"//third_party/kotlin/kotlinx_atomicfu",
"//third_party/kotlin/kotlinx_coroutines",
Expand Down
8 changes: 8 additions & 0 deletions java/arcs/android/storage/service/MuxedStorageChannelImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import arcs.android.storage.decodeStorageServiceMessageProto
import arcs.android.storage.toProto
import arcs.core.storage.DirectStoreMuxer
import arcs.core.storage.UntypedDirectStoreMuxer
import arcs.flags.BuildFlagDisabledError
import arcs.flags.BuildFlags
import kotlinx.coroutines.CoroutineScope

/** Implementation of [IStorageChannel] for communicating with a [DirectStoreMuxer]. */
Expand All @@ -25,6 +27,12 @@ class MuxedStorageChannelImpl(
scope: CoroutineScope,
private val statisticsSink: BindingContextStatisticsSink
) : BaseStorageChannel(scope, statisticsSink) {
init {
if (!BuildFlags.ENTITY_HANDLE_API) {
throw BuildFlagDisabledError("ENTITY_HANDLE_API")
}
}

override val tag = "MuxedStorageChannel"

override fun sendMessage(encodedMessage: ByteArray, resultCallback: IResultCallback) {
Expand Down
8 changes: 8 additions & 0 deletions java/arcs/android/storage/service/MuxedStorageServiceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import arcs.core.storage.DriverFactory
import arcs.core.storage.StorageKey
import arcs.core.storage.UntypedDirectStoreMuxer
import arcs.core.storage.WriteBackProvider
import arcs.flags.BuildFlagDisabledError
import arcs.flags.BuildFlags
import java.util.concurrent.ConcurrentHashMap
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
Expand All @@ -31,6 +33,12 @@ class MuxedStorageServiceImpl(
private val writeBackProvider: WriteBackProvider,
private val devToolsProxy: DevToolsProxyImpl?
) : IMuxedStorageService.Stub() {
init {
if (!BuildFlags.ENTITY_HANDLE_API) {
throw BuildFlagDisabledError("ENTITY_HANDLE_API")
}
}

// TODO(b/162747024): Replace this with an LruCache so its size doesn't grow unbounded.
private val directStoreMuxers = ConcurrentHashMap<StorageKey, UntypedDirectStoreMuxer>()

Expand Down
1 change: 1 addition & 0 deletions java/arcs/sdk/android/storage/service/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ arcs_kt_android_library(
"//java/arcs/core/storage/driver:driver_providers",
"//java/arcs/core/util",
"//java/arcs/core/util/performance",
"//java/arcs/flags",
"//third_party/java/androidx/annotation",
"//third_party/java/androidx/work",
"//third_party/kotlin/kotlinx_atomicfu",
Expand Down
10 changes: 7 additions & 3 deletions java/arcs/sdk/android/storage/service/StorageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import arcs.core.storage.driver.DatabaseDriverProvider
import arcs.core.util.TaggedLog
import arcs.core.util.performance.MemoryStats
import arcs.core.util.performance.PerformanceStatistics
import arcs.flags.BuildFlags
import java.io.FileDescriptor
import java.io.PrintWriter
import java.util.concurrent.ConcurrentHashMap
Expand Down Expand Up @@ -176,9 +177,6 @@ open class StorageService : ResurrectorService() {
MANAGER_ACTION -> {
return StorageServiceManager(storesScope, driverFactory, stores)
}
MUXED_STORAGE_SERVICE_ACTION -> {
return MuxedStorageServiceImpl(storesScope, driverFactory, writeBackProvider, devToolsProxy)
}
DEVTOOLS_ACTION -> {
val flags = application?.applicationInfo?.flags ?: 0
require(flags and ApplicationInfo.FLAG_DEBUGGABLE != 0) {
Expand All @@ -191,6 +189,12 @@ open class StorageService : ResurrectorService() {
}
}

if (BuildFlags.ENTITY_HANDLE_API) {
if (intent.action == MUXED_STORAGE_SERVICE_ACTION) {
return MuxedStorageServiceImpl(storesScope, driverFactory, writeBackProvider, devToolsProxy)
}
}

// If we got this far, assume we want to bind IStorageService.

val parcelableOptions = requireNotNull(
Expand Down
2 changes: 2 additions & 0 deletions javatests/arcs/android/storage/service/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ arcs_kt_android_test_suite(
"//java/arcs/core/testutil/handles",
"//java/arcs/core/util",
"//java/arcs/core/util/testutil",
"//java/arcs/flags/testing",
"//java/arcs/jvm/util",
"//java/arcs/jvm/util/testutil",
"//java/arcs/sdk/android/storage",
Expand All @@ -47,6 +48,7 @@ arcs_kt_android_test_suite(
"//third_party/java/junit:junit-android",
"//third_party/java/robolectric",
"//third_party/java/truth:truth-android",
"//third_party/kotlin/kotlin:kotlin_test",
"//third_party/kotlin/kotlinx_atomicfu",
"//third_party/kotlin/kotlinx_coroutines",
"//third_party/kotlin/kotlinx_coroutines:kotlinx_coroutines_test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,47 @@ import arcs.core.storage.MuxedProxyMessage
import arcs.core.storage.ProxyMessage
import arcs.core.storage.UntypedDirectStoreMuxer
import arcs.core.storage.testutil.NoopDirectStoreMuxer
import arcs.flags.BuildFlagDisabledError
import arcs.flags.BuildFlags
import arcs.flags.testing.BuildFlagsRule
import com.google.common.truth.Truth.assertThat
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import kotlin.test.assertFailsWith
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@OptIn(ExperimentalCoroutinesApi::class)
class MuxedStorageChannelImplTest {
private val DUMMY_MESSAGE = MuxedProxyMessage<CrdtData, CrdtOperation, Any?>(
"thing0",
ProxyMessage.SyncRequest(null)
)

@get:Rule
val buildFlagsRule = BuildFlagsRule()

private lateinit var messageCallback: IMessageCallback
private lateinit var resultCallback: FakeResultCallback

@Before
fun setUp() {
BuildFlags.ENTITY_HANDLE_API = true
messageCallback = mock {}
resultCallback = FakeResultCallback()
}

@Test
fun requiresBuildFlag() = runBlockingTest {
BuildFlags.ENTITY_HANDLE_API = false

assertFailsWith<BuildFlagDisabledError> { createChannel(this) }
}

@Test
fun proxiesMessagesFromDirectStoreMuxer() = runBlockingTest {
var muxedProxyCallback: MuxedProxyCallback<CrdtData, CrdtOperation, Any?>? = null
Expand Down Expand Up @@ -156,4 +168,11 @@ class MuxedStorageChannelImplTest {
assertThat(result).isNull()
return channel
}

companion object {
private val DUMMY_MESSAGE = MuxedProxyMessage<CrdtData, CrdtOperation, Any?>(
"thing0",
ProxyMessage.SyncRequest(null)
)
}
}
10 changes: 10 additions & 0 deletions third_party/java/arcs/flags/flags.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ ARCS_BUILD_FLAGS = [
"example_feature_1",
],
),
arcs_build_flag(
name = "entity_handle_api",
desc = "EntityHandle API and supporting code in the storage stack",
bug_id = "b/162747024",
status = "NOT_READY",
stopwords = [
"entity.?handle.?api",
"muxed.?storage",
],
),
]

validate_flag_list(ARCS_BUILD_FLAGS)

0 comments on commit 899fd52

Please sign in to comment.