1 4 package com.tctest; 5 6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 7 8 import com.tc.object.config.ConfigVisitor; 9 import com.tc.object.config.DSOClientConfigHelper; 10 import com.tc.object.config.TransparencyClassSpec; 11 import com.tc.simulator.app.ApplicationConfig; 12 import com.tc.simulator.listener.ListenerProvider; 13 import com.tc.util.Assert; 14 import com.tctest.runner.AbstractTransparentApp; 15 16 import java.lang.reflect.Field ; 17 import java.util.HashMap ; 18 19 public class RootReplacementTestApp extends AbstractTransparentApp { 20 21 private final SyncRoot syncRoot = new SyncRoot(); 22 23 private static Integer staticIntegerRoot; 24 private static int staticPrimitiveIntRoot; 25 private static SyncRoot staticSyncRoot; 26 27 private boolean primitiveBoolean = true; 28 29 private Integer integerRoot; 30 private int primitiveIntRoot; 31 32 private Integer nonReplaceableIntegerRoot = new Integer (15); 33 private int nonReplaceableIntRoot = 15; 34 35 private final int nonSharedPrimitiveInt = 45; 36 private final int sharedPrimitiveInt = 50; 37 38 private Integer nonSharedIntegerObject = new Integer (45); 39 private Integer sharedIntegerObject = new Integer (50); 40 41 private SyncRoot replaceableSyncRoot = new SyncRoot(5); 42 private SyncRoot nonReplaceableSyncRoot = new SyncRoot(15); 43 44 private final SyncRoot nonSharedSyncRoot = new SyncRoot(45); 45 private final SyncRoot sharedSyncRoot = new SyncRoot(50); 46 47 private Class classRoot; 48 49 private final CyclicBarrier barrier; 50 51 public RootReplacementTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 52 super(appId, cfg, listenerProvider); 53 barrier = new CyclicBarrier(getParticipantCount()); 54 } 55 56 public void run() { 57 try { 58 testBooleanChange(); 59 60 62 testStaticRootSetting(); 63 testMultipleClientRootSetting(); 64 testPrimitiveRootSetting(); 65 testLiteralRootSetting(); 66 testClassRootSetting(); 67 testObjectRootSetting(); testRootSettingThroughReflection(); 69 testNonReplaceableSetting(); 70 71 testPrimitiveIntIncrement(); 72 } catch (Throwable t) { 73 notifyError(t); 74 } 75 } 76 77 private void testClassRootSetting() throws Exception { 78 clear(); 79 80 int index = -1; 81 synchronized (syncRoot) { 82 index = syncRoot.getIndex(); 83 syncRoot.setIndex(index + 1); 84 } 85 86 barrier.barrier(); 87 88 if (index == 0) { 89 classRoot = SyncRoot.class; 90 } 91 92 barrier.barrier(); 93 94 Object o = classRoot.newInstance(); 95 96 Assert.eval(o instanceof SyncRoot); 97 98 barrier.barrier(); 99 100 if (index == 1) { 101 classRoot = HashMap .class; 102 } 103 104 barrier.barrier(); 105 106 o = classRoot.newInstance(); 107 108 Assert.eval(o instanceof HashMap ); 109 110 barrier.barrier(); 111 } 112 113 private void testPrimitiveIntIncrement() throws Exception { 114 clear(); 115 116 int index = -1; 117 synchronized (syncRoot) { 118 index = syncRoot.getIndex(); 119 syncRoot.setIndex(index + 1); 120 } 121 122 barrier.barrier(); 123 124 if (index == 0) { 125 int NUM_OF_COUNT = 5000; 126 long startTime = System.currentTimeMillis(); 127 int i=0; 128 while (i < NUM_OF_COUNT) { 129 i++; 130 } 131 long endTime = System.currentTimeMillis(); 132 133 System.err.println("Elapsed time for non-shared primitive int: " + (endTime-startTime) + "ms"); 134 135 startTime = System.currentTimeMillis(); 136 primitiveIntRoot = 0; 137 i = 0; 138 while (primitiveIntRoot < NUM_OF_COUNT) { 139 Assert.assertEquals(i, primitiveIntRoot); 140 primitiveIntRoot++; 141 i++; 142 } 143 endTime = System.currentTimeMillis(); 144 145 System.err.println("Elapsed time for replaceable int root: " + (endTime-startTime) + "ms"); 146 } 147 } 148 149 private void testBooleanChange() throws Exception { 150 clear(); 151 152 int index = -1; 153 synchronized (syncRoot) { 154 index = syncRoot.getIndex(); 155 syncRoot.setIndex(index + 1); 156 } 157 158 barrier.barrier(); 159 160 if (index == 0) { 161 primitiveBoolean = false; 162 } 163 164 barrier.barrier(); 165 Assert.assertEquals(false, primitiveBoolean); 166 barrier.barrier(); 167 } 168 169 private void testMultipleClientRootSetting() throws Exception { 170 clear(); 171 int index = -1; 172 synchronized (syncRoot) { 173 index = syncRoot.getIndex(); 174 syncRoot.setIndex(index + 1); 175 } 176 177 barrier.barrier(); 178 179 Assert.assertEquals(0, primitiveIntRoot); 180 181 barrier.barrier(); 182 183 if (index == 0) { 184 primitiveIntRoot = 10; 185 } 186 187 barrier.barrier(); 188 189 if (index == 1) { 190 primitiveIntRoot = 20; 191 } 192 193 barrier.barrier(); 194 195 Assert.assertEquals(20, primitiveIntRoot); 196 197 barrier.barrier(); 198 } 199 200 private void testStaticRootSetting() throws Exception { 201 clear(); 202 int index = -1; 203 synchronized (syncRoot) { 204 index = syncRoot.getIndex(); 205 syncRoot.setIndex(index + 1); 206 } 207 208 barrier.barrier(); 209 210 if (index == 0) { 211 staticPrimitiveIntRoot = 10; 212 } 213 214 barrier.barrier(); 215 216 Assert.assertEquals(10, staticPrimitiveIntRoot); 217 218 barrier.barrier(); 219 220 if (index == 1) { 221 staticPrimitiveIntRoot = 20; 222 } 223 224 barrier.barrier(); 225 226 Assert.assertEquals(20, staticPrimitiveIntRoot); 227 228 barrier.barrier(); 229 230 if (index == 0) { 231 staticIntegerRoot = new Integer (10); 232 } 233 234 barrier.barrier(); 235 236 Assert.assertEquals(new Integer (10), staticIntegerRoot); 237 238 barrier.barrier(); 239 240 if (index == 1) { 241 staticIntegerRoot = new Integer (20); 242 } 243 244 barrier.barrier(); 245 246 Assert.assertEquals(new Integer (20), staticIntegerRoot); 247 248 barrier.barrier(); 249 250 if (index == 0) { 251 staticSyncRoot = new SyncRoot(10); 252 } 253 254 barrier.barrier(); 255 256 Assert.assertEquals(10, staticSyncRoot.getValue()); 257 258 barrier.barrier(); 259 260 if (index == 1) { 261 staticSyncRoot = new SyncRoot(20); 262 } 263 264 barrier.barrier(); 265 266 Assert.assertEquals(20, staticSyncRoot.getValue()); 267 268 barrier.barrier(); 269 } 270 271 private void testRootSettingThroughReflection() throws Exception { 272 clear(); 273 int index = -1; 274 synchronized (syncRoot) { 275 index = syncRoot.getIndex(); 276 syncRoot.setIndex(index + 1); 277 } 278 279 barrier.barrier(); 280 281 Field primitiveField = this.getClass().getDeclaredField("primitiveIntRoot"); 282 primitiveField.setAccessible(true); 283 284 Field integerField = this.getClass().getDeclaredField("integerRoot"); 285 integerField.setAccessible(true); 286 287 Field objField = this.getClass().getDeclaredField("replaceableSyncRoot"); 288 objField.setAccessible(true); 289 290 if (index == 0) { 291 primitiveField.setInt(this, 11); 292 } 293 294 barrier.barrier(); 295 296 Assert.assertEquals(11, primitiveIntRoot); 297 298 barrier.barrier(); 299 300 if (index == 1) { 301 primitiveField.setInt(this, 13); 302 } 303 304 barrier.barrier(); 305 306 Assert.assertEquals(13, primitiveIntRoot); 307 308 barrier.barrier(); 309 310 if (index == 0) { 311 integerField.set(this, new Integer (11)); 312 } 313 314 barrier.barrier(); 315 316 Assert.assertEquals(new Integer (11), integerRoot); 317 318 barrier.barrier(); 319 320 if (index == 1) { 321 integerField.set(this, new Integer (13)); 322 } 323 324 barrier.barrier(); 325 326 Assert.assertEquals(new Integer (13), integerRoot); 327 328 barrier.barrier(); 329 330 if (index == 0) { 331 objField.set(this, new SyncRoot(11)); 332 } 333 334 barrier.barrier(); 335 336 Assert.assertEquals(11, replaceableSyncRoot.getValue()); 337 338 barrier.barrier(); 339 340 if (index == 1) { 341 objField.set(this, new SyncRoot(13)); 342 } 343 344 barrier.barrier(); 345 346 Assert.assertEquals(13, replaceableSyncRoot.getValue()); 347 348 barrier.barrier(); 349 350 if (index == 2) { 351 objField.set(this, sharedSyncRoot); 352 } 353 354 barrier.barrier(); 355 356 Assert.assertEquals(50, replaceableSyncRoot.getValue()); 357 358 barrier.barrier(); 359 360 sharedSyncRoot.setValue(53); 361 362 Assert.assertEquals(53, replaceableSyncRoot.getValue()); 363 364 barrier.barrier(); 365 } 366 367 private void testNonReplaceableSetting() throws Exception { 368 clear(); 369 int index = -1; 370 synchronized (syncRoot) { 371 index = syncRoot.getIndex(); 372 syncRoot.setIndex(index + 1); 373 } 374 375 barrier.barrier(); 376 377 if (index == 0) { 378 nonReplaceableIntRoot = 20; 379 } 380 381 barrier.barrier(); 382 383 Assert.assertEquals(15, nonReplaceableIntRoot); 384 385 barrier.barrier(); 386 387 if (index == 0) { 388 nonReplaceableIntegerRoot = new Integer (20); 389 } 390 391 barrier.barrier(); 392 393 Assert.assertEquals(new Integer (15), nonReplaceableIntegerRoot); 394 395 barrier.barrier(); 396 397 if (index == 0) { 398 nonReplaceableSyncRoot = new SyncRoot(20); 399 } 400 401 barrier.barrier(); 402 403 Assert.assertEquals(15, nonReplaceableSyncRoot.getValue()); 404 405 barrier.barrier(); 406 } 407 408 private void testObjectRootSetting() throws Exception { 409 clear(); 410 int index = -1; 411 synchronized (syncRoot) { 412 index = syncRoot.getIndex(); 413 syncRoot.setIndex(index + 1); 414 } 415 416 barrier.barrier(); 417 418 if (index == 0) { 419 replaceableSyncRoot = new SyncRoot(10); 420 } 421 422 barrier.barrier(); 423 424 Assert.assertEquals(10, replaceableSyncRoot.getValue()); 425 426 barrier.barrier(); 427 428 if (index == 0) { 429 replaceableSyncRoot = sharedSyncRoot; 430 } 431 432 barrier.barrier(); 433 434 Assert.assertEquals(50, replaceableSyncRoot.getValue()); 435 436 barrier.barrier(); 437 438 if (index == 0) { 439 replaceableSyncRoot.setValue(51); 440 } 441 442 barrier.barrier(); 443 444 Assert.assertEquals(51, sharedSyncRoot.getValue()); 445 446 barrier.barrier(); 447 448 if (index == 0) { 449 replaceableSyncRoot = nonSharedSyncRoot; 450 } 451 452 barrier.barrier(); 453 454 if (index == 0) { 455 sharedSyncRoot.setValue(52); 456 } 457 458 barrier.barrier(); 459 460 Assert.assertEquals(52, sharedSyncRoot.getValue()); 461 Assert.assertEquals(45, replaceableSyncRoot.getValue()); 462 463 barrier.barrier(); 464 465 if (index == 0) { 466 nonSharedSyncRoot.setValue(47); 467 } 468 469 barrier.barrier(); 470 471 Assert.assertEquals(47, replaceableSyncRoot.getValue()); 472 473 barrier.barrier(); 474 475 if (index == 0) { sharedSyncRoot.setValue(50); 477 nonSharedSyncRoot.setValue(45); 478 } 479 480 barrier.barrier(); 481 } 482 483 private void testLiteralRootSetting() throws Exception { 484 clear(); 485 486 int index = -1; 487 synchronized (syncRoot) { 488 index = syncRoot.getIndex(); 489 syncRoot.setIndex(index + 1); 490 } 491 492 barrier.barrier(); 493 494 if (index == 0) { 495 integerRoot = new Integer (10); 496 } 497 498 barrier.barrier(); 499 500 Assert.assertEquals(new Integer (10), integerRoot); 501 502 barrier.barrier(); 503 504 if (index == 0) { 505 integerRoot = sharedIntegerObject; 506 } 507 508 barrier.barrier(); 509 510 Assert.assertEquals(new Integer (50), integerRoot); 511 512 barrier.barrier(); 513 514 if (index == 0) { 515 integerRoot = nonSharedIntegerObject; 516 } 517 518 barrier.barrier(); 519 520 Assert.assertEquals(new Integer (45), integerRoot); 521 522 barrier.barrier(); 523 } 524 525 private void testPrimitiveRootSetting() throws Exception { 526 clear(); 527 528 int index = -1; 529 synchronized (syncRoot) { 530 index = syncRoot.getIndex(); 531 syncRoot.setIndex(index + 1); 532 } 533 534 barrier.barrier(); 535 536 if (index == 0) { 537 primitiveIntRoot = 10; 538 } 539 540 barrier.barrier(); 541 542 Assert.assertEquals(10, primitiveIntRoot); 543 544 barrier.barrier(); 545 546 if (index == 0) { 547 primitiveIntRoot = sharedPrimitiveInt; 548 } 549 550 barrier.barrier(); 551 552 Assert.assertEquals(50, primitiveIntRoot); 553 554 barrier.barrier(); 555 556 if (index == 0) { 557 primitiveIntRoot = nonSharedPrimitiveInt; 558 } 559 560 barrier.barrier(); 561 562 Assert.assertEquals(45, primitiveIntRoot); 563 564 barrier.barrier(); 565 566 if (index == 0) { 567 primitiveIntRoot++; 568 } 569 570 barrier.barrier(); 571 572 Assert.assertEquals(46, primitiveIntRoot); 573 574 barrier.barrier(); 575 576 if (index == 0) { 577 primitiveIntRoot += 3; 578 } 579 580 barrier.barrier(); 581 582 Assert.assertEquals(49, primitiveIntRoot); 583 584 barrier.barrier(); 585 586 if (index == 0) { 587 primitiveIntRoot -= 2; 588 } 589 590 barrier.barrier(); 591 592 Assert.assertEquals(47, primitiveIntRoot); 593 594 barrier.barrier(); 595 596 if (index == 0) { 597 primitiveIntRoot--; 598 } 599 600 barrier.barrier(); 601 602 Assert.assertEquals(46, primitiveIntRoot); 603 604 barrier.barrier(); 605 } 606 607 private void clear() throws Exception { 608 synchronized (syncRoot) { 609 syncRoot.clear(); 610 } 611 612 barrier.barrier(); 613 } 614 615 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 616 TransparencyClassSpec spec = config.getOrCreateSpec(CyclicBarrier.class.getName()); 617 config.addWriteAutolock("* " + CyclicBarrier.class.getName() + "*.*(..)"); 618 619 String testClass = RootReplacementTestApp.class.getName(); 620 spec = config.getOrCreateSpec(testClass); 621 622 config.addIncludePattern(testClass + "$*"); 623 624 String methodExpression = "* " + testClass + "*.*(..)"; 625 config.addWriteAutolock(methodExpression); 626 627 spec.addRoot("primitiveBoolean", "primitiveBoolean"); 628 629 spec.addRoot("primitiveIntRoot", "primitiveIntRoot"); 630 spec.addRoot("integerRoot", "integerRoot", false); 631 spec.addRoot("nonReplaceableIntRoot", "nonReplaceableIntRoot", true); 632 spec.addRoot("nonReplaceableIntegerRoot", "nonReplaceableIntegerRoot"); 633 634 spec.addRoot("sharedIntegerObject", "sharedIntegerObject"); 635 spec.addRoot("sharedPrimitiveInt", "sharedPrimitiveInt", true); 636 637 spec.addRoot("replaceableSyncRoot", "replaceableSyncRoot", false); 638 spec.addRoot("nonReplaceableSyncRoot", "nonReplaceableSyncRoot"); 639 spec.addRoot("sharedSyncRoot", "sharedSyncRoot"); 640 641 spec.addRoot("classRoot", "classRoot", false); 642 643 spec.addRoot("staticIntegerRoot", "staticIntegerRoot", false); 644 spec.addRoot("staticPrimitiveIntRoot", "staticPrimitiveIntRoot"); 645 spec.addRoot("staticSyncRoot", "staticSyncRoot", false); 646 647 spec.addRoot("syncRoot", "syncRoot"); 648 649 spec.addRoot("barrier", "barrier"); 650 } 651 652 private static class SyncRoot { 653 private int index = 0; 654 private int value = 0; 655 private Object obj; 656 657 public SyncRoot() { 658 super(); 659 } 660 661 public SyncRoot(int value) { 662 this.value = value; 663 } 664 665 public SyncRoot(Object o) { 666 this.obj = o; 667 } 668 669 public int getIndex() { 670 return index; 671 } 672 673 public void setIndex(int index) { 674 this.index = index; 675 } 676 677 public synchronized int getValue() { 678 return value; 679 } 680 681 public synchronized void setValue(int value) { 682 this.value = value; 683 } 684 685 public synchronized Object getObj() { 686 return obj; 687 } 688 689 public synchronized void setObj(Object obj) { 690 this.obj = obj; 691 } 692 693 public boolean isSameReferencedObject(Object o) { 694 return this.obj == o; 695 } 696 697 public void clear() { 698 this.index = 0; 699 this.value = 0; 700 this.obj = null; 701 } 702 } 703 704 } 705 | Popular Tags |