1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache.constructs.asynchronous;
18
19 import junit.framework.TestCase;
20 import net.sf.ehcache.CacheException;
21 import net.sf.ehcache.Ehcache;
22 import net.sf.ehcache.StopWatch;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29
30
31
32
33
34
35 public class AsynchronousCommandExecutorTest extends TestCase {
36
37 private static final Log LOG = LogFactory.getLog(AsynchronousCommandExecutorTest.class.getName());
38
39 private static List messages = new ArrayList();
40
41
42
43
44
45 private volatile int counter;
46
47
48 private Ehcache messageCache;
49
50
51
52
53
54
55 protected void setUp() throws Exception {
56 messages = new ArrayList();
57 AsynchronousCommandExecutor.getInstance().setUnsafeDispatcherThreadIntervalSeconds(2);
58 messageCache = AsynchronousCommandExecutor.getInstance().getMessageCache();
59 messageCache.removeAll();
60 messages.clear();
61 }
62
63
64
65
66 public static List getMessages() {
67 return messages;
68 }
69
70
71
72
73
74 public String appendTextMessage() throws Exception {
75 ListAppenderCommand command = new ListAppenderCommand("test", 0, null);
76 return AsynchronousCommandExecutor.getInstance().queueForExecution(command);
77 }
78
79
80
81
82
83
84
85 public String sendSerializableMessage() throws AsynchronousCommandException, CacheException, InterruptedException {
86 ListAppenderCommand command = new ListAppenderCommand(new IsSerializable(), 0, null);
87 return AsynchronousCommandExecutor.getInstance().queueForExecution(command);
88 }
89
90
91
92
93
94
95
96
97
98
99 public String sendNonSerializableMessage() throws Exception {
100 ListAppenderCommand command = new ListAppenderCommand(new NonSerializable(), 0, null);
101 return AsynchronousCommandExecutor.getInstance().queueForExecution(command);
102 }
103
104 private void waitForProcessing() throws InterruptedException {
105 Thread.sleep((long) (1700 + (200 * StopWatch.getSpeedAdjustmentFactor())));
106 }
107
108 private void assertCommandsInCache(int number) throws CacheException {
109 assertEquals(number - 1, messageCache.getSize());
110 }
111
112 private void assertNoCommandsInCache() throws CacheException {
113 assertEquals(1, messageCache.getSize());
114 }
115
116
117
118
119
120
121
122
123 public void testSendSerializableMessage() throws AsynchronousCommandException, CacheException, InterruptedException {
124 ListAppenderCommand command = new ListAppenderCommand(new IsSerializable(), 0, null);
125 AsynchronousCommandExecutor.getInstance().queueForExecution(command);
126 waitForProcessing();
127 assertEquals(1, messages.size());
128 assertNoCommandsInCache();
129 }
130
131
132
133
134 public void testAsynchronousSerializableCommandExecution() throws Exception {
135 ListAppenderCommand command = new ListAppenderCommand(new IsSerializable(), 10, null);
136
137 for (int i = 0; i < 12; i++) {
138 AsynchronousCommandExecutor.getInstance().queueForExecution(command);
139 }
140 waitForProcessing();
141 assertEquals(12, messages.size());
142 assertNoCommandsInCache();
143 }
144
145
146
147
148 public void testThreadingAsynchronousSerializableCommandExecution() throws Exception {
149 ListAppenderCommand command = new ListAppenderCommand(new IsSerializable(), 0, null);
150
151 for (int i = 0; i < 12; i++) {
152 AsynchronousCommandExecutor.getInstance().queueForExecution(command);
153 }
154 int count = 0;
155 while ((count = AsynchronousCommandExecutor.getInstance().countCachedPublishCommands()) != 0) {
156 LOG.info("waiting: count is: " + count);
157 }
158 waitForProcessing();
159 assertEquals(12, messages.size());
160 assertNoCommandsInCache();
161 }
162
163
164
165
166
167
168 public void testAsynchronousNonSerializableCommandExecution() throws Exception {
169 for (int i = 0; i < 12; i++) {
170 sendNonSerializableMessage();
171 }
172 waitForProcessing();
173 assertEquals(0, messages.size());
174 }
175
176
177
178
179
180
181
182
183
184 public void testMessageSentImmediately() throws Exception {
185
186 AsynchronousCommandExecutor.getInstance().setDispatcherThreadIntervalSeconds(1000000);
187 Thread.sleep(6000);
188 sendSerializableMessage();
189 Thread.sleep(2000);
190 sendSerializableMessage();
191
192 Thread.sleep(2000);
193
194 assertEquals(2, messages.size());
195 assertNoCommandsInCache();
196 }
197
198
199
200
201
202
203
204 public void testMixOfGoodAndBadMessages() throws Exception {
205
206 for (int i = 0; i < 2; i++) {
207 sendSerializableMessage();
208
209 }
210
211 Thread.sleep(9000);
212 assertEquals(2, messages.size());
213 assertNoCommandsInCache();
214
215 }
216
217
218
219
220 public void testMessagesNotRetriedBeforeAllowed() throws Exception {
221 ListAppenderCommand command = new ListAppenderCommand(new IsSerializable(), 0, Exception.class);
222 AsynchronousCommandExecutor commandExecutor = AsynchronousCommandExecutor.getInstance();
223 String uid = commandExecutor.queueForExecution(command);
224 Thread.sleep(4000);
225
226 int attempts = commandExecutor.getExecuteAttemptsForCommand(uid);
227
228 assertEquals(1, attempts);
229
230 assertEquals(2, messageCache.getSize());
231 }
232
233
234
235
236
237
238
239
240
241 public void testConcurrentExecutors() throws Exception {
242
243
244 final List executables = new ArrayList();
245 for (int i = 0; i < 50; i++) {
246 final Executable executable = new Executable() {
247 public void execute() throws Exception {
248 sendSerializableMessage();
249 counter++;
250 }
251 };
252 executables.add(executable);
253 }
254
255 runThreads(executables);
256 waitForProcessing();
257 int count = messages.size();
258 assertEquals(counter, count);
259
260 }
261
262
263
264
265
266
267 protected void runThreads(final List executables) throws Exception {
268
269 final long endTime = System.currentTimeMillis() + 10000;
270 final Throwable[] errors = new Throwable[1];
271
272
273 final Thread[] threads = new Thread[executables.size()];
274 for (int i = 0; i < threads.length; i++) {
275 final Executable executable = (Executable) executables.get(i);
276 threads[i] = new Thread() {
277 public void run() {
278 try {
279
280 while (System.currentTimeMillis() < endTime) {
281 executable.execute();
282 Thread.sleep(2000);
283 }
284 } catch (Throwable t) {
285
286 errors[0] = t;
287 }
288 }
289 };
290 threads[i].start();
291
292 }
293
294 long time = (long) System.currentTimeMillis();
295
296
297 int maximumWait = 30000;
298 for (int i = 0; i < threads.length; i++) {
299 threads[i].join(maximumWait);
300 if (System.currentTimeMillis() >= time + maximumWait) {
301 LOG.error("Killed Threads after timeout");
302 }
303 }
304
305
306 if (errors[0] != null) {
307 throw new Exception("Test thread failed.", errors[0]);
308 }
309 }
310
311
312
313
314 protected interface Executable {
315
316
317
318 void execute() throws Exception;
319 }
320
321
322 }