Search in sources :

Example 11 with BinarySerializer

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

the class QueuedFileMessageWriterTest method testAFewWrites_serviceCalls.

@Test
@SuppressWarnings("unchecked")
public void testAFewWrites_serviceCalls() 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);
                    PServiceCall tmp = new PServiceCall("test", PServiceCallType.CALL, j, generator.generate(CompactFields.kDescriptor));
                    writer.write(tmp);
                } catch (IOException e) {
                    throw new UncheckedIOException(e.getMessage(), e);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
    sleep(10L);
    executorService.shutdown();
    assertTrue(executorService.awaitTermination(10, TimeUnit.SECONDS));
    sleep(1L);
    writer.close();
}
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) PServiceCall(net.morimekta.providence.PServiceCall) ExecutorService(java.util.concurrent.ExecutorService) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Test(org.junit.Test)

Example 12 with BinarySerializer

use of net.morimekta.providence.serializer.BinarySerializer 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)

Example 13 with BinarySerializer

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

the class NonblockingSocketClientHandlerTest method setUpServer.

@Before
public void setUpServer() throws Exception {
    setDefaultPollDelay(10, TimeUnit.MILLISECONDS);
    serializer = new BinarySerializer();
    TProtocolFactory factory = new TBinaryProtocol.Factory();
    impl = Mockito.mock(Iface.class);
    TNonblockingServerTransport transport = new TNonblockingServerSocket(0);
    server = new TNonblockingServer(new TNonblockingServer.Args(transport).protocolFactory(factory).processor(new Processor<>(impl)));
    port = ((TNonblockingServerSocket) transport).getPort();
    executor = Executors.newSingleThreadExecutor();
    executor.submit(server::serve);
    address = new InetSocketAddress("localhost", port);
}
Also used : TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) Iface(net.morimekta.test.thrift.thrift.service.MyService.Iface) TNonblockingServerTransport(org.apache.thrift.transport.TNonblockingServerTransport) InetSocketAddress(java.net.InetSocketAddress) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TNonblockingServer(org.apache.thrift.server.TNonblockingServer) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Before(org.junit.Before)

Example 14 with BinarySerializer

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

the class SocketClientHandlerTest method setUpServer.

@BeforeClass
public static void setUpServer() throws Exception {
    Awaitility.setDefaultPollDelay(2, TimeUnit.MILLISECONDS);
    impl = Mockito.mock(Iface.class);
    TServerSocket transport = new TServerSocket(0);
    server = new TSimpleServer(new TServer.Args(transport).protocolFactory(new TBinaryProtocol.Factory()).processor(new Processor<>(impl)));
    executor = Executors.newSingleThreadExecutor();
    executor.submit(server::serve);
    serializer = new BinarySerializer();
    port = transport.getServerSocket().getLocalPort();
    address = new InetSocketAddress("localhost", port);
}
Also used : TServerSocket(org.apache.thrift.transport.TServerSocket) Iface(net.morimekta.test.thrift.thrift.service.MyService.Iface) InetSocketAddress(java.net.InetSocketAddress) TSimpleServer(org.apache.thrift.server.TSimpleServer) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) BeforeClass(org.junit.BeforeClass)

Example 15 with BinarySerializer

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

the class SocketServerTest method setUpClass.

@BeforeClass
public static void setUpClass() {
    setDefaultPollDelay(20, TimeUnit.MILLISECONDS);
    impl = mock(Iface.class);
    instrumentation = mock(ServiceCallInstrumentation.class);
    server = SocketServer.builder(new Processor(impl)).withSerializer(new BinarySerializer(true)).withInstrumentation(instrumentation).withMaxBacklog(1).withThreadFactory(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("test-%d").build()).withWorkerThreads(1).withClientTimeout(200).start();
    port = server.getPort();
}
Also used : Iface(net.morimekta.test.providence.thrift.service.MyService.Iface) Processor(net.morimekta.test.providence.thrift.service.MyService.Processor) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ServiceCallInstrumentation(net.morimekta.providence.util.ServiceCallInstrumentation) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) BeforeClass(org.junit.BeforeClass)

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