|
15 | 15 | #endif
|
16 | 16 | #endif
|
17 | 17 |
|
| 18 | +import Dispatch |
| 19 | + |
18 | 20 | class TestFileManager : XCTestCase {
|
19 | 21 | #if os(Windows)
|
20 | 22 | let pathSep = "\\"
|
@@ -1870,6 +1872,49 @@ VIDEOS=StopgapVideos
|
1870 | 1872 | try checkPath(path: path)
|
1871 | 1873 | }
|
1872 | 1874 | }
|
| 1875 | + |
| 1876 | + /** |
| 1877 | + Tests that we can get an item replacement directory concurrently. |
| 1878 | + |
| 1879 | + - Bug: [SR-12272](https://bugs.swift.org/browse/SR-12272) |
| 1880 | + */ |
| 1881 | + func test_concurrentGetItemReplacementDirectory() throws { |
| 1882 | + let fileManager = FileManager.default |
| 1883 | + |
| 1884 | + let operationCount = 10 |
| 1885 | + |
| 1886 | + var directoryURLs = [URL?](repeating: nil, count: operationCount) |
| 1887 | + var errors = [Error?](repeating: nil, count: operationCount) |
| 1888 | + |
| 1889 | + let dispatchGroup = DispatchGroup() |
| 1890 | + for operationIndex in 0..<operationCount { |
| 1891 | + DispatchQueue.global().async(group: dispatchGroup) { |
| 1892 | + do { |
| 1893 | + let directory = try fileManager.url(for: .itemReplacementDirectory, |
| 1894 | + in: .userDomainMask, |
| 1895 | + appropriateFor: URL(fileURLWithPath: NSTemporaryDirectory(), |
| 1896 | + isDirectory: true), |
| 1897 | + create: true) |
| 1898 | + directoryURLs[operationIndex] = directory |
| 1899 | + } catch { |
| 1900 | + errors[operationIndex] = error |
| 1901 | + } |
| 1902 | + } |
| 1903 | + } |
| 1904 | + dispatchGroup.wait() |
| 1905 | + |
| 1906 | + for directoryURL in directoryURLs { |
| 1907 | + if let directoryURL = directoryURL { |
| 1908 | + try? fileManager.removeItem(at: directoryURL) |
| 1909 | + } |
| 1910 | + } |
| 1911 | + |
| 1912 | + for error in errors { |
| 1913 | + if let error = error { |
| 1914 | + XCTFail("One of the concurrent calls to get the item replacement directory failed: \(error)") |
| 1915 | + } |
| 1916 | + } |
| 1917 | + } |
1873 | 1918 |
|
1874 | 1919 | // -----
|
1875 | 1920 |
|
@@ -1928,6 +1973,7 @@ VIDEOS=StopgapVideos
|
1928 | 1973 | ("test_contentsEqual", test_contentsEqual),
|
1929 | 1974 | /* ⚠️ */ ("test_replacement", testExpectedToFail(test_replacement,
|
1930 | 1975 | /* ⚠️ */ "<https://bugs.swift.org/browse/SR-10819> Re-enable Foundation test TestFileManager.test_replacement")),
|
| 1976 | + ("test_concurrentGetItemReplacementDirectory", test_concurrentGetItemReplacementDirectory), |
1931 | 1977 | ]
|
1932 | 1978 |
|
1933 | 1979 | #if !DEPLOYMENT_RUNTIME_OBJC && NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT && !os(Android)
|
|
0 commit comments