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

Choose port for LocalGcdHelper dynamically #190

Merged
merged 3 commits into from
Sep 30, 2015
Merged
Show file tree
Hide file tree
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 @@ -34,6 +34,7 @@
public class DatastoreOptionsTest {

private static final String PROJECT_ID = "project_id";
private static final int PORT = LocalGcdHelper.findOpenPort();

This comment was marked as spam.

This comment was marked as spam.

private DatastoreRpcFactory datastoreRpcFactory;
private DatastoreRpc datastoreRpc;
private DatastoreOptions.Builder options;
Expand All @@ -46,7 +47,7 @@ public void setUp() throws IOException, InterruptedException {
.normalizeDataset(false)
.serviceRpcFactory(datastoreRpcFactory)
.projectId(PROJECT_ID)
.host("http://localhost:" + LocalGcdHelper.PORT);
.host("http://localhost:" + PORT);
EasyMock.expect(datastoreRpcFactory.create(EasyMock.anyObject(DatastoreOptions.class)))
.andReturn(datastoreRpc)
.anyTimes();
Expand All @@ -60,7 +61,7 @@ public void testProjectId() throws Exception {

@Test
public void testHost() throws Exception {
assertEquals("http://localhost:" + LocalGcdHelper.PORT, options.build().host());
assertEquals("http://localhost:" + PORT, options.build().host());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,20 @@ public class DatastoreTest {
private Datastore datastore;

private static LocalGcdHelper gcdHelper;
private static final int PORT = LocalGcdHelper.findOpenPort();

@BeforeClass
public static void beforeClass() throws IOException, InterruptedException {
if (!LocalGcdHelper.isActive(PROJECT_ID)) {
gcdHelper = LocalGcdHelper.start(PROJECT_ID);
if (!LocalGcdHelper.isActive(PROJECT_ID, PORT)) {
gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT);
}
}

@Before
public void setUp() throws IOException, InterruptedException {
options = DatastoreOptions.builder()
.projectId(PROJECT_ID)
.host("http://localhost:" + LocalGcdHelper.PORT)
.host("http://localhost:" + PORT)
.build();
datastore = DatastoreFactory.instance().get(options);
StructuredQuery<Key> query = Query.keyQueryBuilder().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
Expand Down Expand Up @@ -68,9 +69,10 @@ public class LocalGcdHelper {
private final String projectId;
private Path gcdPath;
private ProcessStreamReader processReader;
private final int port;

public static final String DEFAULT_PROJECT_ID = "projectid1";
public static final int PORT = 8080;
private static final int DEFAULT_PORT = 8080;
private static final String GCD_VERSION = "v1beta2";
private static final String GCD_BUILD = "rev1-2.1.2b";
private static final String GCD_BASENAME = "gcd-" + GCD_VERSION + "-" + GCD_BUILD;
Expand All @@ -94,6 +96,17 @@ public class LocalGcdHelper {
}
}

public static int findOpenPort() {
int port;
try (ServerSocket temp_socket = new ServerSocket(0)) {
port = temp_socket.getLocalPort();
temp_socket.close();

This comment was marked as spam.

} catch (IOException e) {
port = DEFAULT_PORT;
}
return port;
}

private static Path installedGcdPath() {
String gcloudExecutableName;
if (isWindows()) {
Expand Down Expand Up @@ -283,8 +296,9 @@ public static CommandWrapper create() {
}
}

public LocalGcdHelper(String projectId) {
public LocalGcdHelper(String projectId, int port) {
this.projectId = projectId;
this.port = port;
}

/**
Expand All @@ -297,7 +311,7 @@ public LocalGcdHelper(String projectId) {
*/
public void start() throws IOException, InterruptedException {
// send a quick request in case we have a hanging process from a previous run
sendQuitRequest();
sendQuitRequest(port);
// Each run is associated with its own folder that is deleted once test completes.
gcdPath = Files.createTempDirectory("gcd");
File gcdFolder = gcdPath.toFile();
Expand Down Expand Up @@ -379,13 +393,12 @@ private void startGcd(Path executablePath) throws IOException, InterruptedExcept
if (log.isLoggable(Level.FINE)) {
log.log(Level.FINE, "Starting datastore emulator for the project: {0}", projectId);
}
Process startProcess =
CommandWrapper.create()
.command(gcdAbsolutePath.toString(), "start", "--testing", "--allow_remote_shutdown",
projectId)
.directory(gcdPath)
.redirectErrorStream()
.start();
Process startProcess = CommandWrapper.create()
.command(gcdAbsolutePath.toString(), "start", "--testing", "--allow_remote_shutdown",
"--port=" + Integer.toString(port), projectId)
.directory(gcdPath)
.redirectErrorStream()
.start();
processReader = ProcessStreamReader.start(startProcess, "Dev App Server is now running");
}

Expand Down Expand Up @@ -419,9 +432,9 @@ private static void extractFile(ZipInputStream zipIn, File filePath) throws IOEx
}
}

public static void sendQuitRequest() {
public static void sendQuitRequest(int port) {
try {
URL url = new URL("http", "localhost", PORT, "/_ah/admin/quit");
URL url = new URL("http", "localhost", port, "/_ah/admin/quit");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
Expand All @@ -439,7 +452,7 @@ public static void sendQuitRequest() {
}

public void stop() throws IOException, InterruptedException {
sendQuitRequest();
sendQuitRequest(port);
if (processReader != null) {
processReader.terminate();
}
Expand Down Expand Up @@ -468,8 +481,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
});
}

public static LocalGcdHelper start(String projectId) throws IOException, InterruptedException {
LocalGcdHelper helper = new LocalGcdHelper(projectId);
public static LocalGcdHelper start(String projectId, int port) throws IOException, InterruptedException {
LocalGcdHelper helper = new LocalGcdHelper(projectId, port);
helper.start();
return helper;
}
Expand All @@ -478,15 +491,15 @@ public static void main(String... args) throws IOException, InterruptedException
if (args.length == 1) {
switch (args[0]) {
case "START":
if (!isActive(DEFAULT_PROJECT_ID)) {
LocalGcdHelper helper = start(DEFAULT_PROJECT_ID);
if (!isActive(DEFAULT_PROJECT_ID, DEFAULT_PORT)) {
LocalGcdHelper helper = start(DEFAULT_PROJECT_ID, DEFAULT_PORT);
try (FileWriter writer = new FileWriter(".local_gcd_helper")) {
writer.write(helper.gcdPath.toAbsolutePath().toString());
}
}
return;
case "STOP":
sendQuitRequest();
sendQuitRequest(DEFAULT_PORT);
File file = new File(".local_gcd_helper");
if (file.exists()) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
Expand All @@ -503,9 +516,9 @@ public static void main(String... args) throws IOException, InterruptedException
throw new RuntimeException("expecting only START | STOP");
}

public static boolean isActive(String projectId) {
public static boolean isActive(String projectId, int port) {
try {
StringBuilder urlBuilder = new StringBuilder("http://localhost:").append(PORT);
StringBuilder urlBuilder = new StringBuilder("http://localhost:").append(port);
urlBuilder.append("/datastore/v1beta2/datasets/").append(projectId).append("/lookup");
URL url = new URL(urlBuilder.toString());
try (BufferedReader reader =
Expand Down