From c0b3041f6fce47bc1c5a21d7e210d0faf90da998 Mon Sep 17 00:00:00 2001 From: MishaDemianenko Date: Tue, 7 Mar 2017 14:15:03 +0100 Subject: [PATCH] Modify FileWatchIt to perform modifications till change will be observed. Because of file last modification precision we can't be sure that we will observe modifications, perform them in test till we will see them. Use highest available sensitivity for file system watcher. --- .../fs/watcher/DefaultFileSystemWatcher.java | 6 +- .../org/neo4j/store/watch/FileWatchIT.java | 59 ++++++++++++------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/community/io/src/main/java/org/neo4j/io/fs/watcher/DefaultFileSystemWatcher.java b/community/io/src/main/java/org/neo4j/io/fs/watcher/DefaultFileSystemWatcher.java index 8000f9469f77..895d01e9011d 100644 --- a/community/io/src/main/java/org/neo4j/io/fs/watcher/DefaultFileSystemWatcher.java +++ b/community/io/src/main/java/org/neo4j/io/fs/watcher/DefaultFileSystemWatcher.java @@ -19,6 +19,7 @@ */ package org.neo4j.io.fs.watcher; +import com.sun.nio.file.SensitivityWatchEventModifier; import org.apache.commons.lang3.StringUtils; import java.io.File; @@ -41,6 +42,8 @@ */ public class DefaultFileSystemWatcher implements FileWatcher { + private static final WatchEvent.Kind[] OBSERVED_EVENTS = + new WatchEvent.Kind[]{StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY}; private final WatchService watchService; private final List listeners = new CopyOnWriteArrayList<>(); private volatile boolean watch; @@ -57,8 +60,7 @@ public WatchedResource watch( File file ) throws IOException { throw new IllegalArgumentException( "Only directories can be registered to be monitored." ); } - WatchKey watchKey = file.toPath() - .register( watchService, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY ); + WatchKey watchKey = file.toPath().register( watchService, OBSERVED_EVENTS, SensitivityWatchEventModifier.HIGH ); return new WatchedFile( watchKey ); } diff --git a/community/neo4j/src/test/java/org/neo4j/store/watch/FileWatchIT.java b/community/neo4j/src/test/java/org/neo4j/store/watch/FileWatchIT.java index 0a78261502e0..d58bfcba8086 100644 --- a/community/neo4j/src/test/java/org/neo4j/store/watch/FileWatchIT.java +++ b/community/neo4j/src/test/java/org/neo4j/store/watch/FileWatchIT.java @@ -97,9 +97,12 @@ public void notifyAboutStoreFileDeletion() throws Exception DeletionLatchEventListener deletionListener = new DeletionLatchEventListener( fileName ); fileWatcher.addFileWatchEventListener( deletionListener ); - createNode( database ); - forceCheckpoint( checkpointer ); - deletionListener.awaitModificationNotification(); + do + { + createNode( database ); + forceCheckpoint( checkpointer ); + } + while ( !deletionListener.awaitModificationNotification() ); deleteFile( storeDir, fileName ); deletionListener.awaitDeletionNotification(); @@ -140,9 +143,12 @@ public void notifyAboutLegacyIndexFolderRemoval() throws InterruptedException, I fileWatcher.addFileWatchEventListener( deletionListener ); fileWatcher.addFileWatchEventListener( modificationEventListener ); - createNode( database ); - forceCheckpoint( checkPointer ); - modificationEventListener.awaitModificationNotification(); + do + { + createNode( database ); + forceCheckpoint( checkPointer ); + } + while ( !modificationEventListener.awaitModificationNotification() ); deleteStoreDirectory( storeDir, monitoredDirectory ); deletionListener.awaitDeletionNotification(); @@ -168,18 +174,24 @@ public void doNotNotifyAboutLuceneIndexFilesDeletion() throws InterruptedExcepti String propertyName = "propertyName"; Label testLabel = Label.label( labelName ); createIndexes( database, propertyName, testLabel ); - createNode( database, propertyName, testLabel ); - forceCheckpoint( checkPointer ); - modificationListener.awaitModificationNotification(); + do + { + createNode( database, propertyName, testLabel ); + forceCheckpoint( checkPointer ); + } + while ( !modificationListener.awaitModificationNotification() ); fileWatcher.removeFileWatchEventListener( modificationListener ); ModificationEventListener afterRemovalListener = new ModificationEventListener( propertyStoreName ); fileWatcher.addFileWatchEventListener( afterRemovalListener ); dropAllIndexes( database ); - createNode( database, propertyName, testLabel ); - forceCheckpoint( checkPointer ); - afterRemovalListener.awaitModificationNotification(); + do + { + createNode( database, propertyName, testLabel ); + forceCheckpoint( checkPointer ); + } + while ( !afterRemovalListener.awaitModificationNotification() ); accumulativeListener.assertDoesNotHaveAnyDeletions(); } @@ -195,9 +207,12 @@ public void doNotMonitorTransactionLogFiles() throws InterruptedException, IOExc new ModificationEventListener( MetaDataStore.DEFAULT_NAME ); fileWatcher.addFileWatchEventListener( modificationEventListener ); - createNode( database ); - forceCheckpoint( checkpointer ); - modificationEventListener.awaitModificationNotification(); + do + { + createNode( database ); + forceCheckpoint( checkpointer ); + } + while ( !modificationEventListener.awaitModificationNotification() ); String fileName = PhysicalLogFile.DEFAULT_NAME + ".0"; DeletionLatchEventListener deletionListener = new DeletionLatchEventListener( fileName ); @@ -222,10 +237,12 @@ public void notifyWhenWholeStoreDirectoryRemoved() throws IOException, Interrupt ModificationEventListener modificationListener = new ModificationEventListener( fileName ); fileWatcher.addFileWatchEventListener( modificationListener ); - createNode( database ); - forceCheckpoint( checkpointer ); - - modificationListener.awaitModificationNotification(); + do + { + createNode( database ); + forceCheckpoint( checkpointer ); + } + while ( !modificationListener.awaitModificationNotification() ); fileWatcher.removeFileWatchEventListener( modificationListener ); String storeDirectoryName = TestDirectory.DATABASE_DIRECTORY; @@ -378,9 +395,9 @@ public void fileModified( String fileName ) } } - void awaitModificationNotification() throws InterruptedException + boolean awaitModificationNotification() throws InterruptedException { - modificationLatch.await(); + return modificationLatch.await(1, TimeUnit.SECONDS); } }