use of org.neo4j.io.pagecache.PageSwapperFactory in project neo4j by neo4j.
the class SingleFilePageSwapperTest method creatingSwapperForExternallyLockedFileMustThrow.
@Test
@DisabledOnOs(OS.WINDOWS)
void creatingSwapperForExternallyLockedFileMustThrow() throws Exception {
PageSwapperFactory factory = createSwapperFactory(fileSystem);
Path file = testDir.file("file");
((StoreChannel) fileSystem.write(file)).close();
var process = start(pb -> pb.directory(Path.of("target/test-classes").toAbsolutePath().toFile()), LockThisFileProgram.class.getCanonicalName(), file.toAbsolutePath().toString());
BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
InputStream stderr = process.getErrorStream();
try {
assumeTrue(LockThisFileProgram.LOCKED_OUTPUT.equals(stdout.readLine()));
} catch (Throwable e) {
int b = stderr.read();
while (b != -1) {
System.err.write(b);
b = stderr.read();
}
System.err.flush();
int exitCode = process.waitFor();
System.out.println("exitCode = " + exitCode);
throw e;
}
try {
assertThrows(FileLockException.class, () -> createSwapper(factory, file, 4, NO_CALLBACK, true));
} finally {
process.getOutputStream().write(0);
process.getOutputStream().flush();
process.waitFor();
}
}
use of org.neo4j.io.pagecache.PageSwapperFactory in project neo4j by neo4j.
the class SingleFilePageSwapperTest method swappingOutMustWritePageToFile.
@Test
void swappingOutMustWritePageToFile() throws Exception {
getFs().write(getPath()).close();
byte[] expected = new byte[] { 1, 2, 3, 4 };
long page = createPage(expected);
PageSwapperFactory factory = createSwapperFactory(getFs());
PageSwapper swapper = createSwapper(factory, getPath(), 4, null, false);
assertEquals(4, swapper.write(0, page));
try (InputStream stream = getFs().openAsInputStream(getPath())) {
byte[] actual = new byte[expected.length];
assertThat(stream.read(actual)).isEqualTo(actual.length);
assertThat(actual).containsExactly(expected);
}
}
use of org.neo4j.io.pagecache.PageSwapperFactory in project neo4j by neo4j.
the class SingleFilePageSwapperTest method creatingSwapperForInternallyLockedFileMustThrow.
@Test
@DisabledOnOs(OS.WINDOWS)
void creatingSwapperForInternallyLockedFileMustThrow() throws Exception {
PageSwapperFactory factory = createSwapperFactory(fileSystem);
Path file = testDir.file("file");
StoreFileChannel channel = fileSystem.write(file);
try (FileLock fileLock = channel.tryLock()) {
assertThat(fileLock).isNotNull();
assertThrows(FileLockException.class, () -> createSwapper(factory, file, 4, NO_CALLBACK, true));
}
}
use of org.neo4j.io.pagecache.PageSwapperFactory in project neo4j by neo4j.
the class SingleFilePageSwapperTest method uninterruptibleRead.
@Test
void uninterruptibleRead() throws Exception {
byte[] pageContent = new byte[] { 1, 2, 3, 4 };
StoreChannel channel = getFs().write(getPath());
channel.writeAll(wrap(pageContent));
channel.close();
PageSwapperFactory factory = createSwapperFactory(getFs());
PageSwapper swapper = createSwapper(factory, getPath(), 4, null, false);
long target = createPage(4);
CountDownLatch startInterruptsLatch = new CountDownLatch(1);
AtomicBoolean readFlag = new AtomicBoolean(true);
var uninterruptibleFuture = operationExecutor.submit(() -> {
startInterruptsLatch.countDown();
while (readFlag.get()) {
try {
swapper.read(0, target);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
});
startInterruptsLatch.await();
assertFalse(threadRegistryFactory.getThreads().isEmpty());
for (int i = 0; i < INTERRUPT_ATTEMPTS; i++) {
threadRegistryFactory.getThreads().forEach(Thread::interrupt);
parkNanos(MILLISECONDS.toNanos(10));
}
readFlag.set(false);
assertDoesNotThrow((ThrowingSupplier<?>) uninterruptibleFuture::get);
}
use of org.neo4j.io.pagecache.PageSwapperFactory in project neo4j by neo4j.
the class SingleFilePageSwapperTest method creatingSwapperForFileMustTakeLockOnFile.
/**
* The OverlappingFileLockException is thrown when tryLock is called on the same file *in the same JVM*.
*/
@Test
@DisabledOnOs(OS.WINDOWS)
void creatingSwapperForFileMustTakeLockOnFile() throws Exception {
PageSwapperFactory factory = createSwapperFactory(fileSystem);
Path file = testDir.file("file");
((StoreChannel) fileSystem.write(file)).close();
PageSwapper pageSwapper = createSwapper(factory, file, 4, NO_CALLBACK, false);
try {
StoreChannel channel = fileSystem.write(file);
assertThrows(OverlappingFileLockException.class, channel::tryLock);
} finally {
pageSwapper.close();
}
}
Aggregations