Search in sources :

Example 1 with FakeClock

use of net.morimekta.testing.time.FakeClock in project providence by morimekta.

the class QueuedFileMessageWriterTest method testFailedWrite.

@Test
public void testFailedWrite() throws IOException {
    MessageWriter target = mock(MessageWriter.class);
    FakeClock clock = new FakeClock();
    long start = clock.millis();
    FakeScheduledExecutor executor = new FakeScheduledExecutor(clock);
    QueuedMessageWriter writer = new FakeQueuedMessageWriter(target, executor, clock);
    writer.write(new CompactFields("foo", 42, "bar"));
    writer.write(new CompactFields("foo", 42, "bar"));
    when(target.write(any(CompactFields.class))).thenThrow(new IOException());
    when(target.write(any(CompactFields.class))).thenAnswer(i -> {
        executor.shutdown();
        throw new IOException();
    });
    clock.tick(1);
    assertThat(clock.millis() - start, is(138L));
    verify(target).write(any(CompactFields.class));
    verify(target).write(any(CompactFields.class));
    verifyNoMoreInteractions(target);
}
Also used : FakeScheduledExecutor(net.morimekta.testing.concurrent.FakeScheduledExecutor) FakeClock(net.morimekta.testing.time.FakeClock) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) CompactFields(net.morimekta.test.providence.core.CompactFields) Test(org.junit.Test)

Example 2 with FakeClock

use of net.morimekta.testing.time.FakeClock in project providence by morimekta.

the class RollingFileMessageWriterTest method testTimeBaseDefaultFormatter.

@Test
public void testTimeBaseDefaultFormatter() {
    FakeClock clock = FakeClock.forCurrentTimeMillis(1234567890000L);
    assertThat(publicGetFormatter("", TimeUnit.DAYS).format(ZonedDateTime.now(clock)), is("2009-02-13"));
    assertThat(publicGetFormatter("", TimeUnit.HOURS).format(ZonedDateTime.now(clock)), is("2009-02-13T23"));
    assertThat(publicGetFormatter("", TimeUnit.MINUTES).format(ZonedDateTime.now(clock)), is("2009-02-13T23:31"));
}
Also used : FakeClock(net.morimekta.testing.time.FakeClock) Test(org.junit.Test)

Example 3 with FakeClock

use of net.morimekta.testing.time.FakeClock 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)

Example 4 with FakeClock

use of net.morimekta.testing.time.FakeClock in project providence by morimekta.

the class FixedConfigSupplierTest method testCreate.

@Test
@SuppressWarnings("unchecked")
public void testCreate() {
    FakeClock clock = FakeClock.forCurrentTimeMillis(System.currentTimeMillis());
    Database value = Database.builder().build();
    ConfigSupplier<Database, Database._Field> supplier = mock(ConfigSupplier.class);
    when(supplier.get()).thenReturn(value).thenReturn(value);
    when(supplier.configTimestamp()).thenAnswer(i -> clock.millis());
    when(supplier.snapshot()).thenCallRealMethod();
    FixedConfigSupplier<Database, Database._Field> fixed = new FixedConfigSupplier<>(supplier);
    clock.tick(1000);
    ConfigSupplier<Database, Database._Field> snapshot = supplier.snapshot();
    assertThat(fixed.get(), is(sameInstance(value)));
    assertThat(snapshot.get(), is(sameInstance(value)));
    assertThat(fixed.configTimestamp(), is(snapshot.configTimestamp() - 1000));
    assertThat(fixed.snapshot(), is(sameInstance(fixed)));
}
Also used : FakeClock(net.morimekta.testing.time.FakeClock) Database(net.morimekta.test.providence.config.Database) Test(org.junit.Test)

Example 5 with FakeClock

use of net.morimekta.testing.time.FakeClock in project providence by morimekta.

the class OverrideConfigSupplierTest method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() {
    clock = new FakeClock();
    base = new TestConfigSupplier<>(clock, Database.builder().setUri("http://hostname:9057/path").setDriver("driver").setCredentials(new Credentials("username", "complicated password that no one guesses")).build());
}
Also used : FakeClock(net.morimekta.testing.time.FakeClock) Credentials(net.morimekta.test.providence.config.Credentials) Before(org.junit.Before)

Aggregations

FakeClock (net.morimekta.testing.time.FakeClock)6 Test (org.junit.Test)4 Before (org.junit.Before)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 TimeBasedCleanupPolicy (net.morimekta.providence.mio.rolling.TimeBasedCleanupPolicy)1 TimeBasedRollingPolicy (net.morimekta.providence.mio.rolling.TimeBasedRollingPolicy)1 JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)1 Credentials (net.morimekta.test.providence.config.Credentials)1 Database (net.morimekta.test.providence.config.Database)1 CompactFields (net.morimekta.test.providence.core.CompactFields)1 FakeScheduledExecutor (net.morimekta.testing.concurrent.FakeScheduledExecutor)1 FileWatcher (net.morimekta.util.FileWatcher)1