ramanathan1504 commented on code in PR #4440:
URL: https://github.com/apache/logging-log4j2/pull/4440#discussion_r4596797370


##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicyTest.java:
##########
@@ -186,4 +192,101 @@ void testFactoryMethodSequence() {
         testFactoryMethod();
         testFactoryMethod();
     }
+
+    private RollingFileManager mockManager(final long fileTime) {
+        final RollingFileManager manager = mock(RollingFileManager.class);
+        when(manager.getFileTime()).thenReturn(fileTime);
+        when(manager.getPatternProcessor()).thenReturn(new 
PatternProcessor("target/testcmd6.log.%d{yyyy-MM-dd}"));
+        return manager;
+    }
+
+    /**
+     * Tests that evaluateOnStartup archives a file from a previous period 
under that period. Otherwise the next
+     * scheduled rollover, which archives under the current period, overwrites 
it.
+     */

Review Comment:
   ```suggestion
   ```



##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicyTest.java:
##########
@@ -186,4 +192,101 @@ void testFactoryMethodSequence() {
         testFactoryMethod();
         testFactoryMethod();
     }
+
+    private RollingFileManager mockManager(final long fileTime) {
+        final RollingFileManager manager = mock(RollingFileManager.class);
+        when(manager.getFileTime()).thenReturn(fileTime);
+        when(manager.getPatternProcessor()).thenReturn(new 
PatternProcessor("target/testcmd6.log.%d{yyyy-MM-dd}"));
+        return manager;
+    }
+
+    /**
+     * Tests that evaluateOnStartup archives a file from a previous period 
under that period. Otherwise the next
+     * scheduled rollover, which archives under the current period, overwrites 
it.
+     */
+    @Test
+    void testEvaluateOnStartupArchivesFileUnderItsOwnPeriod() throws Exception 
{
+        final CronExpression cronExpression = new 
CronExpression(CRON_EXPRESSION);
+        final Date periodStart = cronExpression.getPrevFireTime(new Date());
+        final Date previousPeriodStart = 
cronExpression.getPrevFireTime(periodStart);
+        final long fileTime = previousPeriodStart.getTime() + 
TimeUnit.HOURS.toMillis(12);
+        final RollingFileManager manager = mockManager(fileTime);
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+            verify(manager).rollover(eq(previousPeriodStart), any(Date.class));
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+    }
+
+    /**
+     * Tests that evaluateOnStartup does not roll over a file whose file time 
equals the current period's fire time.
+     */
+    @Test
+    void testEvaluateOnStartupDoesNotRollFileCreatedAtFireTime() throws 
Exception {
+        final Date periodStart = new 
CronExpression(CRON_EXPRESSION).getPrevFireTime(new Date());
+        final RollingFileManager manager = mockManager(periodStart.getTime());
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+            verify(manager, never()).rollover(any(Date.class), 
any(Date.class));
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+    }
+
+    /**
+     * Tests that, after a missed fire, evaluateOnStartup archives a file 
created by a scheduled rollover under that
+     * rollover's period. Such a file has a file time of exactly the fire 
time. Archiving it one period early would
+     * overwrite the archive that the scheduled rollover created.
+     */
+    @Test
+    void testEvaluateOnStartupArchivesFileCreatedAtFireTimeUnderItsOwnPeriod() 
throws Exception {
+        final CronExpression cronExpression = new 
CronExpression(CRON_EXPRESSION);
+        final Date previousPeriodStart = 
cronExpression.getPrevFireTime(cronExpression.getPrevFireTime(new Date()));
+        final RollingFileManager manager = 
mockManager(previousPeriodStart.getTime());
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+            verify(manager).rollover(eq(previousPeriodStart), any(Date.class));
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+    }
+
+    /**
+     * Initializes a policy for a file whose file time is offset from the 
current period's fire time.
+     */

Review Comment:
   ```suggestion
   ```



##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicyTest.java:
##########
@@ -186,4 +192,101 @@ void testFactoryMethodSequence() {
         testFactoryMethod();
         testFactoryMethod();
     }
