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.core.net.server;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertTrue;
18  
19  import java.io.ObjectInputStream;
20  import java.net.InetAddress;
21  import java.net.ServerSocket;
22  import java.net.Socket;
23  import java.util.concurrent.ScheduledExecutorService;
24  import java.util.concurrent.TimeUnit;
25  
26  import org.junit.After;
27  import org.junit.Before;
28  import org.junit.Ignore;
29  import org.junit.Test;
30  
31  import ch.qos.logback.core.net.mock.MockContext;
32  import ch.qos.logback.core.net.server.test.ServerSocketUtil;
33  import ch.qos.logback.core.util.ExecutorServiceUtil;
34  
35  /**
36   * A functional test for {@link AbstractServerSocketAppender}.
37   *
38   * @author Carl Harris
39   */
40  @Ignore
41  public class ServerSocketAppenderBaseFunctionalTest {
42  
43      private static final String TEST_EVENT = "test event";
44  
45      private static final int EVENT_COUNT = 10;
46  
47      private ScheduledExecutorService executor = ExecutorServiceUtil.newScheduledExecutorService();
48      private MockContext context = new MockContext(executor);
49      private ServerSocket serverSocket;
50      private InstrumentedServerSocketAppenderBase appender;
51  
52      @Before
53      public void setUp() throws Exception {
54  
55          serverSocket = ServerSocketUtil.createServerSocket();
56  
57          appender = new InstrumentedServerSocketAppenderBase(serverSocket);
58          appender.setContext(context);
59      }
60  
61      @After
62      public void tearDown() throws Exception {
63          executor.shutdownNow();
64          executor.awaitTermination(10000, TimeUnit.MILLISECONDS);
65          assertTrue(executor.isTerminated());
66      }
67  
68      @Test
69      public void testLogEventClient() throws Exception {
70          appender.start();
71          Socket socket = new Socket(InetAddress.getLocalHost(), serverSocket.getLocalPort());
72  
73          socket.setSoTimeout(1000);
74          ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
75  
76          for (int i = 0; i < EVENT_COUNT; i++) {
77              appender.append(TEST_EVENT + i);
78              assertEquals(TEST_EVENT + i, ois.readObject());
79          }
80  
81          socket.close();
82          appender.stop();
83      }
84  
85  }