Skip to content

Commit a91e9b9

Browse files
committed
restore disabled FileSystemTests
motivation: better test coverage changes: * refactor the common part of testInMemoryFileSystemFileLock, testLocalFileSystemFileLock, testRerootedFileSystemViewFileLock * resove the potential reader race by making sure file have content prior to starting the threads * enable the above disabled tests in FileSystemTests rdar://71560894
1 parent 4dcf37d commit a91e9b9

File tree

1 file changed

+15
-108
lines changed

1 file changed

+15
-108
lines changed

Tests/TSCBasicTests/FileSystemTests.swift

Lines changed: 15 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -749,9 +749,6 @@ class FileSystemTests: XCTestCase {
749749
}
750750

751751
func testInMemoryFileSystemFileLock() throws {
752-
// Disabled until rdar://71560894 is fixed.
753-
try XCTSkipIf(true)
754-
755752
let fs = InMemoryFileSystem()
756753
let path = AbsolutePath("/")
757754
try fs.createDirectory(path)
@@ -760,118 +757,20 @@ class FileSystemTests: XCTestCase {
760757
let fileB = path.appending(component: "fileB")
761758
let lockFile = path.appending(component: "lockfile")
762759

763-
let writerThreads = (0..<100).map { _ in
764-
return Thread {
765-
try! fs.withLock(on: lockFile, type: .exclusive) {
766-
// Get thr current contents of the file if any.
767-
let valueA: Int
768-
if fs.exists(fileA) {
769-
valueA = Int(try fs.readFileContents(fileA).description) ?? 0
770-
} else {
771-
valueA = 0
772-
}
773-
// Sum and write back to file.
774-
try fs.writeFileContents(fileA, bytes: ByteString(encodingAsUTF8: String(valueA + 1)))
775-
776-
Thread.yield()
777-
778-
// Get thr current contents of the file if any.
779-
let valueB: Int
780-
if fs.exists(fileB) {
781-
valueB = Int(try fs.readFileContents(fileB).description) ?? 0
782-
} else {
783-
valueB = 0
784-
}
785-
// Sum and write back to file.
786-
try fs.writeFileContents(fileB, bytes: ByteString(encodingAsUTF8: String(valueB + 1)))
787-
}
788-
}
789-
}
790-
791-
let readerThreads = (0..<20).map { _ in
792-
return Thread {
793-
try! fs.withLock(on: lockFile, type: .shared) {
794-
try XCTAssertEqual(fs.readFileContents(fileA), fs.readFileContents(fileB))
795-
796-
Thread.yield()
797-
798-
try XCTAssertEqual(fs.readFileContents(fileA), fs.readFileContents(fileB))
799-
}
800-
}
801-
}
802-
803-
writerThreads.forEach { $0.start() }
804-
readerThreads.forEach { $0.start() }
805-
writerThreads.forEach { $0.join() }
806-
readerThreads.forEach { $0.join() }
807-
808-
try XCTAssertEqual(fs.readFileContents(fileA), "100")
809-
try XCTAssertEqual(fs.readFileContents(fileB), "100")
760+
try _testFileSystemFileLock(fileSystem: fs, fileA: fileA, fileB: fileB, lockFile: lockFile)
810761
}
811762

812763
func testLocalFileSystemFileLock() throws {
813-
// Disabled until rdar://71560894 is fixed.
814-
try XCTSkipIf(true)
815-
816764
try withTemporaryDirectory { tempDir in
817765
let fileA = tempDir.appending(component: "fileA")
818766
let fileB = tempDir.appending(component: "fileB")
819767
let lockFile = tempDir.appending(component: "lockfile")
820768

821-
let writerThreads = (0..<100).map { _ in
822-
return Thread {
823-
try! localFileSystem.withLock(on: lockFile, type: .exclusive) {
824-
// Get thr current contents of the file if any.
825-
let valueA: Int
826-
if localFileSystem.exists(fileA) {
827-
valueA = Int(try localFileSystem.readFileContents(fileA).description) ?? 0
828-
} else {
829-
valueA = 0
830-
}
831-
// Sum and write back to file.
832-
try localFileSystem.writeFileContents(fileA, bytes: ByteString(encodingAsUTF8: String(valueA + 1)))
833-
834-
Thread.yield()
835-
836-
// Get thr current contents of the file if any.
837-
let valueB: Int
838-
if localFileSystem.exists(fileB) {
839-
valueB = Int(try localFileSystem.readFileContents(fileB).description) ?? 0
840-
} else {
841-
valueB = 0
842-
}
843-
// Sum and write back to file.
844-
try localFileSystem.writeFileContents(fileB, bytes: ByteString(encodingAsUTF8: String(valueB + 1)))
845-
}
846-
}
847-
}
848-
849-
let readerThreads = (0..<20).map { _ in
850-
return Thread {
851-
try! localFileSystem.withLock(on: lockFile, type: .shared) {
852-
try XCTAssertEqual(localFileSystem.readFileContents(fileA), localFileSystem.readFileContents(fileB))
853-
854-
Thread.yield()
855-
856-
try XCTAssertEqual(localFileSystem.readFileContents(fileA), localFileSystem.readFileContents(fileB))
857-
}
858-
}
859-
}
860-
861-
writerThreads.forEach { $0.start() }
862-
readerThreads.forEach { $0.start() }
863-
writerThreads.forEach { $0.join() }
864-
readerThreads.forEach { $0.join() }
865-
866-
try XCTAssertEqual(localFileSystem.readFileContents(fileA), "100")
867-
try XCTAssertEqual(localFileSystem.readFileContents(fileB), "100")
769+
try _testFileSystemFileLock(fileSystem: localFileSystem, fileA: fileA, fileB: fileB, lockFile: lockFile)
868770
}
869771
}
870772

871773
func testRerootedFileSystemViewFileLock() throws {
872-
// Disabled until rdar://71560894 is fixed.
873-
try XCTSkipIf(true)
874-
875774
let inMemoryFS = InMemoryFileSystem()
876775
let rootPath = AbsolutePath("/tmp")
877776
try inMemoryFS.createDirectory(rootPath)
@@ -884,22 +783,31 @@ class FileSystemTests: XCTestCase {
884783
let fileB = path.appending(component: "fileB")
885784
let lockFile = path.appending(component: "lockfile")
886785

786+
try _testFileSystemFileLock(fileSystem: fs, fileA: fileA, fileB: fileB, lockFile: lockFile)
787+
}
788+
789+
private func _testFileSystemFileLock(fileSystem fs: FileSystem, fileA: AbsolutePath, fileB: AbsolutePath, lockFile: AbsolutePath) throws {
790+
791+
// write initial value, since reader may start before writers and files would not exist
792+
try fs.writeFileContents(fileA, bytes: [0])
793+
try fs.writeFileContents(fileB, bytes: [0])
794+
887795
let writerThreads = (0..<100).map { _ in
888796
return Thread {
889797
try! fs.withLock(on: lockFile, type: .exclusive) {
890-
// Get thr current contents of the file if any.
798+
// Get the current contents of the file if any.
891799
let valueA: Int
892800
if fs.exists(fileA) {
893-
valueA = Int(try! fs.readFileContents(fileA).description) ?? 0
801+
valueA = Int(try fs.readFileContents(fileA).description) ?? 0
894802
} else {
895803
valueA = 0
896804
}
897805
// Sum and write back to file.
898-
try! fs.writeFileContents(fileA, bytes: ByteString(encodingAsUTF8: String(valueA + 1)))
806+
try fs.writeFileContents(fileA, bytes: ByteString(encodingAsUTF8: String(valueA + 1)))
899807

900808
Thread.yield()
901809

902-
// Get thr current contents of the file if any.
810+
// Get the current contents of the file if any.
903811
let valueB: Int
904812
if fs.exists(fileB) {
905813
valueB = Int(try fs.readFileContents(fileB).description) ?? 0
@@ -932,7 +840,6 @@ class FileSystemTests: XCTestCase {
932840
try XCTAssertEqual(fs.readFileContents(fileA), "100")
933841
try XCTAssertEqual(fs.readFileContents(fileB), "100")
934842
}
935-
936843
}
937844

938845
/// Helper method to test file tree removal method on the given file system.

0 commit comments

Comments
 (0)