Search in sources :

Example 51 with EphemeralFileSystemAbstraction

use of org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction in project neo4j by neo4j.

the class LegacyLogsTest method shouldMoveFiles.

@Test
public void shouldMoveFiles() throws Exception {
    // given
    try (EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction()) {
        fs.mkdirs(storeDir);
        fs.mkdirs(migrationDir);
        final Set<File> logsInStoreDir = new HashSet<>(Arrays.asList(new File(storeDir, getLegacyLogFilename(1)), new File(storeDir, getLegacyLogFilename(2))));
        final List<File> logsInMigrationDir = Arrays.asList(new File(migrationDir, getLegacyLogFilename(1)), new File(migrationDir, getLegacyLogFilename(2)));
        for (File file : logsInMigrationDir) {
            try (StoreChannel channel = fs.create(file)) {
                ByteBuffer buffer = ByteBuffer.allocate(8);
                buffer.putLong(42);
                buffer.flip();
                channel.write(buffer);
            }
        }
        // should override older files
        for (File file : logsInStoreDir) {
            try (StoreChannel channel = fs.create(file)) {
                ByteBuffer buffer = ByteBuffer.allocate(8);
                buffer.putLong(13);
                buffer.flip();
                channel.write(buffer);
            }
        }
        // when
        new LegacyLogs(fs, reader, writer).operate(FileOperation.MOVE, migrationDir, storeDir);
        // then
        assertEquals(logsInStoreDir, new HashSet<>(Arrays.asList(fs.listFiles(storeDir))));
        for (File file : logsInStoreDir) {
            try (StoreChannel channel = fs.open(file, "r")) {
                ByteBuffer buffer = ByteBuffer.allocate(8);
                channel.read(buffer);
                buffer.flip();
                assertEquals(42, buffer.getLong());
            }
        }
    }
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 52 with EphemeralFileSystemAbstraction

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());
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) ReadPastEndException(org.neo4j.storageengine.api.ReadPastEndException) Test(org.junit.Test)

Example 53 with EphemeralFileSystemAbstraction

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();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Test(org.junit.Test)

Example 54 with EphemeralFileSystemAbstraction

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();
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) Test(org.junit.Test)

Aggregations

EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)54 Test (org.junit.Test)37 File (java.io.File)27 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)10 StoreChannel (org.neo4j.io.fs.StoreChannel)8 IOException (java.io.IOException)7 AdversarialFileSystemAbstraction (org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction)7 Before (org.junit.Before)6 CountingAdversary (org.neo4j.adversaries.CountingAdversary)6 Transaction (org.neo4j.graphdb.Transaction)6 ByteBuffer (java.nio.ByteBuffer)5 MethodGuardedAdversary (org.neo4j.adversaries.MethodGuardedAdversary)5 StateRecoveryManager (org.neo4j.causalclustering.core.state.StateRecoveryManager)5 EndOfStreamException (org.neo4j.causalclustering.messaging.EndOfStreamException)5 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Node (org.neo4j.graphdb.Node)4 SelectiveFileSystemAbstraction (org.neo4j.graphdb.mockfs.SelectiveFileSystemAbstraction)4 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)4 NodeRecord (org.neo4j.kernel.impl.store.record.NodeRecord)4