001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.asynchronous;
023:
024: import junit.framework.TestCase;
025:
026: import org.jboss.aspects.asynchronous.AsynchronousConstants;
027: import org.jboss.aspects.asynchronous.AsynchronousTask;
028: import org.jboss.aspects.asynchronous.aspects.AsynchronousFacade;
029: import org.jboss.aspects.asynchronous.concurrent.ThreadManagerFactory;
030:
031: /**
032:
033: *
034:
035: * @version <tt>$Revision: 57186 $</tt>
036:
037: * @author <a href="mailto:chussenet@yahoo.com">{Claude Hussenet Independent Consultant}</a>.
038:
039: */
040:
041: public class JUnitTestAsynchronousAspects
042:
043: extends TestCase
044:
045: implements AsynchronousConstants {
046:
047: public JUnitTestAsynchronousAspects(String arg0) {
048:
049: super (arg0);
050:
051: BusinessModel bm1 = new BusinessModel();
052:
053: bm1.processBusinessModel();
054:
055: AsynchronousTask aT = ((AsynchronousFacade) bm1)
056: .getAsynchronousTask();
057:
058: aT.getResponse();
059:
060: }
061:
062: public static void testAsynchronousCall() {
063:
064: BusinessModel bm1 = new BusinessModel(200);
065:
066: long t0 = System.currentTimeMillis();
067:
068: bm1.processBusinessModel();
069:
070: long t1 = System.currentTimeMillis();
071:
072: assertTrue(
073:
074: "Not an asynchronous call:" + (t1 - t0) + " ms.",
075:
076: (t1 - t0) < 100);
077:
078: }
079:
080: public static void testMixinAsynchronousFacadeInterface() {
081:
082: BusinessModel bm1 = new BusinessModel();
083:
084: assertTrue(
085:
086: "not an instance of AsynchronousFacade",
087:
088: bm1 instanceof AsynchronousFacade);
089:
090: }
091:
092: public static void testMixinAsynchronousFacadeInterface2() {
093:
094: BusinessModel bm1 = new BusinessModel();
095:
096: BusinessModel bm2 = new BusinessModel();
097:
098: assertTrue(
099:
100: "bm1 is not an instance of AsynchronousFacade",
101:
102: bm1 instanceof AsynchronousFacade);
103:
104: assertTrue(
105:
106: "bm2 is not an instance of AsynchronousFacade",
107:
108: bm2 instanceof AsynchronousFacade);
109:
110: AsynchronousFacade asynchronousFacade1 = (AsynchronousFacade) bm1;
111:
112: AsynchronousFacade asynchronousFacade2 = (AsynchronousFacade) bm2;
113:
114: asynchronousFacade1.setId("OK");
115:
116: asynchronousFacade2.setId("KO");
117:
118: assertNotSame(
119:
120: "same instances(ID) of AsynchronousFacade",
121:
122: asynchronousFacade1.getId(),
123:
124: asynchronousFacade2.getId());
125:
126: }
127:
128: public static void test2AsynchronousCallOnSameInstance() {
129:
130: BusinessModel bm1 = new BusinessModel();
131:
132: bm1.processBusinessModel2(15);
133:
134: AsynchronousFacade asynchronousFacade = (AsynchronousFacade) bm1;
135:
136: AsynchronousTask asynchronousTask1 =
137:
138: asynchronousFacade.getAsynchronousTask();
139:
140: bm1.processBusinessModel2(10);
141:
142: AsynchronousTask asynchronousTask2 =
143:
144: asynchronousFacade.getAsynchronousTask();
145:
146: assertEquals(
147:
148: "Method is not succesfull !",
149:
150: OK,
151:
152: asynchronousFacade.getResponseCode(asynchronousTask1));
153:
154: assertTrue(
155:
156: "value returned is not an instance of Long",
157:
158: asynchronousFacade.getReturnValue(asynchronousTask1)
159:
160: instanceof Long);
161:
162: assertEquals(
163:
164: "Method does not return the right value !"
165:
166: + ((Long) asynchronousFacade.getReturnValue(asynchronousTask1))
167:
168: .longValue(),
169:
170: ((Long) asynchronousFacade.getReturnValue(asynchronousTask1))
171:
172: .longValue(),
173:
174: 15);
175:
176: assertEquals(
177:
178: "Method is not succesfull !",
179:
180: asynchronousFacade.getResponseCode(asynchronousTask2),
181:
182: OK);
183:
184: assertTrue(
185:
186: "value returned is not an instance of Long",
187:
188: asynchronousFacade.getReturnValue(asynchronousTask2)
189:
190: instanceof Long);
191:
192: assertEquals(
193:
194: "Method does not return the right value !"
195:
196: + ((Long) asynchronousFacade.getReturnValue(asynchronousTask2))
197:
198: .longValue(),
199:
200: ((Long) asynchronousFacade.getReturnValue(asynchronousTask2))
201:
202: .longValue(),
203:
204: 10);
205:
206: }
207:
208: public static void test2AsynchronousCallOnSameInstance2() {
209:
210: BusinessModel bm1 = new BusinessModel();
211:
212: AsynchronousFacade asynchronousFacade = (AsynchronousFacade) bm1;
213:
214: asynchronousFacade.setTimeout(50);
215:
216: bm1.processBusinessModel2(1000);
217:
218: AsynchronousTask asynchronousTask1 =
219:
220: asynchronousFacade.getAsynchronousTask();
221:
222: asynchronousFacade.setTimeout(200);
223:
224: bm1.processBusinessModel2(10);
225:
226: AsynchronousTask asynchronousTask2 =
227:
228: asynchronousFacade.getAsynchronousTask();
229:
230: assertEquals(
231:
232: "Method did not timeout !",
233:
234: TIMEOUT,
235:
236: asynchronousFacade.getResponseCode(asynchronousTask1));
237:
238: assertEquals(
239:
240: "Method is not succesfull !",
241:
242: asynchronousFacade.getResponseCode(asynchronousTask2),
243:
244: OK);
245:
246: assertTrue(
247:
248: "value returned is not an instance of Long",
249:
250: asynchronousFacade.getReturnValue(asynchronousTask2)
251:
252: instanceof Long);
253:
254: assertEquals(
255:
256: "Method does not return the right value !"
257:
258: + ((Long) asynchronousFacade.getReturnValue(asynchronousTask2))
259:
260: .longValue(),
261:
262: ((Long) asynchronousFacade.getReturnValue(asynchronousTask2))
263:
264: .longValue(),
265:
266: 10);
267:
268: }
269:
270: public static void testTimeout() {
271:
272: BusinessModel bm1 = new BusinessModel(200);
273:
274: AsynchronousFacade asynchronousFacade = (AsynchronousFacade) bm1;
275:
276: asynchronousFacade.setTimeout(100);
277:
278: bm1.processBusinessModel();
279:
280: long t0 = System.currentTimeMillis();
281:
282: //System.out.println(
283:
284: // asynchronousFacade.getAsynchronousTask().getResponse());
285:
286: assertEquals(
287:
288: "Method did not timeout !",
289:
290: TIMEOUT,
291:
292: asynchronousFacade.getResponseCode());
293:
294: long t1 = System.currentTimeMillis();
295:
296: assertTrue("Method time out in " + (t1 - t0) + " ms.",
297: (t1 - t0) < 120);
298:
299: assertTrue("Method did not run " + (t1 - t0) + " ms.",
300: (t1 - t0) > 80);
301:
302: }
303:
304: public static void testReturnValue() {
305:
306: BusinessModel bm1 = new BusinessModel();
307:
308: bm1.processBusinessModel2(10);
309:
310: AsynchronousFacade asynchronousFacade = (AsynchronousFacade) bm1;
311:
312: assertEquals(
313:
314: "Method is not succesfull !",
315:
316: OK,
317:
318: asynchronousFacade.getResponseCode());
319:
320: assertTrue(
321:
322: "value returned is not an instance of Long",
323:
324: asynchronousFacade.getReturnValue() instanceof Long);
325:
326: assertEquals(
327:
328: "Method does not return the right value !"
329:
330: + ((Long) asynchronousFacade.getReturnValue()).longValue(),
331:
332: ((Long) asynchronousFacade.getReturnValue()).longValue(),
333:
334: 10);
335:
336: }
337:
338: public static void testCleanupCallWhenTimeout() {
339:
340: BusinessModelWithCleanup bm1 = new BusinessModelWithCleanup();
341:
342: BusinessModelWithCleanup bm2 = new BusinessModelWithCleanup();
343:
344: AsynchronousFacade asynchronousFacade1 = (AsynchronousFacade) bm1;
345:
346: AsynchronousFacade asynchronousFacade2 = (AsynchronousFacade) bm2;
347:
348: asynchronousFacade1.setTimeout(100);
349:
350: bm1.processBusinessModel2(200);
351:
352: bm2.processBusinessModel2(200);
353:
354: assertEquals(
355:
356: "Method did not timeout !",
357:
358: TIMEOUT,
359:
360: asynchronousFacade1.getResponseCode());
361:
362: assertEquals(
363:
364: "Method is not successfull !",
365:
366: OK,
367:
368: asynchronousFacade2.getResponseCode());
369:
370: assertEquals("Cleanup method not called", true,
371: bm1.bCleanupCalled);
372:
373: assertEquals("Cleanup method called", false, bm2.bCleanupCalled);
374:
375: }
376:
377: public static void testIsDone() {
378:
379: BusinessModel bm1 = new BusinessModel(200);
380:
381: bm1.processBusinessModel();
382:
383: AsynchronousFacade asynchronousFacade = (AsynchronousFacade) bm1;
384:
385: long t0 = System.currentTimeMillis();
386:
387: assertFalse("isDone returns TRUE !", asynchronousFacade
388: .isDone());
389:
390: long t1 = System.currentTimeMillis();
391:
392: assertTrue(
393:
394: "isDone is a blocking call " + (t1 - t0) + " ms.",
395:
396: (t1 - t0) < 20);
397:
398: assertEquals(
399:
400: "Method is not succesfull !",
401:
402: OK,
403:
404: asynchronousFacade.getResponseCode());
405:
406: assertTrue("isDone returns FALSE !", asynchronousFacade
407: .isDone());
408:
409: }
410:
411: public static void testExceptionRaisedInMethodCall() {
412:
413: BusinessModel bm1 = new BusinessModel();
414:
415: bm1.processBusinessModel2(-1);
416:
417: AsynchronousFacade asynchronousFacade = (AsynchronousFacade) bm1;
418:
419: assertEquals(
420:
421: "EXCEPTIONCAUGHT error not returned !",
422:
423: EXCEPTIONCAUGHT,
424:
425: asynchronousFacade.getResponseCode());
426:
427: }
428:
429: public static void testPoolSizeFull() {
430:
431: ThreadManagerFactory.getThreadManager().setMaximumPoolSize(10);
432:
433: for (int i = 0; i < 10; i++) {
434:
435: BusinessModel bm1 = new BusinessModel(200);
436:
437: bm1.processBusinessModel();
438:
439: }
440:
441: BusinessModel bm1 = new BusinessModel(200);
442:
443: bm1.processBusinessModel();
444:
445: AsynchronousFacade asynchronousFacade = (AsynchronousFacade) bm1;
446:
447: assertEquals(
448:
449: "Pool size not full !",
450:
451: CAN_NOT_PROCESS,
452:
453: asynchronousFacade.getResponseCode());
454:
455: }
456:
457: public static void testPerformance() {
458:
459: ThreadManagerFactory.getThreadManager()
460: .setMaximumPoolSize(5000);
461:
462: long tt0 = System.currentTimeMillis();
463:
464: float nbt = 100000;
465:
466: for (int i = 0; i < nbt; i++) {
467:
468: new BusinessModel(200);
469:
470: }
471:
472: long tt1 = System.currentTimeMillis();
473:
474: float time = (tt1 - tt0) / nbt;
475:
476: System.out.println(
477:
478: (int) nbt
479:
480: + " advised instances created in "
481:
482: + (tt1 - tt0)
483:
484: + " (ms).Average time "
485:
486: + time
487:
488: + " (ms).");
489:
490: BusinessModel bm = new BusinessModel(10);
491:
492: AsynchronousFacade Fbm = (AsynchronousFacade) bm;
493:
494: long total = 0;
495:
496: int iOk = 0;
497:
498: int nb1 = 200;
499:
500: int nb2 = 5;
501:
502: AsynchronousTask[] Tbm = new AsynchronousTask[nb2];
503:
504: for (int j = 0; j < nb1; j++) {
505:
506: long t0 = System.currentTimeMillis();
507:
508: for (int i = 0; i < nb2; i++) {
509:
510: bm.processBusinessModel();
511:
512: Tbm[i] = Fbm.getAsynchronousTask();
513:
514: }
515:
516: long t1 = System.currentTimeMillis();
517:
518: total += (t1 - t0);
519:
520: for (int i = 0; i < nb2; i++) {
521:
522: int ok = Fbm.getResponseCode(Tbm[i]);
523:
524: if (ok == OK)
525:
526: iOk++;
527:
528: }
529:
530: }
531:
532: System.out.println(
533:
534: nb1 * nb2
535:
536: + " asynchronous method invocations in "
537:
538: + total
539:
540: + " (ms).Average time "
541:
542: + (total / (float) (nb1 * nb2))
543:
544: + " (ms).");
545:
546: assertEquals("Some errors:", nb1 * nb2, iOk);
547:
548: }
549:
550: public static void testResponseTimeReturned() {
551:
552: BusinessModel bm1 = new BusinessModel(200);
553:
554: int ERROR = 20;
555:
556: long t0 = System.currentTimeMillis();
557:
558: bm1.processBusinessModel();
559:
560: AsynchronousFacade asynchronousFacade1 = (AsynchronousFacade) bm1;
561:
562: assertEquals(
563:
564: "Method is not succesfull !",
565:
566: OK,
567:
568: asynchronousFacade1.getResponseCode());
569:
570: long t1 = System.currentTimeMillis();
571:
572: long startingTime =
573:
574: asynchronousFacade1.getThreadManagerResponse()
575: .getStartingTime();
576:
577: long endingTime =
578:
579: asynchronousFacade1.getThreadManagerResponse().getEndingTime();
580:
581: assertTrue(
582:
583: "starting time issue ? " + (startingTime - t0),
584:
585: (startingTime - t0) < ERROR);
586:
587: assertTrue(
588:
589: "ending time issue ? " + (t1 - endingTime),
590:
591: (t1 - endingTime) < ERROR);
592:
593: }
594:
595: public static void testAsynchronousCallOnPrivateMethod() {
596:
597: BusinessModel bm1 = new BusinessModel(200);
598:
599: long t0 = System.currentTimeMillis();
600:
601: bm1.callPrivateMethod();
602:
603: long t1 = System.currentTimeMillis();
604:
605: assertTrue(
606:
607: "private method not an asynchronous call:" + (t1 - t0)
608: + " ms.",
609:
610: (t1 - t0) < 100);
611:
612: }
613:
614: public static void test2AsynchronousCallsOnSameInstanceFrom2DifferentThreads() {
615:
616: BusinessModel bm1 = new BusinessModel(100);
617:
618: BusinessThread businessThread1 =
619:
620: new BusinessThread(bm1, 100, 100, "First Call");
621:
622: BusinessThread businessThread2 =
623:
624: new BusinessThread(bm1, 200, 200, "Second Call");
625:
626: Thread th1 = new Thread(businessThread1);
627:
628: th1.start();
629:
630: Thread th2 = new Thread(businessThread2);
631:
632: th2.start();
633:
634: BusinessModel.sleep(500);
635:
636: assertNotSame(
637:
638: "The result from 2 differents threads are the same !",
639:
640: businessThread1.result,
641:
642: businessThread2.result);
643:
644: }
645:
646: public static void testAsynchronousCallOnStaticMethod() {
647:
648: Parameter object = new Parameter();
649:
650: long t0 = System.currentTimeMillis();
651:
652: BusinessModel.processBusinessModel4(200, object);
653:
654: long t1 = System.currentTimeMillis();
655:
656: assertTrue(
657:
658: "not an instance of AsynchronousFacade",
659:
660: object instanceof AsynchronousFacade);
661:
662: assertTrue(
663:
664: "static method not an asynchronous call:" + (t1 - t0) + " ms.",
665:
666: (t1 - t0) < 100);
667:
668: AsynchronousFacade asynchronousFacade = (AsynchronousFacade) object;
669:
670: AsynchronousTask asynchronousTask1 =
671:
672: asynchronousFacade.getAsynchronousTask();
673:
674: assertEquals(
675:
676: "Method does not return the right value !",
677:
678: 200,
679:
680: ((Long) asynchronousFacade.getReturnValue(asynchronousTask1))
681:
682: .longValue());
683:
684: }
685:
686: public static void testAsynchronousCallOnStaticMethodWithTimeout() {
687:
688: int nb = 1;
689:
690: for (int i = 0; i < nb; i++) {
691:
692: AsynchronousCallOnStaticMethodWithTimeout();
693:
694: }
695:
696: }
697:
698: public static void AsynchronousCallOnStaticMethodWithTimeout() {
699:
700: Parameter object1 = new Parameter();
701:
702: Parameter object2 = new Parameter();
703:
704: Parameter object3 = new Parameter();
705:
706: Parameter object4 = new Parameter();
707:
708: assertTrue(
709:
710: "not an instance of AsynchronousFacade",
711:
712: object1 instanceof AsynchronousFacade
713:
714: || object2 instanceof AsynchronousFacade);
715:
716: ((AsynchronousFacade) object1).setTimeout(100);
717:
718: ((AsynchronousFacade) object3).setTimeout(100);
719:
720: ((AsynchronousFacade) object4).setTimeout(100);
721:
722: long t0 = System.currentTimeMillis();
723:
724: BusinessModel.processBusinessModel4(200, object1);
725:
726: BusinessModel.processBusinessModel4(150, object2);
727:
728: BusinessModelWithStaticCleanup.processBusinessModel4(200,
729: object3);
730:
731: BusinessModelWithStaticCleanupWithParameters
732: .processBusinessModel4(
733:
734: 200,
735:
736: object4);
737:
738: System.out.println(System.currentTimeMillis() - t0);
739:
740: long t1 = System.currentTimeMillis();
741:
742: assertTrue(
743:
744: "static method not an asynchronous call:" + (t1 - t0) + " ms.",
745:
746: (t1 - t0) < 160);
747:
748: AsynchronousFacade asynchronousFacade1 = (AsynchronousFacade) object1;
749:
750: assertEquals(
751:
752: "Method did not timeout !",
753:
754: TIMEOUT,
755:
756: asynchronousFacade1.getResponseCode());
757:
758: AsynchronousFacade asynchronousFacade = (AsynchronousFacade) object2;
759:
760: AsynchronousTask asynchronousTask2 =
761:
762: asynchronousFacade.getAsynchronousTask();
763:
764: assertEquals(
765:
766: "Method does not return the right value !",
767:
768: 150,
769:
770: ((Long) asynchronousFacade.getReturnValue(asynchronousTask2))
771:
772: .longValue());
773:
774: }
775:
776: }
|