Skip to content

Commit 7afb62e

Browse files
committed
Use timeSource in GuideTest to increase stability of cancel tests, reknit
1 parent 5557245 commit 7afb62e

File tree

7 files changed

+12
-8
lines changed

7 files changed

+12
-8
lines changed

core/kotlinx-coroutines-core/src/TimeSource.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package kotlinx.coroutines.experimental
77
import java.util.concurrent.locks.LockSupport
88

99
internal interface TimeSource {
10+
fun currentTimeMillis(): Long
1011
fun nanoTime(): Long
1112
fun trackTask(block: Runnable): Runnable
1213
fun unTrackTask()
@@ -17,6 +18,7 @@ internal interface TimeSource {
1718
}
1819

1920
internal object DefaultTimeSource : TimeSource {
21+
override fun currentTimeMillis(): Long = System.currentTimeMillis()
2022
override fun nanoTime(): Long = System.nanoTime()
2123
override fun trackTask(block: Runnable): Runnable = block
2224
override fun unTrackTask() {}

core/kotlinx-coroutines-core/test/guide/example-cancel-02.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ package kotlinx.coroutines.experimental.guide.cancel02
88
import kotlinx.coroutines.experimental.*
99

1010
fun main(args: Array<String>) = runBlocking<Unit> {
11-
val startTime = System.currentTimeMillis()
11+
val startTime = timeSource.currentTimeMillis()
1212
val job = launch {
1313
var nextPrintTime = startTime
1414
var i = 0
1515
while (i < 5) { // computation loop, just wastes CPU
1616
// print a message twice a second
17-
if (System.currentTimeMillis() >= nextPrintTime) {
17+
if (timeSource.currentTimeMillis() >= nextPrintTime) {
1818
println("I'm sleeping ${i++} ...")
1919
nextPrintTime += 500L
2020
}

core/kotlinx-coroutines-core/test/guide/example-cancel-03.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ package kotlinx.coroutines.experimental.guide.cancel03
88
import kotlinx.coroutines.experimental.*
99

1010
fun main(args: Array<String>) = runBlocking<Unit> {
11-
val startTime = System.currentTimeMillis()
11+
val startTime = timeSource.currentTimeMillis()
1212
val job = launch {
1313
var nextPrintTime = startTime
1414
var i = 0
1515
while (isActive) { // cancellable computation loop
1616
// print a message twice a second
17-
if (System.currentTimeMillis() >= nextPrintTime) {
17+
if (timeSource.currentTimeMillis() >= nextPrintTime) {
1818
println("I'm sleeping ${i++} ...")
1919
nextPrintTime += 500L
2020
}

core/kotlinx-coroutines-core/test/guide/example-channel-10.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import kotlinx.coroutines.experimental.channels.*
1111
fun main(args: Array<String>) = runBlocking<Unit> {
1212
val tickerChannel = ticker(delay = 100, initialDelay = 0) // create ticker channel
1313
var nextElement = withTimeoutOrNull(1) { tickerChannel.receive() }
14-
println("Initial element is available immediately: $nextElement") // Initial delay hasn't passed yet
14+
println("Initial element is available immediately: $nextElement") // initial delay hasn't passed yet
1515

16-
nextElement = withTimeoutOrNull(50) { tickerChannel.receive() } // All subsequent elements has 100ms delay
16+
nextElement = withTimeoutOrNull(50) { tickerChannel.receive() } // all subsequent elements has 100ms delay
1717
println("Next element is not ready in 50 ms: $nextElement")
1818

1919
nextElement = withTimeoutOrNull(60) { tickerChannel.receive() }

core/kotlinx-coroutines-core/test/guide/test/TestUtil.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ private class TestTimeSource(
130130

131131
private val threads = ConcurrentHashMap<Thread, ThreadStatus>()
132132

133+
override fun currentTimeMillis(): Long = TimeUnit.NANOSECONDS.toMillis(time)
133134
override fun nanoTime(): Long = time
134135

135136
@Synchronized

knit/src/Knit.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ fun knit(markdownFileName: String): Boolean {
214214
outLines += line
215215
}
216216
}
217-
outLines += codeLines
217+
for (code in codeLines) {
218+
outLines += code.replace("System.currentTimeMillis()", "timeSource.currentTimeMillis()")
219+
}
218220
codeLines.clear()
219221
writeLinesIfNeeded(file, outLines)
220222
}

ui/coroutines-guide-ui.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,6 @@ After delay
687687
[Job.cancel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/cancel.html
688688
[withContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/with-context.html
689689
[CommonPool]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-common-pool/index.html
690-
[NonCancellable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-non-cancellable/index.html
691690
[CoroutineStart]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-start/index.html
692691
[async]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/async.html
693692
[CoroutineStart.UNDISPATCHED]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-start/-u-n-d-i-s-p-a-t-c-h-e-d.html

0 commit comments

Comments
 (0)