Skip to content

Commit c176901

Browse files
hfhbddkhalanskyjb
authored andcommitted
TestTimeSource: Expose comparable timemarks (#3617)
1 parent 1245d7e commit c176901

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

kotlinx-coroutines-test/api/kotlinx-coroutines-test.api

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public final class kotlinx/coroutines/test/TestCoroutineScheduler : kotlin/corou
6969
public final fun advanceTimeBy-LRDsOJo (J)V
7070
public final fun advanceUntilIdle ()V
7171
public final fun getCurrentTime ()J
72-
public final fun getTimeSource ()Lkotlin/time/TimeSource;
72+
public final fun getTimeSource ()Lkotlin/time/TimeSource$WithComparableMarks;
7373
public final fun runCurrent ()V
7474
}
7575

@@ -121,7 +121,7 @@ public final class kotlinx/coroutines/test/TestScopeKt {
121121
public static final fun advanceTimeBy-HG0u8IE (Lkotlinx/coroutines/test/TestScope;J)V
122122
public static final fun advanceUntilIdle (Lkotlinx/coroutines/test/TestScope;)V
123123
public static final fun getCurrentTime (Lkotlinx/coroutines/test/TestScope;)J
124-
public static final fun getTestTimeSource (Lkotlinx/coroutines/test/TestScope;)Lkotlin/time/TimeSource;
124+
public static final fun getTestTimeSource (Lkotlinx/coroutines/test/TestScope;)Lkotlin/time/TimeSource$WithComparableMarks;
125125
public static final fun runCurrent (Lkotlinx/coroutines/test/TestScope;)V
126126
}
127127

kotlinx-coroutines-test/common/src/TestCoroutineScheduler.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public class TestCoroutineScheduler : AbstractCoroutineContextElement(TestCorout
224224
* Returns the [TimeSource] representation of the virtual time of this scheduler.
225225
*/
226226
@ExperimentalTime
227-
public val timeSource: TimeSource = object : AbstractLongTimeSource(DurationUnit.MILLISECONDS) {
227+
public val timeSource: TimeSource.WithComparableMarks = object : AbstractLongTimeSource(DurationUnit.MILLISECONDS) {
228228
override fun read(): Long = currentTime
229229
}
230230
}

kotlinx-coroutines-test/common/src/TestScope.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public fun TestScope.advanceTimeBy(delayTime: Duration): Unit = testScheduler.ad
136136
*/
137137
@ExperimentalCoroutinesApi
138138
@ExperimentalTime
139-
public val TestScope.testTimeSource: TimeSource get() = testScheduler.timeSource
139+
public val TestScope.testTimeSource: TimeSource.WithComparableMarks get() = testScheduler.timeSource
140140

141141
/**
142142
* Creates a [TestScope].

kotlinx-coroutines-test/common/test/TestCoroutineSchedulerTest.kt

+4
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,14 @@ class TestCoroutineSchedulerTest {
315315
@ExperimentalTime
316316
fun testAdvanceTimeSource() = runTest {
317317
val expected = 1.seconds
318+
val before = testTimeSource.markNow()
318319
val actual = testTimeSource.measureTime {
319320
delay(expected)
320321
}
321322
assertEquals(expected, actual)
323+
val after = testTimeSource.markNow()
324+
assertTrue(before < after)
325+
assertEquals(expected, after - before)
322326
}
323327

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

0 commit comments

Comments
 (0)