1 package org.tigris.scarab.util.word; 2 3 48 49 import org.tigris.scarab.om.IssueType; 50 import org.tigris.scarab.om.Module; 51 import org.tigris.scarab.om.ScarabUser; 52 import org.tigris.scarab.test.BaseScarabTestCase; 53 54 67 public class IssueSearchFactoryTest extends BaseScarabTestCase 68 { 69 private IssueSearch search; 70 71 72 76 public void testSingleThread() 77 throws Exception 78 { 79 IssueSearchFactory issueSearchFactory = new IssueSearchFactory() 80 { 81 int getMaxInstances() 82 { 83 return 5; 84 } 85 int getMaxWait() 86 { 87 return 0; 88 } 89 }; 90 91 Module module = getModule(); 92 IssueType it = getDefaultIssueType(); 93 ScarabUser user = getUser1(); 94 95 IssueSearch[] isa = new IssueSearch[5]; 96 for (int i=0; i<5; i++) 97 { 98 isa[i] = issueSearchFactory.getInstance(module, it, user); 99 } 100 101 try 103 { 104 issueSearchFactory.getInstance(module, it, getUser1()); 105 fail("Created more than maxInstances"); 106 } 107 catch (MaxConcurrentSearchException expected) 108 { 109 } 110 111 issueSearchFactory.notifyDone(); 113 114 try 116 { 117 issueSearchFactory.getInstance(module, it, getUser1()); 118 } 119 catch (MaxConcurrentSearchException failure) 120 { 121 fail("Could not create new instance after returning one."); 122 } 123 } 124 125 130 public void OFFtestConcurrency() 131 throws Exception 132 { 133 String message = multipleThreads(1); 134 assertTrue(message, message.length() == 0); 135 message = multipleThreads(2 * 50); assertTrue("Didn't timeout. " + message, 137 message.startsWith("Exception: ")); 138 } 139 140 private String multipleThreads(final int holdTime) 141 throws Exception 142 { 143 final long startTime = System.currentTimeMillis(); 144 145 final StringBuffer sb = new StringBuffer (20); 146 147 final ISFactoryTest[] pts = new ISFactoryTest[2 * 5]; final ThreadGroup threadGroup = new ThreadGroup ("foo") 149 { 150 public void uncaughtException(Thread t, Throwable e) 151 { 152 for (int i = 0; i < pts.length; i++) 153 { 154 pts[i].stop(); 155 } 156 157 sb.append("Exception: " + e.getMessage()); 158 } 159 }; 160 161 IssueSearchFactory issueSearchFactory = new IssueSearchFactory() 166 { 167 168 int getMaxInstances() 169 { 170 return 5; 171 } 172 int getMaxWait() 173 { 174 return 50; 175 } 176 }; 177 178 Module module = getModule(); 179 IssueType it = getDefaultIssueType(); 180 ScarabUser user = getUser1(); 181 182 183 for (int i = 0; i < pts.length; i++) 184 { 185 pts[i] = new ISFactoryTest(threadGroup, holdTime, 186 issueSearchFactory, 187 module, it, user); 188 } 189 190 Thread.sleep(1000); 192 for (int i = 0; i < pts.length; i++) 193 { 194 pts[i].stop(); 195 } 196 197 Thread.sleep(200); 199 for (int i = 0; i < pts.length; i++) 200 { 201 if (!pts[i].getState().startsWith("Stopped")) 203 { 204 sb.append("Possible deadlock. First try increasing sleep time."); 205 } 206 } 207 208 long time = System.currentTimeMillis() - startTime; 209 System.out.println("Multithread test time = " + time + " ms"); 210 return sb.toString(); 211 } 212 213 private static int currentThreadCount = 0; 214 215 private class ISFactoryTest implements Runnable 216 { 217 220 private final int isHoldTime; 221 private final Module module; 222 private final IssueType it; 223 private final ScarabUser user; 224 private final IssueSearchFactory isFactory; 225 226 private boolean isRun; 227 228 private String state; 229 private int runCounter; 230 231 protected ISFactoryTest(ThreadGroup threadGroup, int isHoldTime, 232 IssueSearchFactory isFactory, 233 Module module, IssueType it, ScarabUser user) 234 { 235 this.isHoldTime = isHoldTime; 236 this.module = module; 237 this.it = it; 238 this.user = user; 239 this.isFactory = isFactory; 240 Thread thread = new Thread (threadGroup, this, 241 "Thread+" + currentThreadCount++); 242 thread.setDaemon(false); 243 thread.start(); 244 } 245 246 public void run() 247 { 248 isRun = true; 249 runCounter = 0; 250 while (isRun) 251 { 252 runCounter++; 253 try 254 { 255 IssueSearch is = null; 256 state = "Getting IS"; 257 is = isFactory.getInstance(module, it, user); 258 state = "Using IS"; 259 assertTrue(null != is); 260 Thread.sleep(isHoldTime); 261 state = "Returning IS"; 262 isFactory.notifyDone(); 263 Thread.sleep(10); 267 } 268 catch (RuntimeException e) 269 { 270 throw e; 271 } 272 catch (Exception e) 273 { 274 throw new RuntimeException (e.toString()); 275 } 276 } 277 state = "Stopped"; 278 } 279 280 public void stop() 281 { 282 isRun = false; 283 } 284 285 public String getState() 286 { 287 return state + "; ran " + runCounter + " times"; 288 } 289 } 290 } 291 292 | Popular Tags |