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.server;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertTrue;
19  
20  import java.io.ObjectOutputStream;
21  import java.net.InetAddress;
22  import java.net.ServerSocket;
23  import java.net.Socket;
24  import java.util.concurrent.ExecutorService;
25  import java.util.concurrent.TimeUnit;
26  
27  import org.junit.After;
28  import org.junit.Before;
29  import org.junit.Ignore;
30  import org.junit.Test;
31  
32  import ch.qos.logback.classic.Level;
33  import ch.qos.logback.classic.Logger;
34  import ch.qos.logback.classic.LoggerContext;
35  import ch.qos.logback.classic.net.mock.MockAppender;
36  import ch.qos.logback.classic.spi.ILoggingEvent;
37  import ch.qos.logback.classic.spi.LoggingEvent;
38  import ch.qos.logback.classic.spi.LoggingEventVO;
39  import ch.qos.logback.core.net.server.test.ServerSocketUtil;
40  
41  /**
42   * A functional test for {@link ServerSocketReceiver}.
43   * <p>
44   * In this test we create a SocketServer, connect to it over the local
45   * network interface, and validate that it receives messages and delivers
46   * them to its appender.
47   */
48  @Ignore
49  public class ServerSocketReceiverFunctionalTest {
50  
51      private static final int EVENT_COUNT = 10;
52      private static final int SHUTDOWN_DELAY = 10000;
53  
54      private MockAppender appender;
55      private Logger logger;
56      private ServerSocket serverSocket;
57      private InstrumentedServerSocketReceiver receiver;
58      private LoggerContext lc;
59  
60      @Before
61      public void setUp() throws Exception {
62          lc = new LoggerContext();
63  
64          appender = new MockAppender();
65          appender.start();
66  
67          logger = lc.getLogger(getClass());
68          logger.addAppender(appender);
69  
70          serverSocket = ServerSocketUtil.createServerSocket();
71  
72          receiver = new InstrumentedServerSocketReceiver(serverSocket);
73  
74          receiver.setContext(lc);
75          receiver.start();
76      }
77  
78      @After
79      public void tearDown() throws Exception {
80          receiver.stop();
81          ExecutorService executor = lc.getExecutorService();
82          executor.shutdownNow();
83          executor.awaitTermination(SHUTDOWN_DELAY, TimeUnit.MILLISECONDS);
84          assertTrue(executor.isTerminated());
85      }
86  
87      @Test
88      public void testLogEventFromClient() throws Exception {
89          ILoggingEvent event = new LoggingEvent(logger.getName(), logger, Level.DEBUG, "test message", null, new Object[0]);
90          Socket socket = new Socket(InetAddress.getLocalHost(), serverSocket.getLocalPort());
91  
92          try {
93              LoggingEventVO eventVO = LoggingEventVO.build(event);
94  
95              ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
96              for (int i = 0; i < EVENT_COUNT; i++) {
97                  oos.writeObject(eventVO);
98              }
99  
100             oos.writeObject(eventVO);
101             oos.flush();
102         } finally {
103             socket.close();
104         }
105 
106         ILoggingEvent rcvdEvent = appender.awaitAppend(SHUTDOWN_DELAY);
107         assertNotNull(rcvdEvent);
108         assertEquals(event.getLoggerName(), rcvdEvent.getLoggerName());
109         assertEquals(event.getLevel(), rcvdEvent.getLevel());
110         assertEquals(event.getMessage(), rcvdEvent.getMessage());
111     }
112 
113 }