1 29 package jegg.impl; 30 31 import jegg.Egg; 32 import jegg.EggBase; 33 import jegg.Message; 34 import jegg.Port; 35 import jegg.PortException; 36 import jegg.impl.Dispatcher; 37 import jegg.impl.EggShell; 38 import jegg.impl.PortImpl; 39 import jegg.impl.PortRegistry; 40 import jegg.impl.Priority; 41 import jegg.timer.Timeout; 42 import jegg.timer.Timer; 43 44 45 48 public class EggTest extends TestBase 49 { 50 static 51 { 52 setTestClass(EggTest.class); 53 } 54 55 56 private static final int ONE_SECOND = 1000; 57 58 private static final int ONE_HUNDRED = 100; 59 60 64 public EggTest(final String name) 65 { 66 super(name); 67 } 68 69 70 private EggShell _eggA1_1; 71 MessageHandlerA1 _heggA1_1; 72 73 74 private EggShell _eggA1_2; 75 private MessageHandlerA1 _heggA1_2; 76 77 78 private Message currentMessage; 79 80 private int numMessages = 0; 81 82 private Port locatePortResponse; 83 84 87 public final void setup() 88 { 89 try 90 { 91 _eggA1_1 = new EggShell("eggA1_1", _heggA1_1=new MessageHandlerA1("eggA1_1"), null); 92 new PortImpl(_eggA1_1); 93 94 _eggA1_2 = new EggShell("eggA1_2", _heggA1_2=new MessageHandlerA1("eggA1_2"), null); 95 new PortImpl(_eggA1_2); 96 } 97 catch (Exception e) 98 { 99 fail(e.toString()); 100 } 101 } 102 103 106 public final void teardown() 107 { 108 _eggA1_1.stop(); 109 _eggA1_2.stop(); 110 } 111 112 113 116 137 public final void test_bindToPort() 138 { 139 PortImpl port_2 = (PortImpl)_eggA1_2.getPort(); 140 _eggA1_1.bindToPort(port_2); 141 Port[] connected_ports = port_2.getConnectedPorts(); 142 assertNotNull("null connected_ports", connected_ports); 143 assertEquals("Wrong # connected ports", 1, connected_ports.length); 144 assertEquals("Wrong connected port", _eggA1_1.getPort(), connected_ports[0]); 145 } 146 147 public final void test_createMessage_Object() 148 { 149 Object o = "Hello"; 150 Message m1 = _eggA1_1.createMessage(o); 151 assertNotNull("failed to create message", m1); 152 assertNotNull("message wraps null object", m1.getMessage()); 153 assertEquals("message wraps wrong object", o, m1.getMessage()); 154 assertNotNull("message has null port", m1.getFrom()); 155 assertEquals("message has wrong port", _eggA1_1.getPort(), m1.getFrom()); 156 assertNotNull("message has null priority", m1.getPriority()); 157 assertEquals("message has wrong priority", Priority.MEDIUM, m1.getPriority()); 158 } 159 160 public final void test_createMessage_Object_Priority() 161 { 162 Object o = "Hello"; 163 Message m1 = _eggA1_1.createMessage(o, Priority.HIGH); 164 assertNotNull("failed to create message", m1); 165 assertNotNull("message wraps null object", m1.getMessage()); 166 assertEquals("message wraps wrong object", o, m1.getMessage()); 167 assertNotNull("message has null port", m1.getFrom()); 168 assertEquals("message has wrong port", _eggA1_1.getPort(), m1.getFrom()); 169 assertNotNull("message has null priority", m1.getPriority()); 170 assertEquals("message has wrong priority", Priority.HIGH, m1.getPriority()); 171 } 172 173 public final void test_createRepeatingTimer() 174 { 175 MessageHandlerC hc = new MessageHandlerC("egg_c"); 176 EggShell egg_c = new EggShell("egg_c", hc, null); new PortImpl(egg_c); 177 egg_c.getDispatcher().start(); 178 179 Timer t = egg_c.createRepeatingTimer(100, 0); 180 assertNotNull("null timer", t); 181 try {Thread.sleep(1000);}catch(Throwable th) {} 182 t.cancel(); 183 egg_c.stop(); 184 assertTrue("Too few timeouts", 9 < hc.messages.size()); 185 assertTrue("Wrong message type", hc.messages.get(0) instanceof Timeout); 186 } 187 188 public final void test_createSingleShotTimer() 189 { 190 MessageHandlerC hc = new MessageHandlerC("egg_c"); 191 EggShell egg_c = new EggShell("egg_c", hc, null); 192 new PortImpl(egg_c); 193 194 Timer t = egg_c.createSingleShotTimer(500); 195 assertNotNull("null timer", t); 196 try {Thread.sleep(1000);}catch(Throwable th) {} 197 t.cancel(); 198 egg_c.stop(); 199 assertTrue("Too few timeouts", 1 == hc.messages.size()); 200 assertTrue("Wrong message type", hc.messages.get(0) instanceof Timeout); 201 } 202 203 public final void test_getCurrentMessage() throws PortException 204 { 205 206 Egg he = new EggBase() 207 { 208 public void handle(Object message) 209 { 210 currentMessage = getContext().getCurrentMessage(); 211 } 212 }; 213 214 EggShell e = new EggShell("e", he, null); 215 new PortImpl(e); 216 if (!e.getDispatcher().isAlive()) 217 e.getDispatcher().start(); 218 219 Message sent = e.createMessage("Hello"); 220 currentMessage = null; 221 e.getPort().send(sent); 222 try { Thread.sleep(1000); }catch(Throwable t) {} 223 e.stop(); 224 assertNotNull("currentMessage is null", currentMessage); 225 assertEquals("message mismatch", sent, currentMessage); 226 assertEquals("message object mismatch", sent.getMessage(),currentMessage.getMessage()); 227 } 228 229 public final void test_getDispatcher() 230 { 231 Dispatcher d = Dispatcher.getAnonymousDispatcher(); 232 MessageHandlerC hc1 = new MessageHandlerC("egg_c1"); 233 MessageHandlerC hc2 = new MessageHandlerC("egg_c2"); 234 MessageHandlerC hc3 = new MessageHandlerC("egg_c3"); 235 236 EggShell egg_c1 = new EggShell("egg_c1", hc1, d); new PortImpl(egg_c1); 237 EggShell egg_c2 = new EggShell("egg_c2", hc2, null); new PortImpl(egg_c2); 238 EggShell egg_c3 = new EggShell("egg_c3", hc3, null); new PortImpl(egg_c3); 239 240 Dispatcher d1 = egg_c1.getDispatcher(); 241 Dispatcher d2 = egg_c2.getDispatcher(); 242 Dispatcher d3 = egg_c3.getDispatcher(); 243 244 egg_c1.stop(); 245 egg_c2.stop(); 246 egg_c3.stop(); 247 248 assertEquals("wrong dispatcher", d, d1); 249 assertEquals("egg_c2 and egg_c3 don't share dispatcher",d2,d3); 250 assertTrue("egg_c2 doesn't have default dispatcher", ! d.equals(d2)); 251 assertTrue("egg_c3 doesn't have default dispatcher", ! d.equals(d3)); 252 } 253 254 public final void test_getFromPort() throws PortException 255 { 256 Egg he1 = new EggBase() 257 { 258 public void handle(Object o) {} 259 }; 260 261 EggShell e1 = new EggShell("e", he1, null); 262 new PortImpl(e1); 263 264 Egg he2 = new EggBase() 265 { 266 public void handle(Object message) 267 { 268 currentMessage = getContext().getCurrentMessage(); 269 } 270 }; 271 272 EggShell e2 = new EggShell("e2", he2, null); 273 new PortImpl(e2); 274 275 Message sent = e1.createMessage("Hello"); 276 currentMessage = null; 277 e2.getPort().send(sent); 278 try { Thread.sleep(1000); }catch(Throwable t) {} 279 e1.stop(); 280 e2.stop(); 281 assertNotNull("message not delivered", currentMessage); 282 assertEquals("wrong 'from' port", e1.getPort(), currentMessage.getFrom()); 283 } 284 285 public final void test_getID() 286 { 287 Object id = new Object (); 288 Egg h = new EggBase() 289 { 290 public void handle(Object o) {} 291 }; 292 293 EggShell e1 = new EggShell(id, h, null); 294 new PortImpl(e1); 295 296 e1.stop(); 297 assertEquals("id mismatch", id,e1.getId()); 298 } 299 300 public final void test_getNumPendingMessage_getNextMessage() throws PortException 301 { 302 Egg h = new EggBase() 303 { 304 public void handle(Object m) 305 { 306 synchronized (m) 307 { 308 try {m.wait();}catch(Throwable t) {} 309 } 310 } 311 }; 312 313 EggShell e1 = new EggShell("e1", h, null); 314 new PortImpl(e1); 315 316 Message blockingMessage = e1.createMessage(e1); 317 e1.getPort().send(blockingMessage); 318 try { Thread.sleep(1000); }catch(Throwable t) {} 319 Object o1 = new Object (); 320 Object o2 = new Object (); 321 Object o3 = new Object (); 322 Message m1 = e1.createMessage(o1); 323 Message m2 = e1.createMessage(o2); 324 Message m3 = e1.createMessage(o3); 325 e1.getPort().send(m1); 326 e1.getPort().send(m2); 327 e1.getPort().send(m3); 328 assertTrue("wrong number messages", 3 == e1.getNumPendingMessages()); 329 Message d1 = e1.getNextMessage(); 330 assertTrue("wrong number messages", 2 == e1.getNumPendingMessages()); 331 Message d2 = e1.getNextMessage(); 332 assertTrue("wrong number messages", 1 == e1.getNumPendingMessages()); 333 Message d3 = e1.getNextMessage(); 334 assertTrue("wrong number messages", 0 == e1.getNumPendingMessages()); 335 assertEquals("message mismatch", m1, d1); 336 assertEquals("message mismatch", m2, d2); 337 assertEquals("message mismatch", m3, d3); 338 assertEquals("object mismatch", o1, m1.getMessage()); 339 assertEquals("object mismatch", o2, m2.getMessage()); 340 assertEquals("object mismatch", o3, m3.getMessage()); 341 e1.stop(); 342 synchronized(e1) 343 { 344 e1.notify(); 345 } 346 } 347 348 public final void test_getPort() 349 { 350 Egg he1 = new EggBase() {public void handle(Object o) {}}; 351 352 EggShell e1 = new EggShell("e1", he1, null); 353 new PortImpl(e1); 354 355 e1.stop(); 356 assertNotNull("null port", e1.getPort()); 357 } 358 359 public final void test_getQueue() throws PortException 360 { 361 Egg he1 = new EggBase() 362 { 363 public void handle(Object m) 364 { 365 synchronized (m) 366 { 367 try {m.wait();}catch(Throwable t) {} 368 } 369 } 370 }; 371 372 EggShell e1 = new EggShell("e1", he1,null); 373 new PortImpl(e1); 374 375 PriorityQueue q = e1.getQueue(); 376 assertNotNull("null queue", q); 377 assertTrue("not empty", 0 == q.size()); 378 assertTrue("not empty", q.isEmpty()); 379 Message blockingMessage = e1.createMessage(e1); 380 e1.getPort().send(blockingMessage); 381 try { Thread.sleep(1000); }catch(Throwable t) {} 382 Object o1 = new Object (); 383 Object o2 = new Object (); 384 Object o3 = new Object (); 385 Message m1 = e1.createMessage(o1); 386 Message m2 = e1.createMessage(o2); 387 Message m3 = e1.createMessage(o3); 388 e1.getPort().send(m1); 389 e1.getPort().send(m2); 390 e1.getPort().send(m3); 391 assertTrue("wrong size", 3 == q.size()); 392 assertTrue("isEmpty() is wrong", !q.isEmpty()); 393 e1.stop(); 394 synchronized(e1) 395 { 396 e1.notify(); 397 } 398 } 399 400 public final void test_publishPort() throws PortException 401 { 402 locatePortResponse = null; 403 Egg he1 = new EggBase() 404 { 405 public void handle(Object m) 406 { 407 String name = (String ) m; 408 getContext().publishPort(name); 409 } 410 public void handle(Port resp) 411 { 412 locatePortResponse = resp; 413 } 414 }; 415 416 EggShell e1 = new EggShell("e1", he1, null); 417 new PortImpl(e1); 418 419 e1.getPort().send(e1.createMessage("e1")); 420 PortRegistry r = PortRegistry.getInstance(); 421 r.getPort().send(e1.createMessage(new LocatePortMessage("e1"))); 422 try {Thread.sleep(1000);}catch(Throwable t) {} 423 assertNotNull("no locateportresponse", locatePortResponse); 424 assertEquals("wrong port", e1.getPort(), locatePortResponse); 425 assertEquals("wrong name", "e1", locatePortResponse.getId()); 426 e1.stop(); 427 } 428 429 public final void test_requestPort() throws PortException 430 { 431 locatePortResponse = null; 432 Egg he1 = new EggBase() 433 { 434 public void handle(Object m) 435 { 436 String name = (String ) m; 437 getContext().publishPort(name); 438 } 439 }; 440 EggShell e1 = new EggShell("e1",he1,null); 441 new PortImpl(e1); 442 443 Egg he2 = new EggBase() 444 { 445 public void handle(Object m) 446 { 447 String name = (String ) m; 448 getContext().requestPort(name); 449 } 450 public void handle(Port resp) 451 { 452 locatePortResponse = resp; 453 } 454 }; 455 EggShell e2 = new EggShell("e2",he2, null); 456 new PortImpl(e2); 457 458 e1.getPort().send(e1.createMessage("abc")); 459 e2.getPort().send(e2.createMessage("abc")); 460 try {Thread.sleep(1000);}catch(Throwable t) {} 461 assertNotNull("no locateportresponse", locatePortResponse); 462 assertEquals("wrong port", e1.getPort(), locatePortResponse); 463 assertEquals("wrong name: "+locatePortResponse.getId(), "e1", locatePortResponse.getId()); 464 e1.stop(); 465 e2.stop(); 466 } 467 468 public final void test_respond() throws PortException 469 { 470 Egg he1 = new EggBase() 471 { 472 public void handle(Object m) 473 { 474 currentMessage = getContext().getCurrentMessage(); 475 } 476 }; 477 EggShell e1 = new EggShell("e1",he1,null); 478 new PortImpl(e1); 479 480 Egg he2 = new EggBase() 481 { 482 public void handle(Object m) 483 { 484 getContext().respond(m); 485 } 486 }; 487 EggShell e2 = new EggShell("e2",he2,null); 488 new PortImpl(e2); 489 490 Object o = new Object (); 491 Message msg = e1.createMessage(o); 492 currentMessage = null; 493 e2.getPort().send(msg); 494 try {Thread.sleep(1000);} catch(Throwable t) {} 495 assertNotNull("message not returned", currentMessage); 496 assertEquals("wrong port", e2.getPort(), currentMessage.getFrom()); 497 assertEquals("wrong object", o, currentMessage.getMessage()); 498 assertFalse("wrong message", msg.equals(currentMessage)); 499 e1.stop(); 500 e2.stop(); 501 } 502 503 public final void test_respond_Priority() throws PortException 504 { 505 506 Egg he1 = new EggBase() 507 { 508 public void handle(Object m) 509 { 510 currentMessage = getContext().getCurrentMessage(); 511 } 512 }; 513 EggShell e1 = new EggShell("e1",he1, null); 514 new PortImpl(e1); 515 516 Egg he2 = new EggBase() 517 { 518 public void handle(Object m) 519 { 520 getContext().respond(m, Priority.HIGH); 521 } 522 }; 523 524 EggShell e2 = new EggShell("e2",he2,null); 525 new PortImpl(e2); 526 527 Object o = new Object (); 528 Message msg = e1.createMessage(o); 529 currentMessage = null; 530 e2.getPort().send(msg); 531 try {Thread.sleep(1000);} catch(Throwable t) {} 532 assertNotNull("message not returned", currentMessage); 533 assertEquals("wrong port", e2.getPort(), currentMessage.getFrom()); 534 assertEquals("wrong priority", Priority.HIGH, currentMessage.getPriority()); 535 assertEquals("wrong object", o, currentMessage.getMessage()); 536 assertFalse("wrong message", msg.equals(currentMessage)); 537 e1.stop(); 538 e2.stop(); 539 } 540 541 public final void test_respond_Port() throws PortException 542 { 543 544 Egg he1 = new EggBase() 545 { 546 public void handle(Object m) 547 { 548 } 550 }; 551 552 Egg he2 = new EggBase() 553 { 554 PortImpl pp; 555 public void handle(Object m) 556 { 557 getContext().respond(pp,m); 558 } 559 public void handle(PortImpl p) 560 { 561 pp = p; 562 } 563 }; 564 565 Egg he3 = new EggBase() 566 { 567 public void handle(Object m) 568 { 569 currentMessage = getContext().getCurrentMessage(); 570 } 571 }; 572 573 EggShell e1 = new EggShell("e1",he1, null); new PortImpl(e1); 574 EggShell e2 = new EggShell("e2",he2, null); new PortImpl(e2); 575 EggShell e3 = new EggShell("e3",he3, null); new PortImpl(e3); 576 577 Message pmsg = e1.createMessage(e3.getPort()); 578 e2.getPort().send(pmsg); 579 Object o = new Object (); 580 Message msg = e1.createMessage(o); 581 currentMessage = null; 582 e2.getPort().send(msg); 583 try {Thread.sleep(1000);} catch(Throwable t) {} 584 assertNotNull("message not returned", currentMessage); 585 assertEquals("wrong port", e2.getPort(), currentMessage.getFrom()); 586 assertEquals("wrong object", o, currentMessage.getMessage()); 587 assertFalse("wrong message", msg.equals(currentMessage)); 588 e1.stop(); 589 e2.stop(); 590 e3.stop(); 591 } 592 593 public final void test_respond_Port_Priority() throws PortException 594 { 595 Egg he1 = new EggBase() 596 597 { 598 public void handle(Object m) 599 { 600 } 602 }; 603 Egg he2 = new EggBase() 604 605 606 { 607 PortImpl pp; 608 public void handle(Object m) 609 { 610 getContext().respond(pp,m,Priority.LOW); 611 } 612 public void handle(PortImpl p) 613 { 614 pp = p; 615 } 616 }; 617 Egg he3 = new EggBase() 618 619 { 620 public void handle(Object m) 621 { 622 currentMessage = getContext().getCurrentMessage(); 623 } 624 }; 625 626 EggShell e1 = new EggShell("e1",he1, null); new PortImpl(e1); 627 EggShell e2 = new EggShell("e2",he2, null); new PortImpl(e2); 628 EggShell e3 = new EggShell("e3",he3, null); new PortImpl(e3); 629 630 Message pmsg = e1.createMessage(e3.getPort()); 631 e2.getPort().send(pmsg); 632 Object o = new Object (); 633 Message msg = e1.createMessage(o); 634 currentMessage = null; 635 e2.getPort().send(msg); 636 try {Thread.sleep(1000);} catch(Throwable t) {} 637 assertNotNull("message not returned", currentMessage); 638 assertEquals("wrong port", e2.getPort(), currentMessage.getFrom()); 639 assertEquals("wrong priority", Priority.LOW, currentMessage.getPriority()); 640 assertEquals("wrong object", o, currentMessage.getMessage()); 641 assertFalse("wrong message", msg.equals(currentMessage)); 642 e1.stop(); 643 e2.stop(); 644 e3.stop(); 645 } 646 647 public final void test_send() throws PortException 648 { 649 Egg he0 = new EggBase() 650 { 651 public void handle(Object m) 652 { 653 } 655 }; 656 Egg he1 = new EggBase() 657 { 658 public void handle(Object m) 659 { 660 numMessages++; 661 } 662 }; 663 Egg he2 = new EggBase() 664 { 665 PortImpl pp; 666 public void handle(Object m) 667 { 668 numMessages++; 669 } 670 }; 671 Egg he3 = new EggBase() 672 { 673 public void handle(Object m) 674 { 675 numMessages++; 676 } 677 }; 678 Egg he4 = new EggBase() 679 { 680 public void handle(Object o) 681 { 682 getContext().send(o); 683 } 684 }; 685 EggShell e0 = new EggShell("e0",he0, null); new PortImpl(e0); 686 EggShell e1 = new EggShell("e1",he1, null); new PortImpl(e1); 687 EggShell e2 = new EggShell("e2",he2, null); new PortImpl(e2); 688 EggShell e3 = new EggShell("e3",he3, null); new PortImpl(e3); 689 EggShell e4 = new EggShell("e4",he4, null); new PortImpl(e4); 690 691 e1.bindToPort(e4.getPort()); 692 e2.bindToPort(e4.getPort()); 693 e3.bindToPort(e4.getPort()); 694 Object oo = new Object (); 695 Message msg = e0.createMessage(oo); 696 numMessages = 0; 697 e4.getPort().send(msg); 698 try { Thread.sleep(1000); } catch(Throwable t) {} 699 assertEquals("not all messages delivered", 3, numMessages); 700 e0.stop(); 701 e1.stop(); 702 e2.stop(); 703 e3.stop(); 704 e4.stop(); 705 } 706 707 public final void testBasicEgg() 708 { 709 prolog 710 ( 711 "Test:\n" 712 + "1. getPort() \n" 713 + "2. createMessage()\n" 714 ); 715 716 Port port = _eggA1_1.getPort(); 717 assertNotNull("Port not set", port); 718 719 Object o = new Object (); 720 Message a_msg = _eggA1_1.createMessage(o); 721 assertNotNull("failed to create message", a_msg); 722 assertTrue("Wrong port", a_msg.getFrom().equals(_eggA1_1.getPort())); 723 assertTrue("wrong message object", a_msg.getMessage().equals(o)); 724 725 assertTrue("wrong name", _eggA1_1.getId().equals("eggA1_1")); 726 727 } 728 729 public void testSendMsg() throws PortException 730 { 731 prolog( 732 "1. Send the egg 100 message of 3 different types\n" 733 + "2. Verify the messages were dispatched to the correct handlers\n" 734 ); 735 736 Object o = new Object (); 737 Long l = new Long (2); 738 Short s = new Short ((short)3); 739 740 Port port = _eggA1_1.getPort(); 741 for (int i = 0; i < ONE_HUNDRED; ++i) 742 { 743 port.send(_eggA1_1.createMessage(o)); 744 port.send(_eggA1_1.createMessage(l)); 745 port.send(_eggA1_1.createMessage(s)); 746 } 747 748 try 749 { 750 Thread.sleep(5 * ONE_SECOND); 751 } 752 catch (Throwable t) 753 { 754 fail("interrupted"); 755 } 756 757 assertEquals( 758 "Messages not handled by correct handler", 759 ONE_HUNDRED, _heggA1_1.getNumMessages1()); 760 761 assertEquals( 762 "Messages not handled by correct handler", 763 ONE_HUNDRED, _heggA1_1.getNumMessages2()); 764 765 assertEquals( 766 "Messages not handled by correct handler", 767 ONE_HUNDRED, _heggA1_1.getNumMessages3()); 768 769 long expectedSum = 770 ONE_HUNDRED * 1 771 + ONE_HUNDRED * l.longValue() 772 + ONE_HUNDRED * s.longValue(); 773 774 long measuredSum = _heggA1_1.getSum(); 775 776 assertTrue( 777 "Sum is incorrect: " 778 + measuredSum 779 + " (expected " 780 + expectedSum 781 + ")", 782 measuredSum == expectedSum); 783 } 784 785 786 public void testEggRespondToEgg() 787 { 788 prolog( 789 "1. Create eggs A and B\n" 790 +"2. A registers port\n" 791 +"3. B gets A's port\n" 792 +"4. B sends message to A\n" 793 +"5. A receives B's message and responds with message to B\n" 794 ); 795 796 try 797 { 798 MessageHandlerA hegg_a = new MessageHandlerA("egg_a"); 799 MessageHandlerB hegg_b = new MessageHandlerB("egg_b"); 800 801 802 EggShell egg_b = new EggShell("egg_b",hegg_b, null); new PortImpl(egg_b); 803 EggShell egg_a = new EggShell("egg_a",hegg_a, null); new PortImpl(egg_a); 804 805 egg_a.getPort().send(new Init()); 806 egg_b.getPort().send(new Init()); 807 808 809 try {Thread.sleep(10000);} catch(Throwable t){} 810 811 assertNotNull("Egg B didn't receive response", hegg_b.response); 812 assertNotNull("Egg A didn't get duplicate name exception", hegg_a.error); 813 } 814 catch (Exception e) 815 { 816 e.printStackTrace(); 817 } 818 } 819 820 private boolean testFailed = false; 821 822 public void testSendPriorityMessage() throws PortException 823 { 824 Egg he1 = new EggBase() 825 { 826 public void handle(EggShell e) throws PortException 827 { 828 Port p = e.getPort(); 829 p.send(getContext().createMessage(new Integer (1),Priority.LOW)); 830 p.send(getContext().createMessage(new Integer (2),Priority.MEDIUM)); 831 p.send(getContext().createMessage(new Integer (3),Priority.HIGH)); 832 } 833 public void handle(Object o) 834 { 835 } 837 }; 838 EggShell e1 = new EggShell("e1",he1, null); new PortImpl(e1); 839 840 Egg he2 = new EggBase() 841 { 842 int n = 3; 843 public void handle(Integer i) 844 { 845 if (i.intValue() != n) testFailed = true; 846 --n; 847 } 848 public void handle(Object o) 849 { 850 } 852 }; 853 EggShell e2 = new EggShell("e2",he2, null); new PortImpl(e2); 854 855 testFailed = false; 856 e1.getPort().send(e1.createMessage(e2)); 857 try {Thread.sleep(5000);} catch(Throwable t){} 858 assertFalse("test failed", testFailed); 859 e1.stop(); 860 e2.stop(); 861 } 862 863 class IntHolder { IntHolder(int n) {i=n;} int i; } 864 IntHolder count; 865 long duration = 0; 866 Object doneObject = new Object (); 867 868 public void testSpeed() throws PortException 869 { 870 class EggCounter extends EggBase 871 { 872 public void handle(Object o) {} 873 public void handle(IntHolder ih) 874 { 875 ih.i--; 876 if (0 < ih.i) 877 { 878 getContext().respond(ih); 879 } 880 else 881 { 882 synchronized(doneObject) { doneObject.notifyAll(); } 883 } 884 } 885 } 886 887 EggCounter he1 = new EggCounter(); 888 EggShell e1 = new EggShell("e1", he1, null); new PortImpl(e1); 889 EggCounter he2 = new EggCounter(); 890 EggShell e2 = new EggShell("e2", he2,null); new PortImpl(e2); 891 892 int num = 100000; 893 IntHolder ih = new IntHolder(num); 894 895 synchronized (doneObject) 896 { 897 long start = System.currentTimeMillis(); 898 e1.getPort().send(e2.createMessage(ih)); 899 e1.getPort().send(e2.createMessage(e2)); 900 try {doneObject.wait();} catch(Throwable t) {} 901 duration = System.currentTimeMillis() - start; 902 } 903 float rate = num / (float)duration * 1000; 904 System.out.println("rate: " + rate); 905 e1.stop(); 906 e2.stop(); 907 } 908 909 public void testSpeed2() 910 { 911 class Counter extends Thread 912 { 913 IntHolder holder; 914 Object holderObj = new Object (); 915 Counter counter; 916 String name; 917 Counter(String n) 918 { 919 name = n; 920 } 921 void setCounter(Counter c) 922 { 923 counter = c; 924 } 925 void primePump(IntHolder ih) 926 { 927 holder = ih; 928 } 929 public void run() 930 { 931 synchronized (doneObject) 932 { 933 doneObject.notifyAll(); 934 try {doneObject.wait();}catch(Throwable t) {return;} 936 } 937 938 while(true) 939 { 940 synchronized (this) 941 { 942 while (null == holder) 943 { 944 try {this.wait();}catch(Throwable t) {return;} 946 } 947 if (0 >= holder.i) 948 break; 949 holder.i--; 951 counter.count(holder); 952 holder = null; 953 } 954 } 955 synchronized(doneObject) 956 { 957 doneObject.notifyAll(); 959 } 960 } 961 synchronized void count(IntHolder ih) 962 { 963 holder = ih; 964 this.notify(); 965 } 966 } 967 968 synchronized (doneObject) 969 { 970 Counter c1 = new Counter("c1"); 971 Counter c2 = new Counter("c2"); 972 c1.setCounter(c2); 973 c2.setCounter(c1); 974 count = new IntHolder(100000); 975 c1.primePump(count); 976 c1.start(); 977 try {doneObject.wait();}catch(Throwable t) {} 978 c2.start(); 979 try {doneObject.wait();}catch(Throwable t) {} 980 long start = System.currentTimeMillis(); 981 doneObject.notifyAll(); 982 try {doneObject.wait();}catch(Throwable t) {} 983 long end = System.currentTimeMillis(); 984 c1.interrupt(); 985 c2.interrupt(); 986 duration = end - start; 987 float rate = 100000 / (float)duration * 1000; 988 System.out.println("rate: " + rate); 989 } 990 } 991 } 992 | Popular Tags |