From 9b3e6896c48ff1146a36d4c4ee4bcf941b641442 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Thu, 22 Oct 2015 14:21:00 -0700 Subject: [PATCH 1/2] Add unit tests for public methods in LocalGcdHelper --- .../datastore/testing/LocalGcdHelper.java | 9 ++- .../gcloud/datastore/LocalGcdHelperTest.java | 66 +++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelperTest.java diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java index c3afe025756b..24c0fbf9faab 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java @@ -524,7 +524,8 @@ private static void extractFile(ZipInputStream zipIn, File filePath) throws IOEx } } - public static void sendQuitRequest(int port) { + public static String sendQuitRequest(int port) { + StringBuilder result = new StringBuilder(); try { URL url = new URL("http", "localhost", port, "/_ah/admin/quit"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); @@ -535,12 +536,14 @@ public static void sendQuitRequest(int port) { out.write("".getBytes()); out.flush(); InputStream in = con.getInputStream(); - while (in.read() != -1) { - // consume input + int currByte = 0; + while ((currByte = in.read()) != -1) { + result.append(((char) currByte)); } } catch (IOException ignore) { // ignore } + return result.toString(); } public void stop() throws IOException, InterruptedException { diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelperTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelperTest.java new file mode 100644 index 000000000000..bfc3ffdaccfe --- /dev/null +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelperTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud.datastore; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.google.gcloud.datastore.testing.LocalGcdHelper; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.io.IOException; +import java.net.ServerSocket; + +@RunWith(JUnit4.class) +public class LocalGcdHelperTest { + + private static final String PROJECT_ID = LocalGcdHelper.DEFAULT_PROJECT_ID; + private static final int PORT = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT); + + @Test + public void testFindAvailablePort() { + int chosenPort = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT); + try (ServerSocket tempSocket = new ServerSocket(chosenPort)) { + // success + } catch (IOException e) { + if (chosenPort != LocalGcdHelper.DEFAULT_PORT) { + fail("Chosen port not free, even though LocalGcdHelper claimed it was."); + } + } + } + + @Test + public void testSendQuitRequest() throws IOException, InterruptedException { + LocalGcdHelper gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT); + assertTrue(LocalGcdHelper.sendQuitRequest(PORT).startsWith("Shutting down local server")); + gcdHelper.stop(); + assertTrue(LocalGcdHelper.sendQuitRequest(PORT).isEmpty()); // shouldn't error + } + + @Test + public void testStartStop() throws IOException, InterruptedException { + LocalGcdHelper gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT); + assertFalse(LocalGcdHelper.isActive("wrong-project-id", PORT)); + assertTrue(LocalGcdHelper.isActive(PROJECT_ID, PORT)); + gcdHelper.stop(); + assertFalse(LocalGcdHelper.isActive(PROJECT_ID, PORT)); + } +} From 494511c54dfd9ff09e4748c2847d32fda8483880 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Fri, 23 Oct 2015 10:41:39 -0700 Subject: [PATCH 2/2] Add timeout to testSendQuitRequest and boolean return value to sendQuitRequest --- .../gcloud/datastore/testing/LocalGcdHelper.java | 4 ++-- .../google/gcloud/datastore/LocalGcdHelperTest.java | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java index 24c0fbf9faab..7c60da50b0b0 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java @@ -524,7 +524,7 @@ private static void extractFile(ZipInputStream zipIn, File filePath) throws IOEx } } - public static String sendQuitRequest(int port) { + public static boolean sendQuitRequest(int port) { StringBuilder result = new StringBuilder(); try { URL url = new URL("http", "localhost", port, "/_ah/admin/quit"); @@ -543,7 +543,7 @@ public static String sendQuitRequest(int port) { } catch (IOException ignore) { // ignore } - return result.toString(); + return result.toString().startsWith("Shutting down local server"); } public void stop() throws IOException, InterruptedException { diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelperTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelperTest.java index bfc3ffdaccfe..40ea62c5a7e0 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelperTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelperTest.java @@ -50,9 +50,16 @@ public void testFindAvailablePort() { @Test public void testSendQuitRequest() throws IOException, InterruptedException { LocalGcdHelper gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT); - assertTrue(LocalGcdHelper.sendQuitRequest(PORT).startsWith("Shutting down local server")); + assertTrue(LocalGcdHelper.sendQuitRequest(PORT)); + long timeoutMillis = 30000; + long startTime = System.currentTimeMillis(); + boolean datastoreActive = LocalGcdHelper.isActive(PROJECT_ID, PORT); + while (datastoreActive && System.currentTimeMillis() - startTime < timeoutMillis) { + datastoreActive = LocalGcdHelper.isActive(PROJECT_ID, PORT); + } + assertFalse(datastoreActive); + assertFalse(LocalGcdHelper.sendQuitRequest(PORT)); gcdHelper.stop(); - assertTrue(LocalGcdHelper.sendQuitRequest(PORT).isEmpty()); // shouldn't error } @Test