@@ -6,7 +6,6 @@ import kotlinx.coroutines.test.TestCoroutineScheduler
6
6
import kotlinx.datetime.Clock
7
7
import kotlinx.datetime.Instant
8
8
import opensavvy.prepared.suite.Time
9
- import opensavvy.prepared.suite.advanceByMillis
10
9
import opensavvy.prepared.suite.nowMillis
11
10
12
11
@ExperimentalCoroutinesApi
@@ -53,13 +52,33 @@ val Time.now: Instant
53
52
/* *
54
53
* Advances the virtual time until it reaches [instant].
55
54
*
55
+ * ### Comparison with delayUntil
56
+ *
57
+ * This function is identical in behavior to [delayUntil].
58
+ * It exists because tests often read better when using it to set the initial date:
59
+ * ```kotlin
60
+ * test("Some test") {
61
+ * // Given:
62
+ * time.set(Instant.parse("2024-02-13T21:32:41Z"))
63
+ *
64
+ * // When:
65
+ * // …
66
+ * delayUntil(Instant.parse("2024-02-13T21:35:01Z"))
67
+ * // …
68
+ *
69
+ * // Then:
70
+ * // …
71
+ * }
72
+ * ```
73
+ *
74
+ * We recommend using [set] to set the initial date at the very start of a test, and using [delayUntil] inside the test
75
+ * logic.
76
+ *
56
77
* It is not possible to set the time to a date in the past.
57
78
*/
58
79
@ExperimentalCoroutinesApi
59
- fun Time.set (instant : Instant ) {
60
- val diff = instant.toEpochMilliseconds() - nowMillis
61
- require(diff >= 0 ) { " Cannot advance to $instant , which is in the past of the current virtual time, $now " }
62
- advanceByMillis(diff)
80
+ suspend fun Time.set (instant : Instant ) {
81
+ delayUntil(instant)
63
82
}
64
83
65
84
/* *
@@ -77,17 +96,42 @@ fun Time.set(instant: Instant) {
77
96
* }
78
97
* ```
79
98
*
99
+ * ### Comparison with delayUntil
100
+ *
101
+ * This function is identical in behavior to [delayUntil].
102
+ * It exists because tests often read better when using it to set the initial date:
103
+ * ```kotlin
104
+ * test("Some test") {
105
+ * // Given:
106
+ * time.set("2024-02-13T21:32:41Z")
107
+ *
108
+ * // When:
109
+ * // …
110
+ * delayUntil("2024-02-13T21:35:01Z")
111
+ * // …
112
+ *
113
+ * // Then:
114
+ * // …
115
+ * }
116
+ * ```
117
+ *
118
+ * We recommend using [set] to set the initial date at the very start of a test, and using [delayUntil] inside the test
119
+ * logic.
120
+ *
80
121
* @see now Access the current time
81
122
* @see delay Wait for some duration
82
123
* @see delayUntil Wait for a specific time
83
124
*/
84
125
@ExperimentalCoroutinesApi
85
- fun Time.set (isoString : String ) {
126
+ suspend fun Time.set (isoString : String ) {
86
127
set(Instant .parse(isoString))
87
128
}
88
129
89
130
/* *
90
131
* Delays until the virtual time reaches [instant], executing all enqueued tasks in order.
132
+ *
133
+ * `delayUntil` is useful to artificially trigger time-dependent algorithms.
134
+ * To set the initial time at the start of the test, use [set].
91
135
*/
92
136
@ExperimentalCoroutinesApi
93
137
suspend fun Time.delayUntil (instant : Instant ) {
@@ -99,6 +143,9 @@ suspend fun Time.delayUntil(instant: Instant) {
99
143
/* *
100
144
* Delays until the virtual time reaches [isoString], formatted as an ISO 8601 timestamp, executing all enqueued tasks in order.
101
145
*
146
+ * `delayUntil` is useful to artificially trigger time-dependent algorithms.
147
+ * To set the initial time at the start of the test, use [set].
148
+ *
102
149
* @see set Set the current time
103
150
* @see now Access the current time
104
151
*/
0 commit comments