Search in sources :

Example 1 with RotationListener

use of org.neo4j.logging.RotatingFileOutputStreamSupplier.RotationListener in project neo4j by neo4j.

the class RotatingFileOutputStreamSupplierTest method shouldNotifyListenerOnRotationErrorDuringRotationIO.

@Test
public void shouldNotifyListenerOnRotationErrorDuringRotationIO() throws Exception {
    RotationListener rotationListener = mock(RotationListener.class);
    FileSystemAbstraction fs = spy(fileSystem);
    RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier(fs, logFile, 10, 0, 10, DIRECT_EXECUTOR, rotationListener);
    OutputStream outputStream = supplier.get();
    IOException exception = new IOException("text exception");
    doThrow(exception).when(fs).renameFile(any(File.class), any(File.class));
    write(supplier, "A string longer than 10 bytes");
    assertThat(supplier.get(), is(outputStream));
    verify(rotationListener).rotationError(eq(exception), any(OutputStream.class));
}
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) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Example 2 with RotationListener

use of org.neo4j.logging.RotatingFileOutputStreamSupplier.RotationListener in project neo4j by neo4j.

the class RotatingFileOutputStreamSupplierTest method shouldNotifyListenerWhenNewLogIsCreated.

@Test
public void shouldNotifyListenerWhenNewLogIsCreated() throws Exception {
    final CountDownLatch allowRotationComplete = new CountDownLatch(1);
    final CountDownLatch rotationComplete = new CountDownLatch(1);
    String outputFileCreatedMessage = "Output file created";
    String rotationCompleteMessage = "Rotation complete";
    RotationListener rotationListener = spy(new RotationListener() {

        @Override
        public void outputFileCreated(OutputStream out) {
            try {
                allowRotationComplete.await(1L, TimeUnit.SECONDS);
                out.write(outputFileCreatedMessage.getBytes());
            } catch (InterruptedException | IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void rotationCompleted(OutputStream out) {
            rotationComplete.countDown();
            try {
                out.write(rotationCompleteMessage.getBytes());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
    RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier(fileSystem, logFile, 10, 0, 10, Executors.newSingleThreadExecutor(), rotationListener);
    write(supplier, "A string longer than 10 bytes");
    write(supplier, "A string longer than 10 bytes");
    allowRotationComplete.countDown();
    rotationComplete.await(1L, TimeUnit.SECONDS);
    verify(rotationListener).outputFileCreated(any(OutputStream.class));
    verify(rotationListener).rotationCompleted(any(OutputStream.class));
}
Also used : RotationListener(org.neo4j.logging.RotatingFileOutputStreamSupplier.RotationListener) AdversarialOutputStream(org.neo4j.adversaries.fs.AdversarialOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with RotationListener

use of org.neo4j.logging.RotatingFileOutputStreamSupplier.RotationListener 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 4 with RotationListener

use of org.neo4j.logging.RotatingFileOutputStreamSupplier.RotationListener in project neo4j by neo4j.

the class RotatingFileOutputStreamSupplierTest method rotationShouldNotDeadlockOnListener.

@Test(timeout = TEST_TIMEOUT_MILLIS)
public void rotationShouldNotDeadlockOnListener() throws Exception {
    String logContent = "Output file created";
    final AtomicReference<Exception> listenerException = new AtomicReference<>(null);
    CountDownLatch latch = new CountDownLatch(1);
    RotationListener listener = new RotationListener() {

        @Override
        public void outputFileCreated(OutputStream out) {
            try {
                Thread thread = new Thread(() -> {
                    try {
                        out.write(logContent.getBytes());
                        out.flush();
                    } catch (IOException e) {
                        listenerException.set(e);
                    }
                });
                thread.start();
                thread.join();
            } catch (Exception e) {
                listenerException.set(e);
            }
            super.outputFileCreated(out);
        }

        @Override
        public void rotationCompleted(OutputStream out) {
            latch.countDown();
        }
    };
    ExecutorService executor = Executors.newSingleThreadExecutor();
    DefaultFileSystemAbstraction defaultFileSystemAbstraction = new DefaultFileSystemAbstraction();
    RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier(defaultFileSystemAbstraction, logFile, 0, 0, 10, executor, listener);
    OutputStream outputStream = supplier.get();
    LockingPrintWriter lockingPrintWriter = new LockingPrintWriter(outputStream);
    lockingPrintWriter.withLock(() -> {
        supplier.rotate();
        latch.await();
        return Void.TYPE;
    });
    executor.shutdown();
    boolean terminated = executor.awaitTermination(TEST_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    if (!terminated) {
        throw new IllegalStateException("Rotation execution failed to complete within reasonable time.");
    }
    List<String> strings = Files.readAllLines(logFile.toPath());
    String actual = String.join("", strings);
    assertEquals(logContent, actual);
    assertNull(listenerException.get());
}
Also used : DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) AdversarialOutputStream(org.neo4j.adversaries.fs.AdversarialOutputStream) OutputStream(java.io.OutputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) FileNotFoundException(java.io.FileNotFoundException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) RotationListener(org.neo4j.logging.RotatingFileOutputStreamSupplier.RotationListener) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 5 with RotationListener

use of org.neo4j.logging.RotatingFileOutputStreamSupplier.RotationListener in project neo4j by neo4j.

the class RotatingFileOutputStreamSupplierTest method shouldNotifyListenerOnRotationErrorDuringJobExecution.

@Test
public void shouldNotifyListenerOnRotationErrorDuringJobExecution() throws Exception {
    RotationListener rotationListener = mock(RotationListener.class);
    Executor executor = mock(Executor.class);
    RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier(fileSystem, logFile, 10, 0, 10, executor, rotationListener);
    OutputStream outputStream = supplier.get();
    RejectedExecutionException exception = new RejectedExecutionException("text exception");
    doThrow(exception).when(executor).execute(any(Runnable.class));
    write(supplier, "A string longer than 10 bytes");
    assertThat(supplier.get(), is(outputStream));
    verify(rotationListener).rotationError(exception, outputStream);
}
Also used : Executor(java.util.concurrent.Executor) RotationListener(org.neo4j.logging.RotatingFileOutputStreamSupplier.RotationListener) AdversarialOutputStream(org.neo4j.adversaries.fs.AdversarialOutputStream) OutputStream(java.io.OutputStream) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 RotationListener (org.neo4j.logging.RotatingFileOutputStreamSupplier.RotationListener)7 OutputStream (java.io.OutputStream)6 AdversarialOutputStream (org.neo4j.adversaries.fs.AdversarialOutputStream)6 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 DefaultFileSystemAbstraction (org.neo4j.io.fs.DefaultFileSystemAbstraction)3 File (java.io.File)2 Executor (java.util.concurrent.Executor)2 AdversarialFileSystemAbstraction (org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction)2 DelegatingFileSystemAbstraction (org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction)2 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)2 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)2 FileNotFoundException (java.io.FileNotFoundException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1