1 21 22 package org.apache.derbyTesting.functionTests.harness; 23 24 import java.io.IOException ; 25 import java.io.File ; 26 import java.io.FileOutputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.InputStream ; 29 import java.io.FileInputStream ; 30 import java.io.BufferedInputStream ; 31 import java.util.Vector ; 32 import java.util.Enumeration ; 33 import org.apache.derby.impl.tools.ij.*; 34 35 import org.apache.derby.iapi.tools.i18n.LocalizedResource; 36 import org.apache.derby.iapi.tools.i18n.LocalizedOutput; 37 38 54 55 public class MultiTest 56 { 57 58 private static final int MAX_WAIT_FOR_COMPLETION = 180; 59 60 private static mtTestSuite suite; 61 private static LocalizedOutput log; 62 private static String inputDir; 63 private static String outputDir; 64 private static String testName; 65 66 67 68 public MultiTest () { }; 69 70 public static void syntax() 71 { 72 System.out.println("Syntax:" 73 +"\n\t <file>\t- the cmd file" 74 +"\n\t[-o <dir>]\t-the output directory" 75 +"\n\t[-i <dir>]\t-the input directory"); 76 } 77 78 84 public static void main(String [] args) 85 throws IOException , ParseException, FileNotFoundException 86 { 87 String cmdFilePath; 88 InputStream in; 89 String cmdFile; 90 mtTester[] testers; 91 int i; 92 int max; 93 94 if ((cmdFile = util.getFileArg(args)) == null) 95 { 96 syntax(); 97 return; 98 } 99 100 LocalizedResource.getInstance(); 101 102 testName = getTestName(cmdFile); 103 inputDir = util.getArg("-i", args); 104 outputDir = util.getArg("-o", args); 105 106 110 cmdFilePath = ((inputDir != null) && (cmdFile.indexOf("/") == -1)) ? 111 (inputDir + "/" + cmdFile) : cmdFile; 112 try 113 { 114 in = new BufferedInputStream (new FileInputStream (cmdFilePath), 115 utilMain.BUFFEREDFILESIZE); 116 } catch (FileNotFoundException e) { 117 System.out.println("MultiTest ERROR: config file not found: "+cmdFile); 118 return; 119 } 120 mtGrammar parser = new mtGrammar(in); 121 suite = parser.grammarStatement(); 122 suite.setRoot(inputDir); 123 suite.init(); 124 125 log = openFile(outputDir, testName + ".log"); 126 127 try 128 { 129 seqRunCases(suite.getInitCases(), 130 "initialization", 131 inputDir, 132 outputDir); 133 } catch (ijFatalException e) 134 { 135 System.exit(1); 136 } 137 138 max = suite.getNumThreads(); 139 System.out.println("...running with "+max+" threads"); 140 testers = new mtTester[max]; 141 142 for (i = 0; i < max; i++) 144 { 145 String tester = "Tester" + (i+1); 146 try 147 { 148 LocalizedOutput out = openFile(outputDir, tester + ".out"); 149 testers[i] = new mtTester(tester, suite, out, log); 150 } catch (IOException e) { 151 System.out.println("MultiTest ERROR: unable open output file "+e); 152 return; 153 } 154 } 155 156 long duration = execTesters(testers); 157 158 log.println(""); 159 log.println("test ran "+duration+" ms"); 160 log.println("total memory is "+Runtime.getRuntime().totalMemory()); 161 log.println("free memory is "+Runtime.getRuntime().freeMemory()); 162 for (i = 0; i < max; i++) 164 { 165 if ( testers[i].noFailure() ) 166 { 167 log.println("Deleting " + "Tester" + (i+1) + ".out" + "(" + outputDir + ")"); 168 File out = new File (outputDir, "Tester" + (i+1) + ".out"); 169 out.delete(); 170 } 171 else 172 { 173 log.println("Tester" + (i+1) + " failed."); 174 } 175 } 176 177 System.exit(0); 178 } 179 180 193 private static long execTesters(mtTester[] testers) 194 throws FileNotFoundException , IOException 195 { 196 boolean interrupted = false; 197 boolean allWereAlive = true; 198 int i; 199 long duration = 0; 200 int max = testers.length; 201 Thread [] threads; 202 byte[] extraMemory; 203 204 ThreadGroup tg = new ThreadGroup ("workers"); 206 208 long start = System.currentTimeMillis(); 210 long runTime = suite.getTimeMillis(); 211 System.out.println("...running duration "+suite.getTime()); 212 213 extraMemory = new byte[4096]; 215 216 threads = new Thread [max]; 217 for (i = 0; i < max; i++) 219 { 220 threads[i] = new Thread (tg, testers[i]); 221 threads[i].start(); 222 } 223 224 while (((duration = (System.currentTimeMillis() - start)) < runTime) && 226 (allWereAlive = allAlive(threads)) && (!interrupted)) 227 { 228 try 229 { 230 Thread.sleep(800L); 231 } catch (InterruptedException e) 232 { 233 interrupted = true; 234 } 235 } 236 237 System.out.println("...stopping testers"); 238 239 240 245 extraMemory = null; 246 System.gc(); 247 248 254 for (i = 0; i < testers.length; i++) 255 { 256 testers[i].stop(); 257 } 258 259 263 System.out.println("...waiting for testers to complete"); 264 for (i = 0; i < MAX_WAIT_FOR_COMPLETION; i++) 265 { 266 try 267 { 268 Thread.sleep((long)1000); 269 } catch (InterruptedException e) 270 { 271 System.out.println("...Unexpected InterrupedException: "+e); 272 } 273 if (allDead(threads)) 274 { 275 break; 276 } 277 } 278 279 if (i == MAX_WAIT_FOR_COMPLETION) 280 { 281 log.println("WARNING: testers didn't die willingly, so I'm going to kill 'em."); 282 log.println("\tThis may result in connection resources that aren't cleaned up"); 283 log.println("\t(e.g. you may see problems in the final script run with deadlocks)."); 284 } 285 286 289 for (i = 0; i < MAX_WAIT_FOR_COMPLETION && (tg.isDestroyed() == false ); i++) 290 { 291 292 tg.interrupt(); 294 try { Thread.sleep((long) 1000); } catch (InterruptedException e) {} 295 296 try 297 { 298 tg.destroy(); 299 } catch (IllegalThreadStateException e) 300 { 301 log.println("...waiting for ThreadGroup.interrupt() to work its magic"); 302 try { Thread.sleep((long)1000); } catch (InterruptedException e2) {} 303 continue; 304 } 305 break; 306 } 307 308 if (interrupted == true) 309 { 310 System.out.println("TEST CASE SUMMARY: run interrupted"); 311 } 312 else if (allWereAlive == false) 313 { 314 System.out.println("TEST CASE SUMMARY: abnormal termination due to error(s)"+ 315 " -- see test log (./"+testName+"/"+testName+".log) for details "); 316 } 317 else 318 { 319 System.out.println("TEST CASE SUMMARY: normal termination"); 320 if (i < MAX_WAIT_FOR_COMPLETION) 321 { 322 try 323 { 324 seqRunCases(suite.getFinalCases(), 325 "last checks", 326 inputDir, 327 outputDir); 328 } catch (ijFatalException e) 329 { 330 System.out.println("...error running final test cases"); 331 } 332 } 333 else 334 { 335 System.out.println("...timed out trying to kill all testers,\n" + 336 " skipping last scripts (if any). NOTE: the\n"+ 337 " likely cause of the problem killing testers is\n"+ 338 " probably not enough VM memory OR test cases that\n"+ 339 " run for very long periods of time (so testers do not\n"+ 340 " have a chance to notice stop() requests"); 341 } 342 } 343 344 return duration; 345 } 346 347 348 352 public static boolean allAlive(Thread [] threads) 353 { 354 int i; 355 for (i = 0; i < threads.length; i++) 356 { 357 if (threads[i].isAlive() == false) 358 break; 359 } 360 return (i == threads.length); 361 } 362 363 367 public static boolean allDead(Thread [] threads) 368 { 369 int i; 370 for (i = 0; i < threads.length; i++) 371 { 372 if (threads[i].isAlive() == true) 373 break; 374 } 375 return (i == threads.length); 376 } 377 378 382 private static LocalizedOutput openFile(String dir, String fileName) 383 throws IOException 384 { 385 386 java.io.File file = new java.io.File (dir, fileName); 387 388 return new LocalizedOutput(new FileOutputStream (file)); 389 } 390 393 private static void seqRunCases(Vector cases, String descr, String inputDir, String outputDir) 394 throws FileNotFoundException , IOException , ijFatalException 395 { 396 LocalizedOutput out; 397 BufferedInputStream in; 398 mtTestCase testCase; 399 400 if (cases == null) 401 { 402 System.out.println("...no "+descr+" being performed"); 403 return; 404 } 405 406 Enumeration e = cases.elements(); 407 408 while (e.hasMoreElements()) 409 { 410 testCase = (mtTestCase)e.nextElement(); 411 String testName = testCase.getFile(); 412 System.out.println("...running "+descr+" via "+testName); 413 String logFileName = 414 testName.substring(0, testName.lastIndexOf('.')); 415 out = openFile(outputDir, logFileName + ".out"); 416 in = testCase.initialize(inputDir); 417 testCase.runMe(log, out, in); 418 } 419 } 420 421 428 private static String getTestName(String cmdFile) 429 { 430 int slash, dotSpot; 431 432 slash = cmdFile.lastIndexOf("/"); 433 if (slash == -1) 434 { 435 slash = 0; 436 } 437 438 dotSpot = cmdFile.lastIndexOf("."); 439 if (dotSpot == -1) 440 { 441 dotSpot = cmdFile.length(); 442 } 443 return cmdFile.substring(slash, dotSpot); 444 445 } 446 } 447 | Popular Tags |