1 package kotlinx.coroutines.stream
2 
3 import kotlinx.coroutines.testing.*
4 import kotlinx.coroutines.*
5 import kotlinx.coroutines.flow.*
6 import org.junit.Test
7 import java.lang.IllegalStateException
8 import kotlin.test.*
9 
10 class ConsumeAsFlowTest : TestBase() {
11 
12     @Test
<lambda>null13     fun testCollect() = runTest {
14         val list = listOf(1, 2, 3)
15         assertEquals(list, list.stream().consumeAsFlow().toList())
16     }
17 
18     @Test
<lambda>null19     fun testCollectInvokesClose() = runTest {
20         val list = listOf(3, 4, 5)
21         expect(1)
22         assertEquals(list, list.stream().onClose { expect(2) }.consumeAsFlow().toList())
23         finish(3)
24     }
25 
26     @Test
<lambda>null27     fun testCollectTwice() = runTest {
28         val list = listOf(2, 3, 9)
29         val flow = list.stream().onClose { expect(2) } .consumeAsFlow()
30         expect(1)
31         assertEquals(list, flow.toList())
32         assertFailsWith<IllegalStateException> { flow.collect() }
33         finish(3)
34     }
35 }
36