File: MultiTest.java

package info (click to toggle)
derby 10.14.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 78,896 kB
  • sloc: java: 691,930; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 60
file content (450 lines) | stat: -rw-r--r-- 11,608 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*

   Derby - Class org.apache.derbyTesting.functionTests.harness.MultiTest

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyTesting.functionTests.harness;

import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.util.Vector;
import java.util.Enumeration;
import org.apache.derby.impl.tools.ij.*;

import org.apache.derby.iapi.tools.i18n.LocalizedResource;
import org.apache.derby.iapi.tools.i18n.LocalizedOutput;
import org.apache.derbyTesting.functionTests.util.TestUtil;


/**
 * MultiTest is a multiuser test harness.  It
 * runs multiple threads with differing scripts
 * against an embedded server.  For syntax see
 * grammar.jj
 *
 * Tests are ended as soon as an unexpected error
 * has occurred or after the specified test duration
 * has transpired.  The main loop ends by explicitly
 * quitting (i.e. doing a System.exit()).
 *
 * Deprecated APIs: this test uses AppStreamWriter instead
 * of the preferred AppStreamWriter.  This is because utilMain()
 * uses AppStreamWriter (deliberately, i think) so since
 * that is called from this, there you have it.
 */

public class MultiTest
{

	private static final  int MAX_WAIT_FOR_COMPLETION = 180; 

	private static mtTestSuite suite;
	private static LocalizedOutput log; 
	private static String	inputDir;
	private static String	outputDir;
	private static String	testName;


	
	public MultiTest () { };

	public static void syntax()
	{
		System.out.println("Syntax:"
				+"\n\t <file>\t- the cmd file"
				+"\n\t[-o <dir>]\t-the output directory"
				+"\n\t[-i <dir>]\t-the input directory");
	}

	/**
	** Main loop
	@exception IOException thrown on error
	@exception ParseException thrown on error
	@exception FileNotFoundException thrown on error
	*/
	public static void main(String[] args) 
			throws IOException, ParseException, FileNotFoundException
	{
		String	cmdFilePath;
		InputStream	in;
		String	cmdFile;
		mtTester[] testers;
		int	i;
		int	max;

		if ((cmdFile = util.getFileArg(args)) == null)
		{
			syntax();
			return;
		}

		LocalizedResource.getInstance();

		testName = getTestName(cmdFile);
		inputDir = util.getArg("-i", args);
		outputDir = util.getArg("-o", args);

		/*
		** If cmdfile doesn't have a path, prepend
		** inputDir
		*/
		cmdFilePath = ((inputDir != null) && (cmdFile.indexOf("/") == -1)) ?
				(inputDir + "/" + cmdFile) : cmdFile;
		try 
		{
			in = new BufferedInputStream(new FileInputStream(cmdFilePath), 
										utilMain.BUFFEREDFILESIZE);
      	} catch (FileNotFoundException e) {
			System.out.println("MultiTest ERROR: config file not found: "+cmdFile);
        	return; 
      	}
		mtGrammar parser = new mtGrammar(in);
		suite = parser.grammarStatement();
		suite.setRoot(inputDir);
		suite.init();
	
		log = openFile(outputDir, testName + ".log");

		try
		{
			seqRunCases(suite.getInitCases(), 
						"initialization", 
						inputDir, 
						outputDir);
		} catch (ijFatalException e) 
		{
			System.exit(1);
		}

		max = suite.getNumThreads();
		System.out.println("...running with "+max+" threads");
		testers = new mtTester[max];

		// create the testers
		for (i = 0; i < max; i++)
		{
			String tester = "Tester" + (i+1);
			try 
			{
				LocalizedOutput out = openFile(outputDir, tester + ".out");
				testers[i] = new mtTester(tester, suite, out, log);
			} catch (IOException e) {
				System.out.println("MultiTest ERROR: unable open output file "+e);
				return;
			}
		}

		long duration = execTesters(testers);

		log.println("");
		log.println("test ran "+duration+" ms");
		log.println("total memory is "+Runtime.getRuntime().totalMemory());
		log.println("free memory  is "+Runtime.getRuntime().freeMemory());
		// Delete the .out files for Testers that did not report errors.
		for (i = 0; i < max; i++)
		{
			if ( testers[i].noFailure() )
			{
				log.println("Deleting " + "Tester" + (i+1) + ".out" + "(" + outputDir + ")");
				File out = new File(outputDir, "Tester" + (i+1) + ".out");
				out.delete();
			}
			else
			{
				log.println("Tester" + (i+1) + " failed.");
			}
		}
        
		System.exit(0);
	}

