use of net.morimekta.test.providence.core.CompactFields in project providence by morimekta.
the class MessageStreamsTest method testToFile.
@Test
public void testToFile() throws IOException {
File file = tmp.newFile();
int size = list.stream().collect(MessageCollectors.toFile(file, new BinarySerializer()));
assertThat(Files.size(file.toPath()), is((long) size));
List<CompactFields> out = MessageStreams.file(file, new BinarySerializer(), CompactFields.kDescriptor).collect(Collectors.toList());
assertThat(out, is(equalTo(list)));
try {
list.stream().collect(MessageCollectors.toFile(new File(tmp.getRoot(), "does/not"), new BinarySerializer()));
fail("no exception");
} catch (Exception e) {
assertThat(e, is(instanceOf(UncheckedIOException.class)));
assertThat(e.getMessage(), is("Unable to open not"));
}
}
use of net.morimekta.test.providence.core.CompactFields in project providence by morimekta.
the class MessageStreamsTest method testStreamCollector.
@Test
public void testStreamCollector() throws IOException {
Serializer serializer = mock(Serializer.class);
OutputStream out = mock(OutputStream.class);
AtomicInteger i = new AtomicInteger();
Collector<CompactFields, AtomicInteger, Integer> collector = MessageCollectors.toStream(out, serializer);
doReturn(false).when(serializer).binaryProtocol();
doThrow(new IOException("write")).when(out).write(MessageStreams.READABLE_ENTRY_SEP);
i.set(5);
AtomicInteger j = new AtomicInteger(6);
assertThat(collector.combiner().apply(i, j).get(), is(11));
try {
collector.accumulator().accept(i, list.get(0));
fail("no exception");
} catch (UncheckedIOException e) {
assertThat(e.getMessage(), is("write"));
}
}
use of net.morimekta.test.providence.core.CompactFields in project providence by morimekta.
the class MessageStreamsTest method testToStream.
@Test
public void testToStream() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int size = list.stream().collect(MessageCollectors.toStream(baos, new JsonSerializer()));
assertThat(baos.size(), is(size));
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
List<CompactFields> out = MessageStreams.stream(bais, new JsonSerializer(), CompactFields.kDescriptor).collect(Collectors.toList());
assertThat(out, is(equalTo(list)));
}
use of net.morimekta.test.providence.core.CompactFields in project providence by morimekta.
the class MessageStreamsTest method testToPath.
@Test
public void testToPath() throws IOException {
Path file = tmp.newFile().toPath();
int size = list.stream().collect(MessageCollectors.toPath(file, new BinarySerializer()));
assertThat(Files.size(file), is((long) size));
List<CompactFields> out = MessageStreams.path(file, new BinarySerializer(), CompactFields.kDescriptor).collect(Collectors.toList());
assertThat(out, is(equalTo(list)));
}
use of net.morimekta.test.providence.core.CompactFields in project providence by morimekta.
the class QueuedFileMessageWriterTest method testAFewWrites.
@Test
@SuppressWarnings("unchecked")
public void testAFewWrites() throws IOException, InterruptedException {
setDefaultPollDelay(new Duration(10, TimeUnit.MILLISECONDS));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOMessageWriter target = new IOMessageWriter(baos, new BinarySerializer());
QueuedMessageWriter writer = new QueuedMessageWriter(target);
ExecutorService executorService = Executors.newFixedThreadPool(11);
for (int i = 0; i < 10; ++i) {
executorService.submit(() -> {
MessageGenerator generator = new MessageGenerator();
for (int j = 0; j < 10; ++j) {
try {
if (j > 0)
sleep(1L);
writer.write(generator.generate(CompactFields.kDescriptor));
} catch (IOException e) {
throw new UncheckedIOException(e.getMessage(), e);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
});
}
sleep(10L);
executorService.shutdown();
assertTrue(executorService.awaitTermination(10, TimeUnit.SECONDS));
sleep(1L);
writer.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
List<CompactFields> result = MessageStreams.stream(bais, new BinarySerializer(), CompactFields.kDescriptor).collect(Collectors.toList());
assertThat(result, hasSize(100));
}
Aggregations