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.Iterator ; 18 import java.util.Map ; 19 import java.util.TreeMap ; 20 import java.util.Map.Entry; 21 22 public class TreeMapTestApp extends AbstractTransparentApp { 23 24 private final TreeMap map = new TreeMap (); 26 27 private final TreeMap map2 = new TreeMap (new WrappedStringComparator()); 30 31 private final CyclicBarrier barrier; 32 33 private final SubMapKey subMapKeyRoot = new SubMapKey(0); 34 35 public TreeMapTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 36 super(appId, cfg, listenerProvider); 37 barrier = new CyclicBarrier(getParticipantCount()); 38 } 39 40 public void run() { 41 try { 42 run0(); 43 run1(); 44 } catch (Throwable t) { 45 notifyError(t); 46 } 47 } 48 49 private void run0() throws Exception { 50 String me = getApplicationId(); 51 52 synchronized (map) { 53 map.put(me, me + "-value"); 54 } 55 56 synchronized (map2) { 57 map2.put(new WrappedString(me), me + "-value"); 58 } 59 60 barrier.barrier(); 61 62 validate(map, getParticipantCount(), null); 63 validate(map2, getParticipantCount(), new WrappedStringComparator()); 64 65 barrier.barrier(); 66 67 synchronized (map) { 68 Object removed = map.remove(me); 69 Assert.assertNotNull(removed); 70 } 71 72 synchronized (map2) { 73 Object removed = map2.remove(new WrappedString(me)); 74 Assert.assertNotNull(removed); 75 } 76 77 barrier.barrier(); 78 79 Assert.assertEquals(0, map.size()); 80 Assert.assertEquals(0, map2.size()); 81 82 barrier.barrier(); 83 84 synchronized (map) { 85 map.put(me, me + "-value"); 86 } 87 88 synchronized (map2) { 89 map2.put(new WrappedString(me), me + "-value"); 90 } 91 92 barrier.barrier(); 93 94 synchronized (map) { 95 if (map.size() == getParticipantCount()) { 96 map.clear(); 97 } else { 98 Assert.assertEquals(0, map.size()); 99 } 100 } 101 102 synchronized (map2) { 103 if (map2.size() == getParticipantCount()) { 104 map2.clear(); 105 } else { 106 Assert.assertEquals(0, map2.size()); 107 } 108 } 109 110 barrier.barrier(); 111 112 synchronized (map) { 113 map.put(me, "initial"); 114 } 115 116 synchronized (map2) { 117 map2.put(new WrappedString(me), "initial"); 118 } 119 120 synchronized (map) { 121 Object prev = map.put(me, "replaced"); 122 Assert.assertEquals("initial", prev); 123 } 124 125 synchronized (map2) { 126 Object prev = map2.put(new WrappedString(me), "replaced"); 127 Assert.assertEquals("initial", prev); 128 } 129 130 barrier.barrier(); 131 132 Assert.assertEquals(getParticipantCount(), map.size()); 133 Assert.assertEquals(getParticipantCount(), map2.size()); 134 135 for (Iterator i = map.values().iterator(); i.hasNext();) { 136 Assert.assertEquals("replaced", i.next()); 137 } 138 139 for (Iterator i = map2.values().iterator(); i.hasNext();) { 140 Assert.assertEquals("replaced", i.next()); 141 } 142 143 barrier.barrier(); 144 } 145 146 private void run1() throws Exception { 147 clear(); 148 initializeMaps(); 149 150 synchronized (map) { 152 if (map.size() == getParticipantCount()) { 153 if (map.size() > 2) { 154 Object fromKey = new Integer (0); 155 Object toKey = new Integer (10); 156 Map subMap = map.subMap(fromKey, toKey); 157 subMap.put(new Integer (1), "subMap-value"); 158 } 159 } else { 160 Assert.assertEquals(getParticipantCount() + 1, map.size()); 161 Assert.assertTrue(map.get(new Integer (1)).equals("subMap-value")); 162 } 163 } 164 165 synchronized (map2) { 166 if (map2.size() == getParticipantCount()) { 167 if (map2.size() > 2) { 168 Object fromKey = new WrappedString(0); 169 Object toKey = new WrappedString(10); 170 Map subMap = map2.subMap(fromKey, toKey); 171 subMap.put(new WrappedString(1), "subMap-value"); 172 } 173 } else { 174 Assert.assertEquals(getParticipantCount() + 1, map2.size()); 175 Assert.assertTrue(map2.get(new WrappedString(1)).equals("subMap-value")); 176 } 177 } 178 179 barrier.barrier(); 180 181 clear(); 182 initializeMaps(); 183 184 synchronized (map) { 186 if (map.size() == getParticipantCount()) { 187 if (map.size() > 2) { 188 Object toKey = new Integer (2); 189 Map headMap = map.headMap(toKey); 190 headMap.put(new Integer (1), "subMap-value"); 191 } 192 } else { 193 Assert.assertEquals(getParticipantCount() + 1, map.size()); 194 Assert.assertTrue(map.get(new Integer (1)).equals("subMap-value")); 195 } 196 } 197 198 synchronized (map2) { 199 if (map2.size() == getParticipantCount()) { 200 if (map2.size() > 2) { 201 Object toKey = new WrappedString(2); 202 Map headMap = map2.headMap(toKey); 203 headMap.put(new WrappedString(1), "subMap-value"); 204 } 205 } else { 206 Assert.assertEquals(getParticipantCount() + 1, map2.size()); 207 Assert.assertTrue(map2.get(new WrappedString(1)).equals("subMap-value")); 208 } 209 } 210 211 barrier.barrier(); 212 213 clear(); 214 initializeMaps(); 215 216 synchronized (map) { 218 if (map.size() == getParticipantCount()) { 219 if (map.size() > 2) { 220 Object fromKey = new Integer (0); 221 Map tailMap = map.tailMap(fromKey); 222 tailMap.put(new Integer (1), "subMap-value"); 223 } 224 } else { 225 Assert.assertEquals(getParticipantCount() + 1, map.size()); 226 Assert.assertTrue(map.get(new Integer (1)).equals("subMap-value")); 227 } 228 } 229 230 synchronized (map2) { 231 if (map2.size() == getParticipantCount()) { 232 if (map2.size() > 2) { 233 Object fromKey = new WrappedString(0); 234 Map tailMap = map2.tailMap(fromKey); 235 tailMap.put(new WrappedString(1), "subMap-value"); 236 } 237 } else { 238 Assert.assertEquals(getParticipantCount() + 1, map2.size()); 239 Assert.assertTrue(map2.get(new WrappedString(1)).equals("subMap-value")); 240 } 241 } 242 243 barrier.barrier(); 244 245 clear(); 246 initializeMaps(); 247 248 synchronized (map) { 250 if (map.size() == getParticipantCount()) { 251 Object fromKey = new Integer (0); 252 Map tailMap = map.tailMap(fromKey); 253 tailMap.clear(); 254 } else { 255 Assert.assertEquals(0, map.size()); 256 } 257 } 258 259 synchronized (map2) { 260 if (map2.size() == getParticipantCount()) { 261 Object fromKey = new WrappedString(0); 262 Map tailMap = map2.tailMap(fromKey); 263 tailMap.clear(); 264 } else { 265 Assert.assertEquals(0, map2.size()); 266 } 267 } 268 269 barrier.barrier(); 270 271 } 272 273 private void clear() throws Exception { 274 synchronized (map) { 275 map.clear(); 276 } 277 synchronized (map2) { 278 map2.clear(); 279 } 280 barrier.barrier(); 281 } 282 283 private void initializeMaps() throws Exception { 284 String me = getApplicationId(); 285 286 synchronized (subMapKeyRoot) { 287 if (subMapKeyRoot.getKey() != 0) { 288 subMapKeyRoot.setKey(0); 289 } 290 } 291 barrier.barrier(); 292 293 synchronized (map) { 294 int key = subMapKeyRoot.getKey(); 295 map.put(new Integer (key), me + "-value"); 296 subMapKeyRoot.setKey(key + 2); 297 } 298 barrier.barrier(); 299 synchronized (subMapKeyRoot) { 300 if (subMapKeyRoot.getKey() != 0) { 301 subMapKeyRoot.setKey(0); 302 } 303 } 304 barrier.barrier(); 305 synchronized (map2) { 306 int key = subMapKeyRoot.getKey(); 307 map2.put(new WrappedString(key), me + "-value"); 308 subMapKeyRoot.setKey(key + 2); 309 } 310 barrier.barrier(); 311 } 312 313 private static void validate(Map map, int count, Comparator comparator) { 314 int expect = count; 315 Assert.assertEquals(expect, map.size()); 316 317 TreeMap compare = comparator == null ? new TreeMap () : new TreeMap (comparator); 318 compare.putAll(map); 319 320 Iterator sharedIter = map.entrySet().iterator(); 321 Iterator localIter = compare.entrySet().iterator(); 322 323 while (true) { 324 Entry sharedEntry = (Entry) sharedIter.next(); 325 Entry localEntry = (Entry) localIter.next(); 326 327 Object sharedKey = sharedEntry.getKey(); 328 Object localKey = localEntry.getKey(); 329 Assert.assertEquals(localKey, sharedKey); 330 331 Object sharedValue = sharedEntry.getValue(); 332 Object localValue = localEntry.getValue(); 333 Assert.assertEquals(localValue, sharedValue); 334 335 if (sharedIter.hasNext()) { 336 Assert.assertTrue(localIter.hasNext()); 337 } else { 338 break; 339 } 340 } 341 342 } 343 344 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 345 TransparencyClassSpec spec = config.getOrCreateSpec(CyclicBarrier.class.getName()); 346 config.addWriteAutolock("* " + CyclicBarrier.class.getName() + "*.*(..)"); 347 348 String testClass = TreeMapTestApp.class.getName(); 349 spec = config.getOrCreateSpec(testClass); 350 351 config.addIncludePattern(testClass + "$*"); 352 353 String methodExpression = "* " + testClass + "*.*(..)"; 354 config.addWriteAutolock(methodExpression); 355 356 spec.addRoot("map", "map"); 357 spec.addRoot("map2", "map2"); 358 spec.addRoot("barrier", "barrier"); 359 spec.addRoot("subMapKeyRoot", "subMapKeyRoot"); 360 361 config.addIncludePattern(WrappedStringComparator.class.getName()); 362 config.addIncludePattern(WrappedString.class.getName()); 363 } 364 365 private static class WrappedString { 367 private final String string; 368 369 WrappedString(String string) { 370 this.string = string; 371 } 372 373 WrappedString(int i) { 374 this.string = String.valueOf(i); 375 } 376 377 String getString() { 378 return this.string; 379 } 380 } 381 382 private static class WrappedStringComparator implements Comparator { 383 384 public int compare(Object o1, Object o2) { 385 WrappedString ws1 = (WrappedString) o1; 386 WrappedString ws2 = (WrappedString) o2; 387 return ws1.getString().compareTo(ws2.getString()); 388 } 389 390 } 391 392 395 private static class SubMapKey { 396 private int key; 397 398 public SubMapKey(int key) { 399 this.key = key; 400 } 401 402 public int getKey() { 403 return key; 404 } 405 406 public void setKey(int key) { 407 this.key = key; 408 } 409 } 410 } 411 | Popular Tags |