1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
| @Test
fun `should emit messages from user using backgroundScope`() =
runTest {
// given
val source =
flow {
emit(Message(fromUserId = "0", text = "A"))
delay(1000)
emit(Message(fromUserId = "1", text = "B"))
emit(Message(fromUserId = "0", text = "C"))
}
val service =
MessagesService(
messagesSource = source,
scope = backgroundScope,
)
// when
val emittedMessages = mutableListOf<Message>()
service.observeMessages("0")
.onEach { emittedMessages.add(it) }
.launchIn(backgroundScope)
delay(1)
// then
assertEquals(
listOf(
Message(fromUserId = "0", text = "A"),
),
emittedMessages,
)
// when
delay(1000)
// then
assertEquals(
listOf(
Message(fromUserId = "0", text = "A"),
Message(fromUserId = "0", text = "C"),
),
emittedMessages,
)
}
|