Search in sources :

Example 1 with LogDispatcher

use of com.yahoo.logserver.LogDispatcher in project vespa by vespa-engine.

the class LogConnectionTestCase method testOverflow.

/**
 * this code is nothing short of completely hideous.  the exception
 * handling is awful and the code is messy, but it should be a fairly
 * efficient and robust way of testing buffer overflow conditions.
 */
@Test
public void testOverflow() {
    final CyclicBarrier barrier = new CyclicBarrier(2);
    // handled.
    class MockHandler extends AbstractLogHandler {

        private final List<LogMessage> messages = new LinkedList<LogMessage>();

        public boolean doHandle(LogMessage msg) {
            messages.add(msg);
            return true;
        }

        public List<LogMessage> getMessages() {
            return messages;
        }

        public void flush() {
        }

        public void close() {
        }

        public String toString() {
            return null;
        }
    }
    Thread serverThread = new Thread() {

        public void run() {
            ServerSocketChannel ss = setUpListenSocket();
            if (ss == null) {
                fail("unable to set up listen socket");
                return;
            }
            setPort(ss.socket().getLocalPort());
            // listen port is up now so we can trigger the barrier
            try {
                barrier.await();
                while (!Thread.currentThread().isInterrupted()) {
                    SocketChannel s = ss.accept();
                    pushBigBuffer(s);
                    s.close();
                }
            } catch (BrokenBarrierException e) {
                fail(e.getMessage());
                return;
            } catch (InterruptedException | java.nio.channels.ClosedByInterruptException e) {
                return;
            } catch (IOException e) {
                log.log(LogLevel.ERROR, "argh", e);
                fail();
                return;
            }
        }
    };
    serverThread.start();
    assertTrue(serverThread.isAlive());
    try {
        barrier.await();
    } catch (BrokenBarrierException e) {
        fail(e.getMessage());
        return;
    } catch (InterruptedException e) {
        return;
    }
    SocketChannel sock;
    try {
        sock = SocketChannel.open(new InetSocketAddress("localhost", port));
    } catch (IOException e) {
        fail(e.getMessage());
        return;
    }
    LogDispatcher dispatcher = new LogDispatcher();
    MockHandler mock = new MockHandler();
    assertTrue(mock.getName().endsWith("MockHandler"));
    dispatcher.registerLogHandler(mock);
    LogConnection logConnection = new LogConnection(sock, null, dispatcher, null);
    try {
        for (int i = 0; i < 100; i++) {
            logConnection.read();
        }
    } catch (java.nio.channels.ClosedChannelException e) {
    // ignore, this is normal
    } catch (IOException e) {
        log.log(LogLevel.ERROR, "error during reading", e);
    }
    // there should be 5 messages
    assertEquals(5, mock.getMessages().size());
    assertEquals(5, mock.getCount());
    // the 4'th message should be long
    String m = (mock.getMessages().get(3)).getPayload();
    assertTrue(m.length() > 10000);
    serverThread.interrupt();
    try {
        serverThread.join();
        assertTrue(true);
    } catch (InterruptedException e) {
        fail();
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) LogConnection(com.yahoo.logserver.net.LogConnection) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AbstractLogHandler(com.yahoo.logserver.handlers.AbstractLogHandler) LogMessage(com.yahoo.log.LogMessage) LogDispatcher(com.yahoo.logserver.LogDispatcher) List(java.util.List) LinkedList(java.util.LinkedList) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Test(org.junit.Test)

Example 2 with LogDispatcher

use of com.yahoo.logserver.LogDispatcher in project vespa by vespa-engine.

the class LogDispatcherTestCase method testMessageCount.

@Test
public void testMessageCount() {
    MockHandler handler = new MockHandler();
    LogDispatcher dispatcher = new LogDispatcher();
    dispatcher.registerLogHandler(handler);
    assertEquals(0, dispatcher.getMessageCount());
    dispatcher.handle(sample1);
    assertEquals(1, dispatcher.getMessageCount());
    dispatcher.handle(sample2);
    assertEquals(2, dispatcher.getMessageCount());
    assertEquals(2, handler.messages.size());
}
Also used : LogDispatcher(com.yahoo.logserver.LogDispatcher)

Example 3 with LogDispatcher

use of com.yahoo.logserver.LogDispatcher in project vespa by vespa-engine.

the class LogDispatcherTestCase method testLogDispatcherBatchMode.

@Test
public void testLogDispatcherBatchMode() {
    MockHandler handler = new MockHandler();
    LogDispatcher dispatcher = new LogDispatcher();
    dispatcher.setBatchedMode(true);
    dispatcher.registerLogHandler(handler);
    assertEquals(0, dispatcher.getMessageCount());
    dispatcher.handle(sample1);
    assertEquals(1, dispatcher.getMessageCount());
    dispatcher.handle(sample2);
    assertEquals(2, dispatcher.getMessageCount());
    assertEquals(0, handler.messages.size());
    dispatcher.flush();
    assertEquals(2, handler.messages.size());
}
Also used : LogDispatcher(com.yahoo.logserver.LogDispatcher)

Example 4 with LogDispatcher

use of com.yahoo.logserver.LogDispatcher in project vespa by vespa-engine.

the class LogDispatcherTestCase method testTestLogHandlerRegistration.

@Test
public void testTestLogHandlerRegistration() {
    MockHandler handler = new MockHandler();
    LogDispatcher dispatcher = new LogDispatcher();
    dispatcher.registerLogHandler(handler);
    assertNotNull(dispatcher.getLogHandlers());
    LogHandler[] handlers = dispatcher.getLogHandlers();
    assertNotNull(handlers);
    assertEquals(1, handlers.length);
    assertTrue(handler == handlers[0]);
}
Also used : LogHandler(com.yahoo.logserver.handlers.LogHandler) LogDispatcher(com.yahoo.logserver.LogDispatcher)

Example 5 with LogDispatcher

use of com.yahoo.logserver.LogDispatcher in project vespa by vespa-engine.

the class LogDispatcherTestCase method testClose.

// TODO: this test makes very little sense until we refactor a bit
@Test
public void testClose() {
    MockHandler handler = new MockHandler();
    LogDispatcher dispatcher = new LogDispatcher();
    dispatcher.registerLogHandler(handler);
    assertEquals(0, handler.flushCalled);
    assertEquals(0, handler.closeCalled);
    dispatcher.close();
    assertEquals(0, handler.closeCalled);
}
Also used : LogDispatcher(com.yahoo.logserver.LogDispatcher)

Aggregations

LogDispatcher (com.yahoo.logserver.LogDispatcher)6 LogMessage (com.yahoo.log.LogMessage)1 AbstractLogHandler (com.yahoo.logserver.handlers.AbstractLogHandler)1 LogHandler (com.yahoo.logserver.handlers.LogHandler)1 LogConnection (com.yahoo.logserver.net.LogConnection)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 Test (org.junit.Test)1