Skip to content

Commit

Permalink
[FLINK-5817] [tests] Use TemporaryFold to create temp files and folds…
Browse files Browse the repository at this point in the history
… for test

This closes apache#3341
  • Loading branch information
wenlong.lwl authored and StephanEwen committed Feb 18, 2017
1 parent d05fc37 commit 709fa1d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.flink.runtime.filecache;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.Future;

import org.apache.flink.core.fs.Path;
Expand All @@ -27,10 +28,12 @@

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.junit.rules.TemporaryFolder;

import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;
Expand All @@ -56,12 +59,15 @@ public class FileCacheDeleteValidationTest {
+ "Da flammt ein blitzendes Verheeren Dem Pfade vor des Donnerschlags. Doch\n"
+ "deine Boten, Herr, verehren Das sanfte Wandeln deines Tags.\n";

@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();

private FileCache fileCache;
private File f;

@Before
public void setup() {
String[] tmpDirectories = System.getProperty("java.io.tmpdir").split(",|" + File.pathSeparator);
public void setup() throws IOException {
String[] tmpDirectories = new String[]{temporaryFolder.newFolder().getAbsolutePath()};
try {
fileCache = new FileCache(tmpDirectories);
}
Expand All @@ -70,7 +76,7 @@ public void setup() {
fail("Cannot create FileCache: " + e.getMessage());
}

f = new File(System.getProperty("java.io.tmpdir"), "cacheFile");
f = temporaryFolder.newFile("cacheFile");
try {
Files.write(testFileContent, f, Charsets.UTF_8);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import org.apache.flink.core.memory.MemorySegment;
import org.apache.flink.runtime.io.disk.iomanager.FileIOChannel.ID;
import org.apache.flink.runtime.io.network.buffer.Buffer;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
Expand All @@ -34,12 +36,15 @@

public class IOManagerTest {

@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void channelEnumerator() {
public void channelEnumerator() throws IOException {
IOManager ioMan = null;

try {
File tempPath = new File(System.getProperty("java.io.tmpdir"));
File tempPath = temporaryFolder.newFolder();

String[] tempDirs = new String[]{
new File(tempPath, "a").getAbsolutePath(),
Expand Down Expand Up @@ -126,4 +131,4 @@ public BulkBlockChannelReader createBulkBlockChannelReader(ID channelID, List<Me
throw new UnsupportedOperationException();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@

import com.google.common.base.Charsets;
import com.google.common.io.Files;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.minicluster.LocalFlinkMiniCluster;

import org.junit.Rule;
import org.junit.rules.TemporaryFolder;

import scala.concurrent.duration.FiniteDuration;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.flink.runtime.akka.AkkaUtils;
Expand All @@ -39,22 +42,23 @@ public abstract class AbstractTestBase extends TestBaseUtils {

/** Configuration to start the testing cluster with */
protected final Configuration config;

private final List<File> tempFiles;


private final FiniteDuration timeout;

protected int taskManagerNumSlots = 1;

protected int numTaskManagers = 1;


@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();


/** The mini cluster that runs the test programs */
protected LocalFlinkMiniCluster executor;


public AbstractTestBase(Configuration config) {
this.config = Objects.requireNonNull(config);
this.tempFiles = new ArrayList<File>();

timeout = AkkaUtils.getTimeout(config);
}
Expand All @@ -74,7 +78,6 @@ public void startCluster() throws Exception {

public void stopCluster() throws Exception {
stopCluster(executor, timeout);
deleteAllTempFiles();
}

//------------------
Expand Down Expand Up @@ -103,12 +106,12 @@ public void setNumTaskManagers(int numTaskManagers) {
// --------------------------------------------------------------------------------------------

public String getTempDirPath(String dirName) throws IOException {
File f = createAndRegisterTempFile(dirName);
File f = temporaryFolder.newFolder(dirName);
return f.toURI().toString();
}

public String getTempFilePath(String fileName) throws IOException {
File f = createAndRegisterTempFile(fileName);
File f = temporaryFolder.newFile(fileName);
return f.toURI().toString();
}

Expand All @@ -119,35 +122,7 @@ public String createTempFile(String fileName, String contents) throws IOExceptio
}

public File createAndRegisterTempFile(String fileName) throws IOException {
File baseDir = new File(System.getProperty("java.io.tmpdir"));
File f = new File(baseDir, this.getClass().getName() + "-" + fileName);

if (f.exists()) {
deleteRecursively(f);
}

File parentToDelete = f;
while (true) {
File parent = parentToDelete.getParentFile();
if (parent == null) {
throw new IOException("Missed temp dir while traversing parents of a temp file.");
}
if (parent.equals(baseDir)) {
break;
}
parentToDelete = parent;
}

Files.createParentDirs(f);
this.tempFiles.add(parentToDelete);
return f;
return temporaryFolder.newFile(fileName);
}

private void deleteAllTempFiles() throws IOException {
for (File f : this.tempFiles) {
if (f.exists()) {
deleteRecursively(f);
}
}
}
}

0 comments on commit 709fa1d

Please sign in to comment.