Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TP-1222: Set GS_HOME to avoid files being created locally #48

Merged
merged 2 commits into from
Sep 5, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Bjornelmers marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
}
}