Skip to content
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

TestTimeSource: Expose comparable timemarks #3617

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions kotlinx-coroutines-test/api/kotlinx-coroutines-test.api
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public final class kotlinx/coroutines/test/TestCoroutineScheduler : kotlin/corou
public final fun advanceTimeBy (J)V
public final fun advanceUntilIdle ()V
public final fun getCurrentTime ()J
public final fun getTimeSource ()Lkotlin/time/TimeSource;
public final fun getTimeSource ()Lkotlin/time/TimeSource$WithComparableMarks;
public final fun runCurrent ()V
}

Expand Down Expand Up @@ -118,7 +118,7 @@ public final class kotlinx/coroutines/test/TestScopeKt {
public static final fun advanceTimeBy (Lkotlinx/coroutines/test/TestScope;J)V
public static final fun advanceUntilIdle (Lkotlinx/coroutines/test/TestScope;)V
public static final fun getCurrentTime (Lkotlinx/coroutines/test/TestScope;)J
public static final fun getTestTimeSource (Lkotlinx/coroutines/test/TestScope;)Lkotlin/time/TimeSource;
public static final fun getTestTimeSource (Lkotlinx/coroutines/test/TestScope;)Lkotlin/time/TimeSource$WithComparableMarks;
public static final fun runCurrent (Lkotlinx/coroutines/test/TestScope;)V
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public class TestCoroutineScheduler : AbstractCoroutineContextElement(TestCorout
*/
@ExperimentalCoroutinesApi
@ExperimentalTime
public val timeSource: TimeSource = object : AbstractLongTimeSource(DurationUnit.MILLISECONDS) {
public val timeSource: TimeSource.WithComparableMarks = object : AbstractLongTimeSource(DurationUnit.MILLISECONDS) {
override fun read(): Long = currentTime
}
}
Expand Down
2 changes: 1 addition & 1 deletion kotlinx-coroutines-test/common/src/TestScope.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public fun TestScope.advanceTimeBy(delayTimeMillis: Long): Unit = testScheduler.
*/
@ExperimentalCoroutinesApi
@ExperimentalTime
public val TestScope.testTimeSource: TimeSource get() = testScheduler.timeSource
public val TestScope.testTimeSource: TimeSource.WithComparableMarks get() = testScheduler.timeSource

/**
* Creates a [TestScope].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,13 @@ class TestCoroutineSchedulerTest {
@ExperimentalTime
fun testAdvanceTimeSource() = runTest {
val expected = 1.seconds
val before = testTimeSource.markNow()
val actual = testTimeSource.measureTime {
delay(expected)
}
assertEquals(expected, actual)
val after = testTimeSource.markNow()
assertTrue(before < after)
}

private fun forTestDispatchers(block: (TestDispatcher) -> Unit): Unit =
Expand Down