@@ -41,16 +41,12 @@ struct DirectoryWatcherTest {
4141 return try await body ( tempDir)
4242 }
4343
44- private class CreatedURLs {
45- public var urls : [ URL ]
44+ private actor CreatedURLs {
45+ nonisolated ( unsafe ) public var urls : [ URL ]
4646
4747 public init ( ) {
4848 self . urls = [ ]
4949 }
50-
51- public func append( url: URL ) {
52- urls. append ( url)
53- }
5450 }
5551
5652 @Test func testWatchingExistingDirectory( ) async throws {
@@ -60,18 +56,16 @@ struct DirectoryWatcherTest {
6056 let createdURLs = CreatedURLs ( )
6157 let name = " newFile "
6258
63- #expect( throws: Never . self) {
64- try watcher. startWatching { [ createdURLs] urls in
65- for url in urls where url. lastPathComponent == name {
66- createdURLs. append ( url: url)
67- }
59+ try await watcher. startWatching { [ createdURLs] urls in
60+ for url in urls where url. lastPathComponent == name {
61+ createdURLs. urls. append ( url)
6862 }
6963 }
7064
71- try await Task . sleep ( for: . milliseconds( 500 ) )
65+ try await Task . sleep ( for: . milliseconds( 100 ) )
7266 let newFile = tempDir. appendingPathComponent ( name)
7367 FileManager . default. createFile ( atPath: newFile. path, contents: nil )
74- try await Task . sleep ( for: . milliseconds( 500 ) )
68+ try await Task . sleep ( for: . milliseconds( 100 ) )
7569
7670 #expect( !createdURLs. urls. isEmpty, " directory watcher failed to detect new file " )
7771 #expect( createdURLs. urls. first!. lastPathComponent == name)
@@ -87,75 +81,53 @@ struct DirectoryWatcherTest {
8781 let createdURLs = CreatedURLs ( )
8882 let name = " newFile "
8983
90- #expect( throws: Never . self) {
91- try watcher. startWatching { [ createdURLs] urls in
92- for url in urls where url. lastPathComponent == name {
93- createdURLs. append ( url: url)
94- }
84+ try await watcher. startWatching { [ createdURLs] urls in
85+ for url in urls where url. lastPathComponent == name {
86+ createdURLs. urls. append ( url)
9587 }
9688 }
9789
98- try await Task . sleep ( for: . milliseconds( 300 ) )
90+ try await Task . sleep ( for: . milliseconds( 100 ) )
9991 try FileManager . default. createDirectory ( at: childDir, withIntermediateDirectories: true )
10092
101- try await Task . sleep ( for: . milliseconds ( 300 ) )
93+ try await Task . sleep ( for: DirectoryWatcher . watchPeriod )
10294 let newFile = childDir. appendingPathComponent ( name)
10395 FileManager . default. createFile ( atPath: newFile. path, contents: nil )
104- try await Task . sleep ( for: . milliseconds( 300 ) )
96+ try await Task . sleep ( for: . milliseconds( 100 ) )
10597
10698 #expect( !createdURLs. urls. isEmpty, " directory watcher failed to detect parent directory " )
10799 #expect( createdURLs. urls. first!. lastPathComponent == name)
108-
109100 }
101+ }
110102
103+ @Test func testWatchingNonExistingParent( ) async throws {
111104 try await withTempDir { tempDir in
112105 let parent = UUID ( ) . uuidString
113- let symlink = UUID ( ) . uuidString
114106 let child = UUID ( ) . uuidString
115-
116- let parentDir = tempDir. appendingPathComponent ( parent)
117- let symlinkDir = tempDir. appendingPathComponent ( symlink)
118- let childDir = symlinkDir. appendingPathComponent ( child)
119-
120- try FileManager . default. createDirectory ( at: parentDir, withIntermediateDirectories: true )
121- try FileManager . default. createSymbolicLink ( at: symlinkDir, withDestinationURL: parentDir)
107+ let childDir = tempDir. appendingPathComponent ( parent) . appendingPathComponent ( child)
122108
123109 let watcher = DirectoryWatcher ( directoryURL: childDir, log: nil )
124110 let createdURLs = CreatedURLs ( )
125111 let name = " newFile "
126112
127- #expect( throws: Never . self) {
128- try watcher. startWatching { [ createdURLs] urls in
129- for url in urls where url. lastPathComponent == name {
130- createdURLs. append ( url: url)
131- }
113+ try await watcher. startWatching { urls in
114+ for url in urls where url. lastPathComponent == name {
115+ createdURLs. urls. append ( url)
132116 }
133117 }
134118
135- try await Task . sleep ( for: . milliseconds ( 300 ) )
119+ try await Task . sleep ( for: . microseconds ( 100 ) )
136120 try FileManager . default. createDirectory ( at: childDir, withIntermediateDirectories: true )
137121
138- try await Task . sleep ( for: . milliseconds( 300 ) )
122+ try await Task . sleep ( for: DirectoryWatcher . watchPeriod)
123+
139124 let newFile = childDir. appendingPathComponent ( name)
140125 FileManager . default. createFile ( atPath: newFile. path, contents: nil )
141- try await Task . sleep ( for: . milliseconds( 300 ) )
126+ try await Task . sleep ( for: . milliseconds( 100 ) )
142127
143- #expect( !createdURLs. urls. isEmpty, " directory watcher failed to detect symbolic parent directory " )
128+ #expect( !createdURLs. urls. isEmpty, " directory watcher failed to detect parent directory " )
144129 #expect( createdURLs. urls. first!. lastPathComponent == name)
145130 }
146131 }
147132
148- @Test func testWatchingNonExistingParent( ) async throws {
149- try await withTempDir { tempDir in
150- let parent = UUID ( ) . uuidString
151- let child = UUID ( ) . uuidString
152- let childDir = tempDir. appendingPathComponent ( parent) . appendingPathComponent ( child)
153-
154- let watcher = DirectoryWatcher ( directoryURL: childDir, log: nil )
155- #expect( throws: ContainerizationError . self, " directory watcher should fail if no parent " ) {
156- try watcher. startWatching { urls in }
157- }
158- }
159- }
160-
161133}
0 commit comments