Delay Can Take Longer than requested

delay in kotlin can take longer to come back to co-routine that suspended itself using delay than the requested amount.

For example in one of tests we had the following:

  override val eventHandler: suspend (rawEvent: Event) -> Unit
    get() = { rawEvent: Event ->
      val actualDuration =
        measureTime {
          // In the test instance in question delayOnEachEvent was 1.millisecond
          if (delayOnEachEvent != null) {
            delay(delayOnEachEvent)
          }
        }

      //...
    }

While at first glance we would expect this to delay for 1 millisecond and come back in reality teh actualDuration of 1K+ events that went through this function looked as follows:

{
  "average": "PT0.007765334S",
  "p50": "PT0.008899958S",
  "p99": "PT0.009027625S",
  "min": "PT0.001121500S",
  "max": "PT0.009152292S",
  "count": 1288,
}

Average was 7.7 milliseconds, and the P50 was 8.8 milliseconds. This was running under Jenkins on mac-mini-M4.