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.classic.net.server; 015 016import static org.junit.Assert.assertEquals; 017import static org.junit.Assert.assertNotNull; 018import static org.junit.Assert.assertTrue; 019 020import java.io.ObjectOutputStream; 021import java.net.InetAddress; 022import java.net.ServerSocket; 023import java.net.Socket; 024import java.util.concurrent.ExecutorService; 025import java.util.concurrent.TimeUnit; 026 027import org.junit.After; 028import org.junit.Before; 029import org.junit.Ignore; 030import org.junit.Test; 031 032import ch.qos.logback.classic.Level; 033import ch.qos.logback.classic.Logger; 034import ch.qos.logback.classic.LoggerContext; 035import ch.qos.logback.classic.net.mock.MockAppender; 036import ch.qos.logback.classic.spi.ILoggingEvent; 037import ch.qos.logback.classic.spi.LoggingEvent; 038import ch.qos.logback.classic.spi.LoggingEventVO; 039import ch.qos.logback.core.net.server.test.ServerSocketUtil; 040 041/** 042 * A functional test for {@link ServerSocketReceiver}. 043 * <p> 044 * In this test we create a SocketServer, connect to it over the local 045 * network interface, and validate that it receives messages and delivers 046 * them to its appender. 047 */ 048@Ignore 049public class ServerSocketReceiverFunctionalTest { 050 051 private static final int EVENT_COUNT = 10; 052 private static final int SHUTDOWN_DELAY = 10000; 053 054 private MockAppender appender; 055 private Logger logger; 056 private ServerSocket serverSocket; 057 private InstrumentedServerSocketReceiver receiver; 058 private LoggerContext lc; 059 060 @Before 061 public void setUp() throws Exception { 062 lc = new LoggerContext(); 063 064 appender = new MockAppender(); 065 appender.start(); 066 067 logger = lc.getLogger(getClass()); 068 logger.addAppender(appender); 069 070 serverSocket = ServerSocketUtil.createServerSocket(); 071 072 receiver = new InstrumentedServerSocketReceiver(serverSocket); 073 074 receiver.setContext(lc); 075 receiver.start(); 076 } 077 078 @After 079 public void tearDown() throws Exception { 080 receiver.stop(); 081 ExecutorService executor = lc.getExecutorService(); 082 executor.shutdownNow(); 083 executor.awaitTermination(SHUTDOWN_DELAY, TimeUnit.MILLISECONDS); 084 assertTrue(executor.isTerminated()); 085 } 086 087 @Test 088 public void testLogEventFromClient() throws Exception { 089 ILoggingEvent event = new LoggingEvent(logger.getName(), logger, Level.DEBUG, "test message", null, new Object[0]); 090 Socket socket = new Socket(InetAddress.getLocalHost(), serverSocket.getLocalPort()); 091 092 try { 093 LoggingEventVO eventVO = LoggingEventVO.build(event); 094 095 ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); 096 for (int i = 0; i < EVENT_COUNT; i++) { 097 oos.writeObject(eventVO); 098 } 099 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}