001: package com.clarkware.junitperf;
002:
003: import junit.framework.*;
004: import junit.extensions.RepeatedTest;
005:
006: /**
007: * The <code>LoadTestTest</code> is a <code>TestCase</code>
008: * for the <code>LoadTest</code> class.
009: *
010: * @author <a href="mailto:mike@clarkware.com">Mike Clark</a>
011: * @author <a href="http://www.clarkware.com">Clarkware Consulting, Inc.</a>
012: * @author Ervin Varga
013: *
014: * @see junit.framework.TestCase
015: */
016:
017: public class LoadTestTest extends TestCase {
018:
019: private TestSuite _successSuite;
020: private TestSuite _rogueThreadSuite;
021: private TestSuite _failureSuite;
022: private TestSuite _errorSuite;
023:
024: public static final long tolerance = 100;
025:
026: public LoadTestTest(String name) {
027: super (name);
028:
029: _successSuite = new TestSuite();
030: _failureSuite = new TestSuite();
031: _rogueThreadSuite = new TestSuite();
032: _errorSuite = new TestSuite();
033:
034: _successSuite.addTest(new MockTest("testSuccess"));
035: _successSuite.addTest(new MockTest("testSuccess"));
036: _rogueThreadSuite.addTest(new MockTest("testRogueThread"));
037: _failureSuite.addTest(new MockTest("testFailure"));
038: _errorSuite.addTest(new MockTest("testError"));
039: }
040:
041: public void testOneUser() {
042:
043: Test test = new LoadTest(_successSuite, 1);
044:
045: assertEquals(2, test.countTestCases());
046:
047: TestResult result = new TestResult();
048: test.run(result);
049:
050: assertEquals(2, result.runCount());
051: assertEquals(0, result.errorCount());
052: assertEquals(0, result.failureCount());
053: }
054:
055: /**
056: * This test will succeed properly only when the load
057: * test does not enforce thread atomicity.
058: * Otherwise, if the load test enforces thread atomicity,
059: * this test will hang indefinitely.
060: */
061: public void testOneUserRogueThread() {
062:
063: LoadTest test = new LoadTest(_rogueThreadSuite, 1);
064:
065: //test.setEnforceThreadAtomicity(true);
066:
067: assertEquals(1, test.countTestCases());
068:
069: TestResult result = new TestResult();
070: test.run(result);
071:
072: assertEquals(1, result.runCount());
073: assertEquals(0, result.errorCount());
074: assertEquals(0, result.failureCount());
075: }
076:
077: public void testMultiUser() {
078:
079: Test test = new LoadTest(_successSuite, 3);
080:
081: assertEquals(6, test.countTestCases());
082:
083: TestResult result = new TestResult();
084: test.run(result);
085:
086: assertEquals(6, result.runCount());
087: assertEquals(0, result.errorCount());
088: assertEquals(0, result.failureCount());
089: }
090:
091: public void testMultiUserWithIterations() {
092:
093: Test test = new LoadTest(_successSuite, 3, 10);
094:
095: assertEquals(60, test.countTestCases());
096:
097: TestResult result = new TestResult();
098: test.run(result);
099:
100: assertEquals(60, result.runCount());
101: assertEquals(0, result.errorCount());
102: assertEquals(0, result.failureCount());
103: }
104:
105: public void testMultiUserWithRepeatedTest() {
106:
107: RepeatedTest repeat = new RepeatedTest(_successSuite, 10);
108: Test test = new LoadTest(repeat, 3);
109:
110: assertEquals(60, test.countTestCases());
111:
112: TestResult result = new TestResult();
113: test.run(result);
114:
115: assertEquals(60, result.runCount());
116: assertEquals(0, result.errorCount());
117: assertEquals(0, result.failureCount());
118: }
119:
120: public void testMultiUserWithDelay() {
121:
122: Test test = new LoadTest(_successSuite, 3, new ConstantTimer(0));
123:
124: assertEquals(6, test.countTestCases());
125:
126: TestResult result = new TestResult();
127: test.run(result);
128:
129: assertEquals(6, result.runCount());
130: assertEquals(0, result.errorCount());
131: assertEquals(0, result.failureCount());
132: }
133:
134: public void testMultiUserWithFailure() {
135:
136: Test test = new LoadTest(_failureSuite, 3);
137:
138: assertEquals(3, test.countTestCases());
139:
140: TestResult result = new TestResult();
141: test.run(result);
142:
143: assertEquals(3, result.runCount());
144: assertEquals(0, result.errorCount());
145: assertEquals(3, result.failureCount());
146: }
147:
148: public void testMultiUserWithError() {
149:
150: Test test = new LoadTest(_errorSuite, 3);
151:
152: assertEquals(3, test.countTestCases());
153:
154: TestResult result = new TestResult();
155: test.run(result);
156:
157: assertEquals(3, result.runCount());
158: assertEquals(3, result.errorCount());
159: assertEquals(0, result.failureCount());
160: }
161:
162: public void testMultiUserWithStop() {
163:
164: Test test = new LoadTest(_failureSuite, 2);
165:
166: assertEquals(2, test.countTestCases());
167:
168: TestResult result = new TestResult();
169: result.stop();
170: test.run(result);
171:
172: assertEquals(0, result.runCount());
173: assertEquals(0, result.errorCount());
174: assertEquals(0, result.failureCount());
175: }
176:
177: public void testNonPositiveUser() {
178:
179: try {
180:
181: Test test1 = new LoadTest(_successSuite, 0);
182: fail("Should throw an IllegalArgumentException");
183:
184: Test test2 = new LoadTest(_successSuite, -1);
185: fail("Should throw an IllegalArgumentException");
186:
187: } catch (IllegalArgumentException success) {
188: return;
189: }
190: }
191:
192: public void testNullTimer() {
193: try {
194:
195: Test test = new LoadTest(_successSuite, 1, null);
196: fail("Should throw an IllegalArgumentException");
197:
198: } catch (IllegalArgumentException success) {
199: return;
200: }
201: }
202:
203: public void testAtomic2SecondResponse() {
204:
205: Test mockTest = new MockTest(
206: "testAtomic2SecondResponseWithWorkerThread");
207: Test loadTest = new LoadTest(mockTest, 1);
208: Test test = new TimedTest(loadTest, 1000 + tolerance);
209:
210: assertEquals(1, test.countTestCases());
211:
212: TestResult result = new TestResult();
213: test.run(result);
214:
215: assertEquals(1, result.runCount());
216: assertEquals(0, result.errorCount());
217: assertEquals(0, result.failureCount());
218: }
219:
220: public void testAtomic2SecondResponseEnforceTestAtomicity() {
221:
222: Test mockTest = new MockTest(
223: "testAtomic2SecondResponseWithWorkerThread");
224: LoadTest loadTest = new LoadTest(mockTest, 1);
225: loadTest.setEnforceTestAtomicity(true);
226: Test test = new TimedTest(loadTest, 1000 + tolerance);
227:
228: assertEquals(1, test.countTestCases());
229:
230: TestResult result = new TestResult();
231: test.run(result);
232:
233: assertEquals(1, result.runCount());
234: assertEquals(0, result.errorCount());
235: assertEquals(1, result.failureCount());
236: }
237:
238: public void testNonAtomic2SecondResponse() {
239:
240: Test mockTest = new MockTest(
241: "testNonAtomic2SecondResponseWithWorkerThread");
242: LoadTest loadTest = new LoadTest(mockTest, 1);
243: Test test = new TimedTest(loadTest, 1000 + tolerance);
244:
245: assertEquals(1, test.countTestCases());
246:
247: TestResult result = new TestResult();
248: test.run(result);
249:
250: assertEquals(1, result.runCount());
251: assertEquals(0, result.errorCount());
252: assertEquals(1, result.failureCount());
253: }
254:
255: public void testNonAtomic2SecondResponseEnforceTestAtomicity() {
256:
257: Test mockTest = new MockTest(
258: "testNonAtomic2SecondResponseWithWorkerThread");
259: LoadTest loadTest = new LoadTest(mockTest, 1);
260: loadTest.setEnforceTestAtomicity(true);
261: Test test = new TimedTest(loadTest, 1000 + tolerance);
262:
263: assertEquals(1, test.countTestCases());
264:
265: TestResult result = new TestResult();
266: test.run(result);
267:
268: assertEquals(1, result.runCount());
269: assertEquals(0, result.errorCount());
270: assertEquals(1, result.failureCount());
271: }
272:
273: public void testTestStateConsistencyFailure() {
274:
275: Test mockTest = new MockTestWithState("testInvariant");
276: LoadTest test = new LoadTest(mockTest, 10, 2);
277:
278: test.setEnforceTestAtomicity(true);
279:
280: assertEquals(20, test.countTestCases());
281:
282: TestResult result = new TestResult();
283: test.run(result);
284:
285: assertEquals(20, result.runCount());
286: assertEquals(0, result.errorCount());
287: assertTrue(result.failureCount() > 0);
288: }
289:
290: public void testTestStateConsistencyWithTestFactory() {
291:
292: Test testFactory = new TestFactory(MockTestWithState.class);
293: LoadTest test = new LoadTest(testFactory, 10, 2);
294:
295: test.setEnforceTestAtomicity(true);
296:
297: assertEquals(20, test.countTestCases());
298:
299: TestResult result = new TestResult();
300: test.run(result);
301:
302: assertEquals(20, result.runCount());
303: assertEquals(0, result.errorCount());
304: assertEquals(0, result.failureCount());
305: }
306:
307: public static Test suite() {
308: return new TestSuite(LoadTestTest.class);
309: }
310:
311: public static void main(String args[]) {
312: junit.textui.TestRunner.run(suite());
313: }
314: }
|