uk.co.thebadgerset.junit.extensions.example.TimingControllerTestPerf Maven / Gradle / Ivy
Go to download
JUnit Toolkit enhances JUnit with performance testing, asymptotic behaviour analysis, and concurrency testing.
The newest version!
/*
* Copyright 2007 Rupert Smith.
*
* Licensed 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 uk.co.thebadgerset.junit.extensions.example;
import junit.framework.Test;
import junit.framework.TestSuite;
import uk.co.thebadgerset.junit.extensions.AsymptoticTestCase;
import uk.co.thebadgerset.junit.extensions.SleepThrottle;
import uk.co.thebadgerset.junit.extensions.Throttle;
import uk.co.thebadgerset.junit.extensions.TimingController;
import uk.co.thebadgerset.junit.extensions.TimingControllerAware;
/**
* Tests the use of the timing controller interface to output additional test timings from a single test method. There
* is also a test to ensure that when the timing controller is called from threads other than the one that called the
* test method, it still functions correctly. This is a common use case when writing asynchronous tests.
*
* CRC Card
* Responsibilities Collaborations
* Check timing controller can accept multiple timings.
* Check timing controller can be called from other threads.
*
*
* @author Rupert Smith
*/
public class TimingControllerTestPerf extends AsymptoticTestCase implements TimingControllerAware
{
/** Defines the test rate, used to space out the test timings. */
private static final float TEST_RATE = 100f;
/** The timing controller. */
private TimingController tc;
/**
* Creates a named timing controller test.
*
* @param name The test name.
*/
public TimingControllerTestPerf(String name)
{
super(name);
}
/**
* Compiles all the tests in this class into a suite.
*
* @return The test suite.
*/
public static Test suite()
{
// Build a new test suite
TestSuite suite = new TestSuite("Timing Controller Example Tests");
suite.addTest(new TimingControllerTestPerf("testMultipleTimingsOtherThread"));
return suite;
}
/**
* Registers multiple test timings from the thread that called this method.
*
* @param num The number of timings to register.
*/
public void testMultipleTimings(int num)
{
// Get the timing controller if it has been made available.
TimingController tc = getTimingController().getControllerForCurrentThread();
// Set up a throttle to space the test results out with.
Throttle throttle = new SleepThrottle();
throttle.setRate(TEST_RATE);
throttle.throttle();
for (int i = 0; i < num; i++)
{
// Restrict the test rate.
throttle.throttle();
// Only output multiple timings when the timing controller is available.
if (tc != null)
{
try
{
tc.completeTest(true, 1);
}
// If the test runner wants to stop right away it will raise InterruptedException. Terminate the
// loop immediately.
catch (InterruptedException e)
{
break;
}
}
}
}
/**
* Registers multiple test timings from a different thread that called this method. This checks that the test runner
* is not confused by the call back to the timing controller coming from another thread, and demonstrates that
* asynchronous testing is possible.
*
* @param num The number of timings to register.
*/
public void testMultipleTimingsOtherThread(final int num)
{
// Get the timing controller if it has been made available.
final TimingController tc = getTimingController().getControllerForCurrentThread();
// Set up a throttle to space the test results out with.
final Throttle throttle = new SleepThrottle();
throttle.setRate(TEST_RATE);
throttle.throttle();
Runnable testRunnable =
new Runnable()
{
public void run()
{
for (int i = 0; i < num; i++)
{
// Restrict the test rate.
throttle.throttle();
// Only output multiple timings when the timing controller is available.
if (tc != null)
{
try
{
tc.completeTest(true, 1);
tc.completeTest(false, 1);
}
// If the test runner wants to stop right away it will raise InterruptedException. Terminate the
// loop immediately.
catch (InterruptedException e)
{
break;
}
}
}
}
};
Thread testThread = new Thread(testRunnable);
testThread.start();
try
{
testThread.join();
}
catch (InterruptedException e)
{ }
}
/**
* Used by test runners that can supply a {@link uk.co.thebadgerset.junit.extensions.TimingController} to set the
* controller on this aware test.
*
* @param controller The timing controller.
*/
public void setTimingController(TimingController controller)
{
tc = controller;
}
/**
* Gets the timing controller passed into the
* {@link #setTimingController(uk.co.thebadgerset.junit.extensions.TimingController)} method.
*
* @return The timing controller, or null if none has been set.
*/
public TimingController getTimingController()
{
return tc;
}
}