File tree 24 files changed +112
-112
lines changed
kotlinx-coroutines-core/jvm/test/guide
24 files changed +112
-112
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 5
5
// This file was automatically generated from flow.md by Knit tool. Do not edit.
6
6
package kotlinx.coroutines.guide.exampleFlow01
7
7
8
- fun foo (): List <Int > = listOf (1 , 2 , 3 )
8
+ fun simple (): List <Int > = listOf (1 , 2 , 3 )
9
9
10
10
fun main () {
11
- foo ().forEach { value -> println (value) }
11
+ simple ().forEach { value -> println (value) }
12
12
}
Original file line number Diff line number Diff line change 5
5
// This file was automatically generated from flow.md by Knit tool. Do not edit.
6
6
package kotlinx.coroutines.guide.exampleFlow02
7
7
8
- fun foo (): Sequence <Int > = sequence { // sequence builder
8
+ fun simple (): Sequence <Int > = sequence { // sequence builder
9
9
for (i in 1 .. 3 ) {
10
10
Thread .sleep(100 ) // pretend we are computing it
11
11
yield (i) // yield next value
12
12
}
13
13
}
14
14
15
15
fun main () {
16
- foo ().forEach { value -> println (value) }
16
+ simple ().forEach { value -> println (value) }
17
17
}
Original file line number Diff line number Diff line change @@ -7,11 +7,11 @@ package kotlinx.coroutines.guide.exampleFlow03
7
7
8
8
import kotlinx.coroutines.*
9
9
10
- suspend fun foo (): List <Int > {
10
+ suspend fun simple (): List <Int > {
11
11
delay(1000 ) // pretend we are doing something asynchronous here
12
12
return listOf (1 , 2 , 3 )
13
13
}
14
14
15
15
fun main () = runBlocking<Unit > {
16
- foo ().forEach { value -> println (value) }
16
+ simple ().forEach { value -> println (value) }
17
17
}
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow04
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
11
- fun foo (): Flow <Int > = flow { // flow builder
11
+ fun simple (): Flow <Int > = flow { // flow builder
12
12
for (i in 1 .. 3 ) {
13
13
delay(100 ) // pretend we are doing something useful here
14
14
emit(i) // emit next value
@@ -24,5 +24,5 @@ fun main() = runBlocking<Unit> {
24
24
}
25
25
}
26
26
// Collect the flow
27
- foo ().collect { value -> println (value) }
27
+ simple ().collect { value -> println (value) }
28
28
}
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow05
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
11
- fun foo (): Flow <Int > = flow {
11
+ fun simple (): Flow <Int > = flow {
12
12
println (" Flow started" )
13
13
for (i in 1 .. 3 ) {
14
14
delay(100 )
@@ -17,8 +17,8 @@ fun foo(): Flow<Int> = flow {
17
17
}
18
18
19
19
fun main () = runBlocking<Unit > {
20
- println (" Calling foo ..." )
21
- val flow = foo ()
20
+ println (" Calling simple function ..." )
21
+ val flow = simple ()
22
22
println (" Calling collect..." )
23
23
flow.collect { value -> println (value) }
24
24
println (" Calling collect again..." )
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow06
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
11
- fun foo (): Flow <Int > = flow {
11
+ fun simple (): Flow <Int > = flow {
12
12
for (i in 1 .. 3 ) {
13
13
delay(100 )
14
14
println (" Emitting $i " )
@@ -18,7 +18,7 @@ fun foo(): Flow<Int> = flow {
18
18
19
19
fun main () = runBlocking<Unit > {
20
20
withTimeoutOrNull(250 ) { // Timeout after 250ms
21
- foo ().collect { value -> println (value) }
21
+ simple ().collect { value -> println (value) }
22
22
}
23
23
println (" Done" )
24
24
}
Original file line number Diff line number Diff line change @@ -10,13 +10,13 @@ import kotlinx.coroutines.flow.*
10
10
11
11
fun log (msg : String ) = println (" [${Thread .currentThread().name} ] $msg " )
12
12
13
- fun foo (): Flow <Int > = flow {
14
- log(" Started foo flow" )
13
+ fun simple (): Flow <Int > = flow {
14
+ log(" Started simple flow" )
15
15
for (i in 1 .. 3 ) {
16
16
emit(i)
17
17
}
18
18
}
19
19
20
20
fun main () = runBlocking<Unit > {
21
- foo ().collect { value -> log(" Collected $value " ) }
21
+ simple ().collect { value -> log(" Collected $value " ) }
22
22
}
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow14
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
11
- fun foo (): Flow <Int > = flow {
11
+ fun simple (): Flow <Int > = flow {
12
12
// The WRONG way to change context for CPU-consuming code in flow builder
13
13
kotlinx.coroutines.withContext(Dispatchers .Default ) {
14
14
for (i in 1 .. 3 ) {
@@ -19,5 +19,5 @@ fun foo(): Flow<Int> = flow {
19
19
}
20
20
21
21
fun main () = runBlocking<Unit > {
22
- foo ().collect { value -> println (value) }
22
+ simple ().collect { value -> println (value) }
23
23
}
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.*
10
10
11
11
fun log (msg : String ) = println (" [${Thread .currentThread().name} ] $msg " )
12
12
13
- fun foo (): Flow <Int > = flow {
13
+ fun simple (): Flow <Int > = flow {
14
14
for (i in 1 .. 3 ) {
15
15
Thread .sleep(100 ) // pretend we are computing it in CPU-consuming way
16
16
log(" Emitting $i " )
@@ -19,7 +19,7 @@ fun foo(): Flow<Int> = flow {
19
19
}.flowOn(Dispatchers .Default ) // RIGHT way to change context for CPU-consuming code in flow builder
20
20
21
21
fun main () = runBlocking<Unit > {
22
- foo ().collect { value ->
22
+ simple ().collect { value ->
23
23
log(" Collected $value " )
24
24
}
25
25
}
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
import kotlin.system.*
11
11
12
- fun foo (): Flow <Int > = flow {
12
+ fun simple (): Flow <Int > = flow {
13
13
for (i in 1 .. 3 ) {
14
14
delay(100 ) // pretend we are asynchronously waiting 100 ms
15
15
emit(i) // emit next value
@@ -18,7 +18,7 @@ fun foo(): Flow<Int> = flow {
18
18
19
19
fun main () = runBlocking<Unit > {
20
20
val time = measureTimeMillis {
21
- foo ().collect { value ->
21
+ simple ().collect { value ->
22
22
delay(300 ) // pretend we are processing it for 300 ms
23
23
println (value)
24
24
}
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
import kotlin.system.*
11
11
12
- fun foo (): Flow <Int > = flow {
12
+ fun simple (): Flow <Int > = flow {
13
13
for (i in 1 .. 3 ) {
14
14
delay(100 ) // pretend we are asynchronously waiting 100 ms
15
15
emit(i) // emit next value
@@ -18,7 +18,7 @@ fun foo(): Flow<Int> = flow {
18
18
19
19
fun main () = runBlocking<Unit > {
20
20
val time = measureTimeMillis {
21
- foo ()
21
+ simple ()
22
22
.buffer() // buffer emissions, don't wait
23
23
.collect { value ->
24
24
delay(300 ) // pretend we are processing it for 300 ms
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
import kotlin.system.*
11
11
12
- fun foo (): Flow <Int > = flow {
12
+ fun simple (): Flow <Int > = flow {
13
13
for (i in 1 .. 3 ) {
14
14
delay(100 ) // pretend we are asynchronously waiting 100 ms
15
15
emit(i) // emit next value
@@ -18,7 +18,7 @@ fun foo(): Flow<Int> = flow {
18
18
19
19
fun main () = runBlocking<Unit > {
20
20
val time = measureTimeMillis {
21
- foo ()
21
+ simple ()
22
22
.conflate() // conflate emissions, don't process each one
23
23
.collect { value ->
24
24
delay(300 ) // pretend we are processing it for 300 ms
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
import kotlin.system.*
11
11
12
- fun foo (): Flow <Int > = flow {
12
+ fun simple (): Flow <Int > = flow {
13
13
for (i in 1 .. 3 ) {
14
14
delay(100 ) // pretend we are asynchronously waiting 100 ms
15
15
emit(i) // emit next value
@@ -18,7 +18,7 @@ fun foo(): Flow<Int> = flow {
18
18
19
19
fun main () = runBlocking<Unit > {
20
20
val time = measureTimeMillis {
21
- foo ()
21
+ simple ()
22
22
.collectLatest { value -> // cancel & restart on the latest value
23
23
println (" Collecting $value " )
24
24
delay(300 ) // pretend we are processing it for 300 ms
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow26
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
11
- fun foo (): Flow <Int > = flow {
11
+ fun simple (): Flow <Int > = flow {
12
12
for (i in 1 .. 3 ) {
13
13
println (" Emitting $i " )
14
14
emit(i) // emit next value
@@ -17,7 +17,7 @@ fun foo(): Flow<Int> = flow {
17
17
18
18
fun main () = runBlocking<Unit > {
19
19
try {
20
- foo ().collect { value ->
20
+ simple ().collect { value ->
21
21
println (value)
22
22
check(value <= 1 ) { " Collected $value " }
23
23
}
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow27
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
11
- fun foo (): Flow <String > =
11
+ fun simple (): Flow <String > =
12
12
flow {
13
13
for (i in 1 .. 3 ) {
14
14
println (" Emitting $i " )
@@ -22,7 +22,7 @@ fun foo(): Flow<String> =
22
22
23
23
fun main () = runBlocking<Unit > {
24
24
try {
25
- foo ().collect { value -> println (value) }
25
+ simple ().collect { value -> println (value) }
26
26
} catch (e: Throwable ) {
27
27
println (" Caught $e " )
28
28
}
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow28
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
11
- fun foo (): Flow <String > =
11
+ fun simple (): Flow <String > =
12
12
flow {
13
13
for (i in 1 .. 3 ) {
14
14
println (" Emitting $i " )
@@ -21,7 +21,7 @@ fun foo(): Flow<String> =
21
21
}
22
22
23
23
fun main () = runBlocking<Unit > {
24
- foo ()
24
+ simple ()
25
25
.catch { e -> emit(" Caught $e " ) } // emit on exception
26
26
.collect { value -> println (value) }
27
27
}
Original file line number Diff line number Diff line change @@ -8,15 +8,15 @@ package kotlinx.coroutines.guide.exampleFlow29
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
11
- fun foo (): Flow <Int > = flow {
11
+ fun simple (): Flow <Int > = flow {
12
12
for (i in 1 .. 3 ) {
13
13
println (" Emitting $i " )
14
14
emit(i)
15
15
}
16
16
}
17
17
18
18
fun main () = runBlocking<Unit > {
19
- foo ()
19
+ simple ()
20
20
.catch { e -> println (" Caught $e " ) } // does not catch downstream exceptions
21
21
.collect { value ->
22
22
check(value <= 1 ) { " Collected $value " }
Original file line number Diff line number Diff line change @@ -8,15 +8,15 @@ package kotlinx.coroutines.guide.exampleFlow30
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
11
- fun foo (): Flow <Int > = flow {
11
+ fun simple (): Flow <Int > = flow {
12
12
for (i in 1 .. 3 ) {
13
13
println (" Emitting $i " )
14
14
emit(i)
15
15
}
16
16
}
17
17
18
18
fun main () = runBlocking<Unit > {
19
- foo ()
19
+ simple ()
20
20
.onEach { value ->
21
21
check(value <= 1 ) { " Collected $value " }
22
22
println (value)
Original file line number Diff line number Diff line change @@ -8,11 +8,11 @@ package kotlinx.coroutines.guide.exampleFlow31
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
11
- fun foo (): Flow <Int > = (1 .. 3 ).asFlow()
11
+ fun simple (): Flow <Int > = (1 .. 3 ).asFlow()
12
12
13
13
fun main () = runBlocking<Unit > {
14
14
try {
15
- foo ().collect { value -> println (value) }
15
+ simple ().collect { value -> println (value) }
16
16
} finally {
17
17
println (" Done" )
18
18
}
Original file line number Diff line number Diff line change @@ -8,10 +8,10 @@ package kotlinx.coroutines.guide.exampleFlow32
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
11
- fun foo (): Flow <Int > = (1 .. 3 ).asFlow()
11
+ fun simple (): Flow <Int > = (1 .. 3 ).asFlow()
12
12
13
13
fun main () = runBlocking<Unit > {
14
- foo ()
14
+ simple ()
15
15
.onCompletion { println (" Done" ) }
16
16
.collect { value -> println (value) }
17
17
}
Original file line number Diff line number Diff line change @@ -8,13 +8,13 @@ package kotlinx.coroutines.guide.exampleFlow33
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
11
- fun foo (): Flow <Int > = flow {
11
+ fun simple (): Flow <Int > = flow {
12
12
emit(1 )
13
13
throw RuntimeException ()
14
14
}
15
15
16
16
fun main () = runBlocking<Unit > {
17
- foo ()
17
+ simple ()
18
18
.onCompletion { cause -> if (cause != null ) println (" Flow completed exceptionally" ) }
19
19
.catch { cause -> println (" Caught exception" ) }
20
20
.collect { value -> println (value) }
Original file line number Diff line number Diff line change @@ -8,10 +8,10 @@ package kotlinx.coroutines.guide.exampleFlow34
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.flow.*
10
10
11
- fun foo (): Flow <Int > = (1 .. 3 ).asFlow()
11
+ fun simple (): Flow <Int > = (1 .. 3 ).asFlow()
12
12
13
13
fun main () = runBlocking<Unit > {
14
- foo ()
14
+ simple ()
15
15
.onCompletion { cause -> println (" Flow completed with $cause " ) }
16
16
.collect { value ->
17
17
check(value <= 1 ) { " Collected $value " }
Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ class FlowGuideTest {
50
50
@Test
51
51
fun testExampleFlow05 () {
52
52
test(" ExampleFlow05" ) { kotlinx.coroutines.guide.exampleFlow05.main() }.verifyLines(
53
- " Calling foo ..." ,
53
+ " Calling simple function ..." ,
54
54
" Calling collect..." ,
55
55
" Flow started" ,
56
56
" 1" ,
@@ -139,7 +139,7 @@ class FlowGuideTest {
139
139
@Test
140
140
fun testExampleFlow13 () {
141
141
test(" ExampleFlow13" ) { kotlinx.coroutines.guide.exampleFlow13.main() }.verifyLinesFlexibleThread(
142
- " [main @coroutine#1] Started foo flow" ,
142
+ " [main @coroutine#1] Started simple flow" ,
143
143
" [main @coroutine#1] Collected 1" ,
144
144
" [main @coroutine#1] Collected 2" ,
145
145
" [main @coroutine#1] Collected 3"
You can’t perform that action at this time.
0 commit comments