Search in sources :

Example 1 with SelectiveFileSystemAbstraction

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

the class SelectiveFileSystemAbstractionTest method provideSelectiveWatcher.

@Test
public void provideSelectiveWatcher() throws IOException {
    File specialFile = new File("special");
    File otherFile = new File("other");
    FileSystemAbstraction normal = mock(FileSystemAbstraction.class);
    FileSystemAbstraction special = mock(FileSystemAbstraction.class);
    FileWatcher specialWatcher = mock(FileWatcher.class);
    FileWatcher normalWatcher = mock(FileWatcher.class);
    WatchedResource specialResource = mock(WatchedResource.class);
    WatchedResource normalResource = mock(WatchedResource.class);
    when(special.fileWatcher()).thenReturn(specialWatcher);
    when(normal.fileWatcher()).thenReturn(normalWatcher);
    when(specialWatcher.watch(specialFile)).thenReturn(specialResource);
    when(normalWatcher.watch(otherFile)).thenReturn(normalResource);
    try (SelectiveFileSystemAbstraction fs = new SelectiveFileSystemAbstraction(specialFile, special, normal)) {
        FileWatcher fileWatcher = fs.fileWatcher();
        assertSame(specialResource, fileWatcher.watch(specialFile));
        assertSame(normalResource, fileWatcher.watch(otherFile));
    }
}
Also used : SelectiveFileSystemAbstraction(org.neo4j.graphdb.mockfs.SelectiveFileSystemAbstraction) FileWatcher(org.neo4j.io.fs.watcher.FileWatcher) SelectiveFileSystemAbstraction(org.neo4j.graphdb.mockfs.SelectiveFileSystemAbstraction) WatchedResource(org.neo4j.io.fs.watcher.resource.WatchedResource) File(java.io.File) Test(org.junit.Test)

Example 2 with SelectiveFileSystemAbstraction

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

the class SelectiveFileSystemAbstractionTest method shouldUseDefaultFileSystemForOtherFiles.

@Test
public void shouldUseDefaultFileSystemForOtherFiles() throws Exception {
    // given
    File specialFile = new File("special");
    File otherFile = new File("other");
    FileSystemAbstraction normal = mock(FileSystemAbstraction.class);
    FileSystemAbstraction special = mock(FileSystemAbstraction.class);
    // when
    try (SelectiveFileSystemAbstraction fs = new SelectiveFileSystemAbstraction(specialFile, special, normal)) {
        fs.create(otherFile);
        fs.open(otherFile, "r");
        // then
        verify(normal).create(otherFile);
        verify(normal).open(otherFile, "r");
        verifyNoMoreInteractions(special);
        verifyNoMoreInteractions(normal);
    }
}
Also used : SelectiveFileSystemAbstraction(org.neo4j.graphdb.mockfs.SelectiveFileSystemAbstraction) SelectiveFileSystemAbstraction(org.neo4j.graphdb.mockfs.SelectiveFileSystemAbstraction) File(java.io.File) Test(org.junit.Test)

Example 3 with SelectiveFileSystemAbstraction

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

the class DurableStateStorageIT method shouldProperlyRecoveryAfterCloseOnActiveFileDuringRotation.

@Test
public void shouldProperlyRecoveryAfterCloseOnActiveFileDuringRotation() throws Exception {
    EphemeralFileSystemAbstraction normalFSA = fileSystemRule.get();
    AdversarialFileSystemAbstraction breakingFSA = new AdversarialFileSystemAbstraction(new MethodGuardedAdversary(new CountingAdversary(5, true), StoreChannel.class.getMethod("close")), normalFSA);
    SelectiveFileSystemAbstraction combinedFSA = new SelectiveFileSystemAbstraction(new File(new File(testDir.directory(), "long-state"), "long.a"), breakingFSA, normalFSA);
    long lastValue = 0;
    try (LongState persistedState = new LongState(combinedFSA, testDir.directory(), 14)) {
        while (// it will break from the Exception that AFS will throw
        true) {
            long tempValue = lastValue + 1;
            persistedState.setTheState(tempValue);
            lastValue = tempValue;
        }
    } catch (Exception expected) {
        // this stack trace should contain close()
        ensureStackTraceContainsExpectedMethod(expected.getStackTrace(), "close");
    }
    try (LongState restoredState = new LongState(normalFSA, testDir.directory(), 14)) {
        assertThat(restoredState.getTheState(), greaterThanOrEqualTo(lastValue));
    }
}
Also used : MethodGuardedAdversary(org.neo4j.adversaries.MethodGuardedAdversary) CountingAdversary(org.neo4j.adversaries.CountingAdversary) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) SelectiveFileSystemAbstraction(org.neo4j.graphdb.mockfs.SelectiveFileSystemAbstraction) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) File(java.io.File) IOException(java.io.IOException) EndOfStreamException(org.neo4j.causalclustering.messaging.EndOfStreamException) Test(org.junit.Test)

Example 4 with SelectiveFileSystemAbstraction

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

the class DurableStateStorageIT method shouldProperlyRecoveryAfterCrashOnFileCreationDuringRotation.

