use of net.morimekta.test.providence.core.CompactFields in project providence by morimekta.
the class MessageStreamsTest method testFileCollector.
@Test
public void testFileCollector() throws IOException {
File file = tmp.newFile("tmp");
Collector<CompactFields, OutputStream, Integer> collector = MessageCollectors.toFile(file, new PrettySerializer().config());
OutputStream out = mock(OutputStream.class);
doThrow(new IOException("oops")).when(out).write(MessageStreams.READABLE_ENTRY_SEP);
doThrow(new IOException("close")).when(out).close();
assertThat(collector.combiner().apply(out, out), is(sameInstance(out)));
try {
collector.accumulator().accept(out, list.get(0));
fail("no exception");
} catch (UncheckedIOException e) {
assertThat(e.getMessage(), is("Unable to write to tmp"));
assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), is("oops"));
}
try {
collector.finisher().apply(out);
fail("no exception");
} catch (UncheckedIOException e) {
assertThat(e.getMessage(), is("Unable to close tmp"));
assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), is("close"));
}
Serializer ms = mock(Serializer.class);
doThrow(new SerializerException("oops2")).when(ms).serialize(out, list.get(0));
collector = MessageCollectors.toFile(file, ms);
try {
collector.accumulator().accept(out, list.get(0));
fail("no exception");
} catch (UncheckedIOException e) {
assertThat(e.getMessage(), is("Bad data"));
assertThat(e.getCause(), is(instanceOf(SerializerException.class)));
assertThat(e.getCause().getMessage(), is("oops2"));
}
}
use of net.morimekta.test.providence.core.CompactFields 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.test.providence.core.CompactFields in project providence by morimekta.
the class QueuedFileMessageWriterTest method testClose_exceptions.
@Test
public void testClose_exceptions() throws IOException, InterruptedException {
MessageWriter target = mock(MessageWriter.class);
ExecutorService executor = mock(ExecutorService.class);
when(target.write(any(CompactFields.class))).thenThrow(new IOException("fail"));
when(executor.isShutdown()).thenReturn(false);
when(executor.awaitTermination(1000L, TimeUnit.MILLISECONDS)).thenThrow(new InterruptedException());
QueuedMessageWriter writer = new QueuedMessageWriter(target, executor);
writer.write(new CompactFields("foo", 42, "bar"));
try {
writer.close();
fail("no exception");
} catch (RuntimeException e) {
assertThat(e.getMessage(), is(nullValue()));
}
verify(executor).submit(any(Runnable.class));
verify(executor).isShutdown();
verify(executor).shutdown();
verify(executor).awaitTermination(1000L, TimeUnit.MILLISECONDS);
verify(target).write(new CompactFields("foo", 42, "bar"));
verify(target).close();
verifyNoMoreInteractions(executor, target);
}
use of net.morimekta.test.providence.core.CompactFields 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"));
}
use of net.morimekta.test.providence.core.CompactFields in project providence by morimekta.
the class MessageStreamsTest method testSpliterator.
@Test
public void testSpliterator() throws IOException {
InputStream in = mock(InputStream.class);
Serializer serializer = mock(Serializer.class);
Consumer<CompactFields> consumer = mock(Consumer.class);
MessageSpliterator<CompactFields, CompactFields._Field> spliterator = new MessageSpliterator<>(in, serializer, CompactFields.kDescriptor);
assertThat(spliterator.trySplit(), is(nullValue()));
assertThat(spliterator.estimateSize(), is(Long.MAX_VALUE));
assertThat(spliterator.getExactSizeIfKnown(), is(-1L));
assertThat(spliterator.characteristics(), is(Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.IMMUTABLE));
assertThat(spliterator.getComparator(), is(notNullValue()));
CompactFields cf = CompactFields.builder().build();
// -- read
doReturn(cf).when(serializer).deserialize(in, CompactFields.kDescriptor);
assertThat(spliterator.tryAdvance(consumer), is(true));
verify(serializer).deserialize(in, CompactFields.kDescriptor);
verify(in).markSupported();
verify(consumer).accept(same(cf));
reset(in, serializer, consumer);
doReturn(false).when(serializer).binaryProtocol();
doReturn(-1).when(in).read();
assertThat(spliterator.tryAdvance(consumer), is(false));
verify(in).markSupported();
verifyNoMoreInteractions(in);
verifyZeroInteractions(consumer);
// fails.
reset(in, serializer, consumer);
spliterator = new MessageSpliterator<>(in, serializer, CompactFields.kDescriptor);
doReturn(true).when(serializer).binaryProtocol();
doReturn(true).when(in).markSupported();
doThrow(new IOException("read")).when(in).read();
doThrow(new IOException("close")).when(in).close();
try {
spliterator.tryAdvance(consumer);
fail("no exception");
} catch (UncheckedIOException e) {
assertThat(e.getMessage(), is("read"));
assertThat(e.getCause(), is(notNullValue()));
assertThat(e.getCause().getSuppressed().length, is(1));
assertThat(e.getCause().getSuppressed()[0].getMessage(), is("close"));
}
verify(in).markSupported();
verify(in).mark(2);
verify(in).read();
verify(in).close();
verifyNoMoreInteractions(in);
verifyZeroInteractions(serializer, consumer);
}
Aggregations