Skip to content

Commit 3528e52

Browse files
committed
Add delay before writing file to disk in tests
Depending on the system, mtime resolution might not be perfectly accurate. Particularly containers appear to have imprecise mtimes. Wait a short time period before writing the new file to avoid situations like the following: - We index a source file and the unit receives a time stamp and wait for indexing to finish - We modify the source file but so quickly after the unit has been modified that the updated source file receives the same mtime as the unit file - We now assume that the we have an up-to-date index for this source file even though we do not. Waiting 10ms appears to be enough to avoid this situation on the systems we care about. rdar://147811044
1 parent c804197 commit 3528e52

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Sources/SKTestSupport/String+writeWithRetry.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ extension String {
2020
/// access to the file but releases it soon after. Retry to save the file if this happens. This matches what a user
2121
/// would do.
2222
package func writeWithRetry(to url: URL) async throws {
23+
// Depending on the system, mtime resolution might not be perfectly accurate. Particularly containers appear to have
24+
// imprecise mtimes.
25+
// Wait a short time period before writing the new file to avoid situations like the following:
26+
// - We index a source file and the unit receives a time stamp and wait for indexing to finish
27+
// - We modify the source file but so quickly after the unit has been modified that the updated source file
28+
// receives the same mtime as the unit file
29+
// - We now assume that the we have an up-to-date index for this source file even though we do not.
30+
//
31+
// Waiting 10ms appears to be enough to avoid this situation on the systems we care about.
32+
//
33+
// Do determine the mtime accuracy on a system, run the following bash commands and look at the time gaps between
34+
// the time stamps
35+
// ```
36+
// mkdir /tmp/dir
37+
// for x in $(seq 1 1000); do touch /tmp/dir/$x; done
38+
// for x in /tmp/dir/*; do stat $x; done | grep Modify | sort | uniq
39+
// ```
40+
try await Task.sleep(for: .milliseconds(10))
41+
2342
#if os(Windows)
2443
try await repeatUntilExpectedResult(timeout: .seconds(10), sleepInterval: .milliseconds(200)) {
2544
do {

0 commit comments

Comments
 (0)