@Test
public void shouldProperlyRecoveryAfterCrashOnFileCreationDuringRotation() throws Exception {
    EphemeralFileSystemAbstraction normalFSA = fileSystemRule.get();
    /*
         * Magic number warning. For a rotation threshold of 14, 998 operations on file A falls on truncation of the
         * file during rotation. This has been discovered via experimentation. The end result is that there is a
         * failure to create the file to rotate to. This should be recoverable.
         */
    AdversarialFileSystemAbstraction breakingFSA = new AdversarialFileSystemAbstraction(new MethodGuardedAdversary(new CountingAdversary(20, true), FileSystemAbstraction.class.getMethod("truncate", File.class, long.class)), normalFSA);
    SelectiveFileSystemAbstraction combinedFSA = new SelectiveFileSystemAbstraction(new File(new File(testDir.directory(), "long-state"), "long.a"), breakingFSA, normalFSA);
    long lastValue = 0;
    try (LongState persistedState = new LongState(combinedFSA, testDir.directory(), 14)) {
        while (// it will break from the Exception that AFS will throw
        true) {
            long tempValue = lastValue + 1;
            persistedState.setTheState(tempValue);
            lastValue = tempValue;
        }
    } catch (Exception expected) {
        // this stack trace should contain FSA.truncate()
        ensureStackTraceContainsExpectedMethod(expected.getStackTrace(), "truncate");
    }
    try (LongState restoredState = new LongState(normalFSA, testDir.directory(), 14)) {
        assertEquals(lastValue, restoredState.getTheState());
    }
}
Also used : MethodGuardedAdversary(org.neo4j.adversaries.MethodGuardedAdversary) CountingAdversary(org.neo4j.adversaries.CountingAdversary) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) SelectiveFileSystemAbstraction(org.neo4j.graphdb.mockfs.SelectiveFileSystemAbstraction) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) File(java.io.File) IOException(java.io.IOException) EndOfStreamException(org.neo4j.causalclustering.messaging.EndOfStreamException) Test(org.junit.Test)

Example 5 with SelectiveFileSystemAbstraction

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

the class DurableStateStorageIT method shouldProperlyRecoveryAfterCrashOnFileForceDuringWrite.

@Test
public void shouldProperlyRecoveryAfterCrashOnFileForceDuringWrite() throws Exception {
    EphemeralFileSystemAbstraction normalFSA = fileSystemRule.get();
    /*
         * Magic number warning. For a rotation threshold of 14, 990 operations on file A falls on a force() of the
         * current active file. This has been discovered via experimentation. The end result is that there is a
         * flush (but not write) a value. This should be recoverable. Interestingly, the failure semantics are a bit
         * unclear on what should happen to that value. We assume that exception during persistence requires recovery
         * to discover if the last argument made it to disk or not. Since we use an EFSA, force is not necessary and
         * the value that caused the failure is actually "persisted" and recovered.
         */
    AdversarialFileSystemAbstraction breakingFSA = new AdversarialFileSystemAbstraction(new MethodGuardedAdversary(new CountingAdversary(40, true), StoreChannel.class.getMethod("force", boolean.class)), normalFSA);
    SelectiveFileSystemAbstraction combinedFSA = new SelectiveFileSystemAbstraction(new File(new File(testDir.directory(), "long-state"), "long.a"), breakingFSA, normalFSA);
    long lastValue = 0;
    try (LongState persistedState = new LongState(combinedFSA, testDir.directory(), 14)) {
        while (// it will break from the Exception that AFS will throw
        true) {
            long tempValue = lastValue + 1;
            persistedState.setTheState(tempValue);
            lastValue = tempValue;
        }
    } catch (Exception expected) {
        // this stack trace should contain force()
        ensureStackTraceContainsExpectedMethod(expected.getStackTrace(), "force");
    }
    try (LongState restoredState = new LongState(normalFSA, testDir.directory(), 14)) {
        assertThat(restoredState.getTheState(), greaterThanOrEqualTo(lastValue));
    }
}
Also used : MethodGuardedAdversary(org.neo4j.adversaries.MethodGuardedAdversary) CountingAdversary(org.neo4j.adversaries.CountingAdversary) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) SelectiveFileSystemAbstraction(org.neo4j.graphdb.mockfs.SelectiveFileSystemAbstraction) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) File(java.io.File) IOException(java.io.IOException) EndOfStreamException(org.neo4j.causalclustering.messaging.EndOfStreamException) Test(org.junit.Test)

Aggregations

File (java.io.File)6 Test (org.junit.Test)6 SelectiveFileSystemAbstraction (org.neo4j.graphdb.mockfs.SelectiveFileSystemAbstraction)6 IOException (java.io.IOException)3 CountingAdversary (org.neo4j.adversaries.CountingAdversary)3 MethodGuardedAdversary (org.neo4j.adversaries.MethodGuardedAdversary)3 AdversarialFileSystemAbstraction (org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction)3 EndOfStreamException (org.neo4j.causalclustering.messaging.EndOfStreamException)3 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)3 FileWatcher (org.neo4j.io.fs.watcher.FileWatcher)1 WatchedResource (org.neo4j.io.fs.watcher.resource.WatchedResource)1