1 package org.xmlBlaster.test.classtest;
2
3 import java.util.logging.Logger;
4 import org.xmlBlaster.util.StopWatch;
5 import org.xmlBlaster.engine.ServerScope;
6 import org.xmlBlaster.util.XmlBlasterException;
7 import org.xmlBlaster.util.def.ErrorCode;
8 import org.xmlBlaster.util.def.MethodName;
9 import org.xmlBlaster.util.recorder.ram.RamRecorder;
10 import org.xmlBlaster.util.recorder.file.FileRecorder;
11 import org.xmlBlaster.util.recorder.I_InvocationRecorder;
12 import org.xmlBlaster.client.protocol.I_XmlBlaster;
13 import org.xmlBlaster.client.qos.PublishReturnQos;
14 import org.xmlBlaster.client.qos.SubscribeReturnQos;
15 import org.xmlBlaster.client.qos.UnSubscribeReturnQos;
16 import org.xmlBlaster.client.qos.EraseReturnQos;
17 import org.xmlBlaster.util.def.Constants;
18 import org.xmlBlaster.util.MsgUnit;
19
20 import junit.framework.*;
21
22 /**
23 * Test RamRecorder.
24 * <p />
25 * Invoke: java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.classtest.InvocationRecorderTest
26 * @see org.xmlBlaster.util.recorder.ram.RamRecorder
27 * @see org.xmlBlaster.util.recorder.file.FileRecorder
28 */
29 public class InvocationRecorderTest extends TestCase {
30 private String ME = "InvocationRecorderTest";
31 protected ServerScope glob;
32 private static Logger log = Logger.getLogger(InvocationRecorderTest.class.getName());
33 private int numSubscribe, numUnSubscribe, numGet, numPublish, numPublishOneway, numPublishArr, numErase, numUpdate, numUpdateOneway;
34 private StopWatch stopWatch = new StopWatch();
35 private boolean testDiscardOldest = false;
36
37 public InvocationRecorderTest(String name) {
38 super(name);
39 }
40
41 protected void setUp() {
42 glob = new ServerScope();
43
44 }
45
46 /*
47 public void testMsgUnitWrapper() {
48 try {
49 org.xmlBlaster.engine.Global global = new org.xmlBlaster.engine.Global();
50 MsgUnit msgUnit = new MsgUnit("<key oid='aaaa'/>", "Hi".getBytes(), "<qos/>");
51 MsgUnitWrapper wr = new MsgUnitWrapper(global, global.getRequestBroker(),
52 new XmlKey(glob, msgUnit.getKey(), true),
53 msgUnit,
54 new PublishQos(glob, msgUnit.getQos()));
55 }
56 catch (XmlBlasterException e) {
57 fail("Exception thrown: " + e.toString());
58 }
59 }
60 */
61
62 public void testPlayback() {
63 playback(new FileRecorder());
64 playback(new RamRecorder());
65 }
66
67 private void playback(I_InvocationRecorder recorder) {
68 ME = "InvocationRecorderTest.testPlayback()";
69 System.out.println("***InvocationRecorderTest: testPlayback ...");
70 try {
71 Tester tester = new Tester();
72
73 long maxEntries = 1000L;
74 recorder.initialize(glob, "test.txt", maxEntries, tester); //, tester);
75
76 {
77 MethodName methodName = MethodName.SUBSCRIBE;
78 log.info("Testing '" + methodName + "' ...");
79 stopWatch = new StopWatch();
80 for (int ii=0; ii<maxEntries; ii++) {
81 recorder.subscribe("<key oid='"+methodName+"'/>", "<qos/>");
82 }
83 recorder.pullback(0, 0, 0);
84 log.info(methodName + " round trip performance: " + stopWatch.nice(maxEntries));
85 assertEquals("Wrong number of "+methodName, maxEntries, numSubscribe);
86 numSubscribe = 0;
87 }
88
89 {
90 MethodName methodName = MethodName.GET;
91 log.info("Testing '" + methodName + "' ...");
92 stopWatch = new StopWatch();
93 for (int ii=0; ii<maxEntries; ii++) {
94 recorder.get("<key oid='"+methodName+"'/>", "<qos/>");
95 }
96 recorder.pullback(0, 0, 0);
97 log.info(methodName + " round trip performance: " + stopWatch.nice(maxEntries));
98 assertEquals("Wrong number of "+methodName, maxEntries, numGet);
99 numGet = 0;
100 }
101
102 {
103 MethodName methodName = MethodName.UNSUBSCRIBE;
104 log.info("Testing '" + methodName + "' ...");
105 stopWatch = new StopWatch();
106 for (int ii=0; ii<maxEntries; ii++) {
107 recorder.unSubscribe("<key oid='"+methodName+"'/>", "<qos/>");
108 }
109 recorder.pullback(0, 0, 0);
110 log.info(methodName + " round trip performance: " + stopWatch.nice(maxEntries));
111 assertEquals("Wrong number of "+methodName, maxEntries, numUnSubscribe);
112 numUnSubscribe = 0;
113 }
114
115 {
116 MethodName methodName = MethodName.PUBLISH;
117 log.info("Testing '" + methodName + "' ...");
118 stopWatch = new StopWatch();
119 for (int ii=0; ii<maxEntries; ii++) {
120 MsgUnit msgUnit = new MsgUnit("<key oid='"+methodName+"'/>", "Ho-"+ii, "<qos/>");
121 recorder.publish(msgUnit);
122 }
123 recorder.pullback(0, 0, 0);
124 log.info(methodName + " round trip performance: " + stopWatch.nice(maxEntries));
125 assertEquals("Wrong number of "+methodName, maxEntries, numPublish);
126 numPublish = 0;
127 }
128
129 {
130 MethodName methodName = MethodName.PUBLISH_ONEWAY;
131 log.info("Testing '" + methodName + "' ...");
132 stopWatch = new StopWatch();
133 for (int ii=0; ii<maxEntries; ii++) {
134 MsgUnit[] msgs = new MsgUnit[2];
135 msgs[0] = new MsgUnit("<key oid='"+methodName+"'/>", "Ha-"+ii, "<qos/>");
136 msgs[1] = new MsgUnit("<key oid='"+methodName+"'/>", "Hu-"+ii, "<qos/>");
137 recorder.publishOneway(msgs);
138 }
139 recorder.pullback(0, 0, 0);
140 log.info(methodName + " round trip performance: " + stopWatch.nice(maxEntries*2));
141 assertEquals("Wrong number of "+methodName, maxEntries, numPublishOneway);
142 numPublishOneway = 0;
143 }
144
145 {
146 MethodName methodName = MethodName.PUBLISH; // PUBLISH_ARR
147 log.info("Testing '" + methodName + "' ...");
148 stopWatch = new StopWatch();
149 for (int ii=0; ii<maxEntries; ii++) {
150 MsgUnit[] msgs = new MsgUnit[2];
151 msgs[0] = new MsgUnit("<key oid='"+methodName+"'/>", "Ha-"+ii, "<qos/>");
152 msgs[1] = new MsgUnit("<key oid='"+methodName+"'/>", "Hu-"+ii, "<qos/>");
153 recorder.publishArr(msgs);
154 }
155 recorder.pullback(0, 0, 0);
156 log.info(methodName + " round trip performance: " + stopWatch.nice(maxEntries*2));
157 assertEquals("Wrong number of "+methodName, maxEntries, numPublishArr);
158 numPublishArr = 0;
159 }
160
161 {
162 MethodName methodName = MethodName.ERASE;
163 log.info("Testing '" + methodName + "' ...");
164 stopWatch = new StopWatch();
165 for (int ii=0; ii<maxEntries; ii++) {
166 recorder.erase("<key oid='"+methodName+"'/>", "<qos/>");
167 }
168 recorder.pullback(0, 0, 0);
169 log.info(methodName + " round trip performance: " + stopWatch.nice(maxEntries));
170 assertEquals("Wrong number of "+methodName, maxEntries, numErase);
171 numErase = 0;
172 }
173
174 /*
175 {
176 MethodName methodName = MethodName.UPDATE;
177 log.info("Testing '" + methodName + "' ...");
178 stopWatch = new StopWatch();
179 for (int ii=0; ii<maxEntries; ii++) {
180 MsgUnit[] msgs = new MsgUnit[2];
181 msgs[0] = new MsgUnit("<key oid='"+methodName+"'/>", "Ha-"+ii, "<qos/>");
182 msgs[1] = new MsgUnit("<key oid='"+methodName+"'/>", "Hu-"+ii, "<qos/>");
183 recorder.update("dummy", msgs);
184 }
185 recorder.pullback(0, 0, 0);
186 log.info(methodName + " round trip performance: " + stopWatch.nice(maxEntries*2));
187 assertEquals("Wrong number of "+methodName, maxEntries, numUpdate);
188 numUpdate = 0;
189 }
190
191 {
192 MethodName methodName = MethodName.UPDATE_ONEWAY;
193 log.info("Testing '" + methodName + "' ...");
194 stopWatch = new StopWatch();
195 for (int ii=0; ii<maxEntries; ii++) {
196 MsgUnit[] msgs = new MsgUnit[2];
197 msgs[0] = new MsgUnit("<key oid='"+methodName+"'/>", "Ha-"+ii, "<qos/>");
198 msgs[1] = new MsgUnit("<key oid='"+methodName+"'/>", "Hu-"+ii, "<qos/>");
199 recorder.updateOneway("dummy", msgs);
200 }
201 recorder.pullback(0, 0, 0);
202 log.info(methodName + " round trip performance: " + stopWatch.nice(maxEntries*2));
203 assertEquals("Wrong number of "+methodName, maxEntries, numUpdateOneway);
204 numUpdateOneway = 0;
205 }
206 */
207
208 //assertEquals("XPath is different", xmlKey.getQueryString(), xpath);
209 System.out.println("***InvocationRecorderTest: testPlayback [SUCCESS]");
210 }
211 catch(XmlBlasterException e) {
212 fail("Exception thrown: " + e.toString());
213 }
214 finally {
215 recorder.destroy();
216 }
217 }
218
219 public void testOnOverflowException() {
220 onOverflowException(new FileRecorder());
221 onOverflowException(new RamRecorder());
222 }
223
224 private void onOverflowException(I_InvocationRecorder recorder) {
225 ME = "InvocationRecorderTest.testOnOverflowException()";
226 System.out.println("***InvocationRecorderTest: testOnOverflowException ...");
227 try {
228 Tester tester = new Tester();
229
230 int maxInvoke = 4;
231 int maxQueueSize = maxInvoke/2;
232 recorder.initialize(glob, "testOverflow.txt", maxQueueSize, tester); //, tester);
233
234 {
235 MethodName methodName = MethodName.PUBLISH;
236 log.info("Testing '" + methodName + "' ...");
237 for (int ii=0; ii<maxInvoke; ii++) {
238 MsgUnit msgUnit = new MsgUnit("<key oid='"+methodName+"'/>", "Ho-"+ii, "<qos/>");
239 try {
240 log.info("Publish ii=" + ii);
241 recorder.publish(msgUnit);
242 if (ii >= maxQueueSize)
243 fail(ME + " Expected exception because of full queue ii=" + ii);
244 }
245 catch (XmlBlasterException e) {
246 if (ii >= maxQueueSize && e.getErrorCode() == ErrorCode.RESOURCE_OVERFLOW_QUEUE_ENTRIES) {
247 log.info("OK, expected exception ii=" + ii);
248 }
249 else {
250 fail(ME + " ii=" + ii + " : " + e.toString());
251 }
252 }
253 }
254 recorder.pullback(0, 0, 0);
255 assertEquals("Wrong number of "+methodName, maxQueueSize, numPublish);
256 numPublish = 0;
257 }
258
259 {
260 MethodName methodName = MethodName.PUBLISH; // PUBLISH_ARR;
261 log.info("Testing '" + methodName + "' ...");
262 for (int ii=0; ii<maxInvoke; ii++) {
263 MsgUnit[] msgs = new MsgUnit[2];
264 msgs[0] = new MsgUnit("<key oid='"+methodName+"'/>", "Ha-"+ii, "<qos/>");
265 msgs[1] = new MsgUnit("<key oid='"+methodName+"'/>", "Hu-"+ii, "<qos/>");
266 try {
267 recorder.publishArr(msgs);
268 if (ii >= maxQueueSize)
269 fail(ME + " Expected exception because of full queue ii=" + ii);
270 }
271 catch (XmlBlasterException e) {
272 if (ii >= maxQueueSize && e.getErrorCode() == ErrorCode.RESOURCE_OVERFLOW_QUEUE_ENTRIES) {
273 log.info("OK, expected exception ii=" + ii);
274 }
275 else {
276 fail(ME + " ii=" + ii + " : " + e.toString());
277 }
278 }
279 }
280 recorder.pullback(0, 0, 0);
281 assertEquals("Wrong number of "+methodName, maxQueueSize, numPublishArr);
282 numPublishArr = 0;
283 }
284
285 System.out.println("***InvocationRecorderTest: testOnOverflowException [SUCCESS]");
286 }
287 catch(XmlBlasterException e) {
288 fail("Exception thrown: " + e.toString());
289 }
290 finally {
291 recorder.destroy();
292 }
293 }
294
295 public void testOnOverflowDiscardOldest() {
296 onOverflowDiscardOldest(new FileRecorder());
297 onOverflowDiscardOldest(new RamRecorder());
298 }
299
300 private void onOverflowDiscardOldest(I_InvocationRecorder recorder) {
301 ME = "InvocationRecorderTest.testOnOverflowDiscardOldest()";
302 System.out.println("***InvocationRecorderTest: testOnOverflowDiscardOldest ...");
303
304 testDiscardOldest = true;
305
306 try {
307 Tester tester = new Tester();
308
309 int maxInvoke = 4;
310 int maxQueueSize = maxInvoke/2;
311 recorder.initialize(glob, (String)null, maxQueueSize, tester); //, tester);
312 recorder.setMode(Constants.ONOVERFLOW_DISCARDOLDEST);
313
314 {
315 MethodName methodName = MethodName.PUBLISH;
316 log.info("Testing '" + methodName + "' ...");
317 for (int ii=0; ii<maxInvoke; ii++) {
318 MsgUnit msgUnit = new MsgUnit("<key oid='"+methodName+"'/>", "Ho-"+ii, "<qos/>");
319 try {
320 recorder.publish(msgUnit);
321 }
322 catch (XmlBlasterException e) {
323 fail(ME + " ii=" + ii + " : " + e.toString());
324 }
325 }
326 assertEquals("Wrong number of lost messages in "+methodName, maxInvoke-maxQueueSize, recorder.getNumLost());
327 recorder.pullback(0, 0, 0);
328 assertEquals("Wrong number of "+methodName, maxQueueSize, numPublish);
329 numPublish = 0;
330 }
331
332 System.out.println("***InvocationRecorderTest: testOnOverflowDiscardOldest [SUCCESS]");
333 }
334 catch(XmlBlasterException e) {
335 fail("Exception thrown: " + e.toString());
336 }
337 finally {
338 recorder.destroy();
339 }
340 }
341
342 public void testOnOverflowDiscard() {
343 onOverflowDiscard(new FileRecorder());
344 onOverflowDiscard(new RamRecorder());
345 }
346
347 private void onOverflowDiscard(I_InvocationRecorder recorder) {
348 ME = "InvocationRecorderTest.testOnOverflowDiscard()";
349 System.out.println("***InvocationRecorderTest: testOnOverflowDiscard ...");
350
351 try {
352 Tester tester = new Tester();
353
354 int maxInvoke = 4;
355 int maxQueueSize = maxInvoke/2;
356 recorder.initialize(glob, (String)null, maxQueueSize, tester); //, tester);
357 recorder.setMode(Constants.ONOVERFLOW_DISCARD);
358
359 {
360 MethodName methodName = MethodName.PUBLISH;
361 log.info("Testing '" + methodName + "' ...");
362 for (int ii=0; ii<maxInvoke; ii++) {
363 MsgUnit msgUnit = new MsgUnit("<key oid='"+methodName+"'/>", "Ho-"+ii, "<qos/>");
364 try {
365 recorder.publish(msgUnit);
366 }
367 catch (XmlBlasterException e) {
368 fail(ME + " ii=" + ii + " : " + e.toString());
369 }
370 }
371 assertEquals("Wrong number of lost messages in "+methodName, maxInvoke-maxQueueSize, recorder.getNumLost());
372 recorder.pullback(0, 0, 0);
373 assertEquals("Wrong number of "+methodName, maxQueueSize, numPublish);
374 numPublish = 0;
375 }
376
377 System.out.println("***InvocationRecorderTest: testOnOverflowDiscard [SUCCESS]");
378 }
379 catch(XmlBlasterException e) {
380 fail("Exception thrown: " + e.toString());
381 }
382 finally {
383 recorder.destroy();
384 }
385 }
386
387 /**
388 * This plays the role of a user of the recoder
389 */
390 class Tester implements I_XmlBlaster//, I_CallbackRaw
391 {
392 public SubscribeReturnQos subscribe(java.lang.String xmlKey, java.lang.String qos) throws XmlBlasterException {
393 assertEquals("Wrong message key", "<key oid='subscribe'/>", xmlKey);
394 assertEquals("Wrong message qos", "<qos/>", qos);
395 numSubscribe++;
396 return null;
397 }
398
399 public org.xmlBlaster.util.MsgUnit[] get(java.lang.String xmlKey, java.lang.String qos) throws XmlBlasterException {
400 assertEquals("Wrong message key", "<key oid='get'/>", xmlKey);
401 assertEquals("Wrong message qos", "<qos/>", qos);
402 numGet++;
403 return new org.xmlBlaster.util.MsgUnit[0];
404 }
405
406 public UnSubscribeReturnQos[] unSubscribe(java.lang.String xmlKey, java.lang.String qos) throws XmlBlasterException {
407 assertEquals("Wrong message key", "<key oid='unSubscribe'/>", xmlKey);
408 assertEquals("Wrong message qos", "<qos/>", qos);
409 numUnSubscribe++;
410 return null;
411 }
412
413 public PublishReturnQos publish(org.xmlBlaster.util.MsgUnit msgUnit) throws XmlBlasterException {
414 //log.severe("Received '" + msgUnit.getKey().trim() + "' from\n" + msgUnit.toXml() );
415 assertEquals("Wrong message key", "<key oid='publish'/>", msgUnit.getKey().trim());
416 if (testDiscardOldest)
417 assertEquals("Wrong message content", "Ho-"+(numPublish+2), msgUnit.getContentStr());
418 else
419 assertEquals("Wrong message content", "Ho-"+numPublish, msgUnit.getContentStr());
420 assertEquals("Wrong message qos", "<qos/>", msgUnit.getQos());
421 numPublish++;
422 return null;
423 }
424
425 public void publishOneway(org.xmlBlaster.util.MsgUnit[] msgUnitArr) {
426 assertEquals("Wrong message array length", 2, msgUnitArr.length);
427
428 assertEquals("Wrong message key", "<key oid='publishOneway'/>", msgUnitArr[0].getKey().trim());
429 assertEquals("Wrong message content", "Ha-"+numPublishOneway, msgUnitArr[0].getContentStr());
430 assertEquals("Wrong message qos", "<qos/>", msgUnitArr[0].getQos());
431
432 assertEquals("Wrong message key", "<key oid='publishOneway'/>", msgUnitArr[1].getKey().trim());
433 assertEquals("Wrong message content", "Hu-"+numPublishOneway, msgUnitArr[1].getContentStr());
434 assertEquals("Wrong message qos", "<qos/>", msgUnitArr[1].getQos());
435
436 numPublishOneway++;
437 }
438
439 public PublishReturnQos[] publishArr(org.xmlBlaster.util.MsgUnit[] msgUnitArr) throws XmlBlasterException {
440 assertEquals("Wrong message array length", 2, msgUnitArr.length);
441
442 //log.severe("Received '" + msgUnitArr[0].getKey().trim() + "' from\n" + msgUnitArr[0].toXml() );
443 assertEquals("Wrong message key", "<key oid='"+ MethodName.PUBLISH + "'/>", msgUnitArr[0].getKey().trim()); // PUBLISH_ARR
444 assertEquals("Wrong message content", "Ha-"+numPublishArr, msgUnitArr[0].getContentStr());
445 assertEquals("Wrong message qos", "<qos/>", msgUnitArr[0].getQos());
446
447 assertEquals("Wrong message key", "<key oid='"+ MethodName.PUBLISH + "'/>", msgUnitArr[1].getKey().trim()); // PUBLISH_ARR
448 assertEquals("Wrong message content", "Hu-"+numPublishArr, msgUnitArr[1].getContentStr());
449 assertEquals("Wrong message qos", "<qos/>", msgUnitArr[1].getQos());
450
451 numPublishArr++;
452 return new PublishReturnQos[0];
453 }
454
455 public EraseReturnQos[] erase(java.lang.String xmlKey, java.lang.String qos) throws XmlBlasterException {
456 assertEquals("Wrong message key", "<key oid='erase'/>", xmlKey);
457 assertEquals("Wrong message qos", "<qos/>", qos);
458
459 numErase++;
460 return new EraseReturnQos[0];
461 }
462
463 /*
464 public String[] update(String cbSessionId, org.xmlBlaster.util.MsgUnit[] msgUnitArr) throws XmlBlasterException {
465 assertEquals("Wrong message array length", 2, msgUnitArr.length);
466 assertEquals("Wrong cbSessionId", "dummy", cbSessionId);
467
468 assertEquals("Wrong message key", "<key oid='update'/>", msgUnitArr[0].getKey().trim());
469 assertEquals("Wrong message content", "Ha-"+numUpdate, msgUnitArr[0].getContentStr());
470 assertEquals("Wrong message qos", "<qos/>", msgUnitArr[0].getQos());
471
472 assertEquals("Wrong message key", "<key oid='update'/>", msgUnitArr[1].getKey().trim());
473 assertEquals("Wrong message content", "Hu-"+numUpdate, msgUnitArr[1].getContentStr());
474 assertEquals("Wrong message qos", "<qos/>", msgUnitArr[1].getQos());
475
476 numUpdate++;
477 return new String[0];
478 }
479
480 public void updateOneway(String cbSessionId, org.xmlBlaster.util.MsgUnit[] msgUnitArr) {
481 assertEquals("Wrong message array length", 2, msgUnitArr.length);
482 assertEquals("Wrong cbSessionId", "dummy", cbSessionId);
483
484 assertEquals("Wrong message key", "<key oid='updateOneway'/>", msgUnitArr[0].getKey().trim());
485 assertEquals("Wrong message content", "Ha-"+numUpdateOneway, msgUnitArr[0].getContentStr());
486 assertEquals("Wrong message qos", "<qos/>", msgUnitArr[0].getQos());
487
488 assertEquals("Wrong message key", "<key oid='updateOneway'/>", msgUnitArr[1].getKey().trim());
489 assertEquals("Wrong message content", "Hu-"+numUpdateOneway, msgUnitArr[1].getContentStr());
490 assertEquals("Wrong message qos", "<qos/>", msgUnitArr[1].getQos());
491
492 numUpdateOneway++;
493 }
494 */
495 }
496
497 protected void tearDown() {
498 }
499
500 /**
501 * For debugging, invoke:
502 * <pre>
503 * java org.xmlBlaster.test.classtest.InvocationRecorderTest -logging/org.xmlBlaster.engine.dispatch FINE -logging/org.xmlBlaster.util.dispatch FINE -logging/org.xmlBlaster.engine FINEST
504 * java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.classtest.InvocationRecorderTest
505 * <pre>
506 */
507 public static void main(String args[]) {
508 ServerScope glob = new ServerScope();
509 if (glob.init(args) != 0) {
510 System.exit(0);
511 }
512 InvocationRecorderTest testSub = new InvocationRecorderTest("InvocationRecorderTest");
513 testSub.setUp();
514 testSub.onOverflowDiscard(new FileRecorder());
515 testSub.tearDown();
516 }
517 }
syntax highlighted by Code2HTML, v. 0.9.1