Skip to content

Commit b97ebfc

Browse files
authored
Update Flow.sample KDoc example timings, add tests (#2259)
* All Flow.debounce/sample KDoc example code snippets are automatically tested with Knit. * Flow.sample timings are made larger, so that they produce an expected output when run under the real time, too. Fixes #2243
1 parent 7897f70 commit b97ebfc

21 files changed

+228
-27
lines changed

docs/knit.properties

-12
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,7 @@
44

55
knit.package=kotlinx.coroutines.guide
66
knit.dir=../kotlinx-coroutines-core/jvm/test/guide/
7-
knit.pattern=example-[a-zA-Z0-9-]+-##\\.kt
8-
knit.include=knit.code.include
97

108
test.package=kotlinx.coroutines.guide.test
119
test.dir=../kotlinx-coroutines-core/jvm/test/guide/test/
12-
test.template=knit.test.template
1310

14-
# Various test validation modes and their corresponding methods from TestUtil
15-
test.mode.=verifyLines
16-
test.mode.STARTS_WITH=verifyLinesStartWith
17-
test.mode.ARBITRARY_TIME=verifyLinesArbitraryTime
18-
test.mode.FLEXIBLE_TIME=verifyLinesFlexibleTime
19-
test.mode.FLEXIBLE_THREAD=verifyLinesFlexibleThread
20-
test.mode.LINES_START_UNORDERED=verifyLinesStartUnordered
21-
test.mode.LINES_START=verifyLinesStart
22-
test.mode.EXCEPTION=verifyExceptions

docs/knit.test.template

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This file was automatically generated from ${file.name} by Knit tool. Do not edit.
66
package ${test.package}
77

8+
import kotlinx.coroutines.knit.*
89
import org.junit.Test
910

1011
class ${test.name} {

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ kotlin_version=1.4.0
1010
# Dependencies
1111
junit_version=4.12
1212
atomicfu_version=0.14.4
13-
knit_version=0.1.3
13+
knit_version=0.2.0
1414
html_version=0.6.8
1515
lincheck_version=2.7.1
1616
dokka_version=0.9.16-rdev-2-mpp-hacks

knit.properties

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
#
4+
5+
knit.include=docs/knit.code.include
6+
test.template=docs/knit.test.template
7+
8+
# Various test validation modes and their corresponding methods from TestUtil
9+
test.mode.=verifyLines
10+
test.mode.STARTS_WITH=verifyLinesStartWith
11+
test.mode.ARBITRARY_TIME=verifyLinesArbitraryTime
12+
test.mode.FLEXIBLE_TIME=verifyLinesFlexibleTime
13+
test.mode.FLEXIBLE_THREAD=verifyLinesFlexibleThread
14+
test.mode.LINES_START_UNORDERED=verifyLinesStartUnordered
15+
test.mode.LINES_START=verifyLinesStart
16+
test.mode.EXCEPTION=verifyExceptions

kotlinx-coroutines-core/common/src/flow/operators/Delay.kt

+60-12
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,30 @@ import kotlinx.coroutines.selects.*
1414
import kotlin.jvm.*
1515
import kotlin.time.*
1616

17+
/* Scaffolding for Knit code examples
18+
<!--- TEST_NAME FlowDelayTest -->
19+
<!--- PREFIX .*-duration-.*
20+
@file:OptIn(ExperimentalTime::class)
21+
----- INCLUDE .*-duration-.*
22+
import kotlin.time.*
23+
----- INCLUDE .*
24+
import kotlinx.coroutines.*
25+
import kotlinx.coroutines.flow.*
26+
27+
fun main() = runBlocking {
28+
----- SUFFIX .*
29+
.toList().joinToString().let { println(it) } }
30+
-->
31+
*/
32+
1733
/**
1834
* Returns a flow that mirrors the original flow, but filters out values
1935
* that are followed by the newer values within the given [timeout][timeoutMillis].
2036
* The latest value is always emitted.
2137
*
2238
* Example:
23-
* ```
39+
*
40+
* ```kotlin
2441
* flow {
2542
* emit(1)
2643
* delay(90)
@@ -33,7 +50,14 @@ import kotlin.time.*
3350
* emit(5)
3451
* }.debounce(1000)
3552
* ```
36-
* produces `3, 4, 5`.
53+
* <!--- KNIT example-delay-01.kt -->
54+
*
55+
* produces the following emissions
56+
*
57+
* ```text
58+
* 3, 4, 5
59+
* ```
60+
* <!--- TEST -->
3761
*
3862
* Note that the resulting flow does not emit anything as long as the original flow emits
3963
* items faster than every [timeoutMillis] milliseconds.
@@ -77,7 +101,8 @@ public fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T> {
77101
* The latest value is always emitted.
78102
*
79103
* Example:
80-
* ```
104+
*
105+
* ```kotlin
81106
* flow {
82107
* emit(1)
83108
* delay(90.milliseconds)
@@ -90,7 +115,14 @@ public fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T> {
90115
* emit(5)
91116
* }.debounce(1000.milliseconds)
92117
* ```
93-
* produces `3, 4, 5`.
118+
* <!--- KNIT example-delay-duration-01.kt -->
119+
*
120+
* produces the following emissions
121+
*
122+
* ```text
123+
* 3, 4, 5
124+
* ```
125+
* <!--- TEST -->
94126
*
95127
* Note that the resulting flow does not emit anything as long as the original flow emits
96128
* items faster than every [timeout] milliseconds.
@@ -103,15 +135,23 @@ public fun <T> Flow<T>.debounce(timeout: Duration): Flow<T> = debounce(timeout.t
103135
* Returns a flow that emits only the latest value emitted by the original flow during the given sampling [period][periodMillis].
104136
*
105137
* Example:
106-
* ```
138+
*
139+
* ```kotlin
107140
* flow {
108141
* repeat(10) {
109142
* emit(it)
110-
* delay(50)
143+
* delay(110)
111144
* }
112-
* }.sample(100)
145+
* }.sample(200)
146+
* ```
147+
* <!--- KNIT example-delay-02.kt -->
148+
*
149+
* produces the following emissions
150+
*
151+
* ```text
152+
* 1, 3, 5, 7, 9
113153
* ```
114-
* produces `1, 3, 5, 7, 9`.
154+
* <!--- TEST -->
115155
*
116156
* Note that the latest element is not emitted if it does not fit into the sampling window.
117157
*/
@@ -166,15 +206,23 @@ internal fun CoroutineScope.fixedPeriodTicker(delayMillis: Long, initialDelayMil
166206
* Returns a flow that emits only the latest value emitted by the original flow during the given sampling [period].
167207
*
168208
* Example:
169-
* ```
209+
*
210+
* ```kotlin
170211
* flow {
171212
* repeat(10) {
172213
* emit(it)
173-
* delay(50.milliseconds)
214+
* delay(110.milliseconds)
174215
* }
175-
* }.sample(100.milliseconds)
216+
* }.sample(200.milliseconds)
217+
* ```
218+
* <!--- KNIT example-delay-duration-02.kt -->
219+
*
220+
* produces the following emissions
221+
*
222+
* ```text
223+
* 1, 3, 5, 7, 9
176224
* ```
177-
* produces `1, 3, 5, 7, 9`.
225+
* <!--- TEST -->
178226
*
179227
* Note that the latest element is not emitted if it does not fit into the sampling window.
180228
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
// This file was automatically generated from Delay.kt by Knit tool. Do not edit.
6+
package kotlinx.coroutines.examples.exampleDelay01
7+
8+
import kotlinx.coroutines.*
9+
import kotlinx.coroutines.flow.*
10+
11+
fun main() = runBlocking {
12+
13+
flow {
14+
emit(1)
15+
delay(90)
16+
emit(2)
17+
delay(90)
18+
emit(3)
19+
delay(1010)
20+
emit(4)
21+
delay(1010)
22+
emit(5)
23+
}.debounce(1000)
24+
.toList().joinToString().let { println(it) } }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
// This file was automatically generated from Delay.kt by Knit tool. Do not edit.
6+
package kotlinx.coroutines.examples.exampleDelay02
7+
8+
import kotlinx.coroutines.*
9+
import kotlinx.coroutines.flow.*
10+
11+
fun main() = runBlocking {
12+
13+
flow {
14+
repeat(10) {
15+
emit(it)
16+
delay(110)
17+
}
18+
}.sample(200)
19+
.toList().joinToString().let { println(it) } }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@file:OptIn(ExperimentalTime::class)
2+
/*
3+
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
4+
*/
5+
6+
// This file was automatically generated from Delay.kt by Knit tool. Do not edit.
7+
package kotlinx.coroutines.examples.exampleDelayDuration01
8+
9+
import kotlin.time.*
10+
import kotlinx.coroutines.*
11+
import kotlinx.coroutines.flow.*
12+
13+
fun main() = runBlocking {
14+
15+
flow {
16+
emit(1)
17+
delay(90.milliseconds)
18+
emit(2)
19+
delay(90.milliseconds)
20+
emit(3)
21+
delay(1010.milliseconds)
22+
emit(4)
23+
delay(1010.milliseconds)
24+
emit(5)
25+
}.debounce(1000.milliseconds)
26+
.toList().joinToString().let { println(it) } }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@file:OptIn(ExperimentalTime::class)
2+
/*
3+
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
4+
*/
5+
6+
// This file was automatically generated from Delay.kt by Knit tool. Do not edit.
7+
package kotlinx.coroutines.examples.exampleDelayDuration02
8+
9+
import kotlin.time.*
10+
import kotlinx.coroutines.*
11+
import kotlinx.coroutines.flow.*
12+
13+
fun main() = runBlocking {
14+
15+
flow {
16+
repeat(10) {
17+
emit(it)
18+
delay(110.milliseconds)
19+
}
20+
}.sample(200.milliseconds)
21+
.toList().joinToString().let { println(it) } }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
// This file was automatically generated from Delay.kt by Knit tool. Do not edit.
6+
package kotlinx.coroutines.examples.test
7+
8+
import kotlinx.coroutines.knit.*
9+
import org.junit.Test
10+
11+
class FlowDelayTest {
12+
@Test
13+
fun testExampleDelay01() {
14+
test("ExampleDelay01") { kotlinx.coroutines.examples.exampleDelay01.main() }.verifyLines(
15+
"3, 4, 5"
16+
)
17+
}
18+
19+
@Test
20+
fun testExampleDelayDuration01() {
21+
test("ExampleDelayDuration01") { kotlinx.coroutines.examples.exampleDelayDuration01.main() }.verifyLines(
22+
"3, 4, 5"
23+
)
24+
}
25+
26+
@Test
27+
fun testExampleDelay02() {
28+
test("ExampleDelay02") { kotlinx.coroutines.examples.exampleDelay02.main() }.verifyLines(
29+
"1, 3, 5, 7, 9"
30+
)
31+
}
32+
33+
@Test
34+
fun testExampleDelayDuration02() {
35+
test("ExampleDelayDuration02") { kotlinx.coroutines.examples.exampleDelayDuration02.main() }.verifyLines(
36+
"1, 3, 5, 7, 9"
37+
)
38+
}
39+
}

kotlinx-coroutines-core/jvm/test/guide/test/BasicsGuideTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This file was automatically generated from basics.md by Knit tool. Do not edit.
66
package kotlinx.coroutines.guide.test
77

8+
import kotlinx.coroutines.knit.*
89
import org.junit.Test
910

1011
class BasicsGuideTest {

kotlinx-coroutines-core/jvm/test/guide/test/CancellationGuideTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This file was automatically generated from cancellation-and-timeouts.md by Knit tool. Do not edit.
66
package kotlinx.coroutines.guide.test
77

8+
import kotlinx.coroutines.knit.*
89
import org.junit.Test
910

1011
class CancellationGuideTest {

kotlinx-coroutines-core/jvm/test/guide/test/ChannelsGuideTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This file was automatically generated from channels.md by Knit tool. Do not edit.
66
package kotlinx.coroutines.guide.test
77

8+
import kotlinx.coroutines.knit.*
89
import org.junit.Test
910

1011
class ChannelsGuideTest {

kotlinx-coroutines-core/jvm/test/guide/test/ComposingGuideTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This file was automatically generated from composing-suspending-functions.md by Knit tool. Do not edit.
66
package kotlinx.coroutines.guide.test
77

8+
import kotlinx.coroutines.knit.*
89
import org.junit.Test
910

1011
class ComposingGuideTest {

kotlinx-coroutines-core/jvm/test/guide/test/DispatcherGuideTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This file was automatically generated from coroutine-context-and-dispatchers.md by Knit tool. Do not edit.
66
package kotlinx.coroutines.guide.test
77

8+
import kotlinx.coroutines.knit.*
89
import org.junit.Test
910

1011
class DispatcherGuideTest {

kotlinx-coroutines-core/jvm/test/guide/test/ExceptionsGuideTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This file was automatically generated from exception-handling.md by Knit tool. Do not edit.
66
package kotlinx.coroutines.guide.test
77

8+
import kotlinx.coroutines.knit.*
89
import org.junit.Test
910

1011
class ExceptionsGuideTest {

kotlinx-coroutines-core/jvm/test/guide/test/FlowGuideTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This file was automatically generated from flow.md by Knit tool. Do not edit.
66
package kotlinx.coroutines.guide.test
77

8+
import kotlinx.coroutines.knit.*
89
import org.junit.Test
910

1011
class FlowGuideTest {

kotlinx-coroutines-core/jvm/test/guide/test/SelectGuideTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This file was automatically generated from select-expression.md by Knit tool. Do not edit.
66
package kotlinx.coroutines.guide.test
77

8+
import kotlinx.coroutines.knit.*
89
import org.junit.Test
910

1011
class SelectGuideTest {

kotlinx-coroutines-core/jvm/test/guide/test/SharedStateGuideTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This file was automatically generated from shared-mutable-state-and-concurrency.md by Knit tool. Do not edit.
66
package kotlinx.coroutines.guide.test
77

8+
import kotlinx.coroutines.knit.*
89
import org.junit.Test
910

1011
class SharedStateGuideTest {

0 commit comments

Comments
 (0)