1 22 23 package test.asynchronous; 24 25 import junit.framework.TestCase; 26 27 import org.jboss.aspects.asynchronous.AsynchronousConstants; 28 import org.jboss.aspects.asynchronous.AsynchronousTask; 29 import org.jboss.aspects.asynchronous.aspects.AsynchronousFacade; 30 import org.jboss.aspects.asynchronous.concurrent.ThreadManagerFactory; 31 32 41 42 public class JUnitTestAsynchronousAspects 43 44 extends TestCase 45 46 implements AsynchronousConstants { 47 48 public JUnitTestAsynchronousAspects(String arg0) { 49 50 super(arg0); 51 52 BusinessModel bm1 = new BusinessModel(); 53 54 55 56 bm1.processBusinessModel(); 57 58 AsynchronousTask aT = ((AsynchronousFacade)bm1).getAsynchronousTask(); 59 60 aT.getResponse(); 61 62 63 64 } 65 66 67 68 public static void testAsynchronousCall() { 69 70 BusinessModel bm1 = new BusinessModel(200); 71 72 long t0 = System.currentTimeMillis(); 73 74 bm1.processBusinessModel(); 75 76 long t1 = System.currentTimeMillis(); 77 78 assertTrue( 79 80 "Not an asynchronous call:" + (t1 - t0) + " ms.", 81 82 (t1 - t0) < 100); 83 84 } 85 86 87 88 public static void testMixinAsynchronousFacadeInterface() { 89 90 BusinessModel bm1 = new BusinessModel(); 91 92 assertTrue( 93 94 "not an instance of AsynchronousFacade", 95 96 bm1 instanceof AsynchronousFacade); 97 98 } 99 100 public static void testMixinAsynchronousFacadeInterface2() { 101 102 BusinessModel bm1 = new BusinessModel(); 103 104 BusinessModel bm2 = new BusinessModel(); 105 106 assertTrue( 107 108 "bm1 is not an instance of AsynchronousFacade", 109 110 bm1 instanceof AsynchronousFacade); 111 112 assertTrue( 113 114 "bm2 is not an instance of AsynchronousFacade", 115 116 bm2 instanceof AsynchronousFacade); 117 118 AsynchronousFacade asynchronousFacade1 = (AsynchronousFacade)bm1; 119 120 AsynchronousFacade asynchronousFacade2 = (AsynchronousFacade)bm2; 121 122 asynchronousFacade1.setId("OK"); 123 124 asynchronousFacade2.setId("KO"); 125 126 assertNotSame( 127 128 "same instances(ID) of AsynchronousFacade", 129 130 asynchronousFacade1.getId(), 131 132 asynchronousFacade2.getId()); 133 134 } 135 136 public static void test2AsynchronousCallOnSameInstance() { 137 138 BusinessModel bm1 = new BusinessModel(); 139 140 bm1.processBusinessModel2(15); 141 142 AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1; 143 144 AsynchronousTask asynchronousTask1 = 145 146 asynchronousFacade.getAsynchronousTask(); 147 148 bm1.processBusinessModel2(10); 149 150 AsynchronousTask asynchronousTask2 = 151 152 asynchronousFacade.getAsynchronousTask(); 153 154 assertEquals( 155 156 "Method is not succesfull !", 157 158 OK, 159 160 asynchronousFacade.getResponseCode(asynchronousTask1)); 161 162 assertTrue( 163 164 "value returned is not an instance of Long", 165 166 asynchronousFacade.getReturnValue(asynchronousTask1) 167 168 instanceof Long ); 169 170 assertEquals( 171 172 "Method does not return the right value !" 173 174 + ((Long )asynchronousFacade.getReturnValue(asynchronousTask1)) 175 176 .longValue(), 177 178 ((Long )asynchronousFacade.getReturnValue(asynchronousTask1)) 179 180 .longValue(), 181 182 15); 183 184 assertEquals( 185 186 "Method is not succesfull !", 187 188 asynchronousFacade.getResponseCode(asynchronousTask2), 189 190 OK); 191 192 assertTrue( 193 194 "value returned is not an instance of Long", 195 196 asynchronousFacade.getReturnValue(asynchronousTask2) 197 198 instanceof Long ); 199 200 assertEquals( 201 202 "Method does not return the right value !" 203 204 + ((Long )asynchronousFacade.getReturnValue(asynchronousTask2)) 205 206 .longValue(), 207 208 ((Long )asynchronousFacade.getReturnValue(asynchronousTask2)) 209 210 .longValue(), 211 212 10); 213 214 } 215 216 public static void test2AsynchronousCallOnSameInstance2() { 217 218 BusinessModel bm1 = new BusinessModel(); 219 220 AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1; 221 222 asynchronousFacade.setTimeout(50); 223 224 bm1.processBusinessModel2(1000); 225 226 AsynchronousTask asynchronousTask1 = 227 228 asynchronousFacade.getAsynchronousTask(); 229 230 asynchronousFacade.setTimeout(200); 231 232 bm1.processBusinessModel2(10); 233 234 AsynchronousTask asynchronousTask2 = 235 236 asynchronousFacade.getAsynchronousTask(); 237 238 assertEquals( 239 240 "Method did not timeout !", 241 242 TIMEOUT, 243 244 asynchronousFacade.getResponseCode(asynchronousTask1)); 245 246 assertEquals( 247 248 "Method is not succesfull !", 249 250 asynchronousFacade.getResponseCode(asynchronousTask2), 251 252 OK); 253 254 assertTrue( 255 256 "value returned is not an instance of Long", 257 258 asynchronousFacade.getReturnValue(asynchronousTask2) 259 260 instanceof Long ); 261 262 assertEquals( 263 264 "Method does not return the right value !" 265 266 + ((Long )asynchronousFacade.getReturnValue(asynchronousTask2)) 267 268 .longValue(), 269 270 ((Long )asynchronousFacade.getReturnValue(asynchronousTask2)) 271 272 .longValue(), 273 274 10); 275 276 } 277 278 279 280 public static void testTimeout() { 281 282 BusinessModel bm1 = new BusinessModel(200); 283 284 AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1; 285 286 asynchronousFacade.setTimeout(100); 287 288 bm1.processBusinessModel(); 289 290 long t0 = System.currentTimeMillis(); 291 292 294 296 assertEquals( 297 298 "Method did not timeout !", 299 300 TIMEOUT, 301 302 asynchronousFacade.getResponseCode()); 303 304 long t1 = System.currentTimeMillis(); 305 306 assertTrue("Method time out in " + (t1 - t0) + " ms.", (t1 - t0) < 120); 307 308 assertTrue("Method did not run " + (t1 - t0) + " ms.", (t1 - t0) > 80); 309 310 } 311 312 public static void testReturnValue() { 313 314 BusinessModel bm1 = new BusinessModel(); 315 316 bm1.processBusinessModel2(10); 317 318 AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1; 319 320 assertEquals( 321 322 "Method is not succesfull !", 323 324 OK, 325 326 asynchronousFacade.getResponseCode()); 327 328 assertTrue( 329 330 "value returned is not an instance of Long", 331 332 asynchronousFacade.getReturnValue() instanceof Long ); 333 334 assertEquals( 335 336 "Method does not return the right value !" 337 338 + ((Long )asynchronousFacade.getReturnValue()).longValue(), 339 340 ((Long )asynchronousFacade.getReturnValue()).longValue(), 341 342 10); 343 344 } 345 346 public static void testCleanupCallWhenTimeout() { 347 348 BusinessModelWithCleanup bm1 = new BusinessModelWithCleanup(); 349 350 BusinessModelWithCleanup bm2 = new BusinessModelWithCleanup(); 351 352 AsynchronousFacade asynchronousFacade1 = (AsynchronousFacade)bm1; 353 354 AsynchronousFacade asynchronousFacade2 = (AsynchronousFacade)bm2; 355 356 asynchronousFacade1.setTimeout(100); 357 358 bm1.processBusinessModel2(200); 359 360 bm2.processBusinessModel2(200); 361 362 assertEquals( 363 364 "Method did not timeout !", 365 366 TIMEOUT, 367 368 asynchronousFacade1.getResponseCode()); 369 370 assertEquals( 371 372 "Method is not successfull !", 373 374 OK, 375 376 asynchronousFacade2.getResponseCode()); 377 378 assertEquals("Cleanup method not called", true, bm1.bCleanupCalled); 379 380 assertEquals("Cleanup method called", false, bm2.bCleanupCalled); 381 382 } 383 384 public static void testIsDone() { 385 386 BusinessModel bm1 = new BusinessModel(200); 387 388 bm1.processBusinessModel(); 389 390 AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1; 391 392 long t0 = System.currentTimeMillis(); 393 394 assertFalse("isDone returns TRUE !", asynchronousFacade.isDone()); 395 396 long t1 = System.currentTimeMillis(); 397 398 assertTrue( 399 400 "isDone is a blocking call " + (t1 - t0) + " ms.", 401 402 (t1 - t0) < 20); 403 404 assertEquals( 405 406 "Method is not succesfull !", 407 408 OK, 409 410 asynchronousFacade.getResponseCode()); 411 412 assertTrue("isDone returns FALSE !", asynchronousFacade.isDone()); 413 414 } 415 416 public static void testExceptionRaisedInMethodCall() { 417 418 BusinessModel bm1 = new BusinessModel(); 419 420 bm1.processBusinessModel2(-1); 421 422 AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1; 423 424 assertEquals( 425 426 "EXCEPTIONCAUGHT error not returned !", 427 428 EXCEPTIONCAUGHT, 429 430 asynchronousFacade.getResponseCode()); 431 432 } 433 434 435 436 public static void testPoolSizeFull() { 437 438 ThreadManagerFactory.getThreadManager().setMaximumPoolSize(10); 439 440 for (int i = 0; i < 10; i++) { 441 442 BusinessModel bm1 = new BusinessModel(200); 443 444 bm1.processBusinessModel(); 445 446 447 448 } 449 450 BusinessModel bm1 = new BusinessModel(200); 451 452 bm1.processBusinessModel(); 453 454 AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1; 455 456 assertEquals( 457 458 "Pool size not full !", 459 460 CAN_NOT_PROCESS, 461 462 asynchronousFacade.getResponseCode()); 463 464 } 465 466 467 468 public static void testPerformance() { 469 470 ThreadManagerFactory.getThreadManager().setMaximumPoolSize(5000); 471 472 long tt0 = System.currentTimeMillis(); 473 474 float nbt = 100000; 475 476 for (int i = 0; i < nbt; i++) { 477 478 new BusinessModel(200); 479 480 } 481 482 long tt1 = System.currentTimeMillis(); 483 484 float time = (tt1 - tt0) / nbt; 485 486 System.out.println( 487 488 (int)nbt 489 490 + " advised instances created in " 491 492 + (tt1 - tt0) 493 494 + " (ms).Average time " 495 496 + time 497 498 + " (ms)."); 499 500 BusinessModel bm = new BusinessModel(10); 501 502 AsynchronousFacade Fbm = (AsynchronousFacade)bm; 503 504 long total = 0; 505 506 int iOk = 0; 507 508 int nb1 = 200; 509 510 int nb2 = 5; 511 512 AsynchronousTask[] Tbm = new AsynchronousTask[nb2]; 513 514 for (int j = 0; j < nb1; j++) { 515 516 long t0 = System.currentTimeMillis(); 517 518 for (int i = 0; i < nb2; i++) { 519 520 bm.processBusinessModel(); 521 522 Tbm[i] = Fbm.getAsynchronousTask(); 523 524 } 525 526 long t1 = System.currentTimeMillis(); 527 528 total += (t1 - t0); 529 530 for (int i = 0; i < nb2; i++) { 531 532 int ok = Fbm.getResponseCode(Tbm[i]); 533 534 if (ok == OK) 535 536 iOk++; 537 538 } 539 540 } 541 542 System.out.println( 543 544 nb1 * nb2 545 546 + " asynchronous method invocations in " 547 548 + total 549 550 + " (ms).Average time " 551 552 + (total / (float) (nb1 * nb2)) 553 554 + " (ms)."); 555 556 assertEquals("Some errors:", nb1 * nb2, iOk); 557 558 } 559 560 public static void testResponseTimeReturned() { 561 562 BusinessModel bm1 = new BusinessModel(200); 563 564 int ERROR = 20; 565 566 long t0 = System.currentTimeMillis(); 567 568 bm1.processBusinessModel(); 569 570 AsynchronousFacade asynchronousFacade1 = (AsynchronousFacade)bm1; 571 572 assertEquals( 573 574 "Method is not succesfull !", 575 576 OK, 577 578 asynchronousFacade1.getResponseCode()); 579 580 long t1 = System.currentTimeMillis(); 581 582 long startingTime = 583 584 asynchronousFacade1.getThreadManagerResponse().getStartingTime(); 585 586 long endingTime = 587 588 asynchronousFacade1.getThreadManagerResponse().getEndingTime(); 589 590 assertTrue( 591 592 "starting time issue ? " + (startingTime - t0), 593 594 (startingTime - t0) < ERROR); 595 596 assertTrue( 597 598 "ending time issue ? " + (t1 - endingTime), 599 600 (t1 - endingTime) < ERROR); 601 602 } 603 604 public static void testAsynchronousCallOnPrivateMethod() { 605 606 BusinessModel bm1 = new BusinessModel(200); 607 608 long t0 = System.currentTimeMillis(); 609 610 bm1.callPrivateMethod(); 611 612 long t1 = System.currentTimeMillis(); 613 614 assertTrue( 615 616 "private method not an asynchronous call:" + (t1 - t0) + " ms.", 617 618 (t1 - t0) < 100); 619 620 } 621 622 public static void test2AsynchronousCallsOnSameInstanceFrom2DifferentThreads() { 623 624 BusinessModel bm1 = new BusinessModel(100); 625 626 BusinessThread businessThread1 = 627 628 new BusinessThread(bm1, 100, 100, "First Call"); 629 630 BusinessThread businessThread2 = 631 632 new BusinessThread(bm1, 200, 200, "Second Call"); 633 634 Thread th1 = new Thread (businessThread1); 635 636 th1.start(); 637 638 Thread th2 = new Thread (businessThread2); 639 640 th2.start(); 641 642 BusinessModel.sleep(500); 643 644 assertNotSame( 645 646 "The result from 2 differents threads are the same !", 647 648 businessThread1.result, 649 650 businessThread2.result); 651 652 } 653 654 public static void testAsynchronousCallOnStaticMethod() { 655 656 Parameter object = new Parameter(); 657 658 long t0 = System.currentTimeMillis(); 659 660 BusinessModel.processBusinessModel4(200, object); 661 662 long t1 = System.currentTimeMillis(); 663 664 assertTrue( 665 666 "not an instance of AsynchronousFacade", 667 668 object instanceof AsynchronousFacade); 669 670 assertTrue( 671 672 "static method not an asynchronous call:" + (t1 - t0) + " ms.", 673 674 (t1 - t0) < 100); 675 676 AsynchronousFacade asynchronousFacade = (AsynchronousFacade)object; 677 678 AsynchronousTask asynchronousTask1 = 679 680 asynchronousFacade.getAsynchronousTask(); 681 682 assertEquals( 683 684 "Method does not return the right value !", 685 686 200, 687 688 ((Long )asynchronousFacade.getReturnValue(asynchronousTask1)) 689 690 .longValue()); 691 692 } 693 694 public static void testAsynchronousCallOnStaticMethodWithTimeout() { 695 696 int nb = 1; 697 698 for (int i = 0; i < nb; i++) { 699 700 AsynchronousCallOnStaticMethodWithTimeout(); 701 702 } 703 704 } 705 706 public static void AsynchronousCallOnStaticMethodWithTimeout() { 707 708 Parameter object1 = new Parameter(); 709 710 Parameter object2 = new Parameter(); 711 712 Parameter object3 = new Parameter(); 713 714 Parameter object4 = new Parameter(); 715 716 assertTrue( 717 718 "not an instance of AsynchronousFacade", 719 720 object1 instanceof AsynchronousFacade 721 722 || object2 instanceof AsynchronousFacade); 723 724 ((AsynchronousFacade)object1).setTimeout(100); 725 726 ((AsynchronousFacade)object3).setTimeout(100); 727 728 ((AsynchronousFacade)object4).setTimeout(100); 729 730 long t0 = System.currentTimeMillis(); 731 732 BusinessModel.processBusinessModel4(200, object1); 733 734 BusinessModel.processBusinessModel4(150, object2); 735 736 BusinessModelWithStaticCleanup.processBusinessModel4(200, object3); 737 738 BusinessModelWithStaticCleanupWithParameters.processBusinessModel4( 739 740 200, 741 742 object4); 743 744 System.out.println(System.currentTimeMillis()-t0); 745 746 long t1 = System.currentTimeMillis(); 747 748 assertTrue( 749 750 "static method not an asynchronous call:" + (t1 - t0) + " ms.", 751 752 (t1 - t0) < 160); 753 754 AsynchronousFacade asynchronousFacade1 = (AsynchronousFacade)object1; 755 756 assertEquals( 757 758 "Method did not timeout !", 759 760 TIMEOUT, 761 762 asynchronousFacade1.getResponseCode()); 763 764 AsynchronousFacade asynchronousFacade = (AsynchronousFacade)object2; 765 766 AsynchronousTask asynchronousTask2 = 767 768 asynchronousFacade.getAsynchronousTask(); 769 770 assertEquals( 771 772 "Method does not return the right value !", 773 774 150, 775 776 ((Long )asynchronousFacade.getReturnValue(asynchronousTask2)) 777 778 .longValue()); 779 780 } 781 782 } | Popular Tags |