use of org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction in project neo4j by neo4j.
the class DurableStateStorageTest method shouldClearFileOnFirstUse.
@Test
public void shouldClearFileOnFirstUse() throws Throwable {
// given
EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
fsa.mkdir(testDir.directory());
int rotationCount = 10;
DurableStateStorage<AtomicInteger> storage = new DurableStateStorage<>(fsa, testDir.directory(), "state", new AtomicIntegerMarshal(), rotationCount, NullLogProvider.getInstance());
int largestValueWritten = 0;
try (Lifespan lifespan = new Lifespan(storage)) {
for (; largestValueWritten < rotationCount * 2; largestValueWritten++) {
storage.persistStoreData(new AtomicInteger(largestValueWritten));
}
}
// now both files are full. We reopen, then write some more.
storage = lifeRule.add(new DurableStateStorage<>(fsa, testDir.directory(), "state", new AtomicIntegerMarshal(), rotationCount, NullLogProvider.getInstance()));
storage.persistStoreData(new AtomicInteger(largestValueWritten++));
storage.persistStoreData(new AtomicInteger(largestValueWritten++));
storage.persistStoreData(new AtomicInteger(largestValueWritten));
/*
* We have written stuff in fileA but not gotten to the end (resulting in rotation). The largestValueWritten
* should nevertheless be correct
*/
ByteBuffer forReadingBackIn = ByteBuffer.allocate(10_000);
StoreChannel lastWrittenTo = fsa.open(stateFileA(), "r");
lastWrittenTo.read(forReadingBackIn);
forReadingBackIn.flip();
AtomicInteger lastRead = null;
while (true) {
try {
lastRead = new AtomicInteger(forReadingBackIn.getInt());
} catch (BufferUnderflowException e) {
break;
}
}
// then
assertNotNull(lastRead);
assertEquals(largestValueWritten, lastRead.get());
}
use of org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction in project neo4j by neo4j.
the class DurableStateStorageTest method shouldRotateBackToFirstStoreFileAfterSufficientEntries.
@Test
public void shouldRotateBackToFirstStoreFileAfterSufficientEntries() throws Exception {
// given
EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
fsa.mkdir(testDir.directory());
final int numberOfEntriesBeforeRotation = 100;
DurableStateStorage<AtomicInteger> storage = lifeRule.add(new DurableStateStorage<>(fsa, testDir.directory(), "state", new AtomicIntegerMarshal(), numberOfEntriesBeforeRotation, NullLogProvider.getInstance()));
// when
for (int i = 0; i < numberOfEntriesBeforeRotation * 2; i++) {
storage.persistStoreData(new AtomicInteger(i));
}
// Force the rotation back to the first store
storage.persistStoreData(new AtomicInteger(9999));
// then
assertEquals(4, fsa.getFileSize(stateFileA()));
assertEquals(numberOfEntriesBeforeRotation * 4, fsa.getFileSize(stateFileB()));
}
use of org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction in project neo4j by neo4j.
the class ReadAheadChannelTest method shouldReturnPositionWithinBufferedStream.
@Test
public void shouldReturnPositionWithinBufferedStream() throws Exception {
// given
EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
File file = new File("foo.txt");
int readAheadSize = 512;
int fileSize = readAheadSize * 8;
createFile(fsa, file, fileSize);
ReadAheadChannel<StoreChannel> bufferedReader = new ReadAheadChannel<>(fsa.open(file, "r"), readAheadSize);
// when
for (int i = 0; i < fileSize / Long.BYTES; i++) {
assertEquals(Long.BYTES * i, bufferedReader.position());
bufferedReader.getLong();
}
assertEquals(fileSize, bufferedReader.position());
try {
bufferedReader.getLong();
fail();
} catch (ReadPastEndException e) {
// expected
}
assertEquals(fileSize, bufferedReader.position());
}
use of org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction in project neo4j by neo4j.
the class TestTxEntries method testStartEntryWrittenOnceOnRollback.
/*
* Starts a JVM, executes a tx that fails on prepare and rollbacks,
* triggering a bug where an extra start entry for that tx is written
* in the xa log.
*/
@Test
public void testStartEntryWrittenOnceOnRollback() throws Exception {
final GraphDatabaseService db = new TestGraphDatabaseFactory().setFileSystem(fs.get()).newImpermanentDatabase(storeDir);
createSomeTransactions(db);
EphemeralFileSystemAbstraction snapshot = fs.snapshot(() -> db.shutdown());
new TestGraphDatabaseFactory().setFileSystem(snapshot).newImpermanentDatabase(storeDir).shutdown();
}
use of org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction in project neo4j by neo4j.
the class TestEphemeralFileChannel method absoluteVersusRelative.
@Test
public void absoluteVersusRelative() throws Exception {
// GIVEN
File file = new File("myfile");
EphemeralFileSystemAbstraction fs = fileSystemRule.get();
StoreChannel channel = fs.open(file, "rw");
byte[] bytes = "test".getBytes();
channel.write(ByteBuffer.wrap(bytes));
channel.close();
// WHEN
channel = fs.open(new File(file.getAbsolutePath()), "r");
byte[] readBytes = new byte[bytes.length];
int nrOfReadBytes = channel.read(ByteBuffer.wrap(readBytes));
// THEN
assertEquals(bytes.length, nrOfReadBytes);
assertTrue(Arrays.equals(bytes, readBytes));
fs.close();
}
Aggregations