SOLR-7156: Fix test failures due to resource leaks on windows

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1662174 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2015-02-25 07:44:27 +00:00
parent 7e060ae688
commit caf9037242
3 changed files with 33 additions and 16 deletions

View File

@ -188,6 +188,9 @@ Other Changes
* SOLR-7142: Fix TestFaceting.testFacets. (Michal Kroliczek via shalin) * SOLR-7142: Fix TestFaceting.testFacets. (Michal Kroliczek via shalin)
* SOLR-7156: Fix test failures due to resource leaks on windows.
(Ishan Chattopadhyaya via shalin)
================== 5.0.0 ================== ================== 5.0.0 ==================
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release. Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.

View File

@ -18,12 +18,14 @@ package org.apache.solr.cloud;
*/ */
import java.io.IOException; import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.apache.lucene.mockfile.FilterPath; import org.apache.lucene.mockfile.FilterPath;
import org.apache.solr.SolrTestCaseJ4.SuppressSSL; import org.apache.solr.SolrTestCaseJ4.SuppressSSL;
import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrClient;
@ -417,9 +419,10 @@ public class BasicDistributedZk2Test extends AbstractFullDistribZkTestBase {
checkBackupStatus.fetchStatus(); checkBackupStatus.fetchStatus();
Thread.sleep(1000); Thread.sleep(1000);
} }
ArrayList<Path> files = Lists.newArrayList(Files.newDirectoryStream(location, "snapshot*").iterator()); try (DirectoryStream<Path> stream = Files.newDirectoryStream(location, "snapshot*")) {
ArrayList<Path> files = Lists.newArrayList(stream.iterator());
assertEquals(Arrays.asList(files).toString(), 1, files.size()); assertEquals(Arrays.asList(files).toString(), 1, files.size());
}
} }

View File

@ -21,6 +21,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -130,18 +131,18 @@ public class TestReplicationHandlerBackup extends SolrJettyTestBase {
} }
//Validate //Validate
Path snapDir = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot*").iterator().next(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot*")) {
Path snapDir = stream.iterator().next();
verify(snapDir, nDocs); verify(snapDir, nDocs);
} }
}
private void verify(Path backup, int nDocs) throws IOException { private void verify(Path backup, int nDocs) throws IOException {
try (Directory dir = new SimpleFSDirectory(backup)) { try (Directory dir = new SimpleFSDirectory(backup);
IndexReader reader = DirectoryReader.open(dir); IndexReader reader = DirectoryReader.open(dir)) {
IndexSearcher searcher = new IndexSearcher(reader); IndexSearcher searcher = new IndexSearcher(reader);
TopDocs hits = searcher.search(new MatchAllDocsQuery(), 1); TopDocs hits = searcher.search(new MatchAllDocsQuery(), 1);
assertEquals(nDocs, hits.totalHits); assertEquals(nDocs, hits.totalHits);
reader.close();
dir.close();
} }
} }
@ -167,7 +168,10 @@ public class TestReplicationHandlerBackup extends SolrJettyTestBase {
Path[] snapDir = new Path[5]; //One extra for the backup on commit Path[] snapDir = new Path[5]; //One extra for the backup on commit
//First snapshot location //First snapshot location
snapDir[0] = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot*").iterator().next(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot*")) {
snapDir[0] = stream.iterator().next();
}
boolean namedBackup = random().nextBoolean(); boolean namedBackup = random().nextBoolean();
String firstBackupTimestamp = null; String firstBackupTimestamp = null;
@ -199,14 +203,19 @@ public class TestReplicationHandlerBackup extends SolrJettyTestBase {
} }
if (!namedBackup) { if (!namedBackup) {
snapDir[i+1] = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot*").iterator().next(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot*")) {
snapDir[i+1] = stream.iterator().next();
}
} else { } else {
snapDir[i+1] = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot." + backupName).iterator().next(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot." + backupName)) {
snapDir[i+1] = stream.iterator().next();
}
} }
verify(snapDir[i+1], nDocs); verify(snapDir[i+1], nDocs);
} }
//Test Deletion of named backup //Test Deletion of named backup
if (namedBackup) { if (namedBackup) {
testDeleteNamedBackup(backupNames); testDeleteNamedBackup(backupNames);
@ -214,11 +223,13 @@ public class TestReplicationHandlerBackup extends SolrJettyTestBase {
//5 backups got created. 4 explicitly and one because a commit was called. //5 backups got created. 4 explicitly and one because a commit was called.
// Only the last two should still exist. // Only the last two should still exist.
int count =0; int count =0;
Iterator<Path> iter = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot*").iterator(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot*")) {
Iterator<Path> iter = stream.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
iter.next(); iter.next();
count ++; count ++;
} }
}
//There will be 2 backups, otherwise 1 //There will be 2 backups, otherwise 1
if (backupKeepParamName.equals(ReplicationHandler.NUMBER_BACKUPS_TO_KEEP_REQUEST_PARAM)) { if (backupKeepParamName.equals(ReplicationHandler.NUMBER_BACKUPS_TO_KEEP_REQUEST_PARAM)) {