Skip to content

Commit

Permalink
Ensure ChromeTraceTest event timestamps are emitted in correct inverv…
Browse files Browse the repository at this point in the history
…als (#21911)

Fixes #21882 flaky test
  • Loading branch information
WojciechMazur authored Nov 8, 2024
2 parents 9e266e4 + d8ba366 commit 8a3f375
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions compiler/test/dotty/tools/dotc/profile/ChromeTraceTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class ChromeTraceTest:
val testStart = System.nanoTime()
testTraceOutputs{ tracer =>
tracer.traceDurationEventStart(cat = "test1", name = "event1")
LockSupport.parkNanos(2.millis.toNanos)
sleep(2.millis)
tracer.traceDurationEventStart(cat = "test2", name = "event2", colour = "RED", pidSuffix = "pid-suffix")
LockSupport.parkNanos(4.millis.toNanos)
sleep(4.millis)
tracer.traceDurationEventEnd(cat = "test2", name = "event2")
LockSupport.parkNanos(8.millis.toNanos)
sleep(8.millis)
tracer.traceDurationEventEnd(cat = "test1", name = "event1", colour = "RED", pidSuffix = "pid-suffix")
}{
case """{"traceEvents":[""" ::
Expand Down Expand Up @@ -89,5 +89,18 @@ class ChromeTraceTest:
assertTrue(ts4 >= ts3 + 8.millis.toMicros)
case _ => fail("unreachable")
}
}
}

private def sleep(duration: FiniteDuration): Unit = {
// A bit of additional precautions to ensure we don't continue execution to early
// Both LockSuppport and Thread.sleep can return earlier then expected (depending on OS)
var remainingNanos = duration.toNanos
val deadline = System.nanoTime() + remainingNanos
while
remainingNanos = deadline - System.nanoTime()
remainingNanos > 0
do
val millis = NANOSECONDS.toMillis(remainingNanos)
Thread.sleep(millis, (remainingNanos % 1.millis.toNanos).toInt)
}
}

0 comments on commit 8a3f375

Please sign in to comment.