1   /*
2    * $Id: KeepSendSocketOpenMule1491TestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.tcp.issues;
12  
13  import org.mule.extras.client.MuleClient;
14  import org.mule.providers.tcp.protocols.LengthProtocol;
15  import org.mule.tck.FunctionalTestCase;
16  import org.mule.umo.UMOMessage;
17  
18  import java.io.BufferedInputStream;
19  import java.net.InetSocketAddress;
20  import java.net.ServerSocket;
21  import java.net.Socket;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
26  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
27  
28  public class KeepSendSocketOpenMule1491TestCase  extends FunctionalTestCase 
29  {
30  
31      protected static String TEST_MESSAGE = "Test TCP Request";
32  
33      public KeepSendSocketOpenMule1491TestCase()
34      {
35          setDisposeManagerPerSuite(true);
36      }
37  
38      protected String getConfigResources()
39      {
40          return "tcp-keep-send-socket-open.xml";
41      }
42  
43      public void testSend() throws Exception
44      {
45          MuleClient client = new MuleClient();
46          Map props = new HashMap();
47          UMOMessage result = client.send("clientEndpoint", TEST_MESSAGE, props);
48          assertEquals(TEST_MESSAGE + " Received", result.getPayloadAsString());
49          // try an extra message in case it's a problem on repeat
50          result = client.send("clientEndpoint", TEST_MESSAGE, props);
51          assertEquals(TEST_MESSAGE + " Received", result.getPayloadAsString());
52      }
53  
54      private void useServer(String endpoint, int port, int count) throws Exception
55      {
56          SimpleServerSocket server = new SimpleServerSocket(port);
57          try
58          {
59              new Thread(server).start();
60              MuleClient client = new MuleClient();
61              client.send(endpoint, "Hello", null);
62              client.send(endpoint, "world", null);
63              assertEquals(count, server.getCount());
64          }
65          finally
66          {
67              server.close();
68          }
69      }
70  
71      public void testOpen() throws Exception
72      {
73          useServer("tcp://localhost:60197?connector=openConnectorLength", 60197, 1);
74      }
75  
76      public void testClose() throws Exception
77      {
78          useServer("tcp://localhost:60196?connector=closeConnectorLength", 60196, 3);
79      }
80  
81      private class SimpleServerSocket implements Runnable
82      {
83          
84          private ServerSocket server;
85          AtomicBoolean running = new AtomicBoolean(true);
86          AtomicInteger count = new AtomicInteger(0);
87  
88          public SimpleServerSocket(int port) throws Exception
89          {
90              server = new ServerSocket();
91              logger.debug("starting server");
92              server.bind(new InetSocketAddress("localhost", port), 3);
93          }
94  
95          public int getCount()
96          {
97              return count.get();
98          }
99  
100         public void run()
101         {
102             try
103             {
104                 LengthProtocol protocol = new LengthProtocol();
105                 // repeat for as many connections as we receive until the close()
106                 // method here causes the accept to thrown a exception
107                 while (true)
108                 {
109                     Socket socket = server.accept();
110                     logger.debug("have connection " + count);
111                     count.incrementAndGet();
112                     try
113                     {
114                         // repeat for as many messages as we receive before closing
115                         // of the socket by the client causes an exception to exit this loop
116                         while (true)
117                         {
118                             String msg =
119                                     new String((byte[]) protocol.read(new BufferedInputStream(socket.getInputStream())));
120                             logger.debug("read: " + msg);
121                             logger.debug("writing reply");
122                             protocol.write(socket.getOutputStream(), "ok");
123                         }
124                     }
125                     catch (Exception e)
126                     {
127                         logger.debug(e);
128                     }
129                 }
130             }
131             catch (Exception e)
132             {
133                 // an exception is expected during shutdown
134                 if (running.get())
135                 {
136                     throw new RuntimeException(e);
137                 }
138             }
139         }
140 
141         public void close()
142         {
143             try
144             {
145                 running.set(false);
146                 server.close();
147             }
148             catch (Exception e)
149             {
150                 // no-op
151             }
152         }
153     }
154 
155 }