Skip to content

Commit 97913a8

Browse files
committed
test(time): Publish the TestMonotonicClock 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 0d1d4c9 commit 97913a8

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import java.util.concurrent.TimeUnit
88
* Advancing by an amount *and a unit* is the point: a stubbed `thenReturn(1001)` against a
99
* nanosecond clock is off by a factor of a million and still compiles, whereas `advance(1001,
1010
* MILLISECONDS)` cannot be.
11+
*
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.
1114
*/
12-
class TestMonotonicClock(private var nanos: Long = 0) : MonotonicClock {
15+
class TestMonotonicClock(@Volatile private var nanos: Long = 0) : MonotonicClock {
1316
override fun tickNanos(): Long = nanos
1417

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

0 commit comments

Comments
 (0)