	/*
	**
	** NOTE ON OUT OF MEMORY PROBLEMS:  in theory 
	** when the VM runs out of memory an OutOfMemoryException
	** should be thrown by the runtime, but unfortunately, that
	** doesn't always seem to be the case.  When running this
	** program the Testers just wind up hanging on memory
	** allocation if there is insufficient memory.  To combat
	** this we try to manually stop each thread, but when
	** there is no memory, this doesn't seem to do anything
	** either.  Also, we grab some memory up front and release
	** that after telling the threads to stop themselves.
	*/
	private static long execTesters(mtTester[] testers)
			throws FileNotFoundException, IOException
	{
		boolean interrupted = false;
		boolean allWereAlive = true;
		int		i;
		long 	duration = 0;
		int 	max = testers.length;
		Thread[] threads;
		byte[] extraMemory;

		// new thread group
		ThreadGroup tg = new ThreadGroup("workers");
		//tg.allowThreadSuspension(false);

		// grab start time
		long start = System.currentTimeMillis();
		long runTime = suite.getTimeMillis();
		System.out.println("...running duration "+suite.getTime());

		// grab some memory to make stopping easier
 		extraMemory = new byte[4096];

		threads = new Thread[max];	
		// run them
		for (i = 0; i < max; i++)
		{
			threads[i] = new Thread(tg, testers[i]);
			threads[i].start();
		}

		// loop sleeping 800ms a bite.
		while (((duration = (System.currentTimeMillis() - start)) < runTime) &&
				(allWereAlive = allAlive(threads)) && (!interrupted))
		{
			try 
			{ 
				Thread.sleep(800L); 
			} catch (InterruptedException e) 
			{ 
				interrupted = true;	
			}
		}

		System.out.println("...stopping testers");


		/*
		** Free up 2k of memory and garbage
		** collect.  That should allow any
		** starved testers to stop themselves.
		*/
		extraMemory = null;
		System.gc();

		/*
		** Now stop everyone. First ask them to
		** willingly stop.  By calling mtTester.stop()
		** we prevent the testers from picking up the
		** next task.  
		*/
		for (i = 0; i < testers.length; i++)
		{
			testers[i].stop();
		}

		/*
		** Sleep 180 seconds, or until everyone
		** is done.
		*/
		System.out.println("...waiting for testers to complete");
		for (i = 0; i < MAX_WAIT_FOR_COMPLETION; i++)
		{
			try 
			{ 
				Thread.sleep((long)1000); 
			} catch (InterruptedException e) 
			{
				System.out.println("...Unexpected InterrupedException: "+e);
			}
			if (allDead(threads))
			{
				break;
			}
		}

		if (i == MAX_WAIT_FOR_COMPLETION)
		{
			log.println("WARNING: testers didn't die willingly, so I'm going to kill 'em.");
			log.println("\tThis may result in connection resources that aren't cleaned up");
			log.println("\t(e.g. you may see problems in the final script run with deadlocks).");
		}
	
		/*	
		** Now stop everyone that hasn't already stopped.
		* First get thread dumps for jdk 15.
		*/
		TestUtil.dumpAllStackTracesIfSupported(log);
		for (i = 0; i < MAX_WAIT_FOR_COMPLETION && (tg.isDestroyed() == false ); i++) 
		{ 

			// can't really stop - deprecated because 'unsafe'. interrupt.
			tg.interrupt();
			try { Thread.sleep((long) 1000); } catch (InterruptedException e) {}

			try 
			{ 
				tg.destroy(); 
			} catch (IllegalThreadStateException e)
			{
				log.println("...waiting for ThreadGroup.interrupt() to work its magic");
				try { Thread.sleep((long)1000); } catch (InterruptedException e2) {}
				continue;
			}
			break;	
		} 

		if (interrupted == true)
		{
			System.out.println("TEST CASE SUMMARY: run interrupted");
		}
		else if (allWereAlive == false)
		{
			System.out.println("TEST CASE SUMMARY: abnormal termination due to error(s)"+
				" -- see test log (./"+testName+"/"+testName+".log) for details ");
		}
		else
		{
			System.out.println("TEST CASE SUMMARY: normal termination");
			if (i < MAX_WAIT_FOR_COMPLETION)
			{
				try
				{
					seqRunCases(suite.getFinalCases(), 
							"last checks", 
							inputDir, 
							outputDir);
				} catch (ijFatalException e)  
				{ 
					System.out.println("...error running final test cases");
				}
			}
			else
			{
				System.out.println("...timed out trying to kill all testers,\n" +
								"   skipping last scripts (if any).  NOTE: the\n"+
								"   likely cause of the problem killing testers is\n"+
								"   probably not enough VM memory OR test cases that\n"+
								"   run for very long periods of time (so testers do not\n"+
								"   have a chance to notice stop() requests");
			}
		}

		return duration;
	}


