Search in sources :

Example 1 with Discovery

use of org.apache.apex.malhar.flume.discovery.Discovery in project apex-malhar by apache.

the class FlumeSinkTest method testServer.

@Test
@SuppressWarnings("SleepWhileInLoop")
public void testServer() throws InterruptedException, IOException {
    final CountDownLatch countdown = new CountDownLatch(1);
    Discovery<byte[]> discovery = new Discovery<byte[]>() {

        @Override
        public void unadvertise(Service<byte[]> service) {
            logger.info("Unadvertise invoked");
            countdown.countDown();
        }

        @Override
        public void advertise(Service<byte[]> service) {
            logger.info("Advertise invoked");
            port = service.getPort();
            logger.debug("listening at {}", service);
            countdown.countDown();
        }

        @Override
        @SuppressWarnings("unchecked")
        public synchronized Collection<Service<byte[]>> discover() {
            logger.info("Discover invoked");
            try {
                countdown.await(30, TimeUnit.SECONDS);
                logger.info("Discover wait completed");
            } catch (InterruptedException ie) {
                throw new RuntimeException(ie);
            }
            return Collections.EMPTY_LIST;
        }
    };
    DefaultEventLoop eventloop = new DefaultEventLoop("Eventloop-TestClient");
    eventloop.start();
    FlumeSink sink = new FlumeSink();
    sink.setName("TeskSink");
    sink.setHostname(hostname);
    sink.setPort(0);
    sink.setAcceptedTolerance(2000);
    sink.setChannel(new MemoryChannel());
    sink.setDiscovery(discovery);
    sink.start();
    logger.info("Sink started");
    AbstractLengthPrependerClient client = new AbstractLengthPrependerClient() {

        private byte[] array;

        private int offset = 2;

        @Override
        public void onMessage(byte[] buffer, int offset, int size) {
            Slice received = new Slice(buffer, offset, size);
            logger.debug("Client Received = {}", received);
            Assert.assertEquals(received, new Slice(Arrays.copyOfRange(array, this.offset, array.length), 0, Server.Request.FIXED_SIZE));
            synchronized (FlumeSinkTest.this) {
                FlumeSinkTest.this.notify();
            }
        }

        @Override
        public void connected() {
            super.connected();
            array = new byte[Server.Request.FIXED_SIZE + offset];
            array[offset] = Server.Command.ECHO.getOrdinal();
            array[offset + 1] = 1;
            array[offset + 2] = 2;
            array[offset + 3] = 3;
            array[offset + 4] = 4;
            array[offset + 5] = 5;
            array[offset + 6] = 6;
            array[offset + 7] = 7;
            array[offset + 8] = 8;
            Server.writeLong(array, offset + Server.Request.TIME_OFFSET, System.currentTimeMillis());
            write(array, offset, Server.Request.FIXED_SIZE);
            logger.info("Connect complete");
        }
    };
    discovery.discover();
    try {
        eventloop.connect(new InetSocketAddress(hostname, port), client);
        try {
            synchronized (this) {
                this.wait();
            }
        } finally {
            eventloop.disconnect(client);
        }
    } finally {
        eventloop.stop();
    }
    sink.stop();
}
Also used : MemoryChannel(org.apache.flume.channel.MemoryChannel) AbstractLengthPrependerClient(com.datatorrent.netlet.AbstractLengthPrependerClient) Slice(com.datatorrent.netlet.util.Slice) InetSocketAddress(java.net.InetSocketAddress) Discovery(org.apache.apex.malhar.flume.discovery.Discovery) DefaultEventLoop(com.datatorrent.netlet.DefaultEventLoop) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

AbstractLengthPrependerClient (com.datatorrent.netlet.AbstractLengthPrependerClient)1 DefaultEventLoop (com.datatorrent.netlet.DefaultEventLoop)1 Slice (com.datatorrent.netlet.util.Slice)1 InetSocketAddress (java.net.InetSocketAddress)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Discovery (org.apache.apex.malhar.flume.discovery.Discovery)1 MemoryChannel (org.apache.flume.channel.MemoryChannel)1 Test (org.junit.Test)1