|
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 = "\\"
|
@@ -1755,6 +1757,49 @@ VIDEOS=StopgapVideos
|
1755 | 1757 | }
|
1756 | 1758 | #endif
|
1757 | 1759 | }
|
| 1760 | + |
| 1761 | + /** |
| 1762 | + Tests that we can get an item replacement directory concurrently. |
| 1763 | + |
| 1764 | + - Bug: [SR-12272](https://bugs.swift.org/browse/SR-12272) |
| 1765 | + */ |
| 1766 | + func test_concurrentGetItemReplacementDirectory() throws { |
| 1767 | + let fileManager = FileManager.default |
| 1768 | + |
| 1769 | + let operationCount = 10 |
| 1770 | + |
| 1771 | + var directoryURLs = [URL?](repeating: nil, count: operationCount) |
| 1772 | + var errors = [Error?](repeating: nil, count: operationCount) |
| 1773 | + |
| 1774 | + let dispatchGroup = DispatchGroup() |
| 1775 | + for operationIndex in 0..<operationCount { |
| 1776 | + DispatchQueue.global().async(group: dispatchGroup) { |
| 1777 | + do { |
| 1778 | + let directory = try fileManager.url(for: .itemReplacementDirectory, |
| 1779 | + in: .userDomainMask, |
| 1780 | + appropriateFor: URL(fileURLWithPath: NSTemporaryDirectory(), |
| 1781 | + isDirectory: true), |
| 1782 | + create: true) |
| 1783 | + directoryURLs[operationIndex] = directory |
| 1784 | + } catch { |
| 1785 | + errors[operationIndex] = error |
| 1786 | + } |
| 1787 | + } |
| 1788 | + } |
| 1789 | + dispatchGroup.wait() |
| 1790 | + |
| 1791 | + for directoryURL in directoryURLs { |
| 1792 | + if let directoryURL = directoryURL { |
| 1793 | + try? fileManager.removeItem(at: directoryURL) |
| 1794 | + } |
| 1795 | + } |
| 1796 | + |
| 1797 | + for error in errors { |
| 1798 | + if let error = error { |
| 1799 | + XCTFail("One of the concurrent calls to get the item replacement directory failed: \(error)") |
| 1800 | + } |
| 1801 | + } |
| 1802 | + } |
1758 | 1803 |
|
1759 | 1804 | // -----
|
1760 | 1805 |
|
@@ -1812,6 +1857,7 @@ VIDEOS=StopgapVideos
|
1812 | 1857 | ("test_contentsEqual", test_contentsEqual),
|
1813 | 1858 | /* ⚠️ */ ("test_replacement", testExpectedToFail(test_replacement,
|
1814 | 1859 | /* ⚠️ */ "<https://bugs.swift.org/browse/SR-10819> Re-enable Foundation test TestFileManager.test_replacement")),
|
| 1860 | + ("test_concurrentGetItemReplacementDirectory", test_concurrentGetItemReplacementDirectory), |
1815 | 1861 | ]
|
1816 | 1862 |
|
1817 | 1863 | #if !DEPLOYMENT_RUNTIME_OBJC && NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT && !os(Android)
|
|
0 commit comments