001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest;
005:
006: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
007:
008: import com.tc.object.config.ConfigVisitor;
009: import com.tc.object.config.DSOClientConfigHelper;
010: import com.tc.object.config.TransparencyClassSpec;
011: import com.tc.simulator.app.ApplicationConfig;
012: import com.tc.simulator.listener.ListenerProvider;
013: import com.tc.util.Assert;
014: import com.tctest.runner.AbstractTransparentApp;
015:
016: import java.lang.reflect.Field;
017: import java.util.HashMap;
018:
019: public class RootReplacementTestApp extends AbstractTransparentApp {
020:
021: private final SyncRoot syncRoot = new SyncRoot();
022:
023: private static Integer staticIntegerRoot;
024: private static int staticPrimitiveIntRoot;
025: private static SyncRoot staticSyncRoot;
026:
027: private boolean primitiveBoolean = true;
028:
029: private Integer integerRoot;
030: private int primitiveIntRoot;
031:
032: private Integer nonReplaceableIntegerRoot = new Integer(15);
033: private int nonReplaceableIntRoot = 15;
034:
035: private final int nonSharedPrimitiveInt = 45;
036: private final int sharedPrimitiveInt = 50;
037:
038: private Integer nonSharedIntegerObject = new Integer(45);
039: private Integer sharedIntegerObject = new Integer(50);
040:
041: private SyncRoot replaceableSyncRoot = new SyncRoot(5);
042: private SyncRoot nonReplaceableSyncRoot = new SyncRoot(15);
043:
044: private final SyncRoot nonSharedSyncRoot = new SyncRoot(45);
045: private final SyncRoot sharedSyncRoot = new SyncRoot(50);
046:
047: private Class classRoot;
048:
049: private final CyclicBarrier barrier;
050:
051: public RootReplacementTestApp(String appId, ApplicationConfig cfg,
052: ListenerProvider listenerProvider) {
053: super (appId, cfg, listenerProvider);
054: barrier = new CyclicBarrier(getParticipantCount());
055: }
056:
057: public void run() {
058: try {
059: testBooleanChange();
060:
061: // testRootCreateOrReplace has been moved to the DsoFinalMethodTest.
062:
063: testStaticRootSetting();
064: testMultipleClientRootSetting();
065: testPrimitiveRootSetting();
066: testLiteralRootSetting();
067: testClassRootSetting();
068: testObjectRootSetting(); // after this test, nonSharedSyncRoot will become shared.
069: testRootSettingThroughReflection();
070: testNonReplaceableSetting();
071:
072: testPrimitiveIntIncrement();
073: } catch (Throwable t) {
074: notifyError(t);
075: }
076: }
077:
078: private void testClassRootSetting() throws Exception {
079: clear();
080:
081: int index = -1;
082: synchronized (syncRoot) {
083: index = syncRoot.getIndex();
084: syncRoot.setIndex(index + 1);
085: }
086:
087: barrier.barrier();
088:
089: if (index == 0) {
090: classRoot = SyncRoot.class;
091: }
092:
093: barrier.barrier();
094:
095: Object o = classRoot.newInstance();
096:
097: Assert.eval(o instanceof SyncRoot);
098:
099: barrier.barrier();
100:
101: if (index == 1) {
102: classRoot = HashMap.class;
103: }
104:
105: barrier.barrier();
106:
107: o = classRoot.newInstance();
108:
109: Assert.eval(o instanceof HashMap);
110:
111: barrier.barrier();
112: }
113:
114: private void testPrimitiveIntIncrement() throws Exception {
115: clear();
116:
117: int index = -1;
118: synchronized (syncRoot) {
119: index = syncRoot.getIndex();
120: syncRoot.setIndex(index + 1);
121: }
122:
123: barrier.barrier();
124:
125: if (index == 0) {
126: int NUM_OF_COUNT = 5000;
127: long startTime = System.currentTimeMillis();
128: int i = 0;
129: while (i < NUM_OF_COUNT) {
130: i++;
131: }
132: long endTime = System.currentTimeMillis();
133:
134: System.err
135: .println("Elapsed time for non-shared primitive int: "
136: + (endTime - startTime) + "ms");
137:
138: startTime = System.currentTimeMillis();
139: primitiveIntRoot = 0;
140: i = 0;
141: while (primitiveIntRoot < NUM_OF_COUNT) {
142: Assert.assertEquals(i, primitiveIntRoot);
143: primitiveIntRoot++;
144: i++;
145: }
146: endTime = System.currentTimeMillis();
147:
148: System.err
149: .println("Elapsed time for replaceable int root: "
150: + (endTime - startTime) + "ms");
151: }
152: }
153:
154: private void testBooleanChange() throws Exception {
155: clear();
156:
157: int index = -1;
158: synchronized (syncRoot) {
159: index = syncRoot.getIndex();
160: syncRoot.setIndex(index + 1);
161: }
162:
163: barrier.barrier();
164:
165: if (index == 0) {
166: primitiveBoolean = false;
167: }
168:
169: barrier.barrier();
170: Assert.assertEquals(false, primitiveBoolean);
171: barrier.barrier();
172: }
173:
174: private void testMultipleClientRootSetting() throws Exception {
175: clear();
176: int index = -1;
177: synchronized (syncRoot) {
178: index = syncRoot.getIndex();
179: syncRoot.setIndex(index + 1);
180: }
181:
182: barrier.barrier();
183:
184: Assert.assertEquals(0, primitiveIntRoot);
185:
186: barrier.barrier();
187:
188: if (index == 0) {
189: primitiveIntRoot = 10;
190: }
191:
192: barrier.barrier();
193:
194: if (index == 1) {
195: primitiveIntRoot = 20;
196: }
197:
198: barrier.barrier();
199:
200: Assert.assertEquals(20, primitiveIntRoot);
201:
202: barrier.barrier();
203: }
204:
205: private void testStaticRootSetting() throws Exception {
206: clear();
207: int index = -1;
208: synchronized (syncRoot) {
209: index = syncRoot.getIndex();
210: syncRoot.setIndex(index + 1);
211: }
212:
213: barrier.barrier();
214:
215: if (index == 0) {
216: staticPrimitiveIntRoot = 10;
217: }
218:
219: barrier.barrier();
220:
221: Assert.assertEquals(10, staticPrimitiveIntRoot);
222:
223: barrier.barrier();
224:
225: if (index == 1) {
226: staticPrimitiveIntRoot = 20;
227: }
228:
229: barrier.barrier();
230:
231: Assert.assertEquals(20, staticPrimitiveIntRoot);
232:
233: barrier.barrier();
234:
235: if (index == 0) {
236: staticIntegerRoot = new Integer(10);
237: }
238:
239: barrier.barrier();
240:
241: Assert.assertEquals(new Integer(10), staticIntegerRoot);
242:
243: barrier.barrier();
244:
245: if (index == 1) {
246: staticIntegerRoot = new Integer(20);
247: }
248:
249: barrier.barrier();
250:
251: Assert.assertEquals(new Integer(20), staticIntegerRoot);
252:
253: barrier.barrier();
254:
255: if (index == 0) {
256: staticSyncRoot = new SyncRoot(10);
257: }
258:
259: barrier.barrier();
260:
261: Assert.assertEquals(10, staticSyncRoot.getValue());
262:
263: barrier.barrier();
264:
265: if (index == 1) {
266: staticSyncRoot = new SyncRoot(20);
267: }
268:
269: barrier.barrier();
270:
271: Assert.assertEquals(20, staticSyncRoot.getValue());
272:
273: barrier.barrier();
274: }
275:
276: private void testRootSettingThroughReflection() throws Exception {
277: clear();
278: int index = -1;
279: synchronized (syncRoot) {
280: index = syncRoot.getIndex();
281: syncRoot.setIndex(index + 1);
282: }
283:
284: barrier.barrier();
285:
286: Field primitiveField = this .getClass().getDeclaredField(
287: "primitiveIntRoot");
288: primitiveField.setAccessible(true);
289:
290: Field integerField = this .getClass().getDeclaredField(
291: "integerRoot");
292: integerField.setAccessible(true);
293:
294: Field objField = this .getClass().getDeclaredField(
295: "replaceableSyncRoot");
296: objField.setAccessible(true);
297:
298: if (index == 0) {
299: primitiveField.setInt(this , 11);
300: }
301:
302: barrier.barrier();
303:
304: Assert.assertEquals(11, primitiveIntRoot);
305:
306: barrier.barrier();
307:
308: if (index == 1) {
309: primitiveField.setInt(this , 13);
310: }
311:
312: barrier.barrier();
313:
314: Assert.assertEquals(13, primitiveIntRoot);
315:
316: barrier.barrier();
317:
318: if (index == 0) {
319: integerField.set(this , new Integer(11));
320: }
321:
322: barrier.barrier();
323:
324: Assert.assertEquals(new Integer(11), integerRoot);
325:
326: barrier.barrier();
327:
328: if (index == 1) {
329: integerField.set(this , new Integer(13));
330: }
331:
332: barrier.barrier();
333:
334: Assert.assertEquals(new Integer(13), integerRoot);
335:
336: barrier.barrier();
337:
338: if (index == 0) {
339: objField.set(this , new SyncRoot(11));
340: }
341:
342: barrier.barrier();
343:
344: Assert.assertEquals(11, replaceableSyncRoot.getValue());
345:
346: barrier.barrier();
347:
348: if (index == 1) {
349: objField.set(this , new SyncRoot(13));
350: }
351:
352: barrier.barrier();
353:
354: Assert.assertEquals(13, replaceableSyncRoot.getValue());
355:
356: barrier.barrier();
357:
358: if (index == 2) {
359: objField.set(this , sharedSyncRoot);
360: }
361:
362: barrier.barrier();
363:
364: Assert.assertEquals(50, replaceableSyncRoot.getValue());
365:
366: barrier.barrier();
367:
368: sharedSyncRoot.setValue(53);
369:
370: Assert.assertEquals(53, replaceableSyncRoot.getValue());
371:
372: barrier.barrier();
373: }
374:
375: private void testNonReplaceableSetting() throws Exception {
376: clear();
377: int index = -1;
378: synchronized (syncRoot) {
379: index = syncRoot.getIndex();
380: syncRoot.setIndex(index + 1);
381: }
382:
383: barrier.barrier();
384:
385: if (index == 0) {
386: nonReplaceableIntRoot = 20;
387: }
388:
389: barrier.barrier();
390:
391: Assert.assertEquals(15, nonReplaceableIntRoot);
392:
393: barrier.barrier();
394:
395: if (index == 0) {
396: nonReplaceableIntegerRoot = new Integer(20);
397: }
398:
399: barrier.barrier();
400:
401: Assert.assertEquals(new Integer(15), nonReplaceableIntegerRoot);
402:
403: barrier.barrier();
404:
405: if (index == 0) {
406: nonReplaceableSyncRoot = new SyncRoot(20);
407: }
408:
409: barrier.barrier();
410:
411: Assert.assertEquals(15, nonReplaceableSyncRoot.getValue());
412:
413: barrier.barrier();
414: }
415:
416: private void testObjectRootSetting() throws Exception {
417: clear();
418: int index = -1;
419: synchronized (syncRoot) {
420: index = syncRoot.getIndex();
421: syncRoot.setIndex(index + 1);
422: }
423:
424: barrier.barrier();
425:
426: if (index == 0) {
427: replaceableSyncRoot = new SyncRoot(10);
428: }
429:
430: barrier.barrier();
431:
432: Assert.assertEquals(10, replaceableSyncRoot.getValue());
433:
434: barrier.barrier();
435:
436: if (index == 0) {
437: replaceableSyncRoot = sharedSyncRoot;
438: }
439:
440: barrier.barrier();
441:
442: Assert.assertEquals(50, replaceableSyncRoot.getValue());
443:
444: barrier.barrier();
445:
446: if (index == 0) {
447: replaceableSyncRoot.setValue(51);
448: }
449:
450: barrier.barrier();
451:
452: Assert.assertEquals(51, sharedSyncRoot.getValue());
453:
454: barrier.barrier();
455:
456: if (index == 0) {
457: replaceableSyncRoot = nonSharedSyncRoot;
458: }
459:
460: barrier.barrier();
461:
462: if (index == 0) {
463: sharedSyncRoot.setValue(52);
464: }
465:
466: barrier.barrier();
467:
468: Assert.assertEquals(52, sharedSyncRoot.getValue());
469: Assert.assertEquals(45, replaceableSyncRoot.getValue());
470:
471: barrier.barrier();
472:
473: if (index == 0) {
474: nonSharedSyncRoot.setValue(47);
475: }
476:
477: barrier.barrier();
478:
479: Assert.assertEquals(47, replaceableSyncRoot.getValue());
480:
481: barrier.barrier();
482:
483: if (index == 0) { // reset the value of sharedSyncRoot and nonSharedSyncRoot.
484: sharedSyncRoot.setValue(50);
485: nonSharedSyncRoot.setValue(45);
486: }
487:
488: barrier.barrier();
489: }
490:
491: private void testLiteralRootSetting() throws Exception {
492: clear();
493:
494: int index = -1;
495: synchronized (syncRoot) {
496: index = syncRoot.getIndex();
497: syncRoot.setIndex(index + 1);
498: }
499:
500: barrier.barrier();
501:
502: if (index == 0) {
503: integerRoot = new Integer(10);
504: }
505:
506: barrier.barrier();
507:
508: Assert.assertEquals(new Integer(10), integerRoot);
509:
510: barrier.barrier();
511:
512: if (index == 0) {
513: integerRoot = sharedIntegerObject;
514: }
515:
516: barrier.barrier();
517:
518: Assert.assertEquals(new Integer(50), integerRoot);
519:
520: barrier.barrier();
521:
522: if (index == 0) {
523: integerRoot = nonSharedIntegerObject;
524: }
525:
526: barrier.barrier();
527:
528: Assert.assertEquals(new Integer(45), integerRoot);
529:
530: barrier.barrier();
531: }
532:
533: private void testPrimitiveRootSetting() throws Exception {
534: clear();
535:
536: int index = -1;
537: synchronized (syncRoot) {
538: index = syncRoot.getIndex();
539: syncRoot.setIndex(index + 1);
540: }
541:
542: barrier.barrier();
543:
544: if (index == 0) {
545: primitiveIntRoot = 10;
546: }
547:
548: barrier.barrier();
549:
550: Assert.assertEquals(10, primitiveIntRoot);
551:
552: barrier.barrier();
553:
554: if (index == 0) {
555: primitiveIntRoot = sharedPrimitiveInt;
556: }
557:
558: barrier.barrier();
559:
560: Assert.assertEquals(50, primitiveIntRoot);
561:
562: barrier.barrier();
563:
564: if (index == 0) {
565: primitiveIntRoot = nonSharedPrimitiveInt;
566: }
567:
568: barrier.barrier();
569:
570: Assert.assertEquals(45, primitiveIntRoot);
571:
572: barrier.barrier();
573:
574: if (index == 0) {
575: primitiveIntRoot++;
576: }
577:
578: barrier.barrier();
579:
580: Assert.assertEquals(46, primitiveIntRoot);
581:
582: barrier.barrier();
583:
584: if (index == 0) {
585: primitiveIntRoot += 3;
586: }
587:
588: barrier.barrier();
589:
590: Assert.assertEquals(49, primitiveIntRoot);
591:
592: barrier.barrier();
593:
594: if (index == 0) {
595: primitiveIntRoot -= 2;
596: }
597:
598: barrier.barrier();
599:
600: Assert.assertEquals(47, primitiveIntRoot);
601:
602: barrier.barrier();
603:
604: if (index == 0) {
605: primitiveIntRoot--;
606: }
607:
608: barrier.barrier();
609:
610: Assert.assertEquals(46, primitiveIntRoot);
611:
612: barrier.barrier();
613: }
614:
615: private void clear() throws Exception {
616: synchronized (syncRoot) {
617: syncRoot.clear();
618: }
619:
620: barrier.barrier();
621: }
622:
623: public static void visitL1DSOConfig(ConfigVisitor visitor,
624: DSOClientConfigHelper config) {
625: TransparencyClassSpec spec = config
626: .getOrCreateSpec(CyclicBarrier.class.getName());
627: config.addWriteAutolock("* " + CyclicBarrier.class.getName()
628: + "*.*(..)");
629:
630: String testClass = RootReplacementTestApp.class.getName();
631: spec = config.getOrCreateSpec(testClass);
632:
633: config.addIncludePattern(testClass + "$*");
634:
635: String methodExpression = "* " + testClass + "*.*(..)";
636: config.addWriteAutolock(methodExpression);
637:
638: spec.addRoot("primitiveBoolean", "primitiveBoolean");
639:
640: spec.addRoot("primitiveIntRoot", "primitiveIntRoot");
641: spec.addRoot("integerRoot", "integerRoot", false);
642: spec.addRoot("nonReplaceableIntRoot", "nonReplaceableIntRoot",
643: true);
644: spec.addRoot("nonReplaceableIntegerRoot",
645: "nonReplaceableIntegerRoot");
646:
647: spec.addRoot("sharedIntegerObject", "sharedIntegerObject");
648: spec.addRoot("sharedPrimitiveInt", "sharedPrimitiveInt", true);
649:
650: spec.addRoot("replaceableSyncRoot", "replaceableSyncRoot",
651: false);
652: spec
653: .addRoot("nonReplaceableSyncRoot",
654: "nonReplaceableSyncRoot");
655: spec.addRoot("sharedSyncRoot", "sharedSyncRoot");
656:
657: spec.addRoot("classRoot", "classRoot", false);
658:
659: spec.addRoot("staticIntegerRoot", "staticIntegerRoot", false);
660: spec
661: .addRoot("staticPrimitiveIntRoot",
662: "staticPrimitiveIntRoot");
663: spec.addRoot("staticSyncRoot", "staticSyncRoot", false);
664:
665: spec.addRoot("syncRoot", "syncRoot");
666:
667: spec.addRoot("barrier", "barrier");
668: }
669:
670: private static class SyncRoot {
671: private int index = 0;
672: private int value = 0;
673: private Object obj;
674:
675: public SyncRoot() {
676: super ();
677: }
678:
679: public SyncRoot(int value) {
680: this .value = value;
681: }
682:
683: public SyncRoot(Object o) {
684: this .obj = o;
685: }
686:
687: public int getIndex() {
688: return index;
689: }
690:
691: public void setIndex(int index) {
692: this .index = index;
693: }
694:
695: public synchronized int getValue() {
696: return value;
697: }
698:
699: public synchronized void setValue(int value) {
700: this .value = value;
701: }
702:
703: public synchronized Object getObj() {
704: return obj;
705: }
706:
707: public synchronized void setObj(Object obj) {
708: this .obj = obj;
709: }
710:
711: public boolean isSameReferencedObject(Object o) {
712: return this .obj == o;
713: }
714:
715: public void clear() {
716: this .index = 0;
717: this .value = 0;
718: this.obj = null;
719: }
720: }
721:
722: }
|