27
27
package engineering .swat .watch ;
28
28
29
29
import static engineering .swat .watch .WatchEvent .Kind .CREATED ;
30
- import static org .awaitility .Awaitility .await ;
30
+ import static engineering .swat .watch .util .WaitFor .await ;
31
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
31
32
32
33
import java .io .IOException ;
33
34
import java .nio .file .Files ;
34
35
import java .nio .file .Path ;
36
+ import java .util .ArrayList ;
37
+ import java .util .Arrays ;
38
+ import java .util .Collection ;
39
+ import java .util .Collections ;
40
+ import java .util .List ;
35
41
import java .util .concurrent .ForkJoinPool ;
36
42
import java .util .concurrent .atomic .AtomicBoolean ;
37
43
import java .util .concurrent .atomic .AtomicReference ;
38
44
import java .util .function .Consumer ;
45
+ import java .util .stream .Collectors ;
39
46
40
47
import org .apache .logging .log4j .LogManager ;
41
48
import org .apache .logging .log4j .Logger ;
49
56
50
57
import engineering .swat .watch .WatchEvent .Kind ;
51
58
import engineering .swat .watch .impl .EventHandlingWatch ;
59
+ import engineering .swat .watch .util .WaitFor ;
52
60
53
61
class RecursiveWatchTests {
54
62
private final Logger logger = LogManager .getLogger ();
@@ -69,7 +77,7 @@ void cleanup() {
69
77
70
78
@ BeforeAll
71
79
static void setupEverything () {
72
- Awaitility .setDefaultTimeout (TestHelper .NORMAL_WAIT );
80
+ WaitFor .setDefaultTimeout (TestHelper .NORMAL_WAIT );
73
81
}
74
82
75
83
@ Test
@@ -99,9 +107,9 @@ void newDirectoryWithFilesChangesDetected() throws IOException {
99
107
target .set (freshFile );
100
108
logger .debug ("Interested in: {}" , freshFile );
101
109
Files .writeString (freshFile , "Hello world" );
102
- await ("New files should have been seen" ).untilTrue (created );
110
+ await ("New files should have been seen" ).until (created );
103
111
Files .writeString (freshFile , "Hello world 2" );
104
- await ("Fresh file change have been detected" ).untilTrue (changed );
112
+ await ("Fresh file change have been detected" ).until (changed );
105
113
}
106
114
}
107
115
@@ -121,7 +129,7 @@ void correctRelativePathIsReported() throws IOException {
121
129
var targetFile = testDir .getTestDirectory ().resolve (relative );
122
130
Files .createDirectories (targetFile .getParent ());
123
131
Files .writeString (targetFile , "Hello World" );
124
- await ("Nested path is seen" ).untilTrue (seen );
132
+ await ("Nested path is seen" ).until (seen );
125
133
}
126
134
127
135
}
@@ -143,19 +151,19 @@ void deleteOfFileInDirectoryShouldBeVisible() throws IOException {
143
151
try (var watch = watchConfig .start ()) {
144
152
Files .delete (target );
145
153
await ("File deletion should generate delete event" )
146
- .untilTrue (seen );
154
+ .until (seen );
147
155
}
148
156
}
149
157
150
158
@ ParameterizedTest
151
159
@ EnumSource // Repeat test for each `Approximation` value
152
160
void overflowsAreRecoveredFrom (Approximation whichFiles ) throws IOException , InterruptedException {
153
161
var parent = testDir .getTestDirectory ();
154
- var descendants = new Path [] {
162
+ var descendants = List . of (
155
163
Path .of ("foo" ),
156
164
Path .of ("bar" ),
157
165
Path .of ("bar" , "x" , "y" , "z" )
158
- } ;
166
+ ) ;
159
167
160
168
// Configure and start watch
161
169
var dropEvents = new AtomicBoolean (false ); // Toggles overflow simulation
@@ -169,28 +177,38 @@ void overflowsAreRecoveredFrom(Approximation whichFiles) throws IOException, Int
169
177
try (var watch = (EventHandlingWatch ) watchConfig .start ()) {
170
178
171
179
// Define helper functions to test which events have happened
172
- Consumer <Path > awaitCreation = p ->
173
- await ("Creation of `" + p + "` should be observed" )
174
- .until (() -> bookkeeper .events ().kind (CREATED ).rootPath (parent ).relativePath (p ).any ());
175
-
176
- Consumer <Path > awaitNotCreation = p ->
177
- await ("Creation of `" + p + "` shouldn't be observed: " + bookkeeper )
178
- .pollDelay (TestHelper .TINY_WAIT )
179
- .until (() -> bookkeeper .events ().kind (CREATED ).rootPath (parent ).relativePath (p ).none ());
180
+ Consumer <Collection <Path >> awaitCreation = paths ->
181
+ WaitFor .await ("Creation should be observed" )
182
+ .untilContainsAll (() ->
183
+ bookkeeper .events ()
184
+ .kind (CREATED )
185
+ .rootPath (parent )
186
+ .relativePath (paths )
187
+ .events ()
188
+ .map (WatchEvent ::getRelativePath )
189
+ , paths );
180
190
181
191
// Begin overflow simulation
182
192
dropEvents .set (true );
183
193
184
194
// Create descendants and files. They *shouldn't* be observed yet.
195
+ var missedCreates = new ArrayList <Path >();
185
196
var file1 = Path .of ("file1.txt" );
186
197
for (var descendant : descendants ) {
187
- Files . createDirectories ( parent .resolve (descendant ) );
188
- Files . createFile ( parent . resolve (descendant ). resolve ( file1 ) );
189
- }
190
- for ( var descendant : descendants ) {
191
- awaitNotCreation . accept (descendant );
192
- awaitNotCreation . accept (descendant .resolve (file1 ));
198
+ var d = parent .resolve (descendant );
199
+ var f = d . resolve (file1 );
200
+ Files . createDirectories ( d );
201
+ Files . createFile ( f );
202
+ missedCreates . add (descendant );
203
+ missedCreates . add (descendant .resolve (file1 ));
193
204
}
205
+ WaitFor .await (() -> "We should not have seen any events" )
206
+ .time (TestHelper .TINY_WAIT )
207
+ .holdsEmpty (() -> bookkeeper .events ()
208
+ .kind (CREATED )
209
+ .rootPath (parent )
210
+ .relativePath (missedCreates )
211
+ .events ());
194
212
195
213
// End overflow simulation, and generate the `OVERFLOW` event. The
196
214
// previous creation of descendants and files *should* now be
@@ -201,10 +219,7 @@ void overflowsAreRecoveredFrom(Approximation whichFiles) throws IOException, Int
201
219
watch .handleEvent (overflow );
202
220
203
221
if (whichFiles != Approximation .NONE ) { // Auto-handler is configured
204
- for (var descendant : descendants ) {
205
- awaitCreation .accept (descendant );
206
- awaitCreation .accept (descendant .resolve (file1 ));
207
- }
222
+ awaitCreation .accept (missedCreates );
208
223
} else {
209
224
// Give the watch some time to process the `OVERFLOW` event and
210
225
// do internal bookkeeping
@@ -213,13 +228,14 @@ void overflowsAreRecoveredFrom(Approximation whichFiles) throws IOException, Int
213
228
214
229
// Create more files. They *should* be observed (regardless of
215
230
// whether an auto-handler for `OVERFLOW` events is configured).
216
- var file2 = Path . of ( "file2.txt" );
217
- for ( var descendant : descendants ) {
218
- Files . createFile ( parent . resolve ( descendant ). resolve ( file2 ));
219
- }
220
- for (var descendant : descendants ) {
221
- awaitCreation . accept ( descendant .resolve (file2 ));
231
+ var moreFiles = descendants . stream ()
232
+ . map ( d -> d . resolve ( Path . of ( "file2.txt" )))
233
+ . collect ( Collectors . toList ( ));
234
+
235
+ for (var f : moreFiles ) {
236
+ Files . createFile ( parent .resolve (f ));
222
237
}
238
+ awaitCreation .accept (moreFiles );
223
239
}
224
240
}
225
241
}
0 commit comments