+
+    private RollingFileManager mockManager(final long fileTime) {
+        final RollingFileManager manager = mock(RollingFileManager.class);
+        when(manager.getFileTime()).thenReturn(fileTime);
+        when(manager.getPatternProcessor()).thenReturn(new 
PatternProcessor("target/testcmd6.log.%d{yyyy-MM-dd}"));
+        return manager;
+    }
+
+    /**
+     * Tests that evaluateOnStartup archives a file from a previous period 
under that period. Otherwise the next
+     * scheduled rollover, which archives under the current period, overwrites 
it.
+     */
+    @Test
+    void testEvaluateOnStartupArchivesFileUnderItsOwnPeriod() throws Exception 
{
+        final CronExpression cronExpression = new 
CronExpression(CRON_EXPRESSION);
+        final Date periodStart = cronExpression.getPrevFireTime(new Date());
+        final Date previousPeriodStart = 
cronExpression.getPrevFireTime(periodStart);
+        final long fileTime = previousPeriodStart.getTime() + 
TimeUnit.HOURS.toMillis(12);
+        final RollingFileManager manager = mockManager(fileTime);
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+            verify(manager).rollover(eq(previousPeriodStart), any(Date.class));
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+    }
+
+    /**
+     * Tests that evaluateOnStartup does not roll over a file whose file time 
equals the current period's fire time.
+     */
+    @Test
+    void testEvaluateOnStartupDoesNotRollFileCreatedAtFireTime() throws 
Exception {
+        final Date periodStart = new 
CronExpression(CRON_EXPRESSION).getPrevFireTime(new Date());
+        final RollingFileManager manager = mockManager(periodStart.getTime());
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+            verify(manager, never()).rollover(any(Date.class), 
any(Date.class));
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+    }
+
+    /**
+     * Tests that, after a missed fire, evaluateOnStartup archives a file 
created by a scheduled rollover under that
+     * rollover's period. Such a file has a file time of exactly the fire 
time. Archiving it one period early would
+     * overwrite the archive that the scheduled rollover created.
+     */
+    @Test
+    void testEvaluateOnStartupArchivesFileCreatedAtFireTimeUnderItsOwnPeriod() 
throws Exception {
+        final CronExpression cronExpression = new 
CronExpression(CRON_EXPRESSION);
+        final Date previousPeriodStart = 
cronExpression.getPrevFireTime(cronExpression.getPrevFireTime(new Date()));
+        final RollingFileManager manager = 
mockManager(previousPeriodStart.getTime());
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+            verify(manager).rollover(eq(previousPeriodStart), any(Date.class));
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+    }
+
+    /**
+     * Initializes a policy for a file whose file time is offset from the 
current period's fire time.
+     */
+    private RollingFileManager initializeWithFileTimeOffset(final long 
offsetMillis) throws Exception {
+        final Date periodStart = new 
CronExpression(CRON_EXPRESSION).getPrevFireTime(new Date());
+        final RollingFileManager manager = mockManager(periodStart.getTime() + 
offsetMillis);
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+        return manager;
+    }
+
+    /**
+     * Tests that evaluateOnStartup still rolls over a file created one 
millisecond or one second before the current
+     * period's fire time.
+     */
+    @Test
+    void testEvaluateOnStartupRollsFileCreatedJustBeforeFireTime() throws 
Exception {
+        verify(initializeWithFileTimeOffset(-1)).rollover(any(Date.class), 
any(Date.class));
+        verify(initializeWithFileTimeOffset(-1000)).rollover(any(Date.class), 
any(Date.class));
+    }
+
+    /**
+     * Tests that evaluateOnStartup does not roll over a file created one 
millisecond or one second after the current
+     * period's fire time.
+     */

Review Comment:
   ```suggestion
   ```



##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicyTest.java:
##########
@@ -186,4 +192,101 @@ void testFactoryMethodSequence() {
         testFactoryMethod();
         testFactoryMethod();
     }
