1
2
3
4
5
6
7
8
9
10
11
12
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
43
44
45
46
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 }