Skip to content

Commit

Permalink
Attempt to make filesystem tests less flaky (#1181)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
swankjesse committed Jan 3, 2023
1 parent 8e559fd commit ae07130
Showing 1 changed file with 11 additions and 2 deletions.
Expand Up @@ -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

Expand Down Expand Up @@ -2356,20 +2357,28 @@ 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)
}

/**
* Returns the latest file system time that could be recorded for an event occurring at this
* 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)
}

/**
Expand Down

0 comments on commit ae07130

Please sign in to comment.