use of com.yahoo.logserver.net.LogConnection 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();
}
}
Aggregations