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.assertEquals;
17  import static org.junit.Assert.assertFalse;
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertNull;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.IOException;
23  import java.io.ObjectOutputStream;
24  import java.net.InetAddress;
25  import java.net.ServerSocket;
26  import java.net.Socket;
27  import java.net.UnknownHostException;
28  import java.util.concurrent.ExecutorService;
29  import java.util.concurrent.TimeUnit;
30  
31  import javax.net.SocketFactory;
32  
33  import org.junit.After;
34  import org.junit.Before;
35  import org.junit.Ignore;
36  import org.junit.Test;
37  
38  import ch.qos.logback.classic.Level;
39  import ch.qos.logback.classic.Logger;
40  import ch.qos.logback.classic.LoggerContext;
41  import ch.qos.logback.classic.net.mock.MockAppender;
42  import ch.qos.logback.classic.spi.ILoggingEvent;
43  import ch.qos.logback.classic.spi.LoggingEvent;
44  import ch.qos.logback.classic.spi.LoggingEventVO;
45  import ch.qos.logback.core.net.SocketConnector;
46  import ch.qos.logback.core.net.server.test.ServerSocketUtil;
47  import ch.qos.logback.core.status.Status;
48  
49  /**
50   * Unit tests for {@link SocketReceiver}.
51   *
52   * @author Carl Harris
53   */
54  @Ignore
55  public class SocketReceiverTest {
56  
57      private static final int DELAY = 1000;
58      private static final String TEST_HOST_NAME = "NOT.A.VALID.HOST.NAME";
59  
60      private ServerSocket serverSocket;
61      private Socket socket;
62      private MockSocketFactory socketFactory = new MockSocketFactory();
63      private MockSocketConnector connector;
64      private MockAppender appender;
65      private LoggerContext lc;
66      private Logger logger;
67  
68      private InstrumentedSocketReceiver receiver = new InstrumentedSocketReceiver();
69  
70      @Before
71      public void setUp() throws Exception {
72          serverSocket = ServerSocketUtil.createServerSocket();
73          socket = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort());
74          connector = new MockSocketConnector(socket);
75  
76          lc = new LoggerContext();
77          lc.reset();
78          receiver.setContext(lc);
79          appender = new MockAppender();
80          appender.start();
81          logger = lc.getLogger(getClass());
82          logger.addAppender(appender);
83      }
84  
85      @After
86      public void tearDown() throws Exception {
87          receiver.stop();
88          ExecutorService executor = lc.getExecutorService();
89          executor.shutdownNow();
90          assertTrue(executor.awaitTermination(DELAY, TimeUnit.MILLISECONDS));
91          socket.close();
92          serverSocket.close();
93          lc.stop();
94      }
95  
96      @Test
97      public void testStartNoRemoteAddress() throws Exception {
98          receiver.start();
99          assertFalse(receiver.isStarted());
100         int count = lc.getStatusManager().getCount();
101         Status status = lc.getStatusManager().getCopyOfStatusList().get(count - 1);
102         assertTrue(status.getMessage().contains("host"));
103     }
104 
105     @Test
106     public void testStartNoPort() throws Exception {
107         receiver.setRemoteHost(TEST_HOST_NAME);
108         receiver.start();
109         assertFalse(receiver.isStarted());
110         int count = lc.getStatusManager().getCount();
111         Status status = lc.getStatusManager().getCopyOfStatusList().get(count - 1);
112         assertTrue(status.getMessage().contains("port"));
113     }
114 
115     @Test
116     public void testStartUnknownHost() throws Exception {
117         receiver.setPort(6000);
118         receiver.setRemoteHost(TEST_HOST_NAME);
119         receiver.start();
120         assertFalse(receiver.isStarted());
121         int count = lc.getStatusManager().getCount();
122         Status status = lc.getStatusManager().getCopyOfStatusList().get(count - 1);
123         assertTrue(status.getMessage().contains("unknown host"));
124     }
125 
126     @Test
127     public void testStartStop() throws Exception {
128         receiver.setRemoteHost(InetAddress.getLocalHost().getHostName());
129         receiver.setPort(6000);
130         receiver.setAcceptConnectionTimeout(DELAY / 2);
131         receiver.start();
132         assertTrue(receiver.isStarted());
133         receiver.awaitConnectorCreated(DELAY);
134         receiver.stop();
135         assertFalse(receiver.isStarted());
136     }
137 
138     @Test
139     public void testServerSlowToAcceptConnection() throws Exception {
140         receiver.setRemoteHost(InetAddress.getLocalHost().getHostName());
141         receiver.setPort(6000);
142         receiver.setAcceptConnectionTimeout(DELAY / 4);
143         receiver.start();
144         assertTrue(receiver.awaitConnectorCreated(DELAY / 2));
145         // note that we don't call serverSocket.accept() here
146         // but processPriorToRemoval (in tearDown) should still clean up everything
147     }
148 
149     @Test
150     public void testServerDropsConnection() throws Exception {
151         receiver.setRemoteHost(InetAddress.getLocalHost().getHostName());
152         receiver.setPort(6000);
153         receiver.start();
154         assertTrue(receiver.awaitConnectorCreated(DELAY));
155         Socket socket = serverSocket.accept();
156         socket.close();
157     }
158 
159     @Test
160     public void testDispatchEventForEnabledLevel() throws Exception {
161         receiver.setRemoteHost(InetAddress.getLocalHost().getHostName());
162         receiver.setPort(6000);
163         receiver.start();
164         assertTrue(receiver.awaitConnectorCreated(DELAY));
165         Socket socket = serverSocket.accept();
166 
167         ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
168 
169         logger.setLevel(Level.DEBUG);
170         ILoggingEvent event = new LoggingEvent(logger.getName(), logger, Level.DEBUG, "test message", null, new Object[0]);
171 
172         LoggingEventVO eventVO = LoggingEventVO.build(event);
173         oos.writeObject(eventVO);
174         oos.flush();
175 
176         ILoggingEvent rcvdEvent = appender.awaitAppend(DELAY);
177         assertNotNull(rcvdEvent);
178         assertEquals(event.getLoggerName(), rcvdEvent.getLoggerName());
179         assertEquals(event.getLevel(), rcvdEvent.getLevel());
180         assertEquals(event.getMessage(), rcvdEvent.getMessage());
181     }
182 
183     @Test
184     public void testNoDispatchEventForDisabledLevel() throws Exception {
185         receiver.setRemoteHost(InetAddress.getLocalHost().getHostName());
186         receiver.setPort(6000);
187         receiver.start();
188         assertTrue(receiver.awaitConnectorCreated(DELAY));
189         Socket socket = serverSocket.accept();
190 
191         ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
192 
193         logger.setLevel(Level.INFO);
194         ILoggingEvent event = new LoggingEvent(logger.getName(), logger, Level.DEBUG, "test message", null, new Object[0]);
195 
196         LoggingEventVO eventVO = LoggingEventVO.build(event);
197         oos.writeObject(eventVO);
198         oos.flush();
199 
200         assertNull(appender.awaitAppend(DELAY));
201     }
202 
203     /**
204      * A {@link SocketReceiver} with instrumentation for unit testing.
205      */
206     private class InstrumentedSocketReceiver extends SocketReceiver {
207 
208         private boolean connectorCreated;
209 
210         @Override
211         protected synchronized SocketConnector newConnector(InetAddress address, int port, int initialDelay, int retryDelay) {
212             connectorCreated = true;
213             notifyAll();
214             return connector;
215         }
216 
217         @Override
218         protected SocketFactory getSocketFactory() {
219             return socketFactory;
220         }
221 
222         public synchronized boolean awaitConnectorCreated(long delay) throws InterruptedException {
223             while (!connectorCreated) {
224                 wait(delay);
225             }
226             return connectorCreated;
227         }
228 
229     }
230 
231     /**
232      * A {@link SocketConnector} with instrumentation for unit testing.
233      */
234     private static class MockSocketConnector implements SocketConnector {
235 
236         private final Socket socket;
237 
238         public MockSocketConnector(Socket socket) {
239             this.socket = socket;
240         }
241 
242         public Socket call() throws InterruptedException {
243             return socket;
244         }
245 
246         public void setExceptionHandler(ExceptionHandler exceptionHandler) {
247         }
248 
249         public void setSocketFactory(SocketFactory socketFactory) {
250         }
251 
252     }
253 
254     /**
255      * A no-op {@link SocketFactory} to support unit testing.
256      */
257     private static class MockSocketFactory extends SocketFactory {
258 
259         @Override
260         public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
261             throw new UnsupportedOperationException();
262         }
263 
264         @Override
265         public Socket createSocket(InetAddress host, int port) throws IOException {
266             throw new UnsupportedOperationException();
267         }
268 
269         @Override
270         public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
271             throw new UnsupportedOperationException();
272         }
273 
274         @Override
275         public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
276             throw new UnsupportedOperationException();
277         }
278 
279     }
280 
281 }