1 4 package com.tctest; 5 6 import com.tc.object.config.ConfigVisitor; 7 import com.tc.object.config.DSOClientConfigHelper; 8 import com.tc.simulator.app.Application; 9 import com.tc.simulator.app.ApplicationConfig; 10 import com.tc.simulator.listener.ListenerProvider; 11 import com.tc.util.Assert; 12 import com.tctest.runner.AbstractTransparentApp; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 25 public class TransparentSetTestApp extends AbstractTransparentApp implements Application { 26 private static final int INITIAL_STAGE = 0; 27 private static final int ADD_COMPLETE_STAGE = 1; 28 private static final int ASSERT_MAX_COUNT_SIZE_STAGE = 2; 29 private static final int REMOVE_COMPLETE_STAGE = 3; 30 private static final int ASSERT_REMOVE_SIZE_STAGE = 4; 31 32 private Set set = new HashSet (); 33 34 public TransparentSetTestApp(String globalId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 35 super(globalId, cfg, listenerProvider); 36 } 37 38 public void run() { 39 int maxCount = getParticipantCount() * getIntensity(); 40 List testObjects = new ArrayList (); 41 moveToStage(INITIAL_STAGE); 42 for (int i = 0; i < getIntensity(); i++) { 43 TestObject to = new TestObject(getApplicationId(), 1); 44 testObjects.add(to); 45 synchronized (set) { 46 int size = set.size(); 47 set.add(to); 48 Assert.eval(set.size() == size + 1); 49 } 50 } 51 moveToStageAndWait(ADD_COMPLETE_STAGE); 52 53 checkSetSize(maxCount); 54 55 moveToStageAndWait(ASSERT_MAX_COUNT_SIZE_STAGE); 56 57 int removeCount = getIntensity() / 2; 58 for (int i = 0; i < removeCount; i++) { 59 synchronized (set) { 60 int size = set.size(); 61 boolean wasRemoved = set.remove(testObjects.get(i)); 62 Assert.eval("Test object should have been removed but wasn't: " + testObjects.get(i), wasRemoved); 63 Assert.eval(set.size() == size - 1); 64 } 65 } 66 67 moveToStageAndWait(REMOVE_COMPLETE_STAGE); 68 69 checkSetSize(maxCount - getParticipantCount() * removeCount); 70 71 moveToStageAndWait(ASSERT_REMOVE_SIZE_STAGE); 72 73 synchronized (set) { 74 set.clear(); 75 Assert.eval(set.size() == 0); 76 } 77 78 checkSetSize(0); 79 notifyResult(Boolean.TRUE); 80 } 81 82 private void checkSetSize(int s) { 83 synchronized (set) { 84 String error = "set size:" + set.size() + " expecting: " + s + " for: " + this.getApplicationId(); 85 System.out.println(error); 86 Map res = new HashMap (); 87 for (Iterator i = set.iterator(); i.hasNext();) { 88 TestObject to = (TestObject) i.next(); 89 String key = to.getId(); 90 if (!res.containsKey(key)) { 91 res.put(key, new Long (0)); 92 } else { 93 long v = ((Long ) res.get(key)).longValue(); 94 res.put(key, new Long (++v)); 95 } 96 } 97 if (set.size() != s) { 98 99 throw new AssertionError ("" + res + error); } 100 } 101 } 102 103 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 104 String testClassName = TransparentSetTestApp.class.getName(); 105 config.addRoot(testClassName, "set", "set", true); 106 String methodExpression = "* " + testClassName + ".*(..)"; 107 System.err.println("Adding autolock for: " + methodExpression); 108 109 config.addWriteAutolock(methodExpression); 110 111 config.addIncludePattern(TestObject.class.getName()); 112 } 113 114 static class TestObject { 115 116 private String id; 117 private int count; 118 119 TestObject(String id, int count) { 120 this.id = id; 121 this.count = count; 122 } 123 124 public String getId() { 125 return id; 126 } 127 128 public String toString() { 129 return "TestObject(" + id + "," + count + ")"; 130 } 131 } 132 133 } | Popular Tags |