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
This commit is contained in:
Andy Wilkinson 2020-08-05 08:51:37 +01:00
parent 4424055793
commit 2238b0d797

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -165,13 +165,13 @@ class FileSystemWatcherTests {
void waitsForQuietPeriod() throws Exception {
setupWatcher(300, 200);
File folder = startWithNewFolder();
for (int i = 0; i < 10; i++) {
for (int i = 0; i < 100; i++) {
touch(new File(folder, i + "test.txt"));
Thread.sleep(100);
Thread.sleep(10);
}
this.watcher.stopAfter(1);
ChangedFiles changedFiles = getSingleChangedFiles();
assertThat(changedFiles.getFiles().size()).isEqualTo(10);
assertThat(changedFiles.getFiles()).hasSize(100);
}
@Test
@ -287,12 +287,12 @@ class FileSystemWatcherTests {
private ChangedFiles getSingleChangedFiles() {
Set<ChangedFiles> singleChange = getSingleOnChange();
assertThat(singleChange.size()).isEqualTo(1);
assertThat(singleChange).hasSize(1);
return singleChange.iterator().next();
}
private Set<ChangedFiles> getSingleOnChange() {
assertThat(this.changes.size()).isEqualTo(1);
assertThat(this.changes).hasSize(1);
return this.changes.get(0);
}