Skip to content

Commit 2238b0d

Browse files
committed
Try to make FileSystemWatcherTests.waitsForQuietPeriod() more robust
Previously, waitsForQuietPeriod would iterate 10 times, touching a new file and then sleeping for 100ms at it did so. With a quiet period of 200ms, this was intended to result in a single change set containing 10 files. However, the test would fail occasionally as multiple change sets were detected. The test is multi-threaded and is, therefore, at the mercy of the scheduler. If the thread that is iterating and touching the files takes over 200ms to be scheduled – exceeding the watcher's quiet period – the watcher may detect a change set while the changes are still being made. Eliminating this possibilty would require the test to participate in the watcher's synchronization, which would require some changes to its implementation. Instead, this commit aims to avoid the problem by sleeping for 1/10 of the time (10ms) and expecting a single change set of 100 files. The hope is that the much shorter sleep time will result in the file touching thread being scheduled well within the 200ms quiet period. Closes gh-22732
1 parent 4424055 commit 2238b0d

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -165,13 +165,13 @@ void waitsForPollingInterval() throws Exception {
165165
void waitsForQuietPeriod() throws Exception {
166166
setupWatcher(300, 200);
167167
File folder = startWithNewFolder();
168-
for (int i = 0; i < 10; i++) {
168+
for (int i = 0; i < 100; i++) {
169169
touch(new File(folder, i + "test.txt"));
170-
Thread.sleep(100);
170+
Thread.sleep(10);
171171
}
172172
this.watcher.stopAfter(1);
173173
ChangedFiles changedFiles = getSingleChangedFiles();
174-
assertThat(changedFiles.getFiles().size()).isEqualTo(10);
174+
assertThat(changedFiles.getFiles()).hasSize(100);
175175
}
176176

177177
@Test
@@ -287,12 +287,12 @@ private File startWithNewFolder() throws IOException {
287287

288288
private ChangedFiles getSingleChangedFiles() {
289289
Set<ChangedFiles> singleChange = getSingleOnChange();
290-
assertThat(singleChange.size()).isEqualTo(1);
290+
assertThat(singleChange).hasSize(1);
291291
return singleChange.iterator().next();
292292
}
293293

294294
private Set<ChangedFiles> getSingleOnChange() {
295-
assertThat(this.changes.size()).isEqualTo(1);
295+
assertThat(this.changes).hasSize(1);
296296
return this.changes.get(0);
297297
}
298298

0 commit comments

Comments
 (0)