Search in sources :

Example 1 with TimeBasedCleanupPolicy

use of net.morimekta.providence.mio.rolling.TimeBasedCleanupPolicy in project providence by morimekta.

the class RollingFileMessageWriterTest method testTimeBasedRolling.

@Test
public void testTimeBasedRolling() throws IOException {
    FakeClock clock = FakeClock.forCurrentTimeMillis(1234567890000L).withZone(ZoneId.of("Europe/Oslo"));
    RollingFileMessageWriter writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new TimeBasedRollingPolicy(5, TimeUnit.MINUTES, "my-log-%d{yyyy-MM-dd_HH-mm}.txt", clock), new TimeBasedCleanupPolicy(15, TimeUnit.MINUTES, "my-log-%d{yyyy-MM-dd_HH-mm}.txt", clock));
    writer.write(generator.generate(CompactFields.kDescriptor));
    for (int i = 0; i < 10; ++i) {
        clock.tick(107, TimeUnit.SECONDS);
        writer.write(generator.generate(CompactFields.kDescriptor));
    }
    String[] files = tmp.getRoot().list();
    assertThat(files, is(notNullValue()));
    assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-2009-02-13_22-35.txt", "my-log-2009-02-13_22-40.txt", "my-log-2009-02-13_22-45.txt")));
    assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
    assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-2009-02-13_22-45.txt"));
    for (int i = 0; i < 10; ++i) {
        clock.tick(107, TimeUnit.SECONDS);
        writer.write(generator.generate(CompactFields.kDescriptor));
    }
    files = tmp.getRoot().list();
    assertThat(files, is(notNullValue()));
    assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-2009-02-13_22-55.txt", "my-log-2009-02-13_23-00.txt", "my-log-2009-02-13_23-05.txt")));
    assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
    assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-2009-02-13_23-05.txt"));
    writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new TimeBasedRollingPolicy(TimeUnit.HOURS, "my-log-%d{yyyy-MM-dd_HH-mm}.txt", clock));
    writer.write(generator.generate(CompactFields.kDescriptor));
    assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
    assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-2009-02-13_23-00.txt"));
}
Also used : FakeClock(net.morimekta.testing.time.FakeClock) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) TimeBasedCleanupPolicy(net.morimekta.providence.mio.rolling.TimeBasedCleanupPolicy) TimeBasedRollingPolicy(net.morimekta.providence.mio.rolling.TimeBasedRollingPolicy) Test(org.junit.Test)

Aggregations

TimeBasedCleanupPolicy (net.morimekta.providence.mio.rolling.TimeBasedCleanupPolicy)1 TimeBasedRollingPolicy (net.morimekta.providence.mio.rolling.TimeBasedRollingPolicy)1 JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)1 FakeClock (net.morimekta.testing.time.FakeClock)1 Test (org.junit.Test)1