Skip to content

Commit

Permalink
#26 Fix failing tests due to the recently added RotationPolicy#stop().
Browse files Browse the repository at this point in the history
  • Loading branch information
vy committed Jul 26, 2021
1 parent ae8747a commit 974e611
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
architecture: x64

- name: Build with Maven
run: ./mvnw -B verify
run: ./mvnw -V -B --no-transfer-progress -e "-DtrimStackTrace=false" verify

- name: Test scheduler shutdown
timeout-minutes: 1
Expand Down
57 changes: 30 additions & 27 deletions src/test/java/com/vlkan/rfos/RotatingFileOutputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;

import static org.assertj.core.api.Assertions.assertThat;

class RotatingFileOutputStreamTest {

private static final Logger LOGGER = LoggerFactory.getLogger(RotatingFileOutputStreamTest.class);
Expand All @@ -67,10 +65,10 @@ void test_write_insensitive_policy_with_compression() throws Exception {
private void test_write_insensitive_policy(boolean compress) throws Exception {

// Determine file names.
String className = RotatingFileOutputStream.class.getSimpleName();
File file = new File(tmpDir, className + ".log");
String fileNamePrefix = "writeInsensitivePolicy-compress-" + String.valueOf(compress).toLowerCase();
File file = new File(tmpDir, fileNamePrefix + ".log");
String fileName = file.getAbsolutePath();
String fileNamePattern = new File(tmpDir, className + "-%d{yyyy}.log").getAbsolutePath();
String fileNamePattern = new File(tmpDir, fileNamePrefix + "-%d{yyyy}.log").getAbsolutePath();
String rotatedFileNameSuffix = compress ? ".gz" : "";
Instant now = Instant.now();
File rotatedFile = new File(
Expand All @@ -92,7 +90,7 @@ private void test_write_insensitive_policy(boolean compress) throws Exception {
.file(fileName)
.filePattern(fileNamePattern)
.policy(policy)
.callback(callback)
.callbacks(Collections.singleton(callback))
.build();
InOrder inOrder = Mockito.inOrder(callback, policy);
RotatingFileOutputStream stream = new RotatingFileOutputStream(config);
Expand Down Expand Up @@ -155,18 +153,19 @@ private void test_write_insensitive_policy(boolean compress) throws Exception {
int expectedRotatedFileLength = compress
? findCompressedLength(payload)
: payload.length;
assertThat(rotatedFileLength).isEqualTo(expectedRotatedFileLength);
assertThat(file.length()).isEqualTo(0);
Assertions.assertThat(rotatedFileLength).isEqualTo(expectedRotatedFileLength);
Assertions.assertThat(file.length()).isEqualTo(0);

// Verify the stream close. (We cannot use InOrder here since we don't
// know whether the rotation background task or the user-invoked close()
// will finish earlier.)
// Verify the stream close and the policy shutdown. (We cannot use
// InOrder here since we don't know whether the rotation background task
// due to compression or the user-invoked close() will finish earlier.)
Mockito
.verify(callback)
.onClose(
Mockito.isNull(),
Mockito.any(Instant.class),
Mockito.any(OutputStream.class));
Mockito.verify(policy).stop();

// Verify no more interactions.
Mockito.verifyNoMoreInteractions(callback);
Expand All @@ -189,10 +188,10 @@ private static int findCompressedLength(byte[] inputBytes) {
void test_write_sensitive_policy() throws Exception {

// Determine file names.
String className = RotatingFileOutputStream.class.getSimpleName();
File file = new File(tmpDir, className + ".log");
String fileNamePrefix = "writeSensitivePolicy";
File file = new File(tmpDir, fileNamePrefix + ".log");
String fileName = file.getAbsolutePath();
String fileNamePattern = new File(tmpDir, className + "-%d{yyyy}.log").getAbsolutePath();
String fileNamePattern = new File(tmpDir, fileNamePrefix + "-%d{yyyy}.log").getAbsolutePath();
File rotatedFile = new File(fileNamePattern.replace("%d{yyyy}", String.valueOf(Calendar.getInstance().get(Calendar.YEAR))));

// Create the stream.
Expand All @@ -205,7 +204,7 @@ void test_write_sensitive_policy() throws Exception {
.file(fileName)
.filePattern(fileNamePattern)
.policy(policy)
.callback(callback)
.callbacks(Collections.singleton(callback))
.build();
RotatingFileOutputStream stream = new RotatingFileOutputStream(config);

Expand All @@ -224,7 +223,7 @@ void test_write_sensitive_policy() throws Exception {
for (int byteIndex = 0; byteIndex < maxByteCount; byteIndex++) {
stream.write(byteIndex);
}
assertThat(file.length()).isEqualTo(maxByteCount);
Assertions.assertThat(file.length()).isEqualTo(maxByteCount);

// Verify no rotations so far.
Mockito.verifyNoMoreInteractions(callback);
Expand Down Expand Up @@ -262,8 +261,8 @@ void test_write_sensitive_policy() throws Exception {
Mockito.same(policy),
Mockito.any(Instant.class),
Mockito.eq(rotatedFile));
assertThat(rotatedFile.length()).isEqualTo(maxByteCount);
assertThat(file.length()).isEqualTo(1);
Assertions.assertThat(rotatedFile.length()).isEqualTo(maxByteCount);
Assertions.assertThat(file.length()).isEqualTo(1);

// Verify no more callback interactions.
Mockito.verifyNoMoreInteractions(callback);
Expand All @@ -289,7 +288,7 @@ void test_empty_files_are_not_rotated() throws Exception {
.file(fileName)
.filePattern(fileNamePattern)
.policy(policy)
.callback(callback)
.callbacks(Collections.singleton(callback))
.build();
RotatingFileOutputStream stream = new RotatingFileOutputStream(config);

Expand All @@ -310,7 +309,7 @@ void test_empty_files_are_not_rotated() throws Exception {
}
stream.write(payload);
stream.flush();
assertThat(file.length()).isEqualTo(payload.length);
Assertions.assertThat(file.length()).isEqualTo(payload.length);

// Verify the rotation trigger.
callbackInOrder
Expand Down Expand Up @@ -342,7 +341,7 @@ void test_adding_file_header() throws IOException {
.file(fileName)
.filePattern(fileNamePattern)
.policy(policy)
.callback(callback)
.callbacks(Collections.singleton(callback))
.build();

// Create the header injectors.
Expand Down Expand Up @@ -503,14 +502,16 @@ private static byte[] copyArrays(byte[]... sources) {
void test_rotation_and_write_failure_after_close() throws Exception {

// Determine file names.
String className = RotatingFileOutputStream.class.getSimpleName();
File file = new File(tmpDir, className + ".log");
String fileNamePrefix = "rotationAndWriteFailureAfterClose";
File file = new File(tmpDir, fileNamePrefix + ".log");
String fileName = file.getAbsolutePath();
String fileNamePattern = new File(tmpDir, className + "-%d{yyyy}.log").getAbsolutePath();
String fileNamePattern = new File(tmpDir, fileNamePrefix + "-%d{HHmmss-SSS}.log").getAbsolutePath();

// Create the stream config.
RotationPolicy policy = Mockito.mock(RotationPolicy.class);
Mockito.when(policy.toString()).thenReturn("MockedPolicy");
RotationCallback callback = Mockito.mock(RotationCallback.class);
Mockito.when(callback.toString()).thenReturn("MockedCallback");
RotationConfig config = RotationConfig
.builder()
.file(fileName)
Expand Down Expand Up @@ -574,20 +575,22 @@ void test_rotation_and_write_failure_after_close() throws Exception {
void test_time_based_policies_are_stopped_after_close() throws Exception {

// Determine file names.
String className = RotatingFileOutputStream.class.getSimpleName();
File file = new File(tmpDir, className + ".log");
String fileNamePrefix = "timeBasedPoliciesAfterClose";
File file = new File(tmpDir, fileNamePrefix + ".log");
String fileName = file.getAbsolutePath();
String fileNamePattern = new File(tmpDir, className + "-%d{yyyy}.log").getAbsolutePath();
String fileNamePattern = new File(tmpDir, fileNamePrefix + "-%d{HHmmss-SSS}.log").getAbsolutePath();

// Create the scheduler mock.
ScheduledFuture<?> scheduledFuture = Mockito.mock(ScheduledFuture.class);
Mockito.when(scheduledFuture.toString()).thenReturn("MockedScheduledFuture");
ScheduledExecutorService executorService = Mockito.mock(ScheduledExecutorService.class);
Mockito
.when(executorService.schedule(
Mockito.any(Runnable.class),
Mockito.anyLong(),
Mockito.same(TimeUnit.MILLISECONDS)))
.thenAnswer((Answer<ScheduledFuture<?>>) invocationOnMock -> scheduledFuture);
Mockito.when(executorService.toString()).thenReturn("MockedScheduledExecutorService");

// Create the stream config.
LinkedHashSet<RotationPolicy> policies =
Expand Down

0 comments on commit 974e611

Please sign in to comment.