summaryrefslogtreecommitdiff
path: root/qpid/java/junit-toolkit/src/main/org/apache/qpid/junit/extensions/ScaledTestDecorator.java
blob: 93e2a3c8557872148dc2d59edbae20d29f1e8a7c (plain)
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
/*
 *
 * 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.qpid.junit.extensions;

import junit.framework.Test;
import junit.framework.TestResult;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * A test decorator that runs a test many times simultaneously in many threads.
 *
 * <p><table id="crc"><caption>CRC Card</caption>
 * <tr><th> Responsibilities <th> Collaborations
 * <tr><td> Clone a test run into many threads and run them simultaneously.
 * <tr><td> Inform the test results of the start and end of each concurrent test batch. <td> {@link TKTestResult}
 * <tr><td> Inform the test results of the concurrency level. <td> {@link TKTestResult}
 * </table>
 *
 * @author Rupert Smith
 */
public class ScaledTestDecorator extends WrappedSuiteTestDecorator implements ShutdownHookable // TestDecorator
{
    /** Used for logging. */
    // private static final Logger log = Logger.getLogger(ScaledTestDecorator.class);

    /** Determines how long to wait for tests to cleanly exit on shutdown. */
    private static final long SHUTDOWN_PAUSE = 3000;

    /**
     * The stress levels or numbers of simultaneous threads to run the test in. The test is repeated at each of
     * the concurrency levels specified here. Defaults to 1 thread.
     */
    private int[] threads = new int[] { 1 };

    /** Used to hold the number of tests currently being run in parallel. */
    private int concurrencyLevel;

    /** The test to run. */
    private WrappedSuiteTestDecorator test;

    /**
     * Used to hold the current {@link TKTestResult} for the tests currently being run. This is made available so that
     * the shutdown hook can ask it to cleanly end the current tests in the event of a shutdown.
     */
    private TKTestResult currentTestResult;

    /** Flag set by the shutdown hook. This decorator will not start any new tests when this is set. */
    private boolean shutdown = false;

    /**
     * Creates an active test with default multiplier (1).
     *
     * @param test The target test.
     */
    public ScaledTestDecorator(WrappedSuiteTestDecorator test)
    {
        super(test);
        this.test = test;
    }

    /**
     * Creates a concurrently scaled test with the specified number of threads.
     *
     * @param test       The target test.
     * @param numThreads The stress level.
     */
    public ScaledTestDecorator(WrappedSuiteTestDecorator test, int numThreads)
    {
        this(test, new int[] { numThreads });
    }

    /**
     * Creates a concurrently scaled test with the specified thread levels, the test is repeated at each level.
     *
     * @param test    The target test.
     * @param threads The concurrency levels.
     */
    public ScaledTestDecorator(WrappedSuiteTestDecorator test, int[] threads)
    {
        super(test);

        /*log.debug("public ScaledTestDecorator(WrappedSuiteTestDecorator test = \"" + test + "\", int[] threads = "
                  + MathUtils.printArray(threads) + "): called");*/

        this.test = test;
        this.threads = threads;
    }

    /**
     * Runs the test simultaneously in at the specified concurrency levels.
     *
     * @param testResult The results object to monitor the test results with.
     */
    public void run(TestResult testResult)
    {
        // log.debug("public void run(TestResult testResult = " + testResult + "): called");

        // Loop through all of the specified concurrent levels for the test, provided shutdown has not been called.
        for (int i = 0; (i < threads.length) && !shutdown; i++)
        {
            // Get the number of threads for this run.
            int numThreads = threads[i];

            // Create test thread handlers for all the threads.
            TestThreadHandler[] threadHandlers = new TestThreadHandler[numThreads];

            // Create a cyclic barrier for the test threads to synch their setups and teardowns on.
            CyclicBarrier barrier = new CyclicBarrier(numThreads);

            // Set up the test thread handlers to output results to the same test results object.
            for (int j = 0; j < numThreads; j++)
            {
                threadHandlers[j] = new TestThreadHandler(testResult, test, barrier);
            }

            // Ensure the concurrency level statistic is set up correctly.
            concurrencyLevel = numThreads;

            // Begin batch.
            if (testResult instanceof TKTestResult)
            {
                TKTestResult tkResult = (TKTestResult) testResult;
                // tkResult.notifyStartBatch();
                tkResult.setConcurrencyLevel(numThreads);

                // Set the test result for the currently running tests, so that the shutdown hook can call it if necessary.
                currentTestResult = tkResult;
            }

            // Run all the tests and wait for them all to finish.
            executeAndWaitForRunnables(threadHandlers);

            // Clear the test result for the currently running tests.
            currentTestResult = null;

            // End batch.
            if (testResult instanceof TKTestResult)
            {
                TKTestResult tkResult = (TKTestResult) testResult;
                tkResult.notifyEndBatch();
            }

            // Clear up all the test threads, they hold references to their associated TestResult object and Test object,
            // which may prevent them from being garbage collected as the TestResult and Test objects are long lived.
            for (int j = 0; j < numThreads; j++)
            {
                threadHandlers[j].testResult = null;
                threadHandlers[j].test = null;
                threadHandlers[j] = null;
            }
        }
    }

    /**
     * Reports the number of tests that the scaled decorator is currently running concurrently.
     *
     * @return The number of tests that the scaled decorator is currently running concurrently.
     */
    public int getConcurrencyLevel()
    {
        return concurrencyLevel;
    }

