private void write( RotatingFileOutputStreamSupplier supplier, String line ) { PrintWriter writer = new PrintWriter( supplier.get() ); writer.println( line ); writer.flush(); }
@Test void shouldReattemptRotationAfterExceptionDuringJobExecution() 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 ) ); assertThat( supplier.get(), is( outputStream ) ); verify( rotationListener, times( 2 ) ).rotationError( exception, outputStream ); }
@Test 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 ); }
@Test 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 ) ); }
new RotatingFileOutputStreamSupplier( defaultFileSystemAbstraction, logFile, 0, 0, 10, executor, listener ); OutputStream outputStream = supplier.get(); LockingPrintWriter lockingPrintWriter = new LockingPrintWriter( outputStream ); lockingPrintWriter.withLock( () ->