1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.tcp.other;
12
13 import org.mule.tck.junit4.AbstractMuleTestCase;
14
15 import java.io.IOException;
16 import java.net.InetSocketAddress;
17 import java.net.ServerSocket;
18 import java.net.Socket;
19
20 import org.junit.Test;
21
22 import static org.junit.Assert.assertEquals;
23
24
25
26
27
28 public class SocketTimingExperimentTestCase extends AbstractMuleTestCase
29 {
30
31 private static int MAX_COUNT = 3;
32 private static int SERVER_PORT = 60323;
33 private static String LOCALHOST = "localhost";
34
35 @Test
36 public void testSocketTiming() throws IOException, InterruptedException
37 {
38 try
39 {
40 boolean expectBadClient = expectBadClient();
41 logger.info("Expected bad client: " + expectBadClient);
42 }
43 catch (Exception e)
44 {
45 logger.info(e);
46 }
47 try
48 {
49 boolean expectBadServer = expectBadServer();
50 logger.info("Expected bad server: " + expectBadServer);
51 }
52 catch (Exception e)
53 {
54 logger.info(e);
55 }
56 }
57
58 protected boolean expectBadClient() throws IOException, InterruptedException
59 {
60 for (int i = 0; i < MAX_COUNT; ++i)
61 {
62 if (! expectBadClientSingle())
63 {
64 return false;
65 }
66 }
67 return true;
68 }
69
70 protected boolean expectBadClientSingle() throws IOException, InterruptedException
71 {
72 ServerSocket server = new ServerSocket();
73 try {
74 server.bind(new InetSocketAddress(LOCALHOST, SERVER_PORT));
75 return badSend(new Socket(LOCALHOST, SERVER_PORT), server.accept(), null);
76 }
77 finally
78 {
79 server.close();
80 }
81 }
82
83 protected boolean badSend(Socket from, Socket to, ServerSocket server) throws IOException, InterruptedException
84 {
85 try
86 {
87
88 to.setReceiveBufferSize(1);
89 from.setSendBufferSize(1);
90
91
92
93 from.setSoLinger(false, 0);
94 to.setSoLinger(false, 0);
95
96 to.setTcpNoDelay(false);
97 from.setTcpNoDelay(false);
98
99
100 from.getOutputStream().write(1);
101 from.getOutputStream().write(2);
102
103
104 from.getOutputStream().write(3);
105
106
107
108
109
110
111 from.close();
112
113 if (null != server)
114 {
115 server.close();
116 }
117
118 Thread.sleep(100);
119
120 if (null != server)
121 {
122 ServerSocket another = new ServerSocket();
123 another.bind(new InetSocketAddress(LOCALHOST, SERVER_PORT));
124 another.setReuseAddress(true);
125 Socket another2 = new Socket(LOCALHOST, SERVER_PORT);
126 Socket another3 = another.accept();
127 another2.getOutputStream().write(9);
128 assertEquals(9, another3.getInputStream().read());
129 another3.close();
130 another2.close();
131 another.close();
132 }
133
134 return 1 == to.getInputStream().read()
135 && 2 == to.getInputStream().read()
136 && 3 == to.getInputStream().read();
137 }
138 finally
139 {
140 to.close();
141 if (!from.isClosed())
142 {
143 from.close();
144 }
145 }
146 }
147
148 protected boolean expectBadServer() throws IOException, InterruptedException
149 {
150 for (int i = 0; i < MAX_COUNT; ++i)
151 {
152 if (! expectBadServerSingle())
153 {
154 return false;
155 }
156 }
157 return true;
158 }
159
160 protected boolean expectBadServerSingle() throws IOException, InterruptedException
161 {
162 ServerSocket server = new ServerSocket();
163 try {
164 server.bind(new InetSocketAddress(LOCALHOST, SERVER_PORT));
165 Socket client = new Socket(LOCALHOST, SERVER_PORT);
166 return badSend(server.accept(), client, server);
167 }
168 finally
169 {
170 server.close();
171 }
172 }
173
174 }