From 112aef209419c9a494259c39fc43f682702f597a Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 19 Dec 2024 20:00:12 -0800 Subject: [PATCH] add tests for streaming --- .../xmtp/android/library/HistorySyncTest.kt | 41 +++++++++++++++++++ .../android/library/PrivatePreferences.kt | 12 ++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/library/src/androidTest/java/org/xmtp/android/library/HistorySyncTest.kt b/library/src/androidTest/java/org/xmtp/android/library/HistorySyncTest.kt index b0f4d893..68ec81fa 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/HistorySyncTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/HistorySyncTest.kt @@ -167,4 +167,45 @@ class HistorySyncTest { assertEquals(alixGroup.consentState(), ConsentState.DENIED) job.cancel() } + + @Test + fun testStreamPreferenceUpdates() { + var preferences = 0 + val job = CoroutineScope(Dispatchers.IO).launch { + try { + alixClient.preferences.streamPreferenceUpdates() + .collect { entry -> + preferences++ + } + } catch (e: Exception) { + } + } + + Thread.sleep(2000) + + runBlocking { + val alixClient3 = runBlocking { + Client().create( + account = alixWallet, + options = ClientOptions( + ClientOptions.Api(XMTPEnvironment.LOCAL, false), + appContext = fixtures.context, + dbEncryptionKey = fixtures.key, + dbDirectory = File(fixtures.context.filesDir.absolutePath, "xmtp_db3").toPath() + .toString() + ) + ) + } + alixClient3.conversations.syncAllConversations() + Thread.sleep(2000) + alixClient.conversations.syncAllConversations() + Thread.sleep(2000) + alixClient2.conversations.syncAllConversations() + Thread.sleep(2000) + } + + Thread.sleep(2000) + assertEquals(2, preferences) + job.cancel() + } } diff --git a/library/src/main/java/org/xmtp/android/library/PrivatePreferences.kt b/library/src/main/java/org/xmtp/android/library/PrivatePreferences.kt index c3a32718..0d27421f 100644 --- a/library/src/main/java/org/xmtp/android/library/PrivatePreferences.kt +++ b/library/src/main/java/org/xmtp/android/library/PrivatePreferences.kt @@ -64,6 +64,10 @@ enum class EntryType { } } +enum class PreferenceType { + HMAC_KEYS; +} + data class ConsentRecord( val value: String, val entryType: EntryType, @@ -100,18 +104,18 @@ data class PrivatePreferences( var client: Client, private val ffiClient: FfiXmtpClient, ) { - suspend fun streamHmacKeys(): Flow = callbackFlow { + suspend fun streamPreferenceUpdates(): Flow = callbackFlow { val preferenceCallback = object : FfiPreferenceCallback { override fun onPreferenceUpdate(preference: List) { preference.iterator().forEach { - when(it) { - is FfiPreferenceUpdate.Hmac -> it.key + when (it) { + is FfiPreferenceUpdate.Hmac -> trySend(PreferenceType.HMAC_KEYS) } } } override fun onError(error: FfiSubscribeException) { - Log.e("XMTP hmac key stream", error.message.toString()) + Log.e("XMTP preference update stream", error.message.toString()) } }