forked from pantsbuild/pants
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance parallel testing junit_tests
Surfaces EXPERIMENTAL support for parallel testing of methods within a test class. - Adds a 'concurrency' parameter to set test concurrency to 'serial' 'parallel', or 'parallel_method' - Adds a 'threads' parameter to junit_tests to control concurrency. - Adds a --test-junit-parallel-methods to allow methods to run in parallel for the entire test run - Added a number of unit test cases and integration tests to exercise these features Note that there is a bug in that the @TestSerial annotation is not respected when the -parallel-methods flag is in use. See pantsbuild#3209 Followup work for this change is to add an `@TestParallelMethods` annotation and come up with a more rational way to pass concurrency options to the junit-runner backend. Testing Done: Integration test and additional unit tests added. CI is green at https://travis-ci.org/pantsbuild/pants/builds/123712609 Bugs closed: 3191, 3210 Reviewed at https://rbcommons.com/s/twitter/r/3707/
- Loading branch information
1 parent
cdca712
commit 96c7ecf
Showing
33 changed files
with
941 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# coding=utf-8 | ||
# Copyright 2016 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import (absolute_import, division, generators, nested_scopes, print_function, | ||
unicode_literals, with_statement) | ||
|
||
|
||
def ensure_arg(args, arg, param=None): | ||
"""Make sure the arg is present in the list of args. | ||
If arg is not present, adds the arg and the optional param. | ||
If present and param != None, sets the parameter following the arg to param. | ||
:param list args: strings representing an argument list. | ||
:param string arg: argument to make sure is present in the list. | ||
:param string param: parameter to add or update after arg in the list. | ||
:return: possibly modified list of args. | ||
""" | ||
found = False | ||
for idx, found_arg in enumerate(args): | ||
if found_arg == arg: | ||
if param is not None: | ||
args[idx + 1] = param | ||
return args | ||
|
||
if not found: | ||
args += [arg] | ||
if param is not None: | ||
args += [param] | ||
return args | ||
|
||
|
||
def remove_arg(args, arg, has_param=False): | ||
"""Removes the first instance of the specified arg from the list of args. | ||
If the arg is present and has_param is set, also removes the parameter that follows | ||
the arg. | ||
:param list args: strings representing an argument list. | ||
:param staring arg: argument to remove from the list. | ||
:param bool has_param: if true, also remove the parameter that follows arg in the list. | ||
:return: possibly modified list of args. | ||
""" | ||
for idx, found_arg in enumerate(args): | ||
if found_arg == arg: | ||
if has_param: | ||
slice_idx = idx + 2 | ||
else: | ||
slice_idx = idx + 1 | ||
args = args[:idx] + args[slice_idx:] | ||
break | ||
return args |
43 changes: 43 additions & 0 deletions
43
testprojects/tests/java/org/pantsbuild/testproject/parallel/AnnotatedParallelTest1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2016 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
package org.pantsbuild.testproject.parallel; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Test; | ||
import org.pantsbuild.junit.annotations.TestParallel; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* This test is designed to exercise the TestParallel annotation. | ||
* A similar test runs in tests/java/... to exercise junit-runner standalone. | ||
* <p> | ||
* For all methods in AnnotatedParallelTest1 and AnnotatedParallelTest2 | ||
* to succeed, both test classes must be running at the same time with the flag: | ||
* <pre> | ||
* --test-junit-parallel-threads 2 | ||
* <pre> | ||
* when running with just these two classes as specs. | ||
* <p> | ||
* Runs in on the order of 10 milliseconds locally, but it may take longer on a CI machine to spin | ||
* up 2 threads, so it has a generous timeout set. | ||
*/ | ||
@TestParallel | ||
public class AnnotatedParallelTest1 { | ||
private static final int NUM_CONCURRENT_TESTS = 2; | ||
private static final int RETRY_TIMEOUT_MS = 3000; | ||
private static CountDownLatch latch = new CountDownLatch(NUM_CONCURRENT_TESTS); | ||
|
||
@Test | ||
public void aptest1() throws Exception { | ||
awaitLatch("aptest1"); | ||
} | ||
|
||
static void awaitLatch(String methodName) throws Exception { | ||
System.out.println("start " + methodName); | ||
latch.countDown(); | ||
assertTrue(latch.await(RETRY_TIMEOUT_MS, TimeUnit.MILLISECONDS)); | ||
System.out.println("end " + methodName); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
testprojects/tests/java/org/pantsbuild/testproject/parallel/AnnotatedParallelTest2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2016 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
package org.pantsbuild.testproject.parallel; | ||
|
||
import org.junit.Test; | ||
import org.pantsbuild.junit.annotations.TestParallel; | ||
|
||
/** | ||
* See {@link AnnotatedParallelTest1} | ||
*/ | ||
@TestParallel | ||
public class AnnotatedParallelTest2 { | ||
|
||
@Test | ||
public void aptest2() throws Exception { | ||
AnnotatedParallelTest1.awaitLatch("aptest2"); | ||
} | ||
} |
Oops, something went wrong.