public void process(MessagePack msgpack, Socket socket) throws IOException {
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
try {
Unpacker unpacker = msgpack.createUnpacker(in);
while (true) {
Event e = unpacker.read(Event.class);
elist1.add(e);
if (elist1.size() >= 1)
break;
}
socket.close();
} catch (EOFException e) {
// ignore
}
}
});
threadManager.submit(fluentd1);
// start a logger
final FluentLogger logger = FluentLogger.getLogger("testtag", host, port);
final ErrorHandler errorHandler = new ErrorHandler() {
@Override
public void handleNetworkError(IOException ex) {
lastError.set(ex);
}
};
logger.setErrorHandler(errorHandler);
assertFalse(logger.isConnected());
{
Map<String, Object> data = new HashMap<String, Object>();
data.put("k1", "v1");
data.put("k2", "v2");
logger.log("test01", data);
}
assertTrue(logger.isConnected());
// close the fluentd to make the situation that the fluentd gets down
TimeUnit.MILLISECONDS.sleep(500);
_logger.info("Closing the current fluentd instance");
fluentd1.closeClientSockets();
fluentd1.close();
// the logger should fail to send an event
TimeUnit.MILLISECONDS.sleep(500);
assertTrue(logger.isConnected());
for (int i = 0; i < 2; i++) {
// repeat twice to test both behaviors on socket write error and connection error
assertNull(lastError.get());
{
// writing to the closed socket
Map<String, Object> data = new HashMap<String, Object>();
data.put("k3", "v3");
data.put("k4", "v4");
logger.log("test01", data);
}
assertTrue(lastError.get() instanceof IOException);
lastError.set(null); // Clear the last error
assertFalse(logger.isConnected());
TimeUnit.MILLISECONDS.sleep(100);
}
// the logger shouldn't call the error handler after calling removeErrorHandler()
logger.removeErrorHandler();
{
// writing to the closed socket
Map<String, Object> data = new HashMap<String, Object>();
data.put("k3", "v3");
data.put("k4", "v4");
logger.log("test01", data);
}
assertNull(lastError.get());
lastError.set(null); // Clear the last error
logger.setErrorHandler(errorHandler);
// run the fluentd again
final List<Event> elist2 = new ArrayList<Event>();
MockFluentd fluentd2 = new MockFluentd(port, new MockFluentd.MockProcess() {
public void process(MessagePack msgpack, Socket socket) throws IOException {
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
try {
Unpacker unpacker = msgpack.createUnpacker(in);
while (true) {
Event e = unpacker.read(Event.class);
elist2.add(e);
}
// socket.close();
} catch (EOFException e) {
// ignore