2626 */
2727package engineering .swat .watch ;
2828
29+ import static engineering .swat .watch .WatchEvent .Kind .CREATED ;
30+ import static engineering .swat .watch .WatchEvent .Kind .DELETED ;
31+ import static engineering .swat .watch .WatchEvent .Kind .MODIFIED ;
32+ import static engineering .swat .watch .WatchEvent .Kind .OVERFLOW ;
2933import static org .awaitility .Awaitility .await ;
3034
3135import java .io .IOException ;
3236import java .nio .file .Files ;
37+ import java .nio .file .Path ;
3338import java .nio .file .attribute .FileTime ;
3439import java .time .Instant ;
40+ import java .util .ArrayList ;
41+ import java .util .Arrays ;
42+ import java .util .Collections ;
43+ import java .util .List ;
3544import java .util .concurrent .atomic .AtomicBoolean ;
3645import java .util .function .Consumer ;
46+ import java .util .function .Predicate ;
47+ import java .util .stream .Stream ;
3748
3849import org .awaitility .Awaitility ;
3950import org .junit .jupiter .api .AfterEach ;
@@ -123,33 +134,36 @@ void singleFileThatMonitorsOnlyADirectory() throws IOException, InterruptedExcep
123134
124135 @ Test
125136 void noRescanOnOverflow () throws IOException , InterruptedException {
126- var whichFiles = OnOverflow .NONE ;
127137 var bookkeeper = new Bookkeeper ();
128- try (var watch = startWatchAndTriggerOverflow (whichFiles , bookkeeper )) {
138+ try (var watch = startWatchAndTriggerOverflow (OnOverflow . NONE , bookkeeper )) {
129139 Thread .sleep (TestHelper .SHORT_WAIT .toMillis ());
130140
131- await ("Overflow shouldn't trigger created events" )
132- .untilFalse (bookkeeper .seenCreated );
133- await ("Overflow shouldn't trigger modified events" )
134- .untilFalse (bookkeeper .seenModified );
141+ await ("Overflow shouldn't trigger created, modified, or deleted events" )
142+ .until (() -> bookkeeper .fullPaths (CREATED , MODIFIED , DELETED ).count () == 0 );
135143 await ("Overflow should be visible to user-defined event handler" )
136- .untilTrue ( bookkeeper .seenOverflow );
144+ .until (() -> bookkeeper .fullPaths ( OVERFLOW ). count () == 1 );
137145 }
138146 }
139147
140148 @ Test
141149 void memorylessRescanOnOverflow () throws IOException , InterruptedException {
142- var whichFiles = OnOverflow .ALL ;
143150 var bookkeeper = new Bookkeeper ();
144- try (var watch = startWatchAndTriggerOverflow (whichFiles , bookkeeper )) {
151+ try (var watch = startWatchAndTriggerOverflow (OnOverflow . ALL , bookkeeper )) {
145152 Thread .sleep (TestHelper .SHORT_WAIT .toMillis ());
146153
147- await ("Overflow should trigger created event" )
148- .untilTrue (bookkeeper .seenCreated );
149- await ("Overflow shouldn't trigger modified event (empty file)" )
150- .untilFalse (bookkeeper .seenModified );
154+ var isFile = Predicate .isEqual (watch .getPath ());
155+ var isNotFile = Predicate .not (isFile );
156+
157+ await ("Overflow should trigger created event for `file`" )
158+ .until (() -> bookkeeper .fullPaths (CREATED ).filter (isFile ).count () == 1 );
159+ await ("Overflow shouldn't trigger created events for other files" )
160+ .until (() -> bookkeeper .fullPaths (CREATED ).filter (isNotFile ).count () == 0 );
161+ await ("Overflow shouldn't trigger modified events (`file` is empty)" )
162+ .until (() -> bookkeeper .fullPaths (MODIFIED ).count () == 0 );
163+ await ("Overflow shouldn't trigger deleted events" )
164+ .until (() -> bookkeeper .fullPaths (DELETED ).count () == 0 );
151165 await ("Overflow should be visible to user-defined event handler" )
152- .untilTrue ( bookkeeper .seenOverflow );
166+ .until (() -> bookkeeper .fullPaths ( OVERFLOW ). count () == 1 );
153167 }
154168 }
155169
@@ -169,25 +183,21 @@ private ActiveWatch startWatchAndTriggerOverflow(OnOverflow whichFiles, Bookkeep
169183 }
170184
171185 private static class Bookkeeper implements Consumer <WatchEvent > {
172- private final AtomicBoolean seenCreated = new AtomicBoolean (false );
173- private final AtomicBoolean seenModified = new AtomicBoolean (false );
174- private final AtomicBoolean seenOverflow = new AtomicBoolean (false );
186+ private final List <WatchEvent > events = Collections .synchronizedList (new ArrayList <>());
187+
188+ public Stream <WatchEvent > events (WatchEvent .Kind ... kinds ) {
189+ var list = Arrays .asList (kinds .length == 0 ? WatchEvent .Kind .values () : kinds );
190+ return events .stream ().filter (e -> list .contains (e .getKind ()));
191+ }
192+
193+ public Stream <Path > fullPaths (WatchEvent .Kind ... kinds ) {
194+ return events (kinds ).map (WatchEvent ::calculateFullPath );
195+ }
175196
176197 @ Override
177198 public void accept (WatchEvent e ) {
178- switch (e .getKind ()) {
179- case CREATED :
180- seenCreated .set (true );
181- break ;
182- case MODIFIED :
183- seenModified .set (true );
184- break ;
185- case OVERFLOW :
186- seenOverflow .set (true );
187- break ;
188- default :
189- break ;
190- }
199+ events .add (e );
200+ System .out .println (e );
191201 }
192202 }
193203}
0 commit comments