From 839f2f15e1b639b0ac61a8502dd0d8b70264938f Mon Sep 17 00:00:00 2001 From: Dmitry Kuleshov Date: Mon, 15 Aug 2016 13:50:13 +0300 Subject: [PATCH] Fixed test failures related to file watcher and corrected logic in file watcher (#2097) * che#2006: added recursive setting up directory watchers Signed-off-by: Dmitry Kuleshov * che#2006: corrected thread pool size in test Signed-off-by: Dmitry Kuleshov --- .../api/vfs/impl/file/FileTreeWatcher.java | 4 +++ .../project/server/ProjectServiceTest.java | 4 ++- .../vfs/impl/file/FileTreeWatcherTest.java | 28 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/wsagent/che-core-api-project/src/main/java/org/eclipse/che/api/vfs/impl/file/FileTreeWatcher.java b/wsagent/che-core-api-project/src/main/java/org/eclipse/che/api/vfs/impl/file/FileTreeWatcher.java index 761e95fd89c..45ccdcb970a 100644 --- a/wsagent/che-core-api-project/src/main/java/org/eclipse/che/api/vfs/impl/file/FileTreeWatcher.java +++ b/wsagent/che-core-api-project/src/main/java/org/eclipse/che/api/vfs/impl/file/FileTreeWatcher.java @@ -225,6 +225,10 @@ private void setupDirectoryWatcher(Path directory) throws IOException { for (Path entry : entries) { watchedDirectory .addItem(new DirectoryItem(entry.getFileName(), Files.isDirectory(entry), getLastModifiedInMillis(entry))); + + if (Files.isDirectory(entry)) { + setupDirectoryWatcher(entry); + } } } watchedDirectories.put(directory, watchedDirectory); diff --git a/wsagent/che-core-api-project/src/test/java/org/eclipse/che/api/project/server/ProjectServiceTest.java b/wsagent/che-core-api-project/src/test/java/org/eclipse/che/api/project/server/ProjectServiceTest.java index e9ffee8ce08..61730509d1f 100644 --- a/wsagent/che-core-api-project/src/test/java/org/eclipse/che/api/project/server/ProjectServiceTest.java +++ b/wsagent/che-core-api-project/src/test/java/org/eclipse/che/api/project/server/ProjectServiceTest.java @@ -941,7 +941,7 @@ public void testDeleteProject() throws Exception { @Test public void testDeleteProjectsConcurrently() throws Exception { - int threadNumber = 100; + int threadNumber = 5 * (Runtime.getRuntime().availableProcessors() + 1); ExecutorService executor = Executors.newFixedThreadPool(threadNumber); CountDownLatch countDownLatch = new CountDownLatch(threadNumber); List> futures = new LinkedList<>(); @@ -981,6 +981,8 @@ public void testDeleteProjectsConcurrently() throws Exception { for (Future future : futures) { assertEquals(future.get().getStatus(), 204, "Error: " + future.get().getEntity()); } + + executor.shutdown(); } @Test diff --git a/wsagent/che-core-api-project/src/test/java/org/eclipse/che/api/vfs/impl/file/FileTreeWatcherTest.java b/wsagent/che-core-api-project/src/test/java/org/eclipse/che/api/vfs/impl/file/FileTreeWatcherTest.java index b2dad644f92..a5647ea73b2 100644 --- a/wsagent/che-core-api-project/src/test/java/org/eclipse/che/api/vfs/impl/file/FileTreeWatcherTest.java +++ b/wsagent/che-core-api-project/src/test/java/org/eclipse/che/api/vfs/impl/file/FileTreeWatcherTest.java @@ -108,6 +108,34 @@ public void watchesCreateDirectoryStructure() throws Exception { assertEquals(newHashSet(created), newHashSet(createdEvents.getAllValues())); } + @Test + public void watchesCreatedSubDirectoriesRecursively() throws Exception { + FileWatcherNotificationHandler notificationHandler = aNotificationHandler(); + fileWatcher = new FileTreeWatcher(testDirectory, newHashSet(), notificationHandler); + fileWatcher.startup(); + + Thread.sleep(500); + + final String first = fileWatcherTestTree.createDirectory("", "first"); + final String second = fileWatcherTestTree.createDirectory(first, "second"); + final String file = fileWatcherTestTree.createFile(second); + + Thread.sleep(5000); + + fileWatcherTestTree.updateFile(file); + + Thread.sleep(5000); + + verify(notificationHandler, never()).errorOccurred(eq(testDirectory), any(Throwable.class)); + verify(notificationHandler, never()).handleFileWatcherEvent(eq(DELETED), eq(testDirectory), anyString(), anyBoolean()); + + verify(notificationHandler, times(3)).handleFileWatcherEvent(eq(CREATED), eq(testDirectory), anyString(), anyBoolean()); + + ArgumentCaptor modifiedEvents = ArgumentCaptor.forClass(String.class); + verify(notificationHandler).handleFileWatcherEvent(eq(MODIFIED), eq(testDirectory), modifiedEvents.capture(), anyBoolean()); + assertEquals(newHashSet(file), newHashSet(modifiedEvents.getAllValues())); + } + @Test public void watchesCreateDirectoryAndStartsWatchingNewlyCreatedDirectory() throws Exception { FileWatcherNotificationHandler notificationHandler = aNotificationHandler();