Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package androidx.test.filters;

import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import org.junit.runner.Description;
import org.junit.runner.manipulation.Filter;

/**
* A JUnit sharding filter uses the hashcode of the test description to assign it to a shard.
*
* @hide
*/
@RestrictTo(Scope.LIBRARY)
public class ShardingFilter extends Filter {
private final int numShards;
private final int shardIndex;

public ShardingFilter(int numShards, int shardIndex) {
this.numShards = numShards;
this.shardIndex = shardIndex;
}

@Override
public boolean shouldRun(Description description) {
if (description.isTest()) {
return (Math.floorMod(description.hashCode(), numShards)) == shardIndex;
}

// The description is a suite, so assume that it can be run so that filtering is
// applied to its children. If after filtering it has no children then it will be
// automatically filtered out.
return true;
}

/** {@inheritDoc} */
@Override
public String describe() {
return String.format("Shard %d of %d shards", shardIndex, numShards);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import androidx.test.filters.CustomFilter;
import androidx.test.filters.RequiresDevice;
import androidx.test.filters.SdkSuppressFilter;
import androidx.test.filters.ShardingFilter;
import androidx.test.filters.TestsRegExFilter;
import androidx.test.internal.runner.ClassPathScanner.ChainedClassNameFilter;
import androidx.test.internal.runner.ClassPathScanner.ExcludeClassNamesFilter;
Expand Down Expand Up @@ -277,34 +278,6 @@ public String describe() {
}
}

private static class ShardingFilter extends Filter {
private final int numShards;
private final int shardIndex;

ShardingFilter(int numShards, int shardIndex) {
this.numShards = numShards;
this.shardIndex = shardIndex;
}

@Override
public boolean shouldRun(Description description) {
if (description.isTest()) {
return (Math.abs(description.hashCode()) % numShards) == shardIndex;
}

// The description is a suite, so assume that it can be run so that filtering is
// applied to its children. If after filtering it has no children then it will be
// automatically filtered out.
return true;
}

/** {@inheritDoc} */
@Override
public String describe() {
return String.format("Shard %s of %s shards", shardIndex, numShards);
}
}

/**
* A {@link Request} that doesn't report an error if all tests are filtered out. Done for
* consistency with InstrumentationTestRunner.
Expand Down
Loading