Search in sources :

Example 11 with DelegatingFileSystemAbstraction

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

the class PageCacheTest method flushingDuringPagedFileCloseMustRetryUntilItSucceeds.

@RepeatRule.Repeat(times = 100)
@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void flushingDuringPagedFileCloseMustRetryUntilItSucceeds() throws IOException {
    FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(this.fs) {

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

                private int writeCount = 0;

                @Override
                public void writeAll(ByteBuffer src, long position) throws IOException {
                    if (writeCount++ < 10) {
                        throw new IOException("This is a benign exception that we expect to be thrown " + "during a flush of a PagedFile.");
                    }
                    super.writeAll(src, position);
                }
            };
        }
    };
    getPageCache(fs, maxPages, pageCachePageSize, PageCacheTracer.NULL, PageCursorTracerSupplier.NULL);
    PrintStream oldSystemErr = System.err;
    try (PagedFile pf = pageCache.map(file("a"), filePageSize);
        PageCursor cursor = pf.io(0, PF_SHARED_WRITE_LOCK)) {
        assertTrue(cursor.next());
        writeRecords(cursor);
        // Silence any stack traces the failed flushes might print.
        System.setErr(new PrintStream(new ByteArrayOutputStream()));
    } finally {
        System.setErr(oldSystemErr);
    }
    verifyRecordsInFile(file("a"), recordsPerFilePage);
}
Also used : PrintStream(java.io.PrintStream) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) Long.toHexString(java.lang.Long.toHexString) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 12 with DelegatingFileSystemAbstraction

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

the class SingleFilePageSwapperTest method mustCloseFilesIfTakingFileLockThrows.

@Test
public void mustCloseFilesIfTakingFileLockThrows() throws Exception {
    // no file locking on Windows.
    assumeFalse("No file locking on Windows", SystemUtils.IS_OS_WINDOWS);
    final AtomicInteger openFilesCounter = new AtomicInteger();
    PageSwapperFactory factory = createSwapperFactory();
    factory.setFileSystemAbstraction(new DelegatingFileSystemAbstraction(fileSystem) {

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

                @Override
                public void close() throws IOException {
                    openFilesCounter.getAndDecrement();
                    super.close();
                }
            };
        }
    });
    File file = testDir.file("file");
    try (StoreChannel ch = fileSystem.create(file);
        FileLock ignore = ch.tryLock()) {
        createSwapper(factory, file, 4, NO_CALLBACK, false).close();
        fail("Creating a page swapper for a locked channel should have thrown");
    } catch (FileLockException e) {
    // As expected.
    }
    assertThat(openFilesCounter.get(), is(0));
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) FileLock(java.nio.channels.FileLock) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 13 with DelegatingFileSystemAbstraction

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

the class StoreLockerTest method shouldNotObtainLockWhenStoreAlreadyInUse.

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

        @Override
        public boolean fileExists(File fileName) {
            return false;
        }

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

                @Override
                public FileLock tryLock() throws IOException {
                    // 'null' implies that the file has been externally locked
                    return null;
                }
            };
        }
    };
    try (StoreLocker storeLocker = new StoreLocker(fileSystemAbstraction)) {
        storeLocker.checkLock(target.directory("unused"));
        fail();
    } catch (StoreLockException e) {
        assertThat(e.getMessage(), containsString("Store and its lock file has been locked by another process"));
    }
}
Also used : DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) StoreLockException(org.neo4j.kernel.StoreLockException) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) Test(org.junit.Test)

Example 14 with DelegatingFileSystemAbstraction

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

the class RotatingFileOutputStreamSupplierTest method shouldNotUpdateOutputStreamWhenClosedDuringRotation.

@Test
public void shouldNotUpdateOutputStreamWhenClosedDuringRotation() throws Exception {
    final CountDownLatch allowRotationComplete = new CountDownLatch(1);
    RotationListener rotationListener = spy(new RotationListener() {

        @Override
        public void outputFileCreated(OutputStream out) {
            try {
                allowRotationComplete.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    final List<OutputStream> mockStreams = new ArrayList<>();
    FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(fileSystem) {

        @Override
        public OutputStream openAsOutputStream(File fileName, boolean append) throws IOException {
            final OutputStream stream = spy(super.openAsOutputStream(fileName, append));
            mockStreams.add(stream);
            return stream;
        }
    };
    RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier(fs, logFile, 10, 0, 10, Executors.newSingleThreadExecutor(), rotationListener);
    OutputStream outputStream = supplier.get();
    write(supplier, "A string longer than 10 bytes");
    assertThat(supplier.get(), is(outputStream));
    allowRotationComplete.countDown();
    supplier.close();
    assertStreamClosed(mockStreams.get(0));
}
Also used : AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) RotationListener(org.neo4j.logging.RotatingFileOutputStreamSupplier.RotationListener) AdversarialOutputStream(org.neo4j.adversaries.fs.AdversarialOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) Test(org.junit.Test)

Example 15 with DelegatingFileSystemAbstraction

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

the class RotatingFileOutputStreamSupplierTest method shouldCloseAllStreamsDespiteError.

@Test
public void shouldCloseAllStreamsDespiteError() throws Exception {
    final List<OutputStream> mockStreams = new ArrayList<>();
    FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(fileSystem) {

        @Override
        public OutputStream openAsOutputStream(File fileName, boolean append) throws IOException {
            final OutputStream stream = spy(super.openAsOutputStream(fileName, append));
            mockStreams.add(stream);
            return stream;
        }
    };
    RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier(fs, logFile, 10, 0, 10, DIRECT_EXECUTOR);
    write(supplier, "A string longer than 10 bytes");
    write(supplier, "A string longer than 10 bytes");
    IOException exception = new IOException("test exception");
    OutputStream mockStream = mockStreams.get(1);
    doThrow(exception).when(mockStream).close();
    try {
        supplier.close();
    } catch (IOException e) {
        assertThat(e, sameInstance(exception));
    }
    verify(mockStream).close();
}
Also used : AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) AdversarialOutputStream(org.neo4j.adversaries.fs.AdversarialOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) 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