Skip to content

Fix unit tests showing warning about datastore_shared_counter #6830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ package com.google.firebase.sessions

import android.content.Context
import android.util.Log
import androidx.datastore.core.DataMigration
import androidx.datastore.core.DataStore
import androidx.datastore.core.DataStoreFactory
import androidx.datastore.core.MultiProcessDataStoreFactory
import androidx.datastore.core.Serializer
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
import androidx.datastore.dataStoreFile
import com.google.android.datatransport.TransportFactory
Expand All @@ -43,6 +46,7 @@ import dagger.BindsInstance
import dagger.Component
import dagger.Module
import dagger.Provides
import java.io.File
import javax.inject.Qualifier
import javax.inject.Singleton
import kotlin.coroutines.CoroutineContext
Expand Down Expand Up @@ -137,7 +141,7 @@ internal interface FirebaseSessionsComponent {
appContext: Context,
@Blocking blockingDispatcher: CoroutineContext,
): DataStore<SessionConfigs> =
MultiProcessDataStoreFactory.create(
createDataStore(
serializer = SessionConfigsSerializer,
corruptionHandler =
ReplaceFileCorruptionHandler { ex ->
Expand All @@ -154,7 +158,7 @@ internal interface FirebaseSessionsComponent {
appContext: Context,
@Blocking blockingDispatcher: CoroutineContext,
): DataStore<SessionData> =
MultiProcessDataStoreFactory.create(
createDataStore(
serializer = SessionDataSerializer,
corruptionHandler =
ReplaceFileCorruptionHandler { ex ->
Expand All @@ -164,6 +168,37 @@ internal interface FirebaseSessionsComponent {
scope = CoroutineScope(blockingDispatcher),
produceFile = { appContext.dataStoreFile("aqs/sessionDataStore.data") },
)

private fun <T> createDataStore(
serializer: Serializer<T>,
corruptionHandler: ReplaceFileCorruptionHandler<T>,
migrations: List<DataMigration<T>> = listOf(),
scope: CoroutineScope,
produceFile: () -> File,
): DataStore<T> =
if (loadDataStoreSharedCounter()) {
MultiProcessDataStoreFactory.create(
serializer,
corruptionHandler,
migrations,
scope,
produceFile,
)
} else {
DataStoreFactory.create(serializer, corruptionHandler, migrations, scope, produceFile)
}

/** This native library in unavailable in some conditions, for example, Robolectric tests */
// TODO(mrober): Remove this when b/392626815 is resolved
private fun loadDataStoreSharedCounter(): Boolean =
try {
System.loadLibrary("datastore_shared_counter")
true
} catch (_: UnsatisfiedLinkError) {
false
} catch (_: SecurityException) {
false
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ internal object SessionEvents {
fun getApplicationInfo(firebaseApp: FirebaseApp): ApplicationInfo {
val context = firebaseApp.applicationContext
val packageName = context.packageName
@Suppress("DEPRECATION") // TODO(mrober): Use ApplicationInfoFlags when target sdk set to 33
val packageInfo = context.packageManager.getPackageInfo(packageName, 0)
val buildVersion =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Expand Down
Loading