Skip to content

Commit ca4588e

Browse files
brettchabotcopybara-androidxtest
authored andcommitted
Move ShardingFilter to androidx.test.filters.
PiperOrigin-RevId: 831630355
1 parent 6b88ec2 commit ca4588e

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package androidx.test.filters;
2+
3+
import androidx.annotation.RestrictTo;
4+
import androidx.annotation.RestrictTo.Scope;
5+
import org.junit.runner.Description;
6+
import org.junit.runner.manipulation.Filter;
7+
8+
/**
9+
* A JUnit sharding filter uses the hashcode of the test description to assign it to a shard.
10+
*
11+
* @hide
12+
*/
13+
@RestrictTo(Scope.LIBRARY)
14+
public class ShardingFilter extends Filter {
15+
private final int numShards;
16+
private final int shardIndex;
17+
18+
public ShardingFilter(int numShards, int shardIndex) {
19+
this.numShards = numShards;
20+
this.shardIndex = shardIndex;
21+
}
22+
23+
@Override
24+
public boolean shouldRun(Description description) {
25+
if (description.isTest()) {
26+
return (Math.floorMod(description.hashCode(), numShards)) == shardIndex;
27+
}
28+
29+
// The description is a suite, so assume that it can be run so that filtering is
30+
// applied to its children. If after filtering it has no children then it will be
31+
// automatically filtered out.
32+
return true;
33+
}
34+
35+
/** {@inheritDoc} */
36+
@Override
37+
public String describe() {
38+
return String.format("Shard %d of %d shards", shardIndex, numShards);
39+
}
40+
}

runner/android_junit_runner/java/androidx/test/internal/runner/TestRequestBuilder.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import androidx.test.filters.CustomFilter;
2626
import androidx.test.filters.RequiresDevice;
2727
import androidx.test.filters.SdkSuppressFilter;
28+
import androidx.test.filters.ShardingFilter;
2829
import androidx.test.filters.TestsRegExFilter;
2930
import androidx.test.internal.runner.ClassPathScanner.ChainedClassNameFilter;
3031
import androidx.test.internal.runner.ClassPathScanner.ExcludeClassNamesFilter;
@@ -277,34 +278,6 @@ public String describe() {
277278
}
278279
}
279280

280-
private static class ShardingFilter extends Filter {
281-
private final int numShards;
282-
private final int shardIndex;
283-
284-
ShardingFilter(int numShards, int shardIndex) {
285-
this.numShards = numShards;
286-
this.shardIndex = shardIndex;
287-
}
288-
289-
@Override
290-
public boolean shouldRun(Description description) {
291-
if (description.isTest()) {
292-
return (Math.abs(description.hashCode()) % numShards) == shardIndex;
293-
}
294-
295-
// The description is a suite, so assume that it can be run so that filtering is
296-
// applied to its children. If after filtering it has no children then it will be
297-
// automatically filtered out.
298-
return true;
299-
}
300-
301-
/** {@inheritDoc} */
302-
@Override
303-
public String describe() {
304-
return String.format("Shard %s of %s shards", shardIndex, numShards);
305-
}
306-
}
307-
308281
/**
309282
* A {@link Request} that doesn't report an error if all tests are filtered out. Done for
310283
* consistency with InstrumentationTestRunner.

0 commit comments

Comments
 (0)