Merge "Fix cleanup after tests"

This commit is contained in:
David Pursehouse
2014-07-17 08:01:57 +00:00
committed by Gerrit Code Review
4 changed files with 18 additions and 10 deletions

View File

@@ -146,6 +146,7 @@ public abstract class AbstractDaemonTest {
db.close(); db.close();
sshSession.close(); sshSession.close();
server.stop(); server.stop();
TempFileUtil.cleanup();
} }
protected PushOneCommit.Result createChange() throws GitAPIException, protected PushOneCommit.Result createChange() throws GitAPIException,

View File

@@ -98,7 +98,7 @@ public class GerritServer {
} }
Injector i = createTestInjector(daemon); Injector i = createTestInjector(daemon);
return new GerritServer(site, i, daemon, daemonService); return new GerritServer(i, daemon, daemonService);
} }
private static File initSite(Config base) throws Exception { private static File initSite(Config base) throws Exception {
@@ -160,7 +160,6 @@ public class GerritServer {
return InetAddress.getLoopbackAddress(); return InetAddress.getLoopbackAddress();
} }
private File sitePath;
private Daemon daemon; private Daemon daemon;
private ExecutorService daemonService; private ExecutorService daemonService;
private Injector testInjector; private Injector testInjector;
@@ -168,9 +167,8 @@ public class GerritServer {
private InetSocketAddress sshdAddress; private InetSocketAddress sshdAddress;
private InetSocketAddress httpAddress; private InetSocketAddress httpAddress;
private GerritServer(File sitePath, Injector testInjector, Daemon daemon, private GerritServer(Injector testInjector, Daemon daemon,
ExecutorService daemonService) throws IOException, ConfigInvalidException { ExecutorService daemonService) throws IOException, ConfigInvalidException {
this.sitePath = sitePath;
this.testInjector = testInjector; this.testInjector = testInjector;
this.daemon = daemon; this.daemon = daemon;
this.daemonService = daemonService; this.daemonService = daemonService;
@@ -209,9 +207,6 @@ public class GerritServer {
daemonService.shutdownNow(); daemonService.shutdownNow();
daemonService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); daemonService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
} }
if (sitePath != null) {
TempFileUtil.recursivelyDelete(sitePath);
}
RepositoryCache.clear(); RepositoryCache.clear();
} }
} }

View File

@@ -16,17 +16,29 @@ package com.google.gerrit.acceptance;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TempFileUtil { public class TempFileUtil {
public static File createTempDirectory() throws IOException { private static List<File> allDirsCreated = new ArrayList<>();
public synchronized static File createTempDirectory() throws IOException {
File tmp = File.createTempFile("gerrit_test_", ""); File tmp = File.createTempFile("gerrit_test_", "");
if (!tmp.delete() || !tmp.mkdir()) { if (!tmp.delete() || !tmp.mkdir()) {
throw new IOException("Cannot create " + tmp.getPath()); throw new IOException("Cannot create " + tmp.getPath());
} }
allDirsCreated.add(tmp);
return tmp; return tmp;
} }
public static void recursivelyDelete(File dir) throws IOException { public static synchronized void cleanup() throws IOException {
for (File dir : allDirsCreated) {
recursivelyDelete(dir);
}
allDirsCreated.clear();
}
private static void recursivelyDelete(File dir) throws IOException {
if (!dir.getPath().equals(dir.getCanonicalPath())) { if (!dir.getPath().equals(dir.getCanonicalPath())) {
// Directory symlink reaching outside of temporary space. // Directory symlink reaching outside of temporary space.
return; return;

View File

@@ -36,7 +36,7 @@ public class ReindexIT {
@After @After
public void destroySite() throws Exception { public void destroySite() throws Exception {
if (sitePath != null) { if (sitePath != null) {
TempFileUtil.recursivelyDelete(sitePath); TempFileUtil.cleanup();
} }
} }