+
+    private RollingFileManager mockManager(final long fileTime) {
+        final RollingFileManager manager = mock(RollingFileManager.class);
+        when(manager.getFileTime()).thenReturn(fileTime);
+        when(manager.getPatternProcessor()).thenReturn(new 
PatternProcessor("target/testcmd6.log.%d{yyyy-MM-dd}"));
+        return manager;
+    }
+
+    /**
+     * Tests that evaluateOnStartup archives a file from a previous period 
under that period. Otherwise the next
+     * scheduled rollover, which archives under the current period, overwrites 
it.
+     */
+    @Test
+    void testEvaluateOnStartupArchivesFileUnderItsOwnPeriod() throws Exception 
{
+        final CronExpression cronExpression = new 
CronExpression(CRON_EXPRESSION);
+        final Date periodStart = cronExpression.getPrevFireTime(new Date());
+        final Date previousPeriodStart = 
cronExpression.getPrevFireTime(periodStart);
+        final long fileTime = previousPeriodStart.getTime() + 
TimeUnit.HOURS.toMillis(12);
+        final RollingFileManager manager = mockManager(fileTime);
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+            verify(manager).rollover(eq(previousPeriodStart), any(Date.class));
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+    }
+
+    /**
+     * Tests that evaluateOnStartup does not roll over a file whose file time 
equals the current period's fire time.
+     */

Review Comment:
   ```suggestion
   ```



##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicyTest.java:
##########
@@ -186,4 +192,101 @@ void testFactoryMethodSequence() {
         testFactoryMethod();
         testFactoryMethod();
     }
