Issue #1507 - ROFS: Simplifying, Documenting, Testing

This commit is contained in:
Joakim Erdfelt 2017-04-27 07:25:13 -07:00
parent f9e9fd645d
commit 1acc064281
2 changed files with 42 additions and 16 deletions

View File

@ -174,13 +174,12 @@ public class RolloverFileOutputStream extends FilterOutputStream
{
if (__rollover==null)
__rollover=new Timer(RolloverFileOutputStream.class.getName(),true);
// Calculate Today's Midnight, based on Configured TimeZone (will be in past, even if by a few milliseconds)
midnight = toMidnight(ZonedDateTime.now(zone.toZoneId()), zone.toZoneId());
ZonedDateTime now = ZonedDateTime.now(zone.toZoneId());
midnight = toMidnight(now, zone.toZoneId());
while (midnight.isBefore(now))
midnight = nextMidnight(midnight);
scheduleNextRollover(now);
// This will schedule the rollover event to the next midnight
scheduleNextRollover();
}
}
@ -204,15 +203,19 @@ public class RolloverFileOutputStream extends FilterOutputStream
*/
public static ZonedDateTime nextMidnight(ZonedDateTime dateTime)
{
// Increment to next day.
// Increment to next day, based on Configured TimeZone, then find start of that day.
// Will always be in the future, even if the Daylights Savings Time kicks in during
// the calculation.
return dateTime.toLocalDate().plus(1, ChronoUnit.DAYS).atStartOfDay(dateTime.getZone());
}
private void scheduleNextRollover(ZonedDateTime now)
private void scheduleNextRollover()
{
_rollTask = new RollTask();
// Get tomorrow's midnight based on Configured TimeZone
midnight = nextMidnight(midnight);
__rollover.schedule(_rollTask,midnight.toInstant().toEpochMilli() - now.toInstant().toEpochMilli());
// Schedule next rollover event to occur, based on local machine's Unix Epoch milliseconds
__rollover.schedule(_rollTask,midnight.toInstant().toEpochMilli() - System.currentTimeMillis());
}
/* ------------------------------------------------------------ */
@ -279,11 +282,12 @@ public class RolloverFileOutputStream extends FilterOutputStream
}
/* ------------------------------------------------------------ */
private void removeOldFiles(ZonedDateTime now)
private void removeOldFiles()
{
if (_retainDays>0)
{
now.minus(_retainDays, ChronoUnit.DAYS);
// Establish expiration time, based on configured TimeZone
ZonedDateTime now = ZonedDateTime.now(this.midnight.getZone());
long expired = now.toInstant().toEpochMilli();
File file= new File(_filename);
@ -356,10 +360,9 @@ public class RolloverFileOutputStream extends FilterOutputStream
{
try
{
ZonedDateTime now = ZonedDateTime.now(midnight.getZone());
RolloverFileOutputStream.this.setFile();
RolloverFileOutputStream.this.scheduleNextRollover(now);
RolloverFileOutputStream.this.removeOldFiles(now);
RolloverFileOutputStream.this.scheduleNextRollover();
RolloverFileOutputStream.this.removeOldFiles();
}
catch(Throwable t)
{

View File

@ -57,14 +57,37 @@ public class RolloverFileOutputStreamTest
for (int i = 0; i < expected.length; i++)
{
long lastMs = nextEvent.toInstant().toEpochMilli();
long currentMillis = nextEvent.toInstant().toEpochMilli();
nextEvent = RolloverFileOutputStream.nextMidnight(nextEvent);
assertThat("Next Event", toString(nextEvent), is(expected[i][0]));
long duration = (nextEvent.toInstant().toEpochMilli() - lastMs);
long duration = (nextEvent.toInstant().toEpochMilli() - currentMillis);
assertThat("Duration to next event", duration, is((long) expected[i][1]));
}
}
/**
* <a href="Issue #1507">https://github.com/eclipse/jetty.project/issues/1507</a>
*/
@Test
public void testMidnightRolloverCalc_PDT_Issue1507()
{
ZoneId zone = toZoneId("PST");
ZonedDateTime initialDate = toDateTime("2017.04.27-08:00:00.0 PM PDT", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate, zone);
assertThat("Midnight", toString(midnight), is("2017.04.27-12:00:00.0 AM PDT"));
Object expected[][] = {
{"2017.04.28-12:00:00.0 AM PDT", 14_400_000L}, // Ensure not negative
{"2017.04.29-12:00:00.0 AM PDT", 86_400_000L},
{"2017.04.30-12:00:00.0 AM PDT", 86_400_000L},
{"2017.05.01-12:00:00.0 AM PDT", 86_400_000L},
{"2017.05.02-12:00:00.0 AM PDT", 86_400_000L},
};
assertSequence(initialDate, expected);
}
@Test
public void testMidnightRolloverCalc_PST_DST_Start()
{