    /**
     * Executes all of the specifed runnable using the thread pool and waits for them all to complete.
     *
     * @param runnables The set of runnables to execute concurrently.
     */
    private void executeAndWaitForRunnables(Runnable[] runnables)
    {
        int numThreads = runnables.length;

        // Used to keep track of the test threads in order to know when they have all completed.
        Thread[] threads = new Thread[numThreads];

        // Create all the test threads.
        for (int j = 0; j < numThreads; j++)
        {
            threads[j] = new Thread(runnables[j]);
        }

        // Start all the test threads.
        for (int j = 0; j < numThreads; j++)
        {
            threads[j].start();
        }

        // Wait for all the test threads to complete.
        for (int j = 0; j < numThreads; j++)
        {
            try
            {
                threads[j].join();
            }
            catch (InterruptedException e)
            {
                // Restore the interrupted state of the thread.
                Thread.currentThread().interrupt();
            }
        }
    }

    /**
     * Supplies the shut-down hook.
     *
     * @return The shut-down hook.
     */
    public Thread getShutdownHook()
    {
        return new Thread(new Runnable()
                {
                    public void run()
                    {
                        // log.debug("ScaledTestDecorator::ShutdownHook: called");

                        // Set the shutdown flag so that no new tests are started.
                        shutdown = true;

                        // Check if tests are currently running, and ask them to complete as soon as possible. Allow
                        // a short pause for this to happen.
                        TKTestResult testResult = currentTestResult;

                        if (testResult != null)
                        {
                            // log.debug("There is a test result currently running tests, asking it to terminate ASAP.");
                            testResult.shutdownNow();

                            try
                            {
                                Thread.sleep(SHUTDOWN_PAUSE);
                            }
                            catch (InterruptedException e)
                            {
                                // Restore the interrupted state of the thread.
                                Thread.currentThread().interrupt();
                            }
                        }
                    }
                });
    }

    /**
     * Prints a string summarizing this test decorator, mainly for debugging purposes.
     *
     * @return String representation for debugging purposes.
     */
    public String toString()
    {
        return "ScaledTestDecorator: [ test = " + test + ", concurrencyLevel = " + concurrencyLevel + " ]";
    }

    /**
     * TestThreadHandler is a runnable used to execute a test in. This is static to avoid implicit 'this' reference to
     * the longer lived ScaledTestDecorator class. The scaled test decorator may execute many repeats but creates fresh
     * handlers for each one. It re-uses the threads in a pool but does not re-use these handlers.
     */
    private static class TestThreadHandler implements Runnable
    {
        /** The test result object for the test to be run with. */
        TestResult testResult;

        /** The test to run. */
        WrappedSuiteTestDecorator test;

        /** Holds the cyclic barrier to synchronize on the end of the setups and before the tear downs. */
        CyclicBarrier barrier;

        /**
         * Creates a new TestThreadHandler object.
         *
         * @param testResult The test result object for the test to be run with.
         * @param test       The test to run in a sperate thread.
         * @param barrier    The barrier implementation to use to synchronize per-thread setup completion and test
         *                   completion before moving on through the setup, test, teardown phases. The barrier should
         *                   be configured for the number of test threads.
         */
        TestThreadHandler(TestResult testResult, WrappedSuiteTestDecorator test, CyclicBarrier barrier)
        {
            this.testResult = testResult;
            this.test = test;
            this.barrier = barrier;
        }

        /**
         * Runs the test associated with this pool.
         */
        public void run()
        {
            try
            {
                // Call setup on all underlying tests in the suite that are thread aware.
                for (Test childTest : test.getAllUnderlyingTests())
                {
                    // Check that the test is concurrency aware, so provides a setup method to call.
                    if (childTest instanceof TestThreadAware)
                    {
                        // Call the tests per thread setup.
                        TestThreadAware setupTest = (TestThreadAware) childTest;
                        setupTest.threadSetUp();
                    }
                }

                // Wait until all test threads have completed their setups.
                barrier.await();


                // Call setup on all underlying tests in the suite that are thread aware.
                for (Test childTest : test.getAllUnderlyingTests())
                {
                    // Check that the test is concurrency aware, so provides a setup method to call.
                    if (childTest instanceof TestThreadAware)
                    {
                        // Call the tests post thread setup.
                        TestThreadAware setupTest = (TestThreadAware) childTest;
                        setupTest.postThreadSetUp();
                    }
                }

                // Wait until all test threads have completed their prefill.
                barrier.await();

                // Start timing the test batch, only after thread setups have completed.
                if (testResult instanceof TKTestResult)
                {
                    ((TKTestResult) testResult).notifyStartBatch();
                }

                // Run the tests.
                test.run(testResult);

                // Wait unitl all test threads have completed their tests.
                barrier.await();

                // Call tear down on all underlying tests in the suite that are thread aware.
                for (Test childTest : test.getAllUnderlyingTests())
                {
                    // Check that the test is concurrency aware, so provides a teardown method to call.
                    if (childTest instanceof TestThreadAware)
                    {
                        // Call the tests per thread tear down.
                        TestThreadAware setupTest = (TestThreadAware) childTest;
                        setupTest.threadTearDown();
                    }
                }
            }
            catch (InterruptedException e)
            {
                // Restore the interrupted state of the thread.
                Thread.currentThread().interrupt();
            }
            catch (BrokenBarrierException e)
            {
                // Set the interrupted state on the thread. The BrokenBarrierException may be caused where one thread
                // waiting for the barrier is interrupted, causing the remaining threads correctly waiting on the
                // barrier to fail. This condition is expected during test interruptions, and the response to it is to
                // interrupt all the other threads running in the same scaled test.
                Thread.currentThread().interrupt();
            }
        }

        /**
         * Prints the name of the test for debugging purposes.
         *
         * @return The name of the test.
         */
        public String toString()
        {
            return "ScaledTestDecorator: [test = \"" + test + "\"]";
        }
    }
}