1 package org.jacorb.test.orb.policies; 2 3 import junit.framework.*; 4 5 import org.omg.TimeBase.*; 6 import org.omg.CORBA.*; 7 8 import org.jacorb.test.*; 9 import org.jacorb.test.common.*; 10 import org.omg.Messaging.*; 11 12 import org.jacorb.util.Time; 13 14 18 public class TimingTest extends CallbackTestCase 19 { 20 private TimingServer server = null; 21 22 public TimingTest(String name, ClientServerSetup setup) 23 { 24 super(name, setup); 25 } 26 27 protected void setUp() throws Exception 28 { 29 server = TimingServerHelper.narrow (setup.getServerObject()); 30 } 31 32 private class ReplyHandler 33 extends CallbackTestCase.ReplyHandler 34 implements AMI_TimingServerHandlerOperations 35 { 36 37 public void ex_op_excep(ExceptionHolder excep_holder) 38 { 39 wrong_exception ("ex_op_excep", excep_holder); 40 } 41 42 public void ex_op(char ami_return_val) 43 { 44 wrong_reply ("ex_op"); 45 } 46 47 public void operation_excep(ExceptionHolder excep_holder) 48 { 49 wrong_exception ("operation_excep", excep_holder); 50 } 51 52 public void operation(int ami_return_val) 53 { 54 wrong_reply ("operation"); 55 } 56 57 public void server_time_excep(ExceptionHolder excep_holder) 58 { 59 wrong_exception ("server_time_excep", excep_holder); 60 } 61 62 public void server_time(long ami_return_val) 63 { 64 wrong_reply ("server_time"); 65 } 66 67 } 68 69 private AMI_TimingServerHandler ref ( ReplyHandler handler ) 70 { 71 AMI_TimingServerHandlerPOATie tie = 72 new AMI_TimingServerHandlerPOATie( handler ) 73 { 74 public org.omg.CORBA.portable.OutputStream 75 _invoke( String method, 76 org.omg.CORBA.portable.InputStream _input, 77 org.omg.CORBA.portable.ResponseHandler handler ) 78 throws org.omg.CORBA.SystemException 79 { 80 try 81 { 82 return super._invoke( method, _input, handler ); 83 } 84 catch( AssertionFailedError e ) 85 { 86 return null; 87 } 88 } 89 }; 90 return tie._this( setup.getClientOrb() ); 91 } 92 93 public static Test suite() 94 { 95 TestSuite suite = new TestSuite ("Timing Policies"); 96 ClientServerSetup setup = 97 new ClientServerSetup 98 (suite, 99 "org.jacorb.test.orb.policies.TimingServerImpl"); 100 101 105 suite.addTest (new TimingTest ("test_sync_no_timing", setup)); 106 suite.addTest (new TimingTest ("test_async_no_timing", setup)); 107 108 suite.addTest (new TimingTest ("test_all_policies_sync_ok", setup)); 109 110 suite.addTest (new TimingTest ("test_request_start_time_sync_expired", setup)); 111 suite.addTest (new TimingTest ("test_request_start_time_sync_wait", setup)); 112 113 suite.addTest (new TimingTest ("test_request_end_time_sync_ok", setup)); 114 suite.addTest (new TimingTest ("test_request_end_time_sync_pre_expired", setup)); 115 suite.addTest (new TimingTest ("test_request_end_time_async_pre_expired", setup)); 116 118 suite.addTest (new TimingTest ("test_request_timeout_sync_ok", setup)); 119 122 suite.addTest (new TimingTest ("test_reply_start_time_sync_expired", setup)); 123 suite.addTest (new TimingTest ("test_reply_start_time_sync_wait", setup)); 124 suite.addTest (new TimingTest ("test_reply_start_time_async_expired", setup)); 125 suite.addTest (new TimingTest ("test_reply_start_time_async_wait", setup)); 126 127 suite.addTest (new TimingTest ("test_reply_end_time_sync_ok", setup)); 128 suite.addTest (new TimingTest ("test_reply_end_time_sync_pre_expired", setup)); 129 suite.addTest (new TimingTest ("test_reply_end_time_sync_expired", setup)); 130 suite.addTest (new TimingTest ("test_reply_end_time_async_ok", setup)); 131 suite.addTest (new TimingTest ("test_reply_end_time_async_expired", setup)); 133 134 suite.addTest (new TimingTest ("test_relative_roundtrip_sync_ok", setup)); 135 suite.addTest (new TimingTest ("test_relative_roundtrip_sync_expired", setup)); 136 suite.addTest (new TimingTest ("test_relative_roundtrip_async_ok", setup)); 137 suite.addTest (new TimingTest ("test_relative_roundtrip_async_expired", setup)); 138 139 return setup; 140 } 141 142 146 public void test_sync_no_timing() 147 { 148 int result = server.operation (1, 0); 149 assertEquals (1, result); 150 151 result = server.operation (2, 10); 152 assertEquals (2, result); 153 154 result = server.operation (3, 100); 155 assertEquals (3, result); 156 157 try 158 { 159 char ch = server.ex_op('e', 50); 160 fail ("should have raised EmptyException"); 161 } 162 catch (EmptyException ex) 163 { 164 } 166 167 try 168 { 169 char ch = server.ex_op('$', 50); 170 fail ("should have raised DATA_CONVERSION"); 171 } 172 catch (EmptyException ex) 173 { 174 fail ("unexpected exception: " + ex); 175 } 176 catch (org.omg.CORBA.DATA_CONVERSION ex) 177 { 178 } 180 181 } 182 183 187 public void test_async_no_timing() 188 { 189 ReplyHandler handler = new ReplyHandler() 190 { 191 public void operation (int ami_return_val) 192 { 193 this.assertEquals (7, ami_return_val); 194 pass(); 195 } 196 }; 197 ((_TimingServerStub)server).sendc_operation (ref (handler), 7, 50); 198 handler.wait_for_reply (300); 199 200 handler = new ReplyHandler() 201 { 202 public void ex_op_excep (ExceptionHolder excep_holder) 203 { 204 this.assertEquals (EmptyException.class, 205 getException (excep_holder).getClass()); 206 pass(); 207 } 208 }; 209 ((_TimingServerStub)server).sendc_ex_op (ref (handler), 'e', 50); 210 handler.wait_for_reply (300); 211 212 handler = new ReplyHandler() 213 { 214 public void ex_op_excep (ExceptionHolder excep_holder) 215 { 216 this.assertEquals (org.omg.CORBA.DATA_CONVERSION .class, 217 getException (excep_holder).getClass()); 218 pass(); 219 } 220 }; 221 ((_TimingServerStub)server).sendc_ex_op (ref (handler), '$', 50); 222 handler.wait_for_reply (300); 223 } 224 225 228 public void test_all_policies_sync_ok() 229 { 230 clearPolicies (server); 231 setRequestStartTime (server, System.currentTimeMillis()); 232 setRequestEndTime (server, System.currentTimeMillis() + 200); 233 setRelativeRequestTimeout (server, System.currentTimeMillis() + 300); 234 setReplyStartTime (server, System.currentTimeMillis()); 235 setReplyEndTime (server, System.currentTimeMillis() + 400); 236 setRelativeRoundtripTimeout(server, System.currentTimeMillis() + 350); 237 238 try 239 { 240 int result = server.operation (434, 50); 241 } 242 catch (org.omg.CORBA.TIMEOUT t) 243 { 244 fail ("should not have been a TIMEOUT"); 245 } 246 } 247 248 249 253 public void test_request_start_time_sync_expired() 254 { 255 clearPolicies (server); 256 long start = System.currentTimeMillis(); 257 setRequestStartTime (server, start); 258 long result = server.server_time (10); 259 260 long delta = System.currentTimeMillis() - start; 261 if (delta > 200) 262 fail ("reply too late (" + delta + "ms)"); 263 } 264 265 269 public void test_request_start_time_sync_wait() 270 { 271 clearPolicies (server); 272 long start = System.currentTimeMillis(); 273 setRequestStartTime (server, start + 200); 274 long time = server.server_time (100); 275 276 long delta = time - start; 277 if (delta < 200) 278 fail ("request started too early (" + delta + "ms)"); 279 else if (delta > 250) 280 fail ("request started too late (" + delta + "ms)"); 281 } 282 283 287 public void test_request_end_time_sync_ok() 288 { 289 clearPolicies (server); 290 setRequestEndTime (server, System.currentTimeMillis() + 400); 291 try 292 { 293 int result = server.operation (434, 500); 294 } 295 catch (org.omg.CORBA.TIMEOUT t) 296 { 297 fail ("should not have been a TIMEOUT"); 298 } 299 } 300 301 305 public void test_request_end_time_sync_pre_expired() 306 { 307 clearPolicies (server); 308 setRequestEndTime (server, System.currentTimeMillis() - 200); 309 try 310 { 311 int result = server.operation (121, 50); 312 fail ("should have been a TIMEOUT"); 313 } 314 catch (org.omg.CORBA.TIMEOUT t) 315 { 316 } 318 } 319 320 323 public void test_request_end_time_async_pre_expired() 324 { 325 ReplyHandler handler = new ReplyHandler(); 326 327 clearPolicies (server); 328 setRequestEndTime (server, System.currentTimeMillis() - 5); 329 try 330 { 331 ((_TimingServerStub)server).sendc_operation (ref (handler), 765, 50); 332 fail ("should have been a TIMEOUT"); 333 } 334 catch (org.omg.CORBA.TIMEOUT e) 335 { 336 } 338 } 339 340 343 public void test_request_end_time_sync_expired() 344 { 345 clearPolicies (server); 346 setRequestEndTime (server, System.currentTimeMillis() + 2); 347 try 348 { 349 int result = server.operation (121, 50); 350 fail ("should have been a TIMEOUT"); 351 } 352 catch (org.omg.CORBA.TIMEOUT t) 353 { 354 } 356 } 357 358 359 363 public void test_request_timeout_sync_ok() 364 { 365 clearPolicies (server); 366 setRelativeRequestTimeout (server, 200); 367 try 368 { 369 int result = server.operation (434, 300); 370 } 371 catch (org.omg.CORBA.TIMEOUT t) 372 { 373 fail ("should not have been a TIMEOUT"); 374 } 375 } 376 377 380 public void test_request_timeout_sync_expired() 381 { 382 clearPolicies (server); 383 setRelativeRequestTimeout (server, 1); 384 try 385 { 386 int result = server.operation (121, 50); 387 fail ("should have been a TIMEOUT"); 388 } 389 catch (org.omg.CORBA.TIMEOUT t) 390 { 391 } 393 } 394 395 399 public void test_request_timeout_async_expired() 400 { 401 ReplyHandler handler = new ReplyHandler() 402 { 403 public void operation (int ami_return_val) 404 { 405 this.fail ("should have raised TIMEOUT"); 406 } 407 408 public void operation_excep (ExceptionHolder excep_holder) 409 { 410 this.assertEquals (org.omg.CORBA.TIMEOUT .class, 411 getException (excep_holder).getClass()); 412 pass(); 413 } 414 }; 415 416 clearPolicies (server); 417 setRelativeRequestTimeout (server, 1); 418 ((_TimingServerStub)server).sendc_operation (ref (handler), 767, 200); 419 handler.wait_for_reply (400); 420 } 421 422 426 public void test_reply_start_time_sync_expired() 427 { 428 clearPolicies (server); 429 long start = System.currentTimeMillis(); 430 setReplyStartTime (server, start); 431 int result = server.operation (18, 10); 432 433 long delta = System.currentTimeMillis() - start; 434 if (delta > 200) 435 fail ("reply too late (" + delta + "ms)"); 436 } 437 438 442 public void test_reply_start_time_sync_wait() 443 { 444 clearPolicies (server); 445 long start = System.currentTimeMillis(); 446 setReplyStartTime (server, start + 200); 447 int result = server.operation (18, 100); 448 assertEquals (18, result); 449 450 long delta = System.currentTimeMillis() - start; 451 if (delta < 200) 452 fail ("reply too early (" + delta + "ms)"); 453 else if (delta > 250) 454 fail ("reply too late (" + delta + "ms)"); 455 } 456 457 461 public void test_reply_start_time_async_expired() 462 { 463 ReplyHandler handler = new ReplyHandler() 464 { 465 public void operation (int ami_return_val) 466 { 467 this.assertEquals (19, ami_return_val); 468 pass(); 469 } 470 }; 471 472 clearPolicies (server); 473 long start = System.currentTimeMillis(); 474 setReplyStartTime (server, start); 475 ((_TimingServerStub)server).sendc_operation (ref (handler), 19, 50); 476 handler.wait_for_reply (100); 477 478 long delta = System.currentTimeMillis() - start; 479 if (delta > 150) 480 fail ("reply too late (" + delta + "ms)"); 481 } 482 483 487 public void test_reply_start_time_async_wait() 488 { 489 ReplyHandler handler = new ReplyHandler() 490 { 491 public void operation (int ami_return_val) 492 { 493 this.assertEquals (19, ami_return_val); 494 pass(); 495 } 496 }; 497 498 clearPolicies (server); 499 long start = System.currentTimeMillis(); 500 setReplyStartTime (server, start + 200); 501 ((_TimingServerStub)server).sendc_operation (ref (handler), 19, 100); 502 handler.wait_for_reply (250); 503 504 long delta = System.currentTimeMillis() - start; 505 if (delta < 200) 506 fail ("reply too early (" + delta + "ms)"); 507 } 508 509 513 public void test_reply_end_time_async_ok() 514 { 515 ReplyHandler handler = new ReplyHandler() 516 { 517 public void operation (int ami_return_val) 518 { 519 this.assertEquals (765, ami_return_val); 520 pass(); 521 } 522 }; 523 524 clearPolicies (server); 525 setReplyEndTime (server, System.currentTimeMillis() + 200); 526 ((_TimingServerStub)server).sendc_operation (ref (handler), 765, 50); 527 handler.wait_for_reply (150); 528 529 } 530 531 532 536 public void test_reply_end_time_async_expired() 537 { 538 ReplyHandler handler = new ReplyHandler() 539 { 540 public void operation (int ami_return_val) 541 { 542 this.fail ("should have raised TIMEOUT"); 543 } 544 545 public void operation_excep (ExceptionHolder excep_holder) 546 { 547 this.assertEquals (org.omg.CORBA.TIMEOUT .class, 548 getException (excep_holder).getClass()); 549 pass(); 550 } 551 }; 552 553 clearPolicies (server); 554 setReplyEndTime (server, System.currentTimeMillis() + 100); 555 ((_TimingServerStub)server).sendc_operation (ref (handler), 767, 200); 556 handler.wait_for_reply (400); 557 } 558 559 563 public void test_reply_end_time_sync_ok() 564 { 565 clearPolicies (server); 566 setReplyEndTime (server, System.currentTimeMillis() + 200); 567 try 568 { 569 int result = server.operation (434, 50); 570 } 571 catch (org.omg.CORBA.TIMEOUT t) 572 { 573 fail ("should not have been a TIMEOUT"); 574 } 575 } 576 577 580 public void test_reply_end_time_sync_pre_expired() 581 { 582 clearPolicies (server); 583 setReplyEndTime (server, System.currentTimeMillis() - 100); 584 try 585 { 586 int result = server.operation (44, 100); 587 fail ("should have raised TIMEOUT"); 588 } 589 catch (org.omg.CORBA.TIMEOUT t) 590 { 591 } 593 } 594 595 599 public void test_reply_end_time_sync_expired() 600 { 601 clearPolicies (server); 602 setReplyEndTime (server, System.currentTimeMillis() + 200); 603 try 604 { 605 int result = server.operation (343, 300); 606 fail ("should have raised TIMEOUT"); 607 } 608 catch (org.omg.CORBA.TIMEOUT t) 609 { 610 } 612 } 613 614 615 619 public void test_relative_roundtrip_sync_ok() 620 { 621 clearPolicies (server); 622 setRelativeRoundtripTimeout (server, 200); 623 try 624 { 625 int result = server.operation (434, 50); 626 } 627 catch (org.omg.CORBA.TIMEOUT t) 628 { 629 fail ("should not have been a TIMEOUT"); 630 } 631 } 632 633 637 public void test_relative_roundtrip_sync_expired() 638 { 639 clearPolicies (server); 640 setRelativeRoundtripTimeout (server, 200); 641 try 642 { 643 int result = server.operation (343, 300); 644 fail ("should have raised TIMEOUT"); 645 } 646 catch (org.omg.CORBA.TIMEOUT t) 647 { 648 } 650 } 651 652 656 public void test_relative_roundtrip_async_ok() 657 { 658 ReplyHandler handler = new ReplyHandler() 659 { 660 public void operation (int ami_return_val) 661 { 662 this.assertEquals (765, ami_return_val); 663 pass(); 664 } 665 }; 666 667 clearPolicies (server); 668 setRelativeRoundtripTimeout (server, 200); 669 ((_TimingServerStub)server).sendc_operation (ref (handler), 765, 50); 670 handler.wait_for_reply (150); 671 672 } 673 674 678 public void test_relative_roundtrip_async_expired() 679 { 680 ReplyHandler handler = new ReplyHandler() 681 { 682 public void operation (int ami_return_val) 683 { 684 this.fail ("should have raised TIMEOUT"); 685 } 686 687 public void operation_excep (ExceptionHolder excep_holder) 688 { 689 this.assertEquals (org.omg.CORBA.TIMEOUT .class, 690 getException (excep_holder).getClass()); 691 pass(); 692 } 693 }; 694 695 clearPolicies (server); 696 setRelativeRoundtripTimeout (server, 50); 697 ((_TimingServerStub)server).sendc_operation (ref (handler), 767, 100); 698 handler.wait_for_reply (200); 699 } 700 701 703 708 private void clearPolicies (TimingServer server) 709 { 710 server._set_policy_override (new Policy[]{}, SetOverrideType.SET_OVERRIDE); 711 } 712 713 private void setRequestStartTime (TimingServer server, long unixTime) 714 { 715 UtcT corbaTime = Time.corbaTime (unixTime); 716 717 org.omg.CORBA.ORB orb = setup.getClientOrb(); 718 org.omg.CORBA.Any a = orb.create_any(); 719 UtcTHelper.insert (a, corbaTime); 720 try 721 { 722 Policy p = 723 orb.create_policy (REQUEST_START_TIME_POLICY_TYPE.value, a); 724 server._set_policy_override (new Policy[]{ p }, 725 SetOverrideType.ADD_OVERRIDE); 726 } 727 catch (PolicyError e) 728 { 729 throw new RuntimeException ("policy error: " + e); 730 } 731 } 732 733 private void setRequestEndTime (TimingServer server, long unixTime) 734 { 735 UtcT corbaTime = Time.corbaTime (unixTime); 736 737 org.omg.CORBA.ORB orb = setup.getClientOrb(); 738 org.omg.CORBA.Any a = orb.create_any(); 739 UtcTHelper.insert (a, corbaTime); 740 try 741 { 742 Policy p = 743 orb.create_policy (REQUEST_END_TIME_POLICY_TYPE.value, a); 744 server._set_policy_override (new Policy[]{ p }, 745 SetOverrideType.ADD_OVERRIDE); 746 } 747 catch (PolicyError e) 748 { 749 throw new RuntimeException ("policy error: " + e); 750 } 751 } 752 753 private void setRelativeRequestTimeout (TimingServer server, 754 long millis) 755 { 756 org.omg.CORBA.ORB orb = setup.getClientOrb(); 757 org.omg.CORBA.Any a = orb.create_any(); 758 a.insert_ulonglong (millis * 10000); 759 try 760 { 761 Policy p = 762 orb.create_policy (RELATIVE_REQ_TIMEOUT_POLICY_TYPE.value, a); 763 server._set_policy_override (new Policy[]{ p }, 764 SetOverrideType.ADD_OVERRIDE); 765 } 766 catch (PolicyError e) 767 { 768 throw new RuntimeException ("policy error: " + e); 769 } 770 } 771 772 private void setReplyStartTime (TimingServer server, long unixTime) 773 { 774 UtcT corbaTime = Time.corbaTime (unixTime); 775 776 org.omg.CORBA.ORB orb = setup.getClientOrb(); 777 org.omg.CORBA.Any a = orb.create_any(); 778 UtcTHelper.insert (a, corbaTime); 779 try 780 { 781 Policy p = 782 orb.create_policy (REPLY_START_TIME_POLICY_TYPE.value, a); 783 server._set_policy_override (new Policy[]{ p }, 784 SetOverrideType.ADD_OVERRIDE); 785 } 786 catch (PolicyError e) 787 { 788 throw new RuntimeException ("policy error: " + e); 789 } 790 } 791 792 private void setReplyEndTime (TimingServer server, long unixTime) 793 { 794 UtcT corbaTime = Time.corbaTime (unixTime); 795 796 org.omg.CORBA.ORB orb = setup.getClientOrb(); 797 org.omg.CORBA.Any a = orb.create_any(); 798 UtcTHelper.insert (a, corbaTime); 799 try 800 { 801 Policy p = 802 orb.create_policy (REPLY_END_TIME_POLICY_TYPE.value, a); 803 server._set_policy_override (new Policy[]{ p }, 804 SetOverrideType.ADD_OVERRIDE); 805 } 806 catch (PolicyError e) 807 { 808 throw new RuntimeException ("policy error: " + e); 809 } 810 } 811 812 private void setRelativeRoundtripTimeout (TimingServer server, 813 long millis) 814 { 815 org.omg.CORBA.ORB orb = setup.getClientOrb(); 816 org.omg.CORBA.Any a = orb.create_any(); 817 a.insert_ulonglong (millis * 10000); 818 try 819 { 820 Policy p = 821 orb.create_policy (RELATIVE_RT_TIMEOUT_POLICY_TYPE.value, a); 822 server._set_policy_override (new Policy[]{ p }, 823 SetOverrideType.ADD_OVERRIDE); 824 } 825 catch (PolicyError e) 826 { 827 throw new RuntimeException ("policy error: " + e); 828 } 829 } 830 831 } 832 | Popular Tags |