Skip to content

Commit e0eb4c9

Browse files
mrobertejasd
authored andcommitted
Fix unit tests showing warning about datastore_shared_counter (#6830)
Fix Robolectric tests showing warning about datastore_shared_counter by falling back to the normal DataStore instance. This is a known issue, see [b/352047731](http://b/352047731).
1 parent d307f57 commit e0eb4c9

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/FirebaseSessionsComponent.kt

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ package com.google.firebase.sessions
1818

1919
import android.content.Context
2020
import android.util.Log
21+
import androidx.datastore.core.DataMigration
2122
import androidx.datastore.core.DataStore
23+
import androidx.datastore.core.DataStoreFactory
2224
import androidx.datastore.core.MultiProcessDataStoreFactory
25+
import androidx.datastore.core.Serializer
2326
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
2427
import androidx.datastore.dataStoreFile
2528
import com.google.android.datatransport.TransportFactory
@@ -43,6 +46,7 @@ import dagger.BindsInstance
4346
import dagger.Component
4447
import dagger.Module
4548
import dagger.Provides
49+
import java.io.File
4650
import javax.inject.Qualifier
4751
import javax.inject.Singleton
4852
import kotlin.coroutines.CoroutineContext
@@ -137,7 +141,7 @@ internal interface FirebaseSessionsComponent {
137141
appContext: Context,
138142
@Blocking blockingDispatcher: CoroutineContext,
139143
): DataStore<SessionConfigs> =
140-
MultiProcessDataStoreFactory.create(
144+
createDataStore(
141145
serializer = SessionConfigsSerializer,
142146
corruptionHandler =
143147
ReplaceFileCorruptionHandler { ex ->
@@ -154,7 +158,7 @@ internal interface FirebaseSessionsComponent {
154158
appContext: Context,
155159
@Blocking blockingDispatcher: CoroutineContext,
156160
): DataStore<SessionData> =
157-
MultiProcessDataStoreFactory.create(
161+
createDataStore(
158162
serializer = SessionDataSerializer,
159163
corruptionHandler =
160164
ReplaceFileCorruptionHandler { ex ->
@@ -164,6 +168,37 @@ internal interface FirebaseSessionsComponent {
164168
scope = CoroutineScope(blockingDispatcher),
165169
produceFile = { appContext.dataStoreFile("aqs/sessionDataStore.data") },
166170
)
171+
172+
private fun <T> createDataStore(
173+
serializer: Serializer<T>,
174+
corruptionHandler: ReplaceFileCorruptionHandler<T>,
175+
migrations: List<DataMigration<T>> = listOf(),
176+
scope: CoroutineScope,
177+
produceFile: () -> File,
178+
): DataStore<T> =
179+
if (loadDataStoreSharedCounter()) {
180+
MultiProcessDataStoreFactory.create(
181+
serializer,
182+
corruptionHandler,
183+
migrations,
184+
scope,
185+
produceFile,
186+
)
187+
} else {
188+
DataStoreFactory.create(serializer, corruptionHandler, migrations, scope, produceFile)
189+
}
190+
191+
/** This native library in unavailable in some conditions, for example, Robolectric tests */
192+
// TODO(mrober): Remove this when b/392626815 is resolved
193+
private fun loadDataStoreSharedCounter(): Boolean =
194+
try {
195+
System.loadLibrary("datastore_shared_counter")
196+
true
197+
} catch (_: UnsatisfiedLinkError) {
198+
false
199+
} catch (_: SecurityException) {
200+
false
201+
}
167202
}
168203
}
169204
}

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionEvents.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ internal object SessionEvents {
6363
fun getApplicationInfo(firebaseApp: FirebaseApp): ApplicationInfo {
6464
val context = firebaseApp.applicationContext
6565
val packageName = context.packageName
66-
@Suppress("DEPRECATION") // TODO(mrober): Use ApplicationInfoFlags when target sdk set to 33
6766
val packageInfo = context.packageManager.getPackageInfo(packageName, 0)
6867
val buildVersion =
6968
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {

0 commit comments

Comments
 (0)