From 2238b0d797be0fe55710f168cdde47cf2eca0dea Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 5 Aug 2020 08:51:37 +0100 Subject: [PATCH] Try to make FileSystemWatcherTests.waitsForQuietPeriod() more robust MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../devtools/filewatch/FileSystemWatcherTests.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java index f698822000..5cbc5b1a40 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java @@ -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 singleChange = getSingleOnChange(); - assertThat(singleChange.size()).isEqualTo(1); + assertThat(singleChange).hasSize(1); return singleChange.iterator().next(); } private Set getSingleOnChange() { - assertThat(this.changes.size()).isEqualTo(1); + assertThat(this.changes).hasSize(1); return this.changes.get(0); }