From 69320180d9d33ad682232c11f901671ef8da05b3 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 17 Jan 2017 10:30:47 +0000 Subject: [PATCH] Make FileSystemWatcherTests.waitsForPollingInterval more robust The intent of the test is to: 1. Make a change 2. Sleep for long enough for that change to be picked up 3. Make another change 4. Stop that watcher after 1 further scan has been performed 5. Assert that a further scan was performed by checking that two separate sets of changes (step 1 and step 3) have been picked up Previously, step 2 relied on simply sleeping for a period of time longer than the polling interval. In reality, the polling interval is only a minimum time between scans and the actual time between them depends on thread scheduling, GC pauses, etc. This lead to the test failing intermittently if the scan didn't happen in a timely manner. This commit removes the sleep and replaces it with a while loop that waits for first change to be picked up. This ensures that the second change will be detected separately from the first and that two separate change sets should always be available once the watcher has stopped. See gh-7782 --- .../boot/devtools/filewatch/FileSystemWatcherTests.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java index c98b3f75a2..a39f2a3a8f 100644 --- a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java +++ b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -155,10 +155,12 @@ public class FileSystemWatcherTests { @Test public void waitsForPollingInterval() throws Exception { - setupWatcher(100, 1); + setupWatcher(10, 1); File folder = startWithNewFolder(); touch(new File(folder, "test1.txt")); - Thread.sleep(200); + while (this.changes.size() != 1) { + Thread.sleep(10); + } touch(new File(folder, "test2.txt")); this.watcher.stopAfter(1); assertThat(this.changes.size()).isEqualTo(2);