Skip to content

Commit

Permalink
Modify FileWatchIt to perform modifications till change will be obser…
Browse files Browse the repository at this point in the history
…ved.

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.
  • Loading branch information
MishaDemianenko committed Mar 7, 2017
1 parent d154b82 commit c0b3041
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.neo4j.io.fs.watcher; package org.neo4j.io.fs.watcher;


import com.sun.nio.file.SensitivityWatchEventModifier;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;


import java.io.File; import java.io.File;
Expand All @@ -41,6 +42,8 @@
*/ */
public class DefaultFileSystemWatcher implements FileWatcher 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 WatchService watchService;
private final List<FileWatchEventListener> listeners = new CopyOnWriteArrayList<>(); private final List<FileWatchEventListener> listeners = new CopyOnWriteArrayList<>();
private volatile boolean watch; private volatile boolean watch;
Expand All @@ -57,8 +60,7 @@ public WatchedResource watch( File file ) throws IOException
{ {
throw new IllegalArgumentException( "Only directories can be registered to be monitored." ); throw new IllegalArgumentException( "Only directories can be registered to be monitored." );
} }
WatchKey watchKey = file.toPath() WatchKey watchKey = file.toPath().register( watchService, OBSERVED_EVENTS, SensitivityWatchEventModifier.HIGH );
.register( watchService, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY );
return new WatchedFile( watchKey ); return new WatchedFile( watchKey );
} }


Expand Down
Expand Up @@ -97,9 +97,12 @@ public void notifyAboutStoreFileDeletion() throws Exception
DeletionLatchEventListener deletionListener = new DeletionLatchEventListener( fileName ); DeletionLatchEventListener deletionListener = new DeletionLatchEventListener( fileName );
fileWatcher.addFileWatchEventListener( deletionListener ); fileWatcher.addFileWatchEventListener( deletionListener );


createNode( database ); do
forceCheckpoint( checkpointer ); {
deletionListener.awaitModificationNotification(); createNode( database );
forceCheckpoint( checkpointer );
}
while ( !deletionListener.awaitModificationNotification() );


deleteFile( storeDir, fileName ); deleteFile( storeDir, fileName );
deletionListener.awaitDeletionNotification(); deletionListener.awaitDeletionNotification();
Expand Down Expand Up @@ -140,9 +143,12 @@ public void notifyAboutLegacyIndexFolderRemoval() throws InterruptedException, I
fileWatcher.addFileWatchEventListener( deletionListener ); fileWatcher.addFileWatchEventListener( deletionListener );
fileWatcher.addFileWatchEventListener( modificationEventListener ); fileWatcher.addFileWatchEventListener( modificationEventListener );


createNode( database ); do
forceCheckpoint( checkPointer ); {
modificationEventListener.awaitModificationNotification(); createNode( database );
forceCheckpoint( checkPointer );
}
while ( !modificationEventListener.awaitModificationNotification() );


deleteStoreDirectory( storeDir, monitoredDirectory ); deleteStoreDirectory( storeDir, monitoredDirectory );
deletionListener.awaitDeletionNotification(); deletionListener.awaitDeletionNotification();
Expand All @@ -168,18 +174,24 @@ public void doNotNotifyAboutLuceneIndexFilesDeletion() throws InterruptedExcepti
String propertyName = "propertyName"; String propertyName = "propertyName";
Label testLabel = Label.label( labelName ); Label testLabel = Label.label( labelName );
createIndexes( database, propertyName, testLabel ); createIndexes( database, propertyName, testLabel );
createNode( database, propertyName, testLabel ); do
forceCheckpoint( checkPointer ); {
modificationListener.awaitModificationNotification(); createNode( database, propertyName, testLabel );
forceCheckpoint( checkPointer );
}
while ( !modificationListener.awaitModificationNotification() );


fileWatcher.removeFileWatchEventListener( modificationListener ); fileWatcher.removeFileWatchEventListener( modificationListener );
ModificationEventListener afterRemovalListener = new ModificationEventListener( propertyStoreName ); ModificationEventListener afterRemovalListener = new ModificationEventListener( propertyStoreName );
fileWatcher.addFileWatchEventListener( afterRemovalListener ); fileWatcher.addFileWatchEventListener( afterRemovalListener );


dropAllIndexes( database ); dropAllIndexes( database );
createNode( database, propertyName, testLabel ); do
forceCheckpoint( checkPointer ); {
afterRemovalListener.awaitModificationNotification(); createNode( database, propertyName, testLabel );
forceCheckpoint( checkPointer );
}
while ( !afterRemovalListener.awaitModificationNotification() );


accumulativeListener.assertDoesNotHaveAnyDeletions(); accumulativeListener.assertDoesNotHaveAnyDeletions();
} }
Expand All @@ -195,9 +207,12 @@ public void doNotMonitorTransactionLogFiles() throws InterruptedException, IOExc
new ModificationEventListener( MetaDataStore.DEFAULT_NAME ); new ModificationEventListener( MetaDataStore.DEFAULT_NAME );
fileWatcher.addFileWatchEventListener( modificationEventListener ); fileWatcher.addFileWatchEventListener( modificationEventListener );


createNode( database ); do
forceCheckpoint( checkpointer ); {
modificationEventListener.awaitModificationNotification(); createNode( database );
forceCheckpoint( checkpointer );
}
while ( !modificationEventListener.awaitModificationNotification() );


String fileName = PhysicalLogFile.DEFAULT_NAME + ".0"; String fileName = PhysicalLogFile.DEFAULT_NAME + ".0";
DeletionLatchEventListener deletionListener = new DeletionLatchEventListener( fileName ); DeletionLatchEventListener deletionListener = new DeletionLatchEventListener( fileName );
Expand All @@ -222,10 +237,12 @@ public void notifyWhenWholeStoreDirectoryRemoved() throws IOException, Interrupt


ModificationEventListener modificationListener = new ModificationEventListener( fileName ); ModificationEventListener modificationListener = new ModificationEventListener( fileName );
fileWatcher.addFileWatchEventListener( modificationListener ); fileWatcher.addFileWatchEventListener( modificationListener );
createNode( database ); do
forceCheckpoint( checkpointer ); {

createNode( database );
modificationListener.awaitModificationNotification(); forceCheckpoint( checkpointer );
}
while ( !modificationListener.awaitModificationNotification() );
fileWatcher.removeFileWatchEventListener( modificationListener ); fileWatcher.removeFileWatchEventListener( modificationListener );


String storeDirectoryName = TestDirectory.DATABASE_DIRECTORY; String storeDirectoryName = TestDirectory.DATABASE_DIRECTORY;
Expand Down Expand Up @@ -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);
} }
} }


Expand Down

0 comments on commit c0b3041

Please sign in to comment.