[ Team LiB ] |
4.14 Running Tests Concurrently4.14.1 ProblemYou want to run several tests concurrently using threads. 4.14.2 SolutionUse the junit.extensions.ActiveTestSuite class to build a suite of tests that run concurrently. 4.14.3 DiscussionThe ActiveTestSuite class runs each of its tests in a separate thread. The suite does not finish until all of the test threads are complete. Example 4-6 shows how to run three different test methods in three different threads. Example 4-6. Running tests in different threadspublic static Test suite( ) { TestSuite suite = new ActiveTestSuite( ); suite.addTest(new TestGame("testCreateFighter")); suite.addTest(new TestGame("testGameInitialState")); suite.addTest(new TestGame("testSameFighters")); return suite; } While you probably won't use this technique often, running tests in threads can serve as a rudimentary stress tester. You might also use ActiveTestSuite to help identify threading problems in your code. By combining ActiveTestSuite with RepeatedTest, you can uncover threading problems that only show up intermittently. Example 4-7 shows how you can combine repeated tests and other test suites into an ActiveTestSuite. Each of the repeated tests runs in a different thread; therefore, you end up with four threads. If you are experiencing occasional threading glitches, you might want to increase the number of iterations and run a similar test suite overnight. Example 4-7. A more advanced test suitepublic static Test suite( ) { TestSuite suite = new ActiveTestSuite( ); // run one test in a thread suite.addTest(new TestGame("testCreateFighter")); // run this test 100 times in a second thread suite.addTest(new RepeatedTest( new TestGame("testGameInitialState"), 100)); // run this test 200 times in a third thread suite.addTest(new RepeatedTest( new TestGame("testSameFighters"), 200)); // run some other test suite in a fourth thread suite.addTest(TestPerson.suite( )); return suite; } 4.14.4 See AlsoRecipe 4.10 explains the RepeatedTest class. |
[ Team LiB ] |