1
1
package org.xmtp.android.library
2
2
3
3
import androidx.test.ext.junit.runners.AndroidJUnit4
4
+ import kotlinx.coroutines.CoroutineScope
5
+ import kotlinx.coroutines.Dispatchers
6
+ import kotlinx.coroutines.launch
7
+ import kotlinx.coroutines.runBlocking
4
8
import org.junit.Assert.assertEquals
5
9
import org.junit.Test
6
10
import org.junit.runner.RunWith
@@ -16,6 +20,7 @@ import org.xmtp.android.library.messages.createDeterministic
16
20
import org.xmtp.android.library.messages.getPublicKeyBundle
17
21
import org.xmtp.android.library.messages.toPublicKeyBundle
18
22
import org.xmtp.android.library.messages.walletAddress
23
+ import java.lang.Thread.sleep
19
24
import java.util.Date
20
25
21
26
@RunWith(AndroidJUnit4 ::class )
@@ -71,4 +76,65 @@ class ConversationsTest {
71
76
assertEquals(conversation.peerAddress, newWallet.address)
72
77
assertEquals(conversation.createdAt.time, created.time)
73
78
}
79
+
80
+ @Test
81
+ fun testStreamAllMessages () = runBlocking {
82
+ val bo = PrivateKeyBuilder ()
83
+ val alix = PrivateKeyBuilder ()
84
+ val clientOptions =
85
+ ClientOptions (api = ClientOptions .Api (env = XMTPEnvironment .LOCAL , isSecure = false ))
86
+ val boClient = Client ().create(bo, clientOptions)
87
+ val alixClient = Client ().create(alix, clientOptions)
88
+ val boConversation = boClient.conversations.newConversation(alixClient.address)
89
+
90
+ // Record message stream across all conversations
91
+ val allMessages = mutableListOf<DecodedMessage >()
92
+
93
+ val job = CoroutineScope (Dispatchers .IO ).launch {
94
+ try {
95
+ alixClient.conversations.streamAllMessages().collect { message ->
96
+ allMessages.add(message)
97
+ }
98
+ } catch (e: Exception ) {}
99
+ }
100
+ sleep(2500 )
101
+
102
+ for (i in 0 until 5 ) {
103
+ boConversation.send(text = " Message $i " )
104
+ sleep(1000 )
105
+ }
106
+ assertEquals(allMessages.size, 5 )
107
+
108
+ val caro = PrivateKeyBuilder ()
109
+ val caroClient = Client ().create(caro, clientOptions)
110
+ val caroConversation = caroClient.conversations.newConversation(alixClient.address)
111
+
112
+ sleep(2500 )
113
+
114
+ for (i in 0 until 5 ) {
115
+ caroConversation.send(text = " Message $i " )
116
+ sleep(1000 )
117
+ }
118
+
119
+ assertEquals(allMessages.size, 10 )
120
+
121
+ job.cancel()
122
+
123
+ CoroutineScope (Dispatchers .IO ).launch {
124
+ try {
125
+ alixClient.conversations.streamAllMessages().collect { message ->
126
+ allMessages.add(message)
127
+ }
128
+ } catch (e: Exception ) {
129
+ }
130
+ }
131
+ sleep(2500 )
132
+
133
+ for (i in 0 until 5 ) {
134
+ boConversation.send(text = " Message $i " )
135
+ sleep(1000 )
136
+ }
137
+
138
+ assertEquals(allMessages.size, 15 )
139
+ }
74
140
}
0 commit comments