@@ -61,34 +61,27 @@ class LockTests: XCTestCase {
61
61
}
62
62
63
63
func testReadWriteFileLock( ) throws {
64
- try XCTSkipIf ( true , " fails spuriously if reader thread beats first writer rdar://78461378 " )
65
64
try withTemporaryDirectory { tempDir in
66
65
let fileA = tempDir. appending ( component: " fileA " )
67
66
let fileB = tempDir. appending ( component: " fileB " )
68
67
68
+ // write initial value, since reader may start before writers and files would not exist
69
+ try localFileSystem. writeFileContents ( fileA, bytes: " 0 " )
70
+ try localFileSystem. writeFileContents ( fileB, bytes: " 0 " )
71
+
69
72
let writerThreads = ( 0 ..< 100 ) . map { _ in
70
73
return Thread {
71
74
let lock = FileLock ( name: " foo " , cachePath: tempDir)
72
75
try ! lock. withLock ( type: . exclusive) {
73
- // Get thr current contents of the file if any.
74
- let valueA : Int
75
- if localFileSystem. exists ( fileA) {
76
- valueA = Int ( try localFileSystem. readFileContents ( fileA) . description) ?? 0
77
- } else {
78
- valueA = 0
79
- }
76
+ // Get the current contents of the file if any.
77
+ let valueA = Int ( try localFileSystem. readFileContents ( fileA) . description) !
80
78
// Sum and write back to file.
81
79
try localFileSystem. writeFileContents ( fileA, bytes: ByteString ( encodingAsUTF8: String ( valueA + 1 ) ) )
82
80
83
81
Thread . yield ( )
84
82
85
- // Get thr current contents of the file if any.
86
- let valueB : Int
87
- if localFileSystem. exists ( fileB) {
88
- valueB = Int ( try localFileSystem. readFileContents ( fileB) . description) ?? 0
89
- } else {
90
- valueB = 0
91
- }
83
+ // Get the current contents of the file if any.
84
+ let valueB = Int ( try localFileSystem. readFileContents ( fileB) . description) !
92
85
// Sum and write back to file.
93
86
try localFileSystem. writeFileContents ( fileB, bytes: ByteString ( encodingAsUTF8: String ( valueB + 1 ) ) )
94
87
}
0 commit comments