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.util.Comparator ; 17 import java.util.TreeMap ; 18 19 public class TreeMapBuggyComparatorTestApp extends AbstractTransparentApp { 20 21 private final TreeMap map = new TreeMap (new BuggyComparator()); 22 private final CyclicBarrier barrier; 23 24 public TreeMapBuggyComparatorTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 25 super(appId, cfg, listenerProvider); 26 27 barrier = new CyclicBarrier(getParticipantCount()); 28 } 29 30 public void run() { 31 try { 32 run0(); 33 } catch (Throwable t) { 34 notifyError(t); 35 } 36 } 37 38 public void run0() throws Exception { 39 BuggyComparator cmp = (BuggyComparator) map.comparator(); 40 cmp.setBuggy(true); 41 42 final boolean first; 43 synchronized (map) { 44 first = map.isEmpty(); 45 if (first) { 46 map.put("key", "value"); 47 } 48 } 49 50 Assert.assertEquals(1, map.size()); 51 Assert.assertFalse(map.containsKey("key")); 53 Assert.assertNull(map.remove("key")); 54 55 barrier.barrier(); 56 57 cmp.setBuggy(false); 58 Assert.assertTrue(map.containsKey("key")); 59 Assert.assertEquals("value", map.get("key")); 60 61 barrier.barrier(); 62 63 synchronized (map) { 64 if (!map.isEmpty()) { 65 Object removed = map.remove("key"); 66 Assert.assertEquals("value", removed); 67 Assert.assertEquals(0, map.size()); 68 } 69 } 70 } 71 72 private static class BuggyComparator implements Comparator { 73 private boolean buggy; 74 75 synchronized void setBuggy(boolean b) { 76 this.buggy = b; 77 } 78 79 public int compare(Object o1, Object o2) { 80 if (buggy) { 81 return 1; 82 } else { 83 return ((Comparable ) o1).compareTo(o2); 84 } 85 } 86 } 87 88 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 89 TransparencyClassSpec spec = config.getOrCreateSpec(CyclicBarrier.class.getName()); 90 config.addWriteAutolock("* " + CyclicBarrier.class.getName() + "*.*(..)"); 91 92 String testClass = TreeMapBuggyComparatorTestApp.class.getName(); 93 spec = config.getOrCreateSpec(testClass); 94 95 String methodExpression = "* " + testClass + "*.*(..)"; 96 config.addWriteAutolock(methodExpression); 97 98 spec.addRoot("map", "map"); 99 spec.addRoot("barrier", "barrier"); 100 101 config.addIncludePattern(BuggyComparator.class.getName()); 102 } 103 104 } 105
| Popular Tags
|