	/**
	** Search through the list of threads and see
	** if they are all alive.
	*/
	public static boolean allAlive(Thread[] threads)
	{
		int	i;
		for (i = 0; i < threads.length; i++)
		{
			if (threads[i].isAlive() == false)
				break;
		}
		return (i == threads.length);
	}

	/**
	** Search through the list of threads and see
	** if they are all alive.
	*/
	public static boolean allDead(Thread[] threads)
	{
		int	i;
		for (i = 0; i < threads.length; i++)
		{
			if (threads[i].isAlive() == true)
				break;
		}
		return (i == threads.length);
	}

	/**
	** Figure out the name of the log file and open
	** it 
	*/
	private static LocalizedOutput openFile(String dir, String fileName) 
			throws IOException
	{
		
		java.io.File file = new java.io.File(dir, fileName);

		return new LocalizedOutput(new FileOutputStream(file));
	}
	/**
	** Sequentially run scripts
	*/
	private static void seqRunCases(Vector cases, String descr, String inputDir, String outputDir) 
		throws FileNotFoundException, IOException, ijFatalException
	{
		LocalizedOutput	out;
		BufferedInputStream	in;
		mtTestCase		testCase;

		if (cases == null)
		{
			System.out.println("...no "+descr+" being performed");
			return;
		}

		Enumeration e = cases.elements();

		while (e.hasMoreElements())
		{
			testCase = (mtTestCase)e.nextElement();
			String testName = testCase.getFile();
			System.out.println("...running "+descr+" via "+testName);
			String logFileName = 
				testName.substring(0, testName.lastIndexOf('.'));
			out = openFile(outputDir, logFileName + ".out");
			in = testCase.initialize(inputDir);
			testCase.runMe(log, out, in);
		}
	}

	/**
	** Given the command file, infer the test name.
	** Takes the portion of the file name between
	** the last '.' and the last '/'.  e.g.
	** x/y/Name.suffix -&gt; Name
	**
	*/
	private static String getTestName(String cmdFile)
	{
		int slash, dotSpot;

		slash = cmdFile.lastIndexOf("/");
		if (slash == -1)
		{
			slash = 0;
		}

		dotSpot = cmdFile.lastIndexOf(".");
		if (dotSpot == -1)
		{
			dotSpot = cmdFile.length();
		}
		return cmdFile.substring(slash, dotSpot);

	}
}