Search in sources :

Example 1 with CompactFields

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"));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) File(java.io.File) SerializerException(net.morimekta.providence.serializer.SerializerException) 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 2 with CompactFields

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);
}
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 3 with CompactFields

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

Example 4 with CompactFields

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"));
}
Also used : SizeBasedRollingPolicy(net.morimekta.providence.mio.rolling.SizeBasedRollingPolicy) KeepLastNCleanupPolicy(net.morimekta.providence.mio.rolling.KeepLastNCleanupPolicy) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) CompactFields(net.morimekta.test.providence.core.CompactFields) Test(org.junit.Test)

Example 5 with CompactFields

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) 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)

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