Search in sources :

Example 6 with DelegatingFileSystemAbstraction

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

the class NodeStoreTest method shouldCloseStoreFileOnFailureToOpen.

@Test
public void shouldCloseStoreFileOnFailureToOpen() throws Exception {
    // GIVEN
    final AtomicBoolean fired = new AtomicBoolean();
    FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(efs.get()) {

        @Override
        public StoreChannel open(File fileName, String mode) throws IOException {
            return new DelegatingStoreChannel(super.open(fileName, mode)) {

                @Override
                public int read(ByteBuffer dst) throws IOException {
                    Exception stack = new Exception();
                    if (containsStackTraceElement(stack, item -> item.getMethodName().equals("initGenerator")) && !containsStackTraceElement(stack, item -> item.getMethodName().equals("createNodeStore"))) {
                        fired.set(true);
                        throw new IOException("Proving a point here");
                    }
                    return super.read(dst);
                }
            };
        }
    };
    // WHEN
    try (PageCache pageCache = pageCacheRule.getPageCache(fs)) {
        newNodeStore(fs);
        fail("Should fail");
    }// Close the page cache here so that we can see failure to close (due to still mapped files)
     catch (Exception e) {
        // THEN
        assertTrue(contains(e, IOException.class));
        assertTrue(fired.get());
    }
}
Also used : IdGeneratorFactory(org.neo4j.kernel.impl.store.id.IdGeneratorFactory) EphemeralFileSystemRule(org.neo4j.test.rule.fs.EphemeralFileSystemRule) Matchers.not(org.hamcrest.Matchers.not) NORMAL(org.neo4j.kernel.impl.store.record.RecordLoad.NORMAL) NO_NEXT_RELATIONSHIP(org.neo4j.kernel.impl.store.record.Record.NO_NEXT_RELATIONSHIP) NullLogProvider(org.neo4j.logging.NullLogProvider) ByteBuffer(java.nio.ByteBuffer) Assert.assertThat(org.junit.Assert.assertThat) StoreChannel(org.neo4j.io.fs.StoreChannel) Arrays.asList(java.util.Arrays.asList) After(org.junit.After) Assert.fail(org.junit.Assert.fail) ClassRule(org.junit.ClassRule) PageCache(org.neo4j.io.pagecache.PageCache) DefaultIdGeneratorFactory(org.neo4j.kernel.impl.store.id.DefaultIdGeneratorFactory) Predicate(java.util.function.Predicate) Collection(java.util.Collection) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) IdType(org.neo4j.kernel.impl.store.id.IdType) Assert.assertFalse(org.junit.Assert.assertFalse) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) Mockito.mock(org.mockito.Mockito.mock) Primitive(org.neo4j.collection.primitive.Primitive) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) PageCacheRule(org.neo4j.test.rule.PageCacheRule) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) NO_NEXT_PROPERTY(org.neo4j.kernel.impl.store.record.Record.NO_NEXT_PROPERTY) DynamicArrayStore.allocateFromNumbers(org.neo4j.kernel.impl.store.DynamicArrayStore.allocateFromNumbers) NodeStore.readOwnerFromDynamicLabelsRecord(org.neo4j.kernel.impl.store.NodeStore.readOwnerFromDynamicLabelsRecord) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) Config(org.neo4j.kernel.configuration.Config) Matchers.empty(org.hamcrest.Matchers.empty) LongStream(java.util.stream.LongStream) ReusableRecordsAllocator(org.neo4j.kernel.impl.store.allocator.ReusableRecordsAllocator) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) Exceptions.contains(org.neo4j.helpers.Exceptions.contains) Rule(org.junit.Rule) Iterables(org.neo4j.helpers.collection.Iterables) NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) Visitor(org.neo4j.helpers.collection.Visitor) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Example 7 with DelegatingFileSystemAbstraction

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

the class StoreLockerTest method shouldNotObtainLockWhenUnableToOpenLockFile.

@Test
public void shouldNotObtainLockWhenUnableToOpenLockFile() throws Exception {
    FileSystemAbstraction fileSystemAbstraction = new DelegatingFileSystemAbstraction(fileSystemRule.get()) {

        @Override
        public StoreChannel open(File fileName, String mode) throws IOException {
            throw new IOException("cannot open lock file");
        }

        @Override
        public boolean fileExists(File fileName) {
            return false;
        }
    };
    File storeDir = target.directory("unused");
    try (StoreLocker storeLocker = new StoreLocker(fileSystemAbstraction)) {
        storeLocker.checkLock(storeDir);
        fail();
    } catch (StoreLockException e) {
        String msg = format("Unable to obtain lock on store lock file: %s. " + "Please ensure no other process is using this database, and that the " + "directory is writable (required even for read-only access)", new File(storeDir, STORE_LOCK_FILENAME));
        assertThat(e.getMessage(), is(msg));
    }
}
Also used : DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) StoreLockException(org.neo4j.kernel.StoreLockException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) Test(org.junit.Test)

