LUCENE-6431: make ExtrasFS reproducible

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1674274 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2015-04-17 11:07:45 +00:00
parent 8611461a94
commit 538f90225e
1 changed files with 16 additions and 8 deletions

View File

@ -25,6 +25,8 @@ import java.nio.file.attribute.FileAttribute;
import java.util.Random; import java.util.Random;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import com.carrotsearch.randomizedtesting.RandomizedContext;
/** /**
* Adds extra files/subdirectories when directories are created. * Adds extra files/subdirectories when directories are created.
* <p> * <p>
@ -33,30 +35,36 @@ import java.util.concurrent.atomic.AtomicLong;
* so we add them and see what breaks. * so we add them and see what breaks.
* <p> * <p>
* When a directory is created, sometimes a file or directory named * When a directory is created, sometimes a file or directory named
* "extraNNNN" will be included with it. * "extra0" will be included with it.
* All other filesystem operations are passed thru as normal. * All other filesystem operations are passed thru as normal.
*/ */
public class ExtrasFS extends FilterFileSystemProvider { public class ExtrasFS extends FilterFileSystemProvider {
final AtomicLong counter = new AtomicLong(); final int seed;
final Random random;
/** /**
* Create a new instance, wrapping {@code delegate}. * Create a new instance, wrapping {@code delegate}.
*/ */
public ExtrasFS(FileSystem delegate, Random random) { public ExtrasFS(FileSystem delegate, Random random) {
super("extras://", delegate); super("extras://", delegate);
this.random = random; this.seed = random.nextInt();
} }
@Override @Override
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException { public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
super.createDirectory(dir, attrs); super.createDirectory(dir, attrs);
// ok, we created the directory successfully. // ok, we created the directory successfully.
if (random.nextInt(4) == 0) {
// a little funky: we only look at hashcode (well-defined) of the target class name.
// using a generator won't reproduce, because we are a per-class resource.
// using hashing on filenames won't reproduce, because many of the names rely on other things
// the test class did.
// so a test gets terrorized with extras or gets none at all depending on the initial seed.
int hash = RandomizedContext.current().getTargetClass().toString().hashCode() ^ seed;
if ((hash & 3) == 0) {
// lets add a bogus file... if this fails, we don't care, its best effort. // lets add a bogus file... if this fails, we don't care, its best effort.
try { try {
Path target = dir.resolve("extra" + counter.incrementAndGet()); Path target = dir.resolve("extra0");
if (random.nextBoolean()) { if (hash < 0) {
super.createDirectory(target); super.createDirectory(target);
} else { } else {
Files.createFile(target); Files.createFile(target);