+
+    private RollingFileManager mockManager(final long fileTime) {
+        final RollingFileManager manager = mock(RollingFileManager.class);
+        when(manager.getFileTime()).thenReturn(fileTime);
+        when(manager.getPatternProcessor()).thenReturn(new 
PatternProcessor("target/testcmd6.log.%d{yyyy-MM-dd}"));

Review Comment:
   `rollover` is mocked here, so the pattern is never used. Can this be a mock, 
so the test does not carry a file name it never writes?
   
   ```suggestion
           
when(manager.getPatternProcessor()).thenReturn(mock(PatternProcessor.class));
   ```
   



##########
src/changelog/.2.x.x/4439_fix_crontriggeringpolicy_evaluateonstartup.xml:
##########
@@ -5,9 +5,9 @@
            https://logging.apache.org/xml/ns
            https://logging.apache.org/xml/ns/log4j-changelog-0.xsd";
        type="fixed">
-    <issue id="2020" 
link="https://github.com/apache/logging-log4j2/issues/2020"/>
-    <issue id="4469" 
link="https://github.com/apache/logging-log4j2/pull/4469"/>
+    <issue id="4439" 
link="https://github.com/apache/logging-log4j2/issues/4439"/>

Review Comment:
   ```suggestion
       <issue id="4439" 
link="https://github.com/apache/logging-log4j2/issues/4439"/>
       <issue id="4449" 
link="https://github.com/apache/logging-log4j2/issues/4449"/>
   ```



##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicyTest.java:
##########
@@ -186,4 +192,101 @@ void testFactoryMethodSequence() {
         testFactoryMethod();
         testFactoryMethod();
     }
+
+    private RollingFileManager mockManager(final long fileTime) {
+        final RollingFileManager manager = mock(RollingFileManager.class);
+        when(manager.getFileTime()).thenReturn(fileTime);
+        when(manager.getPatternProcessor()).thenReturn(new 
PatternProcessor("target/testcmd6.log.%d{yyyy-MM-dd}"));
+        return manager;
+    }
+
+    /**
+     * Tests that evaluateOnStartup archives a file from a previous period 
under that period. Otherwise the next
+     * scheduled rollover, which archives under the current period, overwrites 
it.
+     */
+    @Test
+    void testEvaluateOnStartupArchivesFileUnderItsOwnPeriod() throws Exception 
{
+        final CronExpression cronExpression = new 
CronExpression(CRON_EXPRESSION);
+        final Date periodStart = cronExpression.getPrevFireTime(new Date());
+        final Date previousPeriodStart = 
cronExpression.getPrevFireTime(periodStart);
+        final long fileTime = previousPeriodStart.getTime() + 
TimeUnit.HOURS.toMillis(12);
+        final RollingFileManager manager = mockManager(fileTime);
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+            verify(manager).rollover(eq(previousPeriodStart), any(Date.class));
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+    }
+
+    /**
+     * Tests that evaluateOnStartup does not roll over a file whose file time 
equals the current period's fire time.
+     */
+    @Test
+    void testEvaluateOnStartupDoesNotRollFileCreatedAtFireTime() throws 
Exception {
+        final Date periodStart = new 
CronExpression(CRON_EXPRESSION).getPrevFireTime(new Date());
+        final RollingFileManager manager = mockManager(periodStart.getTime());
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+            verify(manager, never()).rollover(any(Date.class), 
any(Date.class));
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+    }
+
+    /**
+     * Tests that, after a missed fire, evaluateOnStartup archives a file 
created by a scheduled rollover under that
+     * rollover's period. Such a file has a file time of exactly the fire 
time. Archiving it one period early would
+     * overwrite the archive that the scheduled rollover created.
+     */

Review Comment:
   ```suggestion
   ```



##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicyTest.java:
##########
@@ -186,4 +192,101 @@ void testFactoryMethodSequence() {
         testFactoryMethod();
         testFactoryMethod();
     }
+
+    private RollingFileManager mockManager(final long fileTime) {
+        final RollingFileManager manager = mock(RollingFileManager.class);
+        when(manager.getFileTime()).thenReturn(fileTime);
+        when(manager.getPatternProcessor()).thenReturn(new 
PatternProcessor("target/testcmd6.log.%d{yyyy-MM-dd}"));
+        return manager;
+    }
+
+    /**
+     * Tests that evaluateOnStartup archives a file from a previous period 
under that period. Otherwise the next
+     * scheduled rollover, which archives under the current period, overwrites 
it.
+     */
+    @Test
+    void testEvaluateOnStartupArchivesFileUnderItsOwnPeriod() throws Exception 
{
+        final CronExpression cronExpression = new 
CronExpression(CRON_EXPRESSION);
+        final Date periodStart = cronExpression.getPrevFireTime(new Date());
+        final Date previousPeriodStart = 
cronExpression.getPrevFireTime(periodStart);
+        final long fileTime = previousPeriodStart.getTime() + 
TimeUnit.HOURS.toMillis(12);
+        final RollingFileManager manager = mockManager(fileTime);
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+            verify(manager).rollover(eq(previousPeriodStart), any(Date.class));
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+    }
+
+    /**
+     * Tests that evaluateOnStartup does not roll over a file whose file time 
equals the current period's fire time.
+     */
+    @Test
+    void testEvaluateOnStartupDoesNotRollFileCreatedAtFireTime() throws 
Exception {
+        final Date periodStart = new 
CronExpression(CRON_EXPRESSION).getPrevFireTime(new Date());
+        final RollingFileManager manager = mockManager(periodStart.getTime());
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+            verify(manager, never()).rollover(any(Date.class), 
any(Date.class));
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+    }
+
+    /**
+     * Tests that, after a missed fire, evaluateOnStartup archives a file 
created by a scheduled rollover under that
+     * rollover's period. Such a file has a file time of exactly the fire 
time. Archiving it one period early would
+     * overwrite the archive that the scheduled rollover created.
+     */
+    @Test
+    void testEvaluateOnStartupArchivesFileCreatedAtFireTimeUnderItsOwnPeriod() 
throws Exception {
+        final CronExpression cronExpression = new 
CronExpression(CRON_EXPRESSION);
+        final Date previousPeriodStart = 
cronExpression.getPrevFireTime(cronExpression.getPrevFireTime(new Date()));
+        final RollingFileManager manager = 
mockManager(previousPeriodStart.getTime());
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+            verify(manager).rollover(eq(previousPeriodStart), any(Date.class));
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+    }
+
+    /**
+     * Initializes a policy for a file whose file time is offset from the 
current period's fire time.
+     */
+    private RollingFileManager initializeWithFileTimeOffset(final long 
offsetMillis) throws Exception {
+        final Date periodStart = new 
CronExpression(CRON_EXPRESSION).getPrevFireTime(new Date());
+        final RollingFileManager manager = mockManager(periodStart.getTime() + 
offsetMillis);
+        final CronTriggeringPolicy policy = createPolicy();
+        try {
+            policy.initialize(manager);
+        } finally {
+            policy.stop(1, TimeUnit.SECONDS);
+        }
+        return manager;
+    }
+
+    /**
+     * Tests that evaluateOnStartup still rolls over a file created one 
millisecond or one second before the current
+     * period's fire time.
+     */

Review Comment:
   ```suggestion
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to