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();
}
}
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());
}
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());
}
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]);
}
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);
}
Aggregations