001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.net.server;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertTrue;
018
019import java.io.ObjectInputStream;
020import java.net.InetAddress;
021import java.net.ServerSocket;
022import java.net.Socket;
023import java.util.concurrent.ScheduledExecutorService;
024import java.util.concurrent.TimeUnit;
025
026import org.junit.After;
027import org.junit.Before;
028import org.junit.Ignore;
029import org.junit.Test;
030
031import ch.qos.logback.core.net.mock.MockContext;
032import ch.qos.logback.core.net.server.test.ServerSocketUtil;
033import ch.qos.logback.core.util.ExecutorServiceUtil;
034
035/**
036 * A functional test for {@link AbstractServerSocketAppender}.
037 *
038 * @author Carl Harris
039 */
040@Ignore
041public class ServerSocketAppenderBaseFunctionalTest {
042
043    private static final String TEST_EVENT = "test event";
044
045    private static final int EVENT_COUNT = 10;
046
047    private ScheduledExecutorService executor = ExecutorServiceUtil.newScheduledExecutorService();
048    private MockContext context = new MockContext(executor);
049    private ServerSocket serverSocket;
050    private InstrumentedServerSocketAppenderBase appender;
051
052    @Before
053    public void setUp() throws Exception {
054
055        serverSocket = ServerSocketUtil.createServerSocket();
056
057        appender = new InstrumentedServerSocketAppenderBase(serverSocket);
058        appender.setContext(context);
059    }
060
061    @After
062    public void tearDown() throws Exception {
063        executor.shutdownNow();
064        executor.awaitTermination(10000, TimeUnit.MILLISECONDS);
065        assertTrue(executor.isTerminated());
066    }
067
068    @Test
069    public void testLogEventClient() throws Exception {
070        appender.start();
071        Socket socket = new Socket(InetAddress.getLocalHost(), serverSocket.getLocalPort());
072
073        socket.setSoTimeout(1000);
074        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
075
076        for (int i = 0; i < EVENT_COUNT; i++) {
077            appender.append(TEST_EVENT + i);
078            assertEquals(TEST_EVENT + i, ois.readObject());
079        }
080
081        socket.close();
082        appender.stop();
083    }
084
085}