@@ -749,9 +749,6 @@ class FileSystemTests: XCTestCase {
749
749
}
750
750
751
751
func testInMemoryFileSystemFileLock( ) throws {
752
- // Disabled until rdar://71560894 is fixed.
753
- try XCTSkipIf ( true )
754
-
755
752
let fs = InMemoryFileSystem ( )
756
753
let path = AbsolutePath ( " / " )
757
754
try fs. createDirectory ( path)
@@ -760,118 +757,20 @@ class FileSystemTests: XCTestCase {
760
757
let fileB = path. appending ( component: " fileB " )
761
758
let lockFile = path. appending ( component: " lockfile " )
762
759
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)
810
761
}
811
762
812
763
func testLocalFileSystemFileLock( ) throws {
813
- // Disabled until rdar://71560894 is fixed.
814
- try XCTSkipIf ( true )
815
-
816
764
try withTemporaryDirectory { tempDir in
817
765
let fileA = tempDir. appending ( component: " fileA " )
818
766
let fileB = tempDir. appending ( component: " fileB " )
819
767
let lockFile = tempDir. appending ( component: " lockfile " )
820
768
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)
868
770
}
869
771
}
870
772
871
773
func testRerootedFileSystemViewFileLock( ) throws {
872
- // Disabled until rdar://71560894 is fixed.
873
- try XCTSkipIf ( true )
874
-
875
774
let inMemoryFS = InMemoryFileSystem ( )
876
775
let rootPath = AbsolutePath ( " /tmp " )
877
776
try inMemoryFS. createDirectory ( rootPath)
@@ -884,22 +783,31 @@ class FileSystemTests: XCTestCase {
884
783
let fileB = path. appending ( component: " fileB " )
885
784
let lockFile = path. appending ( component: " lockfile " )
886
785
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
+
887
795
let writerThreads = ( 0 ..< 100 ) . map { _ in
888
796
return Thread {
889
797
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.
891
799
let valueA : Int
892
800
if fs. exists ( fileA) {
893
- valueA = Int ( try ! fs. readFileContents ( fileA) . description) ?? 0
801
+ valueA = Int ( try fs. readFileContents ( fileA) . description) ?? 0
894
802
} else {
895
803
valueA = 0
896
804
}
897
805
// 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 ) ) )
899
807
900
808
Thread . yield ( )
901
809
902
- // Get thr current contents of the file if any.
810
+ // Get the current contents of the file if any.
903
811
let valueB : Int
904
812
if fs. exists ( fileB) {
905
813
valueB = Int ( try fs. readFileContents ( fileB) . description) ?? 0
@@ -932,7 +840,6 @@ class FileSystemTests: XCTestCase {
932
840
try XCTAssertEqual ( fs. readFileContents ( fileA) , " 100 " )
933
841
try XCTAssertEqual ( fs. readFileContents ( fileB) , " 100 " )
934
842
}
935
-
936
843
}
937
844
938
845
/// Helper method to test file tree removal method on the given file system.
0 commit comments