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);
}
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"));
}
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"));
}
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)));
}
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());
}
Aggregations