1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.tcp.issues;
12
13 import org.mule.api.MuleMessage;
14 import org.mule.module.client.MuleClient;
15 import org.mule.tck.FunctionalTestCase;
16 import org.mule.transport.tcp.protocols.LengthProtocol;
17
18 import java.io.BufferedInputStream;
19 import java.io.InputStream;
20 import java.net.InetSocketAddress;
21 import java.net.ServerSocket;
22 import java.net.Socket;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
27 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
28
29 public class KeepSendSocketOpenMule1491TestCase extends FunctionalTestCase
30 {
31
32 protected static String TEST_MESSAGE = "Test TCP Request";
33
34 protected String getConfigResources()
35 {
36 return "tcp-keep-send-socket-open.xml";
37 }
38
39 public void testSend() throws Exception
40 {
41 MuleClient client = new MuleClient();
42 Map props = new HashMap();
43 MuleMessage result = client.send("clientEndpoint", TEST_MESSAGE, props);
44 assertEquals(TEST_MESSAGE + " Received", result.getPayloadAsString());
45
46 result = client.send("clientEndpoint", TEST_MESSAGE, props);
47 assertEquals(TEST_MESSAGE + " Received", result.getPayloadAsString());
48 }
49
50 private void useServer(String endpoint, int port, int count) throws Exception
51 {
52 SimpleServerSocket server = new SimpleServerSocket(port);
53 try
54 {
55 new Thread(server).start();
56 MuleClient client = new MuleClient();
57 client.send(endpoint, "Hello", null);
58 client.send(endpoint, "world", null);
59 assertEquals(count, server.getCount());
60 }
61 finally
62 {
63 server.close();
64 }
65 }
66
67 public void testOpen() throws Exception
68 {
69 useServer("tcp://localhost:60197?connector=openConnectorLength", 60197, 1);
70 }
71
72 public void testClose() throws Exception
73 {
74 useServer("tcp://localhost:60196?connector=closeConnectorLength", 60196, 2);
75 }
76
77 private class SimpleServerSocket implements Runnable
78 {
79
80 private ServerSocket server;
81 AtomicBoolean running = new AtomicBoolean(true);
82 AtomicInteger count = new AtomicInteger(0);
83
84 public SimpleServerSocket(int port) throws Exception
85 {
86 server = new ServerSocket();
87 logger.debug("starting server");
88 server.bind(new InetSocketAddress("localhost", port), 3);
89 }
90
91 public int getCount()
92 {
93 return count.get();
94 }
95
96 public void run()
97 {
98 try
99 {
100 LengthProtocol protocol = new LengthProtocol();
101 while (true)
102 {
103 Socket socket = server.accept();
104 logger.debug("have connection " + count);
105 count.incrementAndGet();
106 InputStream stream = new BufferedInputStream(socket.getInputStream());
107
108 while (true)
109 {
110 Object read = protocol.read(stream);
111 if (null == read)
112 {
113 break;
114 }
115 String msg = new String((byte[]) read);
116 logger.debug("read: " + msg);
117 logger.debug("writing reply");
118 protocol.write(socket.getOutputStream(), "ok");
119 }
120 }
121 }
122 catch (Exception e)
123 {
124
125 if (running.get())
126 {
127 throw new RuntimeException(e);
128 }
129 }
130 }
131
132 public void close()
133 {
134 try
135 {
136 running.set(false);
137 server.close();
138 }
139 catch (Exception e)
140 {
141
142 }
143 }
144 }
145
146 }