Skip to content

Commit

Permalink
TP-1222: Set GS_HOME to avoid files being created locally
Browse files Browse the repository at this point in the history
* Setting `GS_HOME` avoids temporary files being created in project directory when running tests
* Instead, set up a temporary folder to store these temporary files, deleting these after test execution completes
  • Loading branch information
pativa committed Sep 5, 2022
1 parent fe7c051 commit db5a217
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@

import static com.gigaspaces.start.SystemInfo.LOOKUP_LOCATORS_SYS_PROP;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gigaspaces.CommonSystemProperties;

/**
* This is intended to simulate an in-memory version of GigaSpaces Manager, and starts the following components:
* <ul>
Expand All @@ -41,8 +48,10 @@ final class InMemoryGigaSpacesManager implements AutoCloseable {

private final InMemoryZooKeeper zooKeeper;
private final InMemoryLus lus;
private final Path gsHome;

public InMemoryGigaSpacesManager() {
this.gsHome = setupGsHome();
if (shouldStartZooKeeper()) {
this.zooKeeper = new InMemoryZooKeeper();
setGigaSpacesManagerProperties(zooKeeper.getZooKeeperConfig());
Expand All @@ -60,6 +69,22 @@ private static boolean shouldStartZooKeeper() {
return !Boolean.getBoolean(DISABLE_ZOOKEEPER_PROPERTY);
}

/**
* Sets up temporary GS_HOME in order to avoid files being created in project directory during tests.
*/
private Path setupGsHome() {
if (System.getProperty(CommonSystemProperties.GS_HOME) != null) {
return null;
}
try {
Path tmpPath = Files.createTempDirectory(getClass().getSimpleName());
System.setProperty(CommonSystemProperties.GS_HOME, tmpPath.toString());
return tmpPath;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static void setGigaSpacesManagerProperties(Path zooKeeperConfig) {
// Set manager to be available with ZooKeeper on localhost
System.setProperty("com.gs.manager.servers", "localhost");
Expand Down Expand Up @@ -94,5 +119,20 @@ public void close() {
LOG.warn("Error while closing ZooKeeper server", e);
}
}
// Delete temporary GS_HOME
if (gsHome != null) {
try (Stream<Path> s = Files.walk(gsHome)) {
s.sorted(Comparator.reverseOrder())
.forEach(p -> {
try {
Files.delete(p);
} catch (IOException e) {
throw new UncheckedIOException("Failed to delete " + p, e);
}
});
} catch (Exception e) {
LOG.warn("Failed deleting GS_HOME at {}", gsHome, e);
}
}
}
}

0 comments on commit db5a217

Please sign in to comment.