Skip to content

Instantly share code, notes, and snippets.

@chromy96
Created June 20, 2018 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chromy96/def29f4e9a0430e90f9ae28434fcc0f7 to your computer and use it in GitHub Desktop.
Save chromy96/def29f4e9a0430e90f9ae28434fcc0f7 to your computer and use it in GitHub Desktop.
Testing file lock for create
@Test
public void emitCreateEventWhenFileLocked() throws IOException, ExecutionException, InterruptedException {
final CompletableFuture future = this.watcher.watchAsync();
final Path child = Files.createTempFile(tmpDir, "child-", ".dat");
FileChannel channel = null;
FileLock lock = null;
try {
File file = child.toFile();
channel = new RandomAccessFile(file, "rw").getChannel();
lock = channel.lock();
try {
future.get(5, TimeUnit.SECONDS);
} catch (TimeoutException e) {
// Expected exception.
}
assertTrue(
"Create event for the child file was notified",
this.recorder.events.stream().anyMatch(
e -> e.eventType() == DirectoryChangeEvent.EventType.CREATE && e.path().getFileName().equals(child.getFileName())));
} finally {
if(lock != null && channel != null && channel.isOpen()){
lock.release();
channel.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment