001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.classic.net; 015 016import static org.junit.Assert.assertTrue; 017 018import java.util.ArrayList; 019import java.util.List; 020import java.util.concurrent.CountDownLatch; 021 022import org.junit.Ignore; 023import org.junit.Test; 024 025import ch.qos.logback.classic.Logger; 026import ch.qos.logback.classic.LoggerContext; 027import ch.qos.logback.classic.spi.ILoggingEvent; 028import ch.qos.logback.core.AppenderBase; 029import ch.qos.logback.core.testUtil.RandomUtil; 030import ch.qos.logback.core.util.Duration; 031 032@Ignore 033public class SocketAppenderMessageLossTest { 034 int runLen = 100; 035 Duration reconnectionDelay = new Duration(1000); 036 037 static final int TIMEOUT = 3000; 038 039 @Test // (timeout = TIMEOUT) 040 public void synchronousSocketAppender() throws Exception { 041 042 SocketAppender socketAppender = new SocketAppender(); 043 socketAppender.setReconnectionDelay(reconnectionDelay); 044 socketAppender.setIncludeCallerData(true); 045 046 runTest(socketAppender); 047 } 048 049 @Test(timeout = TIMEOUT) 050 public void smallQueueSocketAppender() throws Exception { 051 052 SocketAppender socketAppender = new SocketAppender(); 053 socketAppender.setReconnectionDelay(reconnectionDelay); 054 socketAppender.setQueueSize(runLen / 10); 055 056 runTest(socketAppender); 057 } 058 059 @Test(timeout = TIMEOUT) 060 public void largeQueueSocketAppender() throws Exception { 061 SocketAppender socketAppender = new SocketAppender(); 062 socketAppender.setReconnectionDelay(reconnectionDelay); 063 socketAppender.setQueueSize(runLen * 5); 064 065 runTest(socketAppender); 066 } 067 068 // appender used to signal when the N'th event (as set in the latch) is received by the server 069 // this allows us to have test which are both more robust and quicker. 070 static public class ListAppenderWithLatch extends AppenderBase<ILoggingEvent> { 071 public List<ILoggingEvent> list = new ArrayList<ILoggingEvent>(); 072 CountDownLatch latch; 073 074 ListAppenderWithLatch(CountDownLatch latch) { 075 this.latch = latch; 076 } 077 078 protected void append(ILoggingEvent e) { 079 list.add(e); 080 latch.countDown(); 081 } 082 } 083 084 public void runTest(SocketAppender socketAppender) throws Exception { 085 final int port = RandomUtil.getRandomServerPort(); 086 087 LoggerContext serverLoggerContext = new LoggerContext(); 088 serverLoggerContext.setName("serverLoggerContext"); 089 090 CountDownLatch allMessagesReceivedLatch = new CountDownLatch(runLen); 091 ListAppenderWithLatch listAppender = new ListAppenderWithLatch(allMessagesReceivedLatch); 092 listAppender.setContext(serverLoggerContext); 093 listAppender.start(); 094 095 Logger serverRootLogger = serverLoggerContext.getLogger(Logger.ROOT_LOGGER_NAME); 096 serverRootLogger.setAdditive(false); 097 serverRootLogger.addAppender(listAppender); 098 099 LoggerContext loggerContext = new LoggerContext(); 100 loggerContext.setName("clientLoggerContext"); 101 socketAppender.setContext(loggerContext); 102 103 CountDownLatch latch = new CountDownLatch(1); 104 SimpleSocketServer simpleSocketServer = new SimpleSocketServer(serverLoggerContext, port); 105 simpleSocketServer.start(); 106 simpleSocketServer.setLatch(latch); 107 108 latch.await(); 109 110 socketAppender.setPort(port); 111 socketAppender.setRemoteHost("localhost"); 112 socketAppender.setReconnectionDelay(reconnectionDelay); 113 socketAppender.start(); 114 assertTrue(socketAppender.isStarted()); 115 116 Logger logger = loggerContext.getLogger(getClass()); 117 logger.setAdditive(false); 118 logger.addAppender(socketAppender); 119 120 for (int i = 0; i < runLen; ++i) { 121 logger.info("hello"); 122 } 123 124 allMessagesReceivedLatch.await(); 125 loggerContext.stop(); 126 simpleSocketServer.close(); 127 128 129 } 130}