Apache Solr open-source search software
SOLR-15643 CachingDirectoryFactoryTest is much too long for little value. (#353)
Mark Robert Miller4c398fb16bcb
diff --git a/solr/core/src/test/org/apache/solr/core/CachingDirectoryFactoryTest.java b/solr/core/src/test/org/apache/solr/core/CachingDirectoryFactoryTest.javaindex 5c6bce79028..eb8520ac4bc 100644--- a/solr/core/src/test/org/apache/solr/core/CachingDirectoryFactoryTest.java+++ b/solr/core/src/test/org/apache/solr/core/CachingDirectoryFactoryTest.java@@ -24,7 +24,6 @@ import java.util.List;import java.util.Map;import java.util.Random;import java.util.concurrent.atomic.AtomicInteger;-import org.apache.lucene.store.AlreadyClosedException;import org.apache.lucene.store.Directory;import org.apache.solr.SolrTestCaseJ4;@@ -38,63 +37,62 @@ public class CachingDirectoryFactoryTest extends SolrTestCaseJ4 {private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());- private Map<String,Tracker> dirs = new HashMap<>();+ private final Map<String, Tracker> dirs = new HashMap<>();private volatile boolean stop = false;-+private static class Tracker {String path;AtomicInteger refCnt = new AtomicInteger(0);Directory dir;}-+@Testpublic void stressTest() throws Exception {doStressTest(new RAMDirectoryFactory());doStressTest(new ByteBuffersDirectoryFactory());}-+private void doStressTest(final CachingDirectoryFactory df) throws Exception {List<Thread> threads = new ArrayList<>();- int threadCount = 11;+ int threadCount = TEST_NIGHTLY ? 11 : 3;for (int i = 0; i < threadCount; i++) {Thread getDirThread = new GetDirThread(df);threads.add(getDirThread);getDirThread.start();}-+for (int i = 0; i < 4; i++) {Thread releaseDirThread = new ReleaseDirThread(df);threads.add(releaseDirThread);releaseDirThread.start();}-+for (int i = 0; i < 2; i++) {Thread incRefThread = new IncRefThread(df);threads.add(incRefThread);incRefThread.start();}- Thread.sleep(TEST_NIGHTLY ? 30000 : 8000);-- Thread closeThread = new Thread() {- public void run() {- try {- df.close();- } catch (IOException e) {- throw new RuntimeException(e);- }- }- };+ Thread.sleep(TEST_NIGHTLY ? 30000 : 4000);++ Thread closeThread =+ new Thread() {+ public void run() {+ try {+ df.close();+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }+ };closeThread.start();--+stop = true;-+for (Thread thread : threads) {thread.join();}--+// do any remaining releasessynchronized (dirs) {int sz = dirs.size();@@ -107,38 +105,35 @@ public class CachingDirectoryFactoryTest extends SolrTestCaseJ4 {}}}-}-- closeThread.join();+ closeThread.join();}-+private class ReleaseDirThread extends Thread {Random random;private CachingDirectoryFactory df;-+public ReleaseDirThread(CachingDirectoryFactory df) {this.df = df;}-+@Overridepublic void run() {random = random();while (!stop) {try {- Thread.sleep(random.nextInt(50) + 1);+ Thread.sleep(random.nextInt(TEST_NIGHTLY ? 50 : 10) + 1);} catch (InterruptedException e1) {throw new RuntimeException(e1);}-+synchronized (dirs) {int sz = dirs.size();List<Tracker> dirsList = new ArrayList<>();dirsList.addAll(dirs.values());if (sz > 0) {- Tracker tracker = dirsList.get(Math.min(dirsList.size() - 1,- random.nextInt(sz + 1)));+ Tracker tracker = dirsList.get(Math.min(dirsList.size() - 1, random.nextInt(sz + 1)));try {if (tracker.refCnt.get() > 0) {if (random.nextInt(10) > 7) {@@ -157,19 +152,18 @@ public class CachingDirectoryFactoryTest extends SolrTestCaseJ4 {}}}-}}}-+private class GetDirThread extends Thread {Random random;private CachingDirectoryFactory df;-+public GetDirThread(CachingDirectoryFactory df) {this.df = df;}-+@Overridepublic void run() {random = random();@@ -187,7 +181,8 @@ public class CachingDirectoryFactoryTest extends SolrTestCaseJ4 {if (random.nextBoolean()) {path = "path" + random.nextInt(20) + "/" + random.nextInt(20);} else {- path = "path" + random.nextInt(20) + "/" + random.nextInt(20) + "/" + random.nextInt(20);+ path =+ "path" + random.nextInt(20) + "/" + random.nextInt(20) + "/" + random.nextInt(20);}}synchronized (dirs) {@@ -202,7 +197,7 @@ public class CachingDirectoryFactoryTest extends SolrTestCaseJ4 {}tracker.refCnt.incrementAndGet();}-+} catch (AlreadyClosedException e) {log.warn("Cannot get dir, factory is already closed");} catch (IOException e) {@@ -211,29 +206,29 @@ public class CachingDirectoryFactoryTest extends SolrTestCaseJ4 {}}}-+private class IncRefThread extends Thread {Random random;private CachingDirectoryFactory df;-+public IncRefThread(CachingDirectoryFactory df) {this.df = df;}-+@Overridepublic void run() {random = random();while (!stop) {try {- Thread.sleep(random.nextInt(300) + 1);+ Thread.sleep(random.nextInt(TEST_NIGHTLY ? 300 : 50) + 1);} catch (InterruptedException e1) {throw new RuntimeException(e1);}-+String path = "path" + random.nextInt(20);synchronized (dirs) {Tracker tracker = dirs.get(path);-+if (tracker != null && tracker.refCnt.get() > 0) {try {df.incRef(tracker.dir);@@ -241,13 +236,11 @@ public class CachingDirectoryFactoryTest extends SolrTestCaseJ4 {log.warn("", e);continue;}-+tracker.refCnt.incrementAndGet();}}-}}}-}