Skip to content

Commit

Permalink
TestTimeSource: Expose comparable timemarks (#3617)
Browse files Browse the repository at this point in the history
  • Loading branch information
hfhbd authored and dkhalanskyjb committed Feb 25, 2023
1 parent 1245d7e commit c176901
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
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 @@ -69,7 +69,7 @@ public final class kotlinx/coroutines/test/TestCoroutineScheduler : kotlin/corou
public final fun advanceTimeBy-LRDsOJo (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 @@ -121,7 +121,7 @@ public final class kotlinx/coroutines/test/TestScopeKt {
public static final fun advanceTimeBy-HG0u8IE (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 @@ -224,7 +224,7 @@ public class TestCoroutineScheduler : AbstractCoroutineContextElement(TestCorout
* Returns the [TimeSource] representation of the virtual time of this scheduler.
*/
@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 @@ -136,7 +136,7 @@ public fun TestScope.advanceTimeBy(delayTime: Duration): Unit = testScheduler.ad
*/
@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 @@ -315,10 +315,14 @@ 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)
assertEquals(expected, after - before)
}

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

0 comments on commit c176901

Please sign in to comment.