Skip to content

Commit 75e85ea

Browse files
committed
test(time): Publish the TestTicker tick to other threads
The ANR tests advance the clock from the test thread while the watchdog thread reads it, which without volatile is a data race that can leave the watchdog looking at a stale tick forever.
1 parent 9829cc8 commit 75e85ea

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

‎sentry-test-support/src/main/kotlin/io/sentry/time/TestTicker.kt‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import java.util.concurrent.TimeUnit
99
* nanosecond clock is off by a factor of a million and still compiles, whereas `advance(1001,
1010
* MILLISECONDS)` cannot be.
1111
*
12+
* The tick is volatile so that a test thread can advance the clock while the code under test reads
13+
* it from another thread.
14+
*
1215
* Implements both clock guarantees so a test can inject it wherever either is declared. Production
1316
* code must never do this — the whole purpose of the two interfaces is that one object cannot
1417
* honestly promise both.
1518
*/
16-
class TestTicker(private var nanos: Long = 0) : UptimeClock, ElapsedRealtimeClock {
19+
class TestTicker(@Volatile private var nanos: Long = 0) : UptimeClock, ElapsedRealtimeClock {
1720
override fun tickNanos(): Long = nanos
1821

1922
fun advance(amount: Long, unit: TimeUnit) {

0 commit comments

Comments
 (0)