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-7156: Fix test failures due to resource leaks on windows.
(Ishan Chattopadhyaya via shalin)
================== 5.0.0 ==================
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.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import com.google.common.collect.Lists;
import org.apache.lucene.mockfile.FilterPath;
import org.apache.solr.SolrTestCaseJ4.SuppressSSL;
import org.apache.solr.client.solrj.SolrClient;
@ -417,9 +419,10 @@ public class BasicDistributedZk2Test extends AbstractFullDistribZkTestBase {
checkBackupStatus.fetchStatus();
Thread.sleep(1000);
}
ArrayList<Path> files = Lists.newArrayList(Files.newDirectoryStream(location, "snapshot*").iterator());
assertEquals(Arrays.asList(files).toString(), 1, files.size());
try (DirectoryStream<Path> stream = Files.newDirectoryStream(location, "snapshot*")) {
ArrayList<Path> files = Lists.newArrayList(stream.iterator());
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.InputStream;
import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -130,18 +131,18 @@ public class TestReplicationHandlerBackup extends SolrJettyTestBase {
}
//Validate
Path snapDir = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot*").iterator().next();
verify(snapDir, nDocs);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot*")) {
Path snapDir = stream.iterator().next();
verify(snapDir, nDocs);
}
}
private void verify(Path backup, int nDocs) throws IOException {
try (Directory dir = new SimpleFSDirectory(backup)) {
IndexReader reader = DirectoryReader.open(dir);
try (Directory dir = new SimpleFSDirectory(backup);
IndexReader reader = DirectoryReader.open(dir)) {
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs hits = searcher.search(new MatchAllDocsQuery(), 1);
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
//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();
String firstBackupTimestamp = null;
@ -199,14 +203,19 @@ public class TestReplicationHandlerBackup extends SolrJettyTestBase {
}
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 {
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);
}
//Test Deletion of named backup
if (namedBackup) {
testDeleteNamedBackup(backupNames);
@ -214,10 +223,12 @@ public class TestReplicationHandlerBackup extends SolrJettyTestBase {
//5 backups got created. 4 explicitly and one because a commit was called.
// Only the last two should still exist.
int count =0;
Iterator<Path> iter = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot*").iterator();
while (iter.hasNext()) {
iter.next();
count ++;
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(master.getDataDir()), "snapshot*")) {
Iterator<Path> iter = stream.iterator();
while (iter.hasNext()) {
iter.next();
count ++;
}
}
//There will be 2 backups, otherwise 1