Skip to content
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

Fix: Stream All Messages not correctly continuing flow #123

Merged
merged 7 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -1,6 +1,10 @@
package org.xmtp.android.library

import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -16,6 +20,7 @@ import org.xmtp.android.library.messages.createDeterministic
import org.xmtp.android.library.messages.getPublicKeyBundle
import org.xmtp.android.library.messages.toPublicKeyBundle
import org.xmtp.android.library.messages.walletAddress
import java.lang.Thread.sleep
import java.util.Date

@RunWith(AndroidJUnit4::class)
Expand Down Expand Up @@ -71,4 +76,65 @@ class ConversationsTest {
assertEquals(conversation.peerAddress, newWallet.address)
assertEquals(conversation.createdAt.time, created.time)
}

@Test
fun testStreamAllMessages() = runBlocking {
val bo = PrivateKeyBuilder()
val alix = PrivateKeyBuilder()
val clientOptions =
ClientOptions(api = ClientOptions.Api(env = XMTPEnvironment.LOCAL, isSecure = false))
val boClient = Client().create(bo, clientOptions)
val alixClient = Client().create(alix, clientOptions)
val boConversation = boClient.conversations.newConversation(alixClient.address)

// Record message stream across all conversations
val allMessages = mutableListOf<DecodedMessage>()

val job = CoroutineScope(Dispatchers.IO).launch {
try {
alixClient.conversations.streamAllMessages().collect { message ->
allMessages.add(message)
}
} catch (e: Exception) {}
}
sleep(2500)

for (i in 0 until 5) {
boConversation.send(text = "Message $i")
sleep(1000)
}
assertEquals(allMessages.size, 5)

val caro = PrivateKeyBuilder()
val caroClient = Client().create(caro, clientOptions)
val caroConversation = caroClient.conversations.newConversation(alixClient.address)

sleep(2500)

for (i in 0 until 5) {
caroConversation.send(text = "Message $i")
sleep(1000)
}

assertEquals(allMessages.size, 10)

job.cancel()

CoroutineScope(Dispatchers.IO).launch {
try {
alixClient.conversations.streamAllMessages().collect { message ->
allMessages.add(message)
}
} catch (e: Exception) {
}
}
sleep(2500)

for (i in 0 until 5) {
boConversation.send(text = "Message $i")
sleep(1000)
}

assertEquals(allMessages.size, 15)
}
}
18 changes: 11 additions & 7 deletions library/src/main/java/org/xmtp/android/library/Conversations.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.xmtp.android.library

import android.util.Log
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.job
import kotlinx.coroutines.runBlocking
import org.xmtp.android.library.GRPCApiClient.Companion.makeQueryRequest
import org.xmtp.android.library.messages.Envelope
Expand Down Expand Up @@ -374,24 +376,26 @@ data class Conversations(
}

envelope.contentTopic.startsWith("/xmtp/0/invite-") -> {
val conversation = fromInvite(envelope)
val conversation = fromInvite(envelope = envelope)
conversationsByTopic[conversation.topic] = conversation
// Break so we can resubscribe with the new conversation
return@collect
topics.add(conversation.topic)
currentCoroutineContext().job.cancel()
}

envelope.contentTopic.startsWith("/xmtp/0/intro-") -> {
val conversation = fromIntro(envelope)
val conversation = fromIntro(envelope = envelope)
conversationsByTopic[conversation.topic] = conversation
val decoded = conversation.decode(envelope)
emit(decoded)
// Break so we can resubscribe with the new conversation
return@collect
topics.add(conversation.topic)
currentCoroutineContext().job.cancel()
}

else -> {}
}
}
} catch (error: Exception) {
throw error
continue
}
}
}
Expand Down
Loading