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.assertNotNull;
17  import static org.junit.Assert.assertTrue;
18  
19  import java.io.IOException;
20  import java.net.InetAddress;
21  import java.net.ServerSocket;
22  import java.net.Socket;
23  
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import ch.qos.logback.core.net.server.Client;
28  import ch.qos.logback.core.net.server.ServerSocketListener;
29  import ch.qos.logback.core.net.server.test.ServerSocketUtil;
30  
31  /**
32   * Unit tests for {@link ServerSocketListener}.
33   *
34   * @author Carl Harris
35   */
36  public class ServerSocketListenerTest {
37  
38      private ServerSocket serverSocket;
39      @SuppressWarnings("rawtypes")
40      private ServerSocketListener listener;
41  
42      @Before
43      public void setUp() throws Exception {
44          serverSocket = ServerSocketUtil.createServerSocket();
45          assertNotNull(serverSocket);
46          listener = new InstrumentedServerSocketListener(serverSocket);
47      }
48  
49      @Test
50      public void testAcceptClient() throws Exception {
51          RunnableClient localClient = new RunnableClient(InetAddress.getLocalHost(), serverSocket.getLocalPort());
52          Thread thread = new Thread(localClient);
53          thread.start();
54          synchronized (localClient) {
55              int retries = 200;
56              while (retries-- > 0 && !localClient.isConnected()) {
57                  localClient.wait(10);
58              }
59          }
60          assertTrue(localClient.isConnected());
61          localClient.close();
62  
63          serverSocket.setSoTimeout(5000);
64          Client client = listener.acceptClient();
65          assertNotNull(client);
66          client.close();
67      }
68  
69      private static class InstrumentedServerSocketListener extends ServerSocketListener<RemoteClient> {
70  
71          public InstrumentedServerSocketListener(ServerSocket serverSocket) {
72              super(serverSocket);
73          }
74  
75          @Override
76          protected RemoteClient createClient(String id, Socket socket) throws IOException {
77              return new RemoteClient(socket);
78          }
79  
80      }
81  
82      private static class RemoteClient implements Client {
83  
84          private final Socket socket;
85  
86          public RemoteClient(Socket socket) {
87              this.socket = socket;
88          }
89  
90          public void run() {
91          }
92  
93          public void close() {
94              try {
95                  socket.close();
96              } catch (IOException ex) {
97                  ex.printStackTrace(System.err);
98              }
99          }
100 
101     }
102 
103     private static class RunnableClient implements Client {
104 
105         private final InetAddress inetAddress;
106         private final int port;
107         private boolean connected;
108         private boolean closed;
109 
110         public RunnableClient(InetAddress inetAddress, int port) {
111             super();
112             this.inetAddress = inetAddress;
113             this.port = port;
114         }
115 
116         public synchronized boolean isConnected() {
117             return connected;
118         }
119 
120         public synchronized void setConnected(boolean connected) {
121             this.connected = connected;
122         }
123 
124         public void run() {
125             try {
126                 Socket socket = new Socket(inetAddress, port);
127                 synchronized (this) {
128                     setConnected(true);
129                     notifyAll();
130                     while (!closed && !Thread.currentThread().isInterrupted()) {
131                         try {
132                             wait();
133                         } catch (InterruptedException ex) {
134                             Thread.currentThread().interrupt();
135                         }
136                     }
137                     socket.close();
138                 }
139             } catch (IOException ex) {
140                 ex.printStackTrace(System.err);
141             }
142         }
143 
144         public synchronized void close() {
145             closed = true;
146             notifyAll();
147         }
148 
149     }
150 }