Skip to content

Update Flow.sample KDoc example timings, add tests #2259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions docs/knit.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@

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

test.package=kotlinx.coroutines.guide.test
test.dir=../kotlinx-coroutines-core/jvm/test/guide/test/
test.template=knit.test.template

# Various test validation modes and their corresponding methods from TestUtil
test.mode.=verifyLines
test.mode.STARTS_WITH=verifyLinesStartWith
test.mode.ARBITRARY_TIME=verifyLinesArbitraryTime
test.mode.FLEXIBLE_TIME=verifyLinesFlexibleTime
test.mode.FLEXIBLE_THREAD=verifyLinesFlexibleThread
test.mode.LINES_START_UNORDERED=verifyLinesStartUnordered
test.mode.LINES_START=verifyLinesStart
test.mode.EXCEPTION=verifyExceptions
1 change: 1 addition & 0 deletions docs/knit.test.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// This file was automatically generated from ${file.name} by Knit tool. Do not edit.
package ${test.package}

import kotlinx.coroutines.knit.*
import org.junit.Test

class ${test.name} {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ kotlin_version=1.4.0
# Dependencies
junit_version=4.12
atomicfu_version=0.14.4
knit_version=0.1.3
knit_version=0.2.0
html_version=0.6.8
lincheck_version=2.7.1
dokka_version=0.9.16-rdev-2-mpp-hacks
Expand Down
16 changes: 16 additions & 0 deletions knit.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
#

knit.include=docs/knit.code.include
test.template=docs/knit.test.template

# Various test validation modes and their corresponding methods from TestUtil
test.mode.=verifyLines
test.mode.STARTS_WITH=verifyLinesStartWith
test.mode.ARBITRARY_TIME=verifyLinesArbitraryTime
test.mode.FLEXIBLE_TIME=verifyLinesFlexibleTime
test.mode.FLEXIBLE_THREAD=verifyLinesFlexibleThread
test.mode.LINES_START_UNORDERED=verifyLinesStartUnordered
test.mode.LINES_START=verifyLinesStart
test.mode.EXCEPTION=verifyExceptions
72 changes: 60 additions & 12 deletions kotlinx-coroutines-core/common/src/flow/operators/Delay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,30 @@ import kotlinx.coroutines.selects.*
import kotlin.jvm.*
import kotlin.time.*

/* Scaffolding for Knit code examples
<!--- TEST_NAME FlowDelayTest -->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, would be nice to hide it in an external file

Copy link
Contributor Author

@elizarov elizarov Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That would be a nice addition. Created Kotlin/kotlinx-knit#18 and Kotlin/kotlinx-knit#19

<!--- PREFIX .*-duration-.*
@file:OptIn(ExperimentalTime::class)
----- INCLUDE .*-duration-.*
import kotlin.time.*
----- INCLUDE .*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
----- SUFFIX .*
.toList().joinToString().let { println(it) } }
-->
*/

/**
* Returns a flow that mirrors the original flow, but filters out values
* that are followed by the newer values within the given [timeout][timeoutMillis].
* The latest value is always emitted.
*
* Example:
* ```
*
* ```kotlin
* flow {
* emit(1)
* delay(90)
Expand All @@ -33,7 +50,14 @@ import kotlin.time.*
* emit(5)
* }.debounce(1000)
* ```
* produces `3, 4, 5`.
* <!--- KNIT example-delay-01.kt -->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if these tests could be numbered automatically without interfering with the source.
In general, I do no mind having Knit-specific directives in our markdown, but this documentation will likely be read from the IDEA rather than from a webpage with processed markdown, so reducing the number of MD tags would be nice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are automatically numbered. When writing those directives you can use any number or even just a placeholder like <!--- KNIT example-delay-num.kt --> and when you run gradlew knit it will renumber them sequentially and will update the corresponding source files.

*
* produces the following emissions
*
* ```text
* 3, 4, 5
* ```
* <!--- TEST -->
*
* Note that the resulting flow does not emit anything as long as the original flow emits
* items faster than every [timeoutMillis] milliseconds.
Expand Down Expand Up @@ -77,7 +101,8 @@ public fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T> {
* The latest value is always emitted.
*
* Example:
* ```
*
* ```kotlin
* flow {
* emit(1)
* delay(90.milliseconds)
Expand All @@ -90,7 +115,14 @@ public fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T> {
* emit(5)
* }.debounce(1000.milliseconds)
* ```
* produces `3, 4, 5`.
* <!--- KNIT example-delay-duration-01.kt -->
*
* produces the following emissions
*
* ```text
* 3, 4, 5
* ```
* <!--- TEST -->
*
* Note that the resulting flow does not emit anything as long as the original flow emits
* items faster than every [timeout] milliseconds.
Expand All @@ -103,15 +135,23 @@ public fun <T> Flow<T>.debounce(timeout: Duration): Flow<T> = debounce(timeout.t
* Returns a flow that emits only the latest value emitted by the original flow during the given sampling [period][periodMillis].
*
* Example:
* ```
*
* ```kotlin
* flow {
* repeat(10) {
* emit(it)
* delay(50)
* delay(110)
* }
* }.sample(100)
* }.sample(200)
* ```
* <!--- KNIT example-delay-02.kt -->
*
* produces the following emissions
*
* ```text
* 1, 3, 5, 7, 9
* ```
* produces `1, 3, 5, 7, 9`.
* <!--- TEST -->
*
* Note that the latest element is not emitted if it does not fit into the sampling window.
*/
Expand Down Expand Up @@ -166,15 +206,23 @@ internal fun CoroutineScope.fixedPeriodTicker(delayMillis: Long, initialDelayMil
* Returns a flow that emits only the latest value emitted by the original flow during the given sampling [period].
*
* Example:
* ```
*
* ```kotlin
* flow {
* repeat(10) {
* emit(it)
* delay(50.milliseconds)
* delay(110.milliseconds)
* }
* }.sample(100.milliseconds)
* }.sample(200.milliseconds)
* ```
* <!--- KNIT example-delay-duration-02.kt -->
*
* produces the following emissions
*
* ```text
* 1, 3, 5, 7, 9
* ```
* produces `1, 3, 5, 7, 9`.
* <!--- TEST -->
*
* Note that the latest element is not emitted if it does not fit into the sampling window.
*/
Expand Down
24 changes: 24 additions & 0 deletions kotlinx-coroutines-core/jvm/test/examples/example-delay-01.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

// This file was automatically generated from Delay.kt by Knit tool. Do not edit.
package kotlinx.coroutines.examples.exampleDelay01

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {

flow {
emit(1)
delay(90)
emit(2)
delay(90)
emit(3)
delay(1010)
emit(4)
delay(1010)
emit(5)
}.debounce(1000)
.toList().joinToString().let { println(it) } }
19 changes: 19 additions & 0 deletions kotlinx-coroutines-core/jvm/test/examples/example-delay-02.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

// This file was automatically generated from Delay.kt by Knit tool. Do not edit.
package kotlinx.coroutines.examples.exampleDelay02

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {

flow {
repeat(10) {
emit(it)
delay(110)
}
}.sample(200)
.toList().joinToString().let { println(it) } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@file:OptIn(ExperimentalTime::class)
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

// This file was automatically generated from Delay.kt by Knit tool. Do not edit.
package kotlinx.coroutines.examples.exampleDelayDuration01

import kotlin.time.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {

flow {
emit(1)
delay(90.milliseconds)
emit(2)
delay(90.milliseconds)
emit(3)
delay(1010.milliseconds)
emit(4)
delay(1010.milliseconds)
emit(5)
}.debounce(1000.milliseconds)
.toList().joinToString().let { println(it) } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@file:OptIn(ExperimentalTime::class)
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

// This file was automatically generated from Delay.kt by Knit tool. Do not edit.
package kotlinx.coroutines.examples.exampleDelayDuration02

import kotlin.time.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {

flow {
repeat(10) {
emit(it)
delay(110.milliseconds)
}
}.sample(200.milliseconds)
.toList().joinToString().let { println(it) } }
39 changes: 39 additions & 0 deletions kotlinx-coroutines-core/jvm/test/examples/test/FlowDelayTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

// This file was automatically generated from Delay.kt by Knit tool. Do not edit.
package kotlinx.coroutines.examples.test

import kotlinx.coroutines.knit.*
import org.junit.Test

class FlowDelayTest {
@Test
fun testExampleDelay01() {
test("ExampleDelay01") { kotlinx.coroutines.examples.exampleDelay01.main() }.verifyLines(
"3, 4, 5"
)
}

@Test
fun testExampleDelayDuration01() {
test("ExampleDelayDuration01") { kotlinx.coroutines.examples.exampleDelayDuration01.main() }.verifyLines(
"3, 4, 5"
)
}

@Test
fun testExampleDelay02() {
test("ExampleDelay02") { kotlinx.coroutines.examples.exampleDelay02.main() }.verifyLines(
"1, 3, 5, 7, 9"
)
}

@Test
fun testExampleDelayDuration02() {
test("ExampleDelayDuration02") { kotlinx.coroutines.examples.exampleDelayDuration02.main() }.verifyLines(
"1, 3, 5, 7, 9"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// This file was automatically generated from basics.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.test

import kotlinx.coroutines.knit.*
import org.junit.Test

class BasicsGuideTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// This file was automatically generated from cancellation-and-timeouts.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.test

import kotlinx.coroutines.knit.*
import org.junit.Test

class CancellationGuideTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// This file was automatically generated from channels.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.test

import kotlinx.coroutines.knit.*
import org.junit.Test

class ChannelsGuideTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// This file was automatically generated from composing-suspending-functions.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.test

import kotlinx.coroutines.knit.*
import org.junit.Test

class ComposingGuideTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// This file was automatically generated from coroutine-context-and-dispatchers.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.test

import kotlinx.coroutines.knit.*
import org.junit.Test

class DispatcherGuideTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// This file was automatically generated from exception-handling.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.test

import kotlinx.coroutines.knit.*
import org.junit.Test

class ExceptionsGuideTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// This file was automatically generated from flow.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.test

import kotlinx.coroutines.knit.*
import org.junit.Test

class FlowGuideTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// This file was automatically generated from select-expression.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.test

import kotlinx.coroutines.knit.*
import org.junit.Test

class SelectGuideTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// This file was automatically generated from shared-mutable-state-and-concurrency.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.test

import kotlinx.coroutines.knit.*
import org.junit.Test

class SharedStateGuideTest {
Expand Down
Loading