Skip to content

Commit

Permalink
add tests for streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Dec 20, 2024
1 parent 6ced314 commit 112aef2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ enum class EntryType {
}
}

enum class PreferenceType {
HMAC_KEYS;
}

data class ConsentRecord(
val value: String,
val entryType: EntryType,
Expand Down Expand Up @@ -100,18 +104,18 @@ data class PrivatePreferences(
var client: Client,
private val ffiClient: FfiXmtpClient,
) {
suspend fun streamHmacKeys(): Flow<Keystore.GetConversationHmacKeysResponse> = callbackFlow {
suspend fun streamPreferenceUpdates(): Flow<PreferenceType> = callbackFlow {
val preferenceCallback = object : FfiPreferenceCallback {
override fun onPreferenceUpdate(preference: List<FfiPreferenceUpdate>) {
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())
}
}

Expand Down

0 comments on commit 112aef2

Please sign in to comment.