Search in sources :

Example 1 with BinarySerializer

use of net.morimekta.providence.serializer.BinarySerializer in project providence by morimekta.

the class IOMessageRWTest method testBinary.

@Test
public void testBinary() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (MessageWriter writer = new IOMessageWriter(baos, new BinarySerializer())) {
        writer.write(m1);
        writer.separator();
        writer.write(m2);
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    try (MessageReader reader = new IOMessageReader(bais, new BinarySerializer())) {
        assertThat(m1, is(equalTo(reader.read(CompactFields.kDescriptor))));
        assertThat(m2, is(equalTo(reader.read(OptionalFields.kDescriptor))));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Test(org.junit.Test)

Example 2 with BinarySerializer

use of net.morimekta.providence.serializer.BinarySerializer in project providence by morimekta.

the class QueuedFileMessageWriterTest method testClose_noQueue.

@Test
public void testClose_noQueue() throws IOException, InterruptedException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOMessageWriter target = new IOMessageWriter(baos, new BinarySerializer());
    ExecutorService executor = mock(ExecutorService.class);
    when(executor.isShutdown()).thenReturn(false);
    when(executor.awaitTermination(1000L, TimeUnit.MILLISECONDS)).thenReturn(false);
    QueuedMessageWriter writer = new QueuedMessageWriter(target, executor);
    writer.close();
    verify(executor).submit(any(Runnable.class));
    verify(executor).isShutdown();
    verify(executor).shutdown();
    verify(executor).awaitTermination(1000L, TimeUnit.MILLISECONDS);
    verify(executor).shutdownNow();
    verifyNoMoreInteractions(executor);
    assertThat(baos.toByteArray(), is(new byte[] {}));
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Test(org.junit.Test)

Example 3 with BinarySerializer

use of net.morimekta.providence.serializer.BinarySerializer in project providence by morimekta.

the class QueuedFileMessageWriterTest method testSeparator.

@Test
public void testSeparator() throws IOException {
    setDefaultPollDelay(new Duration(10, TimeUnit.MILLISECONDS));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOMessageWriter target = new IOMessageWriter(baos, new BinarySerializer());
    QueuedMessageWriter writer = new QueuedMessageWriter(target);
    assertThat(writer.separator(), is(0));
    writer.close();
    assertThat(baos.size(), is(0));
}
Also used : Duration(org.awaitility.Duration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Test(org.junit.Test)

Example 4 with BinarySerializer

use of net.morimekta.providence.serializer.BinarySerializer in project providence by morimekta.

the class NonblockingSocketClientHandlerTest method testSimpleRequest_cannotConnect.

@Test
public void testSimpleRequest_cannotConnect() throws IOException, Failure {
    Serializer serializer = new BinarySerializer();
    InetSocketAddress address = new InetSocketAddress("localhost", port - 10);
    try (NonblockingSocketClientHandler handler = new NonblockingSocketClientHandler(serializer, address)) {
        MyService.Iface client = new MyService.Client(handler);
        try {
            client.test(Request.builder().setText("test").build());
            fail("no exception");
        } catch (ConnectException e) {
        // The exception message is entirely localized, so it's impossible to reliably match against.
        // assertThat(e.getMessage(), startsWith("Connection refused"));
        }
        verifyZeroInteractions(impl);
    }
}
Also used : Iface(net.morimekta.test.thrift.thrift.service.MyService.Iface) InetSocketAddress(java.net.InetSocketAddress) MyService(net.morimekta.test.providence.thrift.service.MyService) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Serializer(net.morimekta.providence.serializer.Serializer) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 5 with BinarySerializer

use of net.morimekta.providence.serializer.BinarySerializer in project providence by morimekta.

the class SocketClientHandlerTest method testSimpleRequest_cannotConnect.

@Test
public void testSimpleRequest_cannotConnect() throws IOException, Failure, InterruptedException {
    Serializer serializer = new BinarySerializer();
    InetSocketAddress address = new InetSocketAddress("localhost", port - 10);
    MyService.Iface client = new MyService.Client(new SocketClientHandler(serializer, address));
    try {
        client.test(new Request(null));
        fail("no exception");
    } catch (ConnectException e) {
        assertThat(e.getMessage(), containsString("Connection refused"));
    }
    Thread.sleep(10L);
    verifyZeroInteractions(impl);
}
Also used : Iface(net.morimekta.test.thrift.thrift.service.MyService.Iface) InetSocketAddress(java.net.InetSocketAddress) MyService(net.morimekta.test.providence.thrift.service.MyService) Request(net.morimekta.test.providence.thrift.service.Request) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Serializer(net.morimekta.providence.serializer.Serializer) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Aggregations

BinarySerializer (net.morimekta.providence.serializer.BinarySerializer)16 Test (org.junit.Test)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 InetSocketAddress (java.net.InetSocketAddress)4 ExecutorService (java.util.concurrent.ExecutorService)4 Iface (net.morimekta.test.thrift.thrift.service.MyService.Iface)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 UncheckedIOException (java.io.UncheckedIOException)3 CompactFields (net.morimekta.test.providence.core.CompactFields)3 Duration (org.awaitility.Duration)3 File (java.io.File)2 ConnectException (java.net.ConnectException)2 Serializer (net.morimekta.providence.serializer.Serializer)2 MessageGenerator (net.morimekta.providence.util_internal.MessageGenerator)2 MyService (net.morimekta.test.providence.thrift.service.MyService)2 BeforeClass (org.junit.BeforeClass)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Path (java.nio.file.Path)1