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.jupiter.api.Assertions.assertEquals;
17  import static org.junit.jupiter.api.Assertions.assertNotNull;
18  import static org.junit.jupiter.api.Assertions.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 ch.qos.logback.classic.util.LogbackMDCAdapter;
28  import org.junit.jupiter.api.AfterEach;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.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 network
45   * interface, and validate that it receives messages and delivers them to its
46   * appender.
47   */
48  public class ServerSocketReceiverFunctionalTest {
49  
50      private static final int EVENT_COUNT = 10;
51      private static final int SHUTDOWN_DELAY = 10000;
52  
53      private MockAppender appender;
54      private Logger logger;
55      private ServerSocket serverSocket;
56      private InstrumentedServerSocketReceiver receiver;
57      private LoggerContext lc;
58      LogbackMDCAdapter logbackMDCAdapter = new LogbackMDCAdapter();
59      @BeforeEach
60      public void setUp() throws Exception {
61          lc = new LoggerContext();
62          lc.setMDCAdapter(logbackMDCAdapter);
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      @AfterEach
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,
90                  new Object[0]);
91          Socket socket = new Socket(InetAddress.getLocalHost(), serverSocket.getLocalPort());
92  
93          try {
94              LoggingEventVO eventVO = LoggingEventVO.build(event);
95  
96              ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
97              for (int i = 0; i < EVENT_COUNT; i++) {
98                  oos.writeObject(eventVO);
99              }
100 
101             oos.writeObject(eventVO);
102             oos.flush();
103         } finally {
104             socket.close();
105         }
106 
107         ILoggingEvent rcvdEvent = appender.awaitAppend(SHUTDOWN_DELAY);
108         assertNotNull(rcvdEvent);
109         assertEquals(event.getLoggerName(), rcvdEvent.getLoggerName());
110         assertEquals(event.getLevel(), rcvdEvent.getLevel());
111         assertEquals(event.getMessage(), rcvdEvent.getMessage());
112     }
113 
114 }