Search in sources :

Example 6 with CompactFields

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"));
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) File(java.io.File) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) CompactFields(net.morimekta.test.providence.core.CompactFields) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UncheckedIOException(java.io.UncheckedIOException) SerializerException(net.morimekta.providence.serializer.SerializerException) Test(org.junit.Test)

Example 7 with CompactFields

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"));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) CompactFields(net.morimekta.test.providence.core.CompactFields) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Serializer(net.morimekta.providence.serializer.Serializer) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) Test(org.junit.Test)

Example 8 with CompactFields

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)));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) CompactFields(net.morimekta.test.providence.core.CompactFields) Test(org.junit.Test)

Example 9 with CompactFields

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)));
}
Also used : Path(java.nio.file.Path) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) CompactFields(net.morimekta.test.providence.core.CompactFields) Test(org.junit.Test)

Example 10 with CompactFields

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));
}
Also used : MessageGenerator(net.morimekta.providence.util_internal.MessageGenerator) Duration(org.awaitility.Duration) UncheckedIOException(java.io.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) CompactFields(net.morimekta.test.providence.core.CompactFields) ByteArrayInputStream(java.io.ByteArrayInputStream) ExecutorService(java.util.concurrent.ExecutorService) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Test(org.junit.Test)

Aggregations

CompactFields (net.morimekta.test.providence.core.CompactFields)10 Test (org.junit.Test)10 IOException (java.io.IOException)7 UncheckedIOException (java.io.UncheckedIOException)7 BinarySerializer (net.morimekta.providence.serializer.BinarySerializer)6 JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 PrettySerializer (net.morimekta.providence.serializer.PrettySerializer)3 Serializer (net.morimekta.providence.serializer.Serializer)3 File (java.io.File)2 OutputStream (java.io.OutputStream)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SerializerException (net.morimekta.providence.serializer.SerializerException)2 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Path (java.nio.file.Path)1 KeepLastNCleanupPolicy (net.morimekta.providence.mio.rolling.KeepLastNCleanupPolicy)1 SizeBasedRollingPolicy (net.morimekta.providence.mio.rolling.SizeBasedRollingPolicy)1