1 package org.jboss.cache.loader; 2 3 import junit.framework.TestCase; 4 import org.jboss.cache.CacheException; 5 import org.jboss.cache.CacheImpl; 6 import org.jboss.cache.Fqn; 7 import org.jboss.cache.Modification; 8 import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig; 9 10 import java.io.ObjectInputStream ; 11 import java.io.ObjectOutputStream ; 12 import java.util.ArrayList ; 13 import java.util.Collection ; 14 import java.util.Collections ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 28 public class InterceptorSynchronizationTest extends TestCase 29 { 30 31 final int CACHELOADER_WAITTIME = 2000; final int numThreadsPerTopLevelNode = 5; 34 35 public void testBlockingProblem() throws Exception 36 { 37 38 CacheImpl cache = new CacheImpl(); 39 cache.setCacheLoader(new TestSlowCacheLoader()); 40 cache.start(); 41 42 long begin = System.currentTimeMillis(); 43 Collection threads = new ArrayList (); 44 45 48 49 for (int i = 0; i < numThreadsPerTopLevelNode; i++) 50 { 51 Thread thread = new Thread (new Retriever(cache, "/Moo/" + i)); 52 threads.add(thread); 53 Thread thread2 = new Thread (new Retriever(cache, "/Meow/" + i)); 54 threads.add(thread2); 55 } 56 for (Iterator iter = threads.iterator(); iter.hasNext();) 57 { 58 Thread thread = (Thread ) iter.next(); 59 thread.start(); 60 61 } 62 for (Iterator iter = threads.iterator(); iter.hasNext();) 63 { 64 Thread thread = (Thread ) iter.next(); 65 thread.join(); 66 } 67 68 long end = System.currentTimeMillis(); 69 long timeTaken = (end - begin); 70 71 77 int totalTimeExpectedToWaitIfNotSerialized = 3 * CACHELOADER_WAITTIME; assertTrue("If it was parallel, it should have finished quicker than this:" + timeTaken, timeTaken < totalTimeExpectedToWaitIfNotSerialized); 79 } 80 81 86 public class TestSlowCacheLoader extends AbstractCacheLoader 87 { 88 public void setConfig(IndividualCacheLoaderConfig config) 89 { 90 } 91 92 public IndividualCacheLoaderConfig getConfig() 93 { 94 return null; 95 } 96 97 public Set getChildrenNames(Fqn arg0) throws Exception 98 { 99 return null; 100 } 101 102 public Object get(Fqn arg0, Object arg1) throws Exception 103 { 104 return null; 105 } 106 107 public Map get(Fqn arg0) throws Exception 108 { 109 Thread.sleep(CACHELOADER_WAITTIME); 110 return Collections.singletonMap("foo", "bar"); 111 } 112 113 public boolean exists(Fqn arg0) throws Exception 114 { 115 return true; 116 } 117 118 public Object put(Fqn arg0, Object arg1, Object arg2) throws Exception 119 { 120 return null; 121 } 122 123 public void put(Fqn arg0, Map arg1) throws Exception 124 { 125 126 } 127 128 public void put(List <Modification> modifications) throws Exception 129 { 130 } 131 132 public Object remove(Fqn arg0, Object arg1) throws Exception 133 { 134 return null; 135 } 136 137 public void remove(Fqn arg0) throws Exception 138 { 139 140 } 141 142 public void removeData(Fqn arg0) throws Exception 143 { 144 145 } 146 147 public void prepare(Object tx, List <Modification> modifications, boolean one_phase) throws Exception 148 { 149 } 150 151 public void commit(Object arg0) throws Exception 152 { 153 154 } 155 156 public void rollback(Object arg0) 157 { 158 } 159 160 176 177 public void loadEntireState(ObjectOutputStream os) throws Exception 178 { 179 } 181 182 public void loadState(Fqn subtree, ObjectOutputStream os) throws Exception 183 { 184 } 186 187 public void storeEntireState(ObjectInputStream is) throws Exception 188 { 189 } 191 192 public void storeState(Fqn subtree, ObjectInputStream is) throws Exception 193 { 194 } 196 197 public void create() throws Exception 198 { 199 200 } 201 202 public void start() throws Exception 203 { 204 205 } 206 207 public void stop() 208 { 209 210 } 211 212 public void destroy() 213 { 214 215 } 216 217 } 218 219 220 private static class Retriever implements Runnable 221 { 222 private final String fqn; 223 private CacheImpl cache; 224 225 private Retriever(CacheImpl cache, String fqn) 226 { 227 this.fqn = fqn; 228 this.cache = cache; 229 } 230 231 public void run() 232 { 233 try 234 { 235 cache.get(fqn, "foo"); 236 } 237 catch (CacheException e) 238 { 239 throw new RuntimeException ("Unexpected", e); 240 } 241 242 } 243 } 244 } 245 | Popular Tags |