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));
}
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));
}
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));
}
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());
}
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);
}
Aggregations