Search in sources :

Example 1 with KeepLastNCleanupPolicy

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

the class RollingFileMessageWriterTest method testSizeBasedRolling.

@Test
public void testSizeBasedRolling() throws IOException {
    CompactFields cf = new CompactFields(" * the message writer MUST be assigned a rolling policy", 1234567890, "Also note that");
    RollingFileMessageWriter writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 1000, "my-log-%03d.txt"), new KeepLastNCleanupPolicy(5, "my-log-[\\d]{3}.txt"));
    writer.write(cf);
    for (int i = 0; i < 100; ++i) {
        writer.write(cf);
    }
    String[] files = tmp.getRoot().list();
    assertThat(files, is(notNullValue()));
    assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-005.txt", "my-log-006.txt", "my-log-007.txt", "my-log-008.txt", "my-log-009.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-009.txt"));
    writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 1000, "my-log-%03d.txt"), new KeepLastNCleanupPolicy(5, "my-log-[\\d]{3}.txt"));
    writer.write(cf);
    for (int i = 0; i < 30; ++i) {
        writer.write(cf);
    }
    files = tmp.getRoot().list();
    assertThat(files, is(notNullValue()));
    assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-001.txt", "my-log-002.txt", "my-log-003.txt", "my-log-008.txt", "my-log-009.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-003.txt"));
    writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 1000, "my-log-%03d.txt"), new KeepLastNCleanupPolicy(5, "my-log-[\\d]{3}.txt"));
    writer.write(cf);
    for (int i = 0; i < 30; ++i) {
        writer.write(cf);
    }
    files = tmp.getRoot().list();
    assertThat(files, is(notNullValue()));
    assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-002.txt", "my-log-003.txt", "my-log-004.txt", "my-log-005.txt", "my-log-006.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-006.txt"));
}
Also used : SizeBasedRollingPolicy(net.morimekta.providence.mio.rolling.SizeBasedRollingPolicy) KeepLastNCleanupPolicy(net.morimekta.providence.mio.rolling.KeepLastNCleanupPolicy) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) CompactFields(net.morimekta.test.providence.core.CompactFields) Test(org.junit.Test)

Aggregations

KeepLastNCleanupPolicy (net.morimekta.providence.mio.rolling.KeepLastNCleanupPolicy)1 SizeBasedRollingPolicy (net.morimekta.providence.mio.rolling.SizeBasedRollingPolicy)1 JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)1 CompactFields (net.morimekta.test.providence.core.CompactFields)1 Test (org.junit.Test)1