Example 8 with DelegatingFileSystemAbstraction

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

the class StoreLockerTest method shouldNotObtainLockWhenStoreDirCannotBeCreated.

@Test
public void shouldNotObtainLockWhenStoreDirCannotBeCreated() throws Exception {
    FileSystemAbstraction fileSystemAbstraction = new DelegatingFileSystemAbstraction(fileSystemRule.get()) {

        @Override
        public void mkdirs(File fileName) throws IOException {
            throw new IOException("store dir could not be created");
        }

        @Override
        public boolean fileExists(File fileName) {
            return false;
        }
    };
    File storeDir = target.directory("unused");
    try (StoreLocker storeLocker = new StoreLocker(fileSystemAbstraction)) {
        storeLocker.checkLock(storeDir);
        fail();
    } catch (StoreLockException e) {
        String msg = format("Unable to create path for store dir: %s. " + "Please ensure no other process is using this database, and that " + "the directory is writable (required even for read-only access)", storeDir);
        assertThat(e.getMessage(), is(msg));
    }
}
Also used : DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) StoreLockException(org.neo4j.kernel.StoreLockException) IOException(java.io.IOException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) Test(org.junit.Test)

Example 9 with DelegatingFileSystemAbstraction

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

the class FileRoleRepositoryTest method shouldRecoverIfCrashedDuringMove.

@Test
public void shouldRecoverIfCrashedDuringMove() throws Throwable {
    // Given
    final IOException exception = new IOException("simulated IO Exception on create");
    FileSystemAbstraction craschingFileSystem = new DelegatingFileSystemAbstraction(fs) {

        @Override
        public void renameFile(File oldLocation, File newLocation, CopyOption... copyOptions) throws IOException {
            if (roleFile.getName().equals(newLocation.getName())) {
                throw exception;
            }
            super.renameFile(oldLocation, newLocation, copyOptions);
        }
    };
    roleRepository = new FileRoleRepository(craschingFileSystem, roleFile, logProvider);
    roleRepository.start();
    RoleRecord role = new RoleRecord("admin", "jake");
    // When
    try {
        roleRepository.create(role);
        fail("Expected an IOException");
    } catch (IOException e) {
        assertSame(exception, e);
    }
    // Then
    assertFalse(craschingFileSystem.fileExists(roleFile));
    assertThat(craschingFileSystem.listFiles(roleFile.getParentFile()).length, equalTo(0));
}
Also used : DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) DelegateFileSystemAbstraction(org.neo4j.io.fs.DelegateFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) CopyOption(java.nio.file.CopyOption) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) Test(org.junit.Test)

Example 10 with DelegatingFileSystemAbstraction

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

the class FileUserRepositoryTest method shouldRecoverIfCrashedDuringMove.

@Test
public void shouldRecoverIfCrashedDuringMove() throws Throwable {
    // Given
    final IOException exception = new IOException("simulated IO Exception on create");
    FileSystemAbstraction craschingFileSystem = new DelegatingFileSystemAbstraction(fs) {

        @Override
        public void renameFile(File oldLocation, File newLocation, CopyOption... copyOptions) throws IOException {
            if (authFile.getName().equals(newLocation.getName())) {
                throw exception;
            }
            super.renameFile(oldLocation, newLocation, copyOptions);
        }
    };
    FileUserRepository users = new FileUserRepository(craschingFileSystem, authFile, logProvider);
    users.start();
    User user = new User.Builder("jake", Credential.INACCESSIBLE).withRequiredPasswordChange(true).build();
    // When
    try {
        users.create(user);
        fail("Expected an IOException");
    } catch (IOException e) {
        assertSame(exception, e);
    }
    // Then
    assertFalse(craschingFileSystem.fileExists(authFile));
    assertThat(craschingFileSystem.listFiles(authFile.getParentFile()).length, equalTo(0));
}
Also used : DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) DelegateFileSystemAbstraction(org.neo4j.io.fs.DelegateFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) CopyOption(java.nio.file.CopyOption) User(org.neo4j.kernel.impl.security.User) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)17 Test (org.junit.Test)17 DelegatingFileSystemAbstraction (org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction)17 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)16 IOException (java.io.IOException)14 DelegatingStoreChannel (org.neo4j.graphdb.mockfs.DelegatingStoreChannel)9 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)9 ByteBuffer (java.nio.ByteBuffer)7 Long.toHexString (java.lang.Long.toHexString)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 AdversarialPagedFile (org.neo4j.adversaries.pagecache.AdversarialPagedFile)5 ArrayList (java.util.ArrayList)4 OutputStream (java.io.OutputStream)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 AdversarialFileSystemAbstraction (org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction)3 AdversarialOutputStream (org.neo4j.adversaries.fs.AdversarialOutputStream)3 DefaultFileSystemAbstraction (org.neo4j.io.fs.DefaultFileSystemAbstraction)3 StoreChannel (org.neo4j.io.fs.StoreChannel)3