1 /*
2  * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.coroutines.stream
6 
7 import kotlinx.coroutines.*
8 import kotlinx.coroutines.flow.*
9 import org.junit.Test
10 import java.lang.IllegalStateException
11 import kotlin.test.*
12 
13 class ConsumeAsFlowTest : TestBase() {
14 
15     @Test
<lambda>null16     fun testCollect() = runTest {
17         val list = listOf(1, 2, 3)
18         assertEquals(list, list.stream().consumeAsFlow().toList())
19     }
20 
21     @Test
<lambda>null22     fun testCollectInvokesClose() = runTest {
23         val list = listOf(3, 4, 5)
24         expect(1)
25         assertEquals(list, list.stream().onClose { expect(2) }.consumeAsFlow().toList())
26         finish(3)
27     }
28 
29     @Test
<lambda>null30     fun testCollectTwice() = runTest {
31         val list = listOf(2, 3, 9)
32         val flow = list.stream().onClose { expect(2) } .consumeAsFlow()
33         expect(1)
34         assertEquals(list, flow.toList())
35         assertFailsWith<IllegalStateException> { flow.collect() }
36         finish(3)
37     }
38 }
39