use of org.neo4j.io.pagecache.PageSwapper in project neo4j by neo4j.
the class SingleFilePageSwapperTest method mustHandleMischiefInPositionedRead.
@Test
public void mustHandleMischiefInPositionedRead() throws Exception {
int bytesTotal = 512;
byte[] data = new byte[bytesTotal];
ThreadLocalRandom.current().nextBytes(data);
PageSwapperFactory factory = createSwapperFactory();
factory.setFileSystemAbstraction(getFs());
File file = getFile();
PageSwapper swapper = createSwapper(factory, file, bytesTotal, NO_CALLBACK, true);
try {
swapper.write(0, new ByteBufferPage(wrap(data)));
} finally {
swapper.close();
}
RandomAdversary adversary = new RandomAdversary(0.5, 0.0, 0.0);
factory.setFileSystemAbstraction(new AdversarialFileSystemAbstraction(adversary, getFs()));
swapper = createSwapper(factory, file, bytesTotal, NO_CALLBACK, false);
ByteBufferPage page = createPage(bytesTotal);
try {
for (int i = 0; i < 10_000; i++) {
clear(page);
assertThat(swapper.read(0, page), is((long) bytesTotal));
assertThat(array(page.buffer), is(data));
}
} finally {
swapper.close();
}
}
use of org.neo4j.io.pagecache.PageSwapper in project neo4j by neo4j.
the class SingleFilePageSwapperTest method fileMustRemainLockedEvenIfChannelIsClosedByStrayInterrupt.
@Test
public void fileMustRemainLockedEvenIfChannelIsClosedByStrayInterrupt() throws Exception {
// no file locking on Windows.
assumeFalse("No file locking on Windows", SystemUtils.IS_OS_WINDOWS);
PageSwapperFactory factory = createSwapperFactory();
factory.setFileSystemAbstraction(fileSystem);
File file = testDir.file("file");
fileSystem.create(file).close();
PageSwapper pageSwapper = createSwapper(factory, file, 4, NO_CALLBACK, false);
try {
StoreChannel channel = fileSystem.open(file, "rw");
Thread.currentThread().interrupt();
pageSwapper.force();
expectedException.expect(OverlappingFileLockException.class);
channel.tryLock();
} finally {
pageSwapper.close();
}
}
use of org.neo4j.io.pagecache.PageSwapper in project neo4j by neo4j.
the class SingleFilePageSwapperTest method swappingOutMustWritePageToFile.
@Test
public void swappingOutMustWritePageToFile() throws Exception {
getFs().create(getFile()).close();
byte[] expected = new byte[] { 1, 2, 3, 4 };
ByteBufferPage page = new ByteBufferPage(wrap(expected));
PageSwapperFactory factory = createSwapperFactory();
PageSwapper swapper = createSwapper(factory, getFile(), 4, null, false);
swapper.write(0, page);
InputStream stream = getFs().openAsInputStream(getFile());
byte[] actual = new byte[expected.length];
assertThat(stream.read(actual), is(actual.length));
assertThat(actual, byteArray(expected));
}
use of org.neo4j.io.pagecache.PageSwapper 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
public void creatingSwapperForFileMustTakeLockOnFile() throws Exception {
assumeFalse("No file locking on Windows", SystemUtils.IS_OS_WINDOWS);
PageSwapperFactory factory = createSwapperFactory();
factory.setFileSystemAbstraction(fileSystem);
File file = testDir.file("file");
fileSystem.create(file).close();
PageSwapper pageSwapper = createSwapper(factory, file, 4, NO_CALLBACK, false);
try {
StoreChannel channel = fileSystem.open(file, "rw");
expectedException.expect(OverlappingFileLockException.class);
channel.tryLock();
} finally {
pageSwapper.close();
}
}
use of org.neo4j.io.pagecache.PageSwapper in project neo4j by neo4j.
the class SingleFilePageSwapperTest method mustHandleMischiefInPositionedWrite.
@Test
public void mustHandleMischiefInPositionedWrite() throws Exception {
int bytesTotal = 512;
byte[] data = new byte[bytesTotal];
ThreadLocalRandom.current().nextBytes(data);
ByteBufferPage zeroPage = createPage(bytesTotal);
clear(zeroPage);
File file = getFile();
PageSwapperFactory factory = createSwapperFactory();
RandomAdversary adversary = new RandomAdversary(0.5, 0.0, 0.0);
factory.setFileSystemAbstraction(new AdversarialFileSystemAbstraction(adversary, getFs()));
PageSwapper swapper = createSwapper(factory, file, bytesTotal, NO_CALLBACK, true);
ByteBufferPage page = createPage(bytesTotal);
try {
for (int i = 0; i < 10_000; i++) {
adversary.setProbabilityFactor(0);
swapper.write(0, zeroPage);
page.putBytes(data, 0, 0, data.length);
adversary.setProbabilityFactor(1);
assertThat(swapper.write(0, page), is((long) bytesTotal));
clear(page);
adversary.setProbabilityFactor(0);
swapper.read(0, page);
assertThat(array(page.buffer), is(data));
}
} finally {
swapper.close();
}
}
Aggregations