Skip to content

Commit d8ba366

Browse files
committed
Ensure ChromeTraceTest event timestamps are emitted in correct invervals
1 parent 63849a0 commit d8ba366

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Diff for: compiler/test/dotty/tools/dotc/profile/ChromeTraceTest.scala

+17-4
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ class ChromeTraceTest:
5454
val testStart = System.nanoTime()
5555
testTraceOutputs{ tracer =>
5656
tracer.traceDurationEventStart(cat = "test1", name = "event1")
57-
LockSupport.parkNanos(2.millis.toNanos)
57+
sleep(2.millis)
5858
tracer.traceDurationEventStart(cat = "test2", name = "event2", colour = "RED", pidSuffix = "pid-suffix")
59-
LockSupport.parkNanos(4.millis.toNanos)
59+
sleep(4.millis)
6060
tracer.traceDurationEventEnd(cat = "test2", name = "event2")
61-
LockSupport.parkNanos(8.millis.toNanos)
61+
sleep(8.millis)
6262
tracer.traceDurationEventEnd(cat = "test1", name = "event1", colour = "RED", pidSuffix = "pid-suffix")
6363
}{
6464
case """{"traceEvents":[""" ::
@@ -89,5 +89,18 @@ class ChromeTraceTest:
8989
assertTrue(ts4 >= ts3 + 8.millis.toMicros)
9090
case _ => fail("unreachable")
9191
}
92+
}
93+
}
94+
95+
private def sleep(duration: FiniteDuration): Unit = {
96+
// A bit of additional precautions to ensure we don't continue execution to early
97+
// Both LockSuppport and Thread.sleep can return earlier then expected (depending on OS)
98+
var remainingNanos = duration.toNanos
99+
val deadline = System.nanoTime() + remainingNanos
100+
while
101+
remainingNanos = deadline - System.nanoTime()
102+
remainingNanos > 0
103+
do
104+
val millis = NANOSECONDS.toMillis(remainingNanos)
105+
Thread.sleep(millis, (remainingNanos % 1.millis.toNanos).toInt)
92106
}
93-
}

0 commit comments

Comments
 (0)