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.DynamicPortTestCase;
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 DynamicPortTestCase
30 {
31 protected static String TEST_TCP_MESSAGE = "Test TCP Request";
32
33 @Override
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(muleContext);
42
43 Map<String, Object> props = new HashMap<String, Object>();
44 MuleMessage result = client.send("clientEndpoint", TEST_TCP_MESSAGE, props);
45 assertEquals(TEST_TCP_MESSAGE + " Received", result.getPayloadAsString());
46
47
48 result = client.send("clientEndpoint", TEST_TCP_MESSAGE, props);
49 assertEquals(TEST_TCP_MESSAGE + " Received", result.getPayloadAsString());
50 }
51
52 private void useServer(String endpoint, int port, int count) throws Exception
53 {
54 SimpleServerSocket server = new SimpleServerSocket(port);
55 try
56 {
57 new Thread(server).start();
58 MuleClient client = new MuleClient(muleContext);
59 client.send(endpoint, "Hello", null);
60 client.send(endpoint, "world", null);
61 assertEquals(count, server.getCount());
62 }
63 finally
64 {
65 server.close();
66 }
67 }
68
69 public void testOpen() throws Exception
70 {
71 useServer("tcp://localhost:" + getPorts().get(1) + "?connector=openConnectorLength", getPorts().get(1), 1);
72 }
73
74 public void testClose() throws Exception
75 {
76 useServer("tcp://localhost:" + getPorts().get(2) + "?connector=closeConnectorLength", getPorts().get(2), 2);
77 }
78
79 @SuppressWarnings("synthetic-access")
80 private class SimpleServerSocket implements Runnable
81 {
82 private ServerSocket server;
83 AtomicBoolean running = new AtomicBoolean(true);
84 AtomicInteger count = new AtomicInteger(0);
85
86 public SimpleServerSocket(int port) throws Exception
87 {
88 server = new ServerSocket();
89 logger.debug("starting server");
90 server.bind(new InetSocketAddress("localhost", port), 3);
91 }
92
93 public int getCount()
94 {
95 return count.get();
96 }
97
98 public void run()
99 {
100 try
101 {
102 LengthProtocol protocol = new LengthProtocol();
103 while (true)
104 {
105 Socket socket = server.accept();
106 logger.debug("have connection " + count);
107 count.incrementAndGet();
108 InputStream stream = new BufferedInputStream(socket.getInputStream());
109
110 while (true)
111 {
112 Object read = protocol.read(stream);
113 if (null == read)
114 {
115 break;
116 }
117 String msg = new String((byte[]) read);
118 logger.debug("read: " + msg);
119 logger.debug("writing reply");
120 protocol.write(socket.getOutputStream(), "ok");
121 }
122 }
123 }
124 catch (Exception e)
125 {
126
127 if (running.get())
128 {
129 throw new RuntimeException(e);
130 }
131 }
132 }
133
134 public void close()
135 {
136 try
137 {
138 running.set(false);
139 server.close();
140 }
141 catch (Exception e)
142 {
143
144 }
145 }
146 }
147
148 @Override
149 protected int getNumPortsToFind()
150 {
151 return 3;
152 }
153 }