Created
June 20, 2018 12:36
-
-
Save chromy96/def29f4e9a0430e90f9ae28434fcc0f7 to your computer and use it in GitHub Desktop.
Testing file lock for create
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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