Skip to content

Commit

Permalink
Make RotatingFileOutputStreamSupplierTest always shut the rotation ex…
Browse files Browse the repository at this point in the history
…ecutor down
  • Loading branch information
chrisvest committed Apr 20, 2017
1 parent 25a7b4b commit 5ed4387
Showing 1 changed file with 17 additions and 8 deletions.
Expand Up @@ -230,19 +230,24 @@ public void rotationCompleted( OutputStream out )
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." );
}
shutDownExecutor( executor );

List<String> strings = Files.readAllLines( logFile.toPath() );
String actual = String.join( "", strings );
assertEquals( logContent, actual );
assertNull( listenerException.get() );
}

private void shutDownExecutor( ExecutorService executor ) throws InterruptedException
{
executor.shutdown();
boolean terminated = executor.awaitTermination( TEST_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS );
if ( !terminated )
{
throw new IllegalStateException( "Rotation execution failed to complete within reasonable time." );
}
}

@Test
public void shouldNotRotateLogWhenSizeExceededButNotDelay() throws Exception
{
Expand Down Expand Up @@ -314,8 +319,9 @@ public void rotationCompleted( OutputStream out )
}
} );

ExecutorService rotationExecutor = Executors.newSingleThreadExecutor();
RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier( fileSystem, logFile, 10, 0,
10, Executors.newSingleThreadExecutor(), rotationListener );
10, rotationExecutor, rotationListener );

write( supplier, "A string longer than 10 bytes" );
write( supplier, "A string longer than 10 bytes" );
Expand All @@ -325,6 +331,7 @@ public void rotationCompleted( OutputStream out )

verify( rotationListener ).outputFileCreated( any( OutputStream.class ) );
verify( rotationListener ).rotationCompleted( any( OutputStream.class ) );
shutDownExecutor( rotationExecutor );
}

@Test
Expand Down Expand Up @@ -415,15 +422,17 @@ public OutputStream openAsOutputStream( File fileName, boolean append ) throws I
}
};

ExecutorService rotationExecutor = Executors.newSingleThreadExecutor();
RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier( fs, logFile, 10, 0,
10, Executors.newSingleThreadExecutor(), rotationListener );
10, rotationExecutor, rotationListener );
OutputStream outputStream = supplier.get();

write( supplier, "A string longer than 10 bytes" );
assertThat( supplier.get(), is( outputStream ) );

allowRotationComplete.countDown();
supplier.close();
shutDownExecutor( rotationExecutor );

assertStreamClosed( mockStreams.get( 0 ) );
}
Expand Down

0 comments on commit 5ed4387

Please sign in to comment.