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
commit f130079302
4 changed files with 18 additions and 10 deletions

View File

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

View File

@ -98,7 +98,7 @@ public class GerritServer {
}
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 {
@ -160,7 +160,6 @@ public class GerritServer {
return InetAddress.getLoopbackAddress();
}
private File sitePath;
private Daemon daemon;
private ExecutorService daemonService;
private Injector testInjector;
@ -168,9 +167,8 @@ public class GerritServer {
private InetSocketAddress sshdAddress;
private InetSocketAddress httpAddress;
private GerritServer(File sitePath, Injector testInjector, Daemon daemon,
private GerritServer(Injector testInjector, Daemon daemon,
ExecutorService daemonService) throws IOException, ConfigInvalidException {
this.sitePath = sitePath;
this.testInjector = testInjector;
this.daemon = daemon;
this.daemonService = daemonService;
@ -209,9 +207,6 @@ public class GerritServer {
daemonService.shutdownNow();
daemonService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
}
if (sitePath != null) {
TempFileUtil.recursivelyDelete(sitePath);
}
RepositoryCache.clear();
}
}

View File

@ -16,17 +16,29 @@ package com.google.gerrit.acceptance;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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_", "");
if (!tmp.delete() || !tmp.mkdir()) {
throw new IOException("Cannot create " + tmp.getPath());
}
allDirsCreated.add(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())) {
// Directory symlink reaching outside of temporary space.
return;

View File

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