From ae071302713321a983cd03f5d41041bfb60a27c3 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Tue, 3 Jan 2023 11:37:49 -0500 Subject: [PATCH] Attempt to make filesystem tests less flaky (#1181) * Attempt to make filesystem tests less flaky I've been observing failures like this: expected 2022-12-31T16:00:40.999Z in 2022-12-31T16:00:41Z..2022-12-31T16:00:43Z (relaxed from 2022-12-31T16:00:41.002314Z..2022-12-31T16:00:41.002365Z) * Spotless --- .../kotlin/okio/AbstractFileSystemTest.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/okio-testing-support/src/commonMain/kotlin/okio/AbstractFileSystemTest.kt b/okio-testing-support/src/commonMain/kotlin/okio/AbstractFileSystemTest.kt index d029529d6f..4cddd44641 100644 --- a/okio-testing-support/src/commonMain/kotlin/okio/AbstractFileSystemTest.kt +++ b/okio-testing-support/src/commonMain/kotlin/okio/AbstractFileSystemTest.kt @@ -31,6 +31,7 @@ import kotlin.test.assertFalse import kotlin.test.assertNull import kotlin.test.assertTrue import kotlin.test.fail +import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.seconds @@ -2356,9 +2357,13 @@ abstract class AbstractFileSystemTest( * Returns the earliest file system time that could be recorded for an event occurring at this * instant. This truncates fractional seconds because most host file systems do not use precise * timestamps for file metadata. + * + * It also pads the result by 200 milliseconds because the host and file system may use different + * clocks, allowing the time on the CPU to drift ahead of the time on the file system. */ private fun Instant.minFileSystemTime(): Instant { - return Instant.fromEpochSeconds(epochSeconds) + val paddedInstant = minus(200.milliseconds) + return Instant.fromEpochSeconds(paddedInstant.epochSeconds) } /** @@ -2366,10 +2371,14 @@ abstract class AbstractFileSystemTest( * instant. This adds 2 seconds and truncates fractional seconds because file systems may defer * assigning the timestamp. * + * It also pads the result by 200 milliseconds because the host and file system may use different + * clocks, allowing the time on the CPU to drift behind the time on the file system. + * * https://docs.microsoft.com/en-us/windows/win32/sysinfo/file-times */ private fun Instant.maxFileSystemTime(): Instant { - return Instant.fromEpochSeconds(plus(2.seconds).epochSeconds) + val paddedInstant = plus(200.milliseconds) + return Instant.fromEpochSeconds(paddedInstant.plus(2.seconds).epochSeconds) } /**