View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.net;
15  
16  import static org.junit.Assert.assertTrue;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.concurrent.CountDownLatch;
21  
22  import org.junit.Ignore;
23  import org.junit.Test;
24  
25  import ch.qos.logback.classic.Logger;
26  import ch.qos.logback.classic.LoggerContext;
27  import ch.qos.logback.classic.spi.ILoggingEvent;
28  import ch.qos.logback.core.AppenderBase;
29  import ch.qos.logback.core.testUtil.RandomUtil;
30  import ch.qos.logback.core.util.Duration;
31  
32  @Ignore
33  public class SocketAppenderMessageLossTest {
34      int runLen = 100;
35      Duration reconnectionDelay = new Duration(1000);
36  
37      static final int TIMEOUT = 3000;
38  
39      @Test  // (timeout = TIMEOUT)
40      public void synchronousSocketAppender() throws Exception {
41  
42          SocketAppender socketAppender = new SocketAppender();
43          socketAppender.setReconnectionDelay(reconnectionDelay);
44          socketAppender.setIncludeCallerData(true);
45  
46          runTest(socketAppender);
47      }
48  
49      @Test(timeout = TIMEOUT)
50      public void smallQueueSocketAppender() throws Exception {
51  
52          SocketAppender socketAppender = new SocketAppender();
53          socketAppender.setReconnectionDelay(reconnectionDelay);
54          socketAppender.setQueueSize(runLen / 10);
55  
56          runTest(socketAppender);
57      }
58  
59      @Test(timeout = TIMEOUT)
60      public void largeQueueSocketAppender() throws Exception {
61          SocketAppender socketAppender = new SocketAppender();
62          socketAppender.setReconnectionDelay(reconnectionDelay);
63          socketAppender.setQueueSize(runLen * 5);
64  
65          runTest(socketAppender);
66      }
67  
68      // appender used to signal when the N'th event (as set in the latch) is received by the server
69      // this allows us to have test which are both more robust and quicker.
70      static public class ListAppenderWithLatch extends AppenderBase<ILoggingEvent> {
71          public List<ILoggingEvent> list = new ArrayList<ILoggingEvent>();
72          CountDownLatch latch;
73  
74          ListAppenderWithLatch(CountDownLatch latch) {
75              this.latch = latch;
76          }
77  
78          protected void append(ILoggingEvent e) {
79              list.add(e);
80              latch.countDown();
81          }
82      }
83  
84      public void runTest(SocketAppender socketAppender) throws Exception {
85          final int port = RandomUtil.getRandomServerPort();
86  
87          LoggerContext serverLoggerContext = new LoggerContext();
88          serverLoggerContext.setName("serverLoggerContext");
89  
90          CountDownLatch allMessagesReceivedLatch = new CountDownLatch(runLen);
91          ListAppenderWithLatch listAppender = new ListAppenderWithLatch(allMessagesReceivedLatch);
92          listAppender.setContext(serverLoggerContext);
93          listAppender.start();
94  
95          Logger serverRootLogger = serverLoggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
96          serverRootLogger.setAdditive(false);
97          serverRootLogger.addAppender(listAppender);
98  
99          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 }