use Rule based TemporaryFolder for cleanup of temp directory/files

This commit is contained in:
Himanshu Gupta 2015-09-03 14:11:39 -05:00
parent 44911039c5
commit 5da58e48e0
3 changed files with 25 additions and 25 deletions

View File

@ -26,7 +26,9 @@ import io.druid.data.input.MapBasedRow;
import io.druid.granularity.QueryGranularity; import io.druid.granularity.QueryGranularity;
import io.druid.query.aggregation.AggregationTestHelper; import io.druid.query.aggregation.AggregationTestHelper;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File; import java.io.File;
@ -36,11 +38,14 @@ public class ApproximateHistogramAggregationTest
{ {
private AggregationTestHelper helper; private AggregationTestHelper helper;
@Rule
public final TemporaryFolder tempFolder = new TemporaryFolder();
public ApproximateHistogramAggregationTest() public ApproximateHistogramAggregationTest()
{ {
ApproximateHistogramDruidModule module = new ApproximateHistogramDruidModule(); ApproximateHistogramDruidModule module = new ApproximateHistogramDruidModule();
module.configure(null); module.configure(null);
helper = new AggregationTestHelper(Lists.newArrayList(module.getJacksonModules())); helper = new AggregationTestHelper(Lists.newArrayList(module.getJacksonModules()), tempFolder);
} }
@Test @Test

View File

@ -29,7 +29,6 @@ import com.google.common.base.Throwables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
import com.google.common.io.Files;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import com.metamx.common.guava.CloseQuietly; import com.metamx.common.guava.CloseQuietly;
import com.metamx.common.guava.Sequence; import com.metamx.common.guava.Sequence;
@ -63,9 +62,9 @@ import io.druid.segment.Segment;
import io.druid.segment.incremental.IncrementalIndex; import io.druid.segment.incremental.IncrementalIndex;
import io.druid.segment.incremental.IndexSizeExceededException; import io.druid.segment.incremental.IndexSizeExceededException;
import io.druid.segment.incremental.OnheapIncrementalIndex; import io.druid.segment.incremental.OnheapIncrementalIndex;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator; import org.apache.commons.io.LineIterator;
import org.junit.rules.TemporaryFolder;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -89,8 +88,11 @@ public class AggregationTestHelper
private final GroupByQueryQueryToolChest toolChest; private final GroupByQueryQueryToolChest toolChest;
private final GroupByQueryRunnerFactory factory; private final GroupByQueryRunnerFactory factory;
public AggregationTestHelper(List<? extends Module> jsonModulesToRegister) private final TemporaryFolder tempFolder;
public AggregationTestHelper(List<? extends Module> jsonModulesToRegister, TemporaryFolder tempFoler)
{ {
this.tempFolder = tempFoler;
mapper = new DefaultObjectMapper(); mapper = new DefaultObjectMapper();
for(Module mod : jsonModulesToRegister) { for(Module mod : jsonModulesToRegister) {
@ -141,13 +143,9 @@ public class AggregationTestHelper
String groupByQueryJson String groupByQueryJson
) throws Exception ) throws Exception
{ {
File segmentDir = Files.createTempDir(); File segmentDir = tempFolder.newFolder();
try {
createIndex(inputDataFile, parserJson, aggregators, segmentDir, minTimestamp, gran, maxRowCount); createIndex(inputDataFile, parserJson, aggregators, segmentDir, minTimestamp, gran, maxRowCount);
return runQueryOnSegments(Lists.newArrayList(segmentDir), groupByQueryJson); return runQueryOnSegments(Lists.newArrayList(segmentDir), groupByQueryJson);
} finally {
FileUtils.deleteDirectory(segmentDir);
}
} }
public Sequence<Row> createIndexAndRunQueryOnSegment( public Sequence<Row> createIndexAndRunQueryOnSegment(
@ -160,13 +158,9 @@ public class AggregationTestHelper
String groupByQueryJson String groupByQueryJson
) throws Exception ) throws Exception
{ {
File segmentDir = Files.createTempDir(); File segmentDir = tempFolder.newFolder();
try {
createIndex(inputDataStream, parserJson, aggregators, segmentDir, minTimestamp, gran, maxRowCount); createIndex(inputDataStream, parserJson, aggregators, segmentDir, minTimestamp, gran, maxRowCount);
return runQueryOnSegments(Lists.newArrayList(segmentDir), groupByQueryJson); return runQueryOnSegments(Lists.newArrayList(segmentDir), groupByQueryJson);
} finally {
FileUtils.deleteDirectory(segmentDir);
}
} }
public void createIndex( public void createIndex(
@ -255,7 +249,7 @@ public class AggregationTestHelper
} }
} }
catch (IndexSizeExceededException ex) { catch (IndexSizeExceededException ex) {
File tmp = Files.createTempDir(); File tmp = tempFolder.newFolder();
toMerge.add(tmp); toMerge.add(tmp);
IndexMerger.persist(index, tmp, null, new IndexSpec()); IndexMerger.persist(index, tmp, null, new IndexSpec());
index.close(); index.close();
@ -264,7 +258,7 @@ public class AggregationTestHelper
} }
if (toMerge.size() > 0) { if (toMerge.size() > 0) {
File tmp = Files.createTempDir(); File tmp = tempFolder.newFolder();
toMerge.add(tmp); toMerge.add(tmp);
IndexMerger.persist(index, tmp, null, new IndexSpec()); IndexMerger.persist(index, tmp, null, new IndexSpec());
@ -285,10 +279,6 @@ public class AggregationTestHelper
if (index != null) { if (index != null) {
index.close(); index.close();
} }
for (File file : toMerge) {
FileUtils.deleteDirectory(file);
}
} }
} }

View File

@ -27,16 +27,21 @@ import io.druid.granularity.QueryGranularity;
import io.druid.jackson.AggregatorsModule; import io.druid.jackson.AggregatorsModule;
import io.druid.query.aggregation.AggregationTestHelper; import io.druid.query.aggregation.AggregationTestHelper;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File; import java.io.File;
public class HyperUniquesAggregationTest public class HyperUniquesAggregationTest
{ {
@Rule
public final TemporaryFolder tempFolder = new TemporaryFolder();
@Test @Test
public void testIngestAndQuery() throws Exception public void testIngestAndQuery() throws Exception
{ {
AggregationTestHelper helper = new AggregationTestHelper(Lists.newArrayList(new AggregatorsModule())); AggregationTestHelper helper = new AggregationTestHelper(Lists.newArrayList(new AggregatorsModule()), tempFolder);
String metricSpec = "[{" String metricSpec = "[{"
+ "\"type\": \"hyperUnique\"," + "\"type\": \"hyperUnique\","