{"action":"created","comment":{"url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/pulls/comments/59047910","id":59047910,"diff_hunk":"@@ -589,80 +540,102 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO\n     });\n   }\n \n-  public static LocalGcdHelper start(String projectId, int port, double consistency)\n-      throws IOException, InterruptedException {\n-    LocalGcdHelper helper = new LocalGcdHelper(projectId, port);\n-    helper.start(consistency);\n-    return helper;\n+  private LocalDatastoreHelper(double consistency) {\n+    projectId = PROJECT_ID_PREFIX + UUID.randomUUID().toString();\n+    this.consistency = consistency;\n+    this.port = findAvailablePort();\n   }\n \n-  public static void main(String... args) throws IOException, InterruptedException {\n-    Map<String, String> parsedArgs = parseArgs(args);\n-    String action = parsedArgs.get(\"action\");\n-    int port =\n-        (parsedArgs.get(\"port\") == null) ? DEFAULT_PORT : Integer.parseInt(parsedArgs.get(\"port\"));\n-    switch (action) {\n-      case \"START\":\n-        if (!isActive(DEFAULT_PROJECT_ID, port)) {\n-          double consistency = parsedArgs.get(\"consistency\") == null\n-              ? DEFAULT_CONSISTENCY : Double.parseDouble(parsedArgs.get(\"consistency\"));\n-          LocalGcdHelper helper = start(DEFAULT_PROJECT_ID, port, consistency);\n-          try (FileWriter writer = new FileWriter(\".local_gcd_helper\")) {\n-            writer.write(helper.gcdPath.toAbsolutePath().toString() + System.lineSeparator());\n-            writer.write(Integer.toString(port));\n-          }\n-        }\n-        return;\n-      case \"STOP\":\n-        File file = new File(\".local_gcd_helper\");\n-        String path = null;\n-        boolean fileExists = file.exists();\n-        if (fileExists) {\n-          try (BufferedReader reader = new BufferedReader(new FileReader(file))) {\n-            path = reader.readLine();\n-            port = Integer.parseInt(reader.readLine());\n-          }\n-        }\n-        sendQuitRequest(port);\n-        if (fileExists) {\n-          deleteRecurse(Paths.get(path));\n-          file.delete();\n-        }\n-        return;\n-      default:\n-        break;\n+  private static int findAvailablePort() {\n+    try (ServerSocket tempSocket = new ServerSocket(0)) {\n+      return tempSocket.getLocalPort();\n+    } catch (IOException e) {\n+      return -1;\n     }\n   }\n \n-  private static Map<String, String> parseArgs(String[] args) {\n-    Map<String, String> parsedArgs = new HashMap<String, String>();\n-    for (String arg : args) {\n-      if (arg.startsWith(\"--port=\")) {\n-        parsedArgs.put(\"port\", arg.substring(\"--port=\".length()));\n-      } else if (arg.equals(\"START\") || arg.equals(\"STOP\")) {\n-        parsedArgs.put(\"action\", arg);\n-      } else {\n-        throw new RuntimeException(\"Only accepts START, STOP, and --port=<port #> as arguments\");\n-      }\n-    }\n-    if (parsedArgs.get(\"action\") == null) {\n-      throw new RuntimeException(\"EXPECTING START | STOP\");\n-    }\n-    return parsedArgs;\n+  /**\n+   * Returns a {@link DatastoreOptions} instance that sets the host to use the Datastore emulator on\n+   * localhost.\n+   */\n+  public DatastoreOptions options() {\n+    return DatastoreOptions.builder()\n+        .projectId(projectId)\n+        .host(\"localhost:\" + Integer.toString(port))\n+        .authCredentials(AuthCredentials.noAuth())\n+        .build();\n   }\n \n-  public static boolean isActive(String projectId, int port) {\n-    try {\n-      StringBuilder urlBuilder = new StringBuilder(\"http://localhost:\").append(port);\n-      urlBuilder.append(\"/datastore/v1beta3/projects/\").append(projectId).append(\":lookup\");\n-      URL url = new URL(urlBuilder.toString());\n-      try (BufferedReader reader =\n-          new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) {\n-        return \"Valid RPC\".equals(reader.readLine());\n-      }\n-    } catch (IOException ignore) {\n-      // assume not active\n-      return false;\n+  /**\n+   * Returns the project ID associated with this local Datastore emulator.\n+   */\n+  public String projectId() {\n+    return projectId;\n+  }\n+\n+  /**\n+   * Returns the port on localhost to which the local Datastore listens for requests.\n+   */\n+  public int port() {","path":"gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalDatastoreHelper.java","position":243,"original_position":243,"commit_id":"561b04fbc2c37101a7a071bbca509f3990cfb347","original_commit_id":"561b04fbc2c37101a7a071bbca509f3990cfb347","user":{"login":"ajkannan","id":5316886,"avatar_url":"https://avatars.githubusercontent.com/u/5316886?v=3","gravatar_id":"","url":"https://api.github.com/users/ajkannan","html_url":"https://github.com/ajkannan","followers_url":"https://api.github.com/users/ajkannan/followers","following_url":"https://api.github.com/users/ajkannan/following{/other_user}","gists_url":"https://api.github.com/users/ajkannan/gists{/gist_id}","starred_url":"https://api.github.com/users/ajkannan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ajkannan/subscriptions","organizations_url":"https://api.github.com/users/ajkannan/orgs","repos_url":"https://api.github.com/users/ajkannan/repos","events_url":"https://api.github.com/users/ajkannan/events{/privacy}","received_events_url":"https://api.github.com/users/ajkannan/received_events","type":"User","site_admin":false},"body":"I don't see a strong use case for it, I can remove it.","created_at":"2016-04-08T16:01:52Z","updated_at":"2016-04-08T16:01:52Z","html_url":"https://github.com/GoogleCloudPlatform/gcloud-java/pull/821#discussion_r59047910","pull_request_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/pulls/821","_links":{"self":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/pulls/comments/59047910"},"html":{"href":"https://github.com/GoogleCloudPlatform/gcloud-java/pull/821#discussion_r59047910"},"pull_request":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/pulls/821"}}},"pull_request":{"url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/pulls/821","id":64560471,"html_url":"https://github.com/GoogleCloudPlatform/gcloud-java/pull/821","diff_url":"https://github.com/GoogleCloudPlatform/gcloud-java/pull/821.diff","patch_url":"https://github.com/GoogleCloudPlatform/gcloud-java/pull/821.patch","issue_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/issues/821","number":821,"state":"open","locked":false,"title":"Clean up LocalGcdHelper and docs","user":{"login":"ajkannan","id":5316886,"avatar_url":"https://avatars.githubusercontent.com/u/5316886?v=3","gravatar_id":"","url":"https://api.github.com/users/ajkannan","html_url":"https://github.com/ajkannan","followers_url":"https://api.github.com/users/ajkannan/followers","following_url":"https://api.github.com/users/ajkannan/following{/other_user}","gists_url":"https://api.github.com/users/ajkannan/gists{/gist_id}","starred_url":"https://api.github.com/users/ajkannan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ajkannan/subscriptions","organizations_url":"https://api.github.com/users/ajkannan/orgs","repos_url":"https://api.github.com/users/ajkannan/repos","events_url":"https://api.github.com/users/ajkannan/events{/privacy}","received_events_url":"https://api.github.com/users/ajkannan/received_events","type":"User","site_admin":false},"body":"* Adds an `options` method to `LocalGcdHelper` to make it more consistent with other test helpers.\r\n* Uses the `options` method in `DatastoreTest`\r\n* Updates the package info and TESTING docs to reflect the recently-added consistency setting\r\n* Allows consistency to be set from command line invocation of `LocalGcdHelper`","created_at":"2016-03-30T01:44:36Z","updated_at":"2016-04-08T16:01:52Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b967ae1eb0a857d1a90ac094f980fb2cfbabfaa6","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/pulls/821/commits","review_comments_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/pulls/821/comments","review_comment_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/issues/821/comments","statuses_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/statuses/561b04fbc2c37101a7a071bbca509f3990cfb347","head":{"label":"ajkannan:cleanup-localgcdhelper","ref":"cleanup-localgcdhelper","sha":"561b04fbc2c37101a7a071bbca509f3990cfb347","user":{"login":"ajkannan","id":5316886,"avatar_url":"https://avatars.githubusercontent.com/u/5316886?v=3","gravatar_id":"","url":"https://api.github.com/users/ajkannan","html_url":"https://github.com/ajkannan","followers_url":"https://api.github.com/users/ajkannan/followers","following_url":"https://api.github.com/users/ajkannan/following{/other_user}","gists_url":"https://api.github.com/users/ajkannan/gists{/gist_id}","starred_url":"https://api.github.com/users/ajkannan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ajkannan/subscriptions","organizations_url":"https://api.github.com/users/ajkannan/orgs","repos_url":"https://api.github.com/users/ajkannan/repos","events_url":"https://api.github.com/users/ajkannan/events{/privacy}","received_events_url":"https://api.github.com/users/ajkannan/received_events","type":"User","site_admin":false},"repo":{"id":40725411,"name":"gcloud-java","full_name":"ajkannan/gcloud-java","owner":{"login":"ajkannan","id":5316886,"avatar_url":"https://avatars.githubusercontent.com/u/5316886?v=3","gravatar_id":"","url":"https://api.github.com/users/ajkannan","html_url":"https://github.com/ajkannan","followers_url":"https://api.github.com/users/ajkannan/followers","following_url":"https://api.github.com/users/ajkannan/following{/other_user}","gists_url":"https://api.github.com/users/ajkannan/gists{/gist_id}","starred_url":"https://api.github.com/users/ajkannan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ajkannan/subscriptions","organizations_url":"https://api.github.com/users/ajkannan/orgs","repos_url":"https://api.github.com/users/ajkannan/repos","events_url":"https://api.github.com/users/ajkannan/events{/privacy}","received_events_url":"https://api.github.com/users/ajkannan/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ajkannan/gcloud-java","description":"Google Cloud Client Library for Java","fork":true,"url":"https://api.github.com/repos/ajkannan/gcloud-java","forks_url":"https://api.github.com/repos/ajkannan/gcloud-java/forks","keys_url":"https://api.github.com/repos/ajkannan/gcloud-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ajkannan/gcloud-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ajkannan/gcloud-java/teams","hooks_url":"https://api.github.com/repos/ajkannan/gcloud-java/hooks","issue_events_url":"https://api.github.com/repos/ajkannan/gcloud-java/issues/events{/number}","events_url":"https://api.github.com/repos/ajkannan/gcloud-java/events","assignees_url":"https://api.github.com/repos/ajkannan/gcloud-java/assignees{/user}","branches_url":"https://api.github.com/repos/ajkannan/gcloud-java/branches{/branch}","tags_url":"https://api.github.com/repos/ajkannan/gcloud-java/tags","blobs_url":"https://api.github.com/repos/ajkannan/gcloud-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ajkannan/gcloud-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ajkannan/gcloud-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/ajkannan/gcloud-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ajkannan/gcloud-java/statuses/{sha}","languages_url":"https://api.github.com/repos/ajkannan/gcloud-java/languages","stargazers_url":"https://api.github.com/repos/ajkannan/gcloud-java/stargazers","contributors_url":"https://api.github.com/repos/ajkannan/gcloud-java/contributors","subscribers_url":"https://api.github.com/repos/ajkannan/gcloud-java/subscribers","subscription_url":"https://api.github.com/repos/ajkannan/gcloud-java/subscription","commits_url":"https://api.github.com/repos/ajkannan/gcloud-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/ajkannan/gcloud-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/ajkannan/gcloud-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/ajkannan/gcloud-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/ajkannan/gcloud-java/contents/{+path}","compare_url":"https://api.github.com/repos/ajkannan/gcloud-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ajkannan/gcloud-java/merges","archive_url":"https://api.github.com/repos/ajkannan/gcloud-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ajkannan/gcloud-java/downloads","issues_url":"https://api.github.com/repos/ajkannan/gcloud-java/issues{/number}","pulls_url":"https://api.github.com/repos/ajkannan/gcloud-java/pulls{/number}","milestones_url":"https://api.github.com/repos/ajkannan/gcloud-java/milestones{/number}","notifications_url":"https://api.github.com/repos/ajkannan/gcloud-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ajkannan/gcloud-java/labels{/name}","releases_url":"https://api.github.com/repos/ajkannan/gcloud-java/releases{/id}","deployments_url":"https://api.github.com/repos/ajkannan/gcloud-java/deployments","created_at":"2015-08-14T16:38:38Z","updated_at":"2016-01-28T17:32:12Z","pushed_at":"2016-04-08T15:49:58Z","git_url":"git://github.com/ajkannan/gcloud-java.git","ssh_url":"git@github.com:ajkannan/gcloud-java.git","clone_url":"https://github.com/ajkannan/gcloud-java.git","svn_url":"https://github.com/ajkannan/gcloud-java","homepage":"","size":17918,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"GoogleCloudPlatform:master","ref":"master","sha":"6987b00bdfc7657b4487e056c6c383b8b41c534d","user":{"login":"GoogleCloudPlatform","id":2810941,"avatar_url":"https://avatars.githubusercontent.com/u/2810941?v=3","gravatar_id":"","url":"https://api.github.com/users/GoogleCloudPlatform","html_url":"https://github.com/GoogleCloudPlatform","followers_url":"https://api.github.com/users/GoogleCloudPlatform/followers","following_url":"https://api.github.com/users/GoogleCloudPlatform/following{/other_user}","gists_url":"https://api.github.com/users/GoogleCloudPlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/GoogleCloudPlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GoogleCloudPlatform/subscriptions","organizations_url":"https://api.github.com/users/GoogleCloudPlatform/orgs","repos_url":"https://api.github.com/users/GoogleCloudPlatform/repos","events_url":"https://api.github.com/users/GoogleCloudPlatform/events{/privacy}","received_events_url":"https://api.github.com/users/GoogleCloudPlatform/received_events","type":"Organization","site_admin":false},"repo":{"id":26181278,"name":"gcloud-java","full_name":"GoogleCloudPlatform/gcloud-java","owner":{"login":"GoogleCloudPlatform","id":2810941,"avatar_url":"https://avatars.githubusercontent.com/u/2810941?v=3","gravatar_id":"","url":"https://api.github.com/users/GoogleCloudPlatform","html_url":"https://github.com/GoogleCloudPlatform","followers_url":"https://api.github.com/users/GoogleCloudPlatform/followers","following_url":"https://api.github.com/users/GoogleCloudPlatform/following{/other_user}","gists_url":"https://api.github.com/users/GoogleCloudPlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/GoogleCloudPlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GoogleCloudPlatform/subscriptions","organizations_url":"https://api.github.com/users/GoogleCloudPlatform/orgs","repos_url":"https://api.github.com/users/GoogleCloudPlatform/repos","events_url":"https://api.github.com/users/GoogleCloudPlatform/events{/privacy}","received_events_url":"https://api.github.com/users/GoogleCloudPlatform/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/GoogleCloudPlatform/gcloud-java","description":"Google Cloud Client Library for Java","fork":false,"url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java","forks_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/forks","keys_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/teams","hooks_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/hooks","issue_events_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/issues/events{/number}","events_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/events","assignees_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/assignees{/user}","branches_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/branches{/branch}","tags_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/tags","blobs_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/statuses/{sha}","languages_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/languages","stargazers_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/stargazers","contributors_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/contributors","subscribers_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/subscribers","subscription_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/subscription","commits_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/contents/{+path}","compare_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/merges","archive_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/downloads","issues_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/issues{/number}","pulls_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/pulls{/number}","milestones_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/milestones{/number}","notifications_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/labels{/name}","releases_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/releases{/id}","deployments_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/deployments","created_at":"2014-11-04T17:57:16Z","updated_at":"2016-04-08T02:50:48Z","pushed_at":"2016-04-08T15:50:27Z","git_url":"git://github.com/GoogleCloudPlatform/gcloud-java.git","ssh_url":"git@github.com:GoogleCloudPlatform/gcloud-java.git","clone_url":"https://github.com/GoogleCloudPlatform/gcloud-java.git","svn_url":"https://github.com/GoogleCloudPlatform/gcloud-java","homepage":"http://googlecloudplatform.github.io/gcloud-java/","size":222008,"stargazers_count":83,"watchers_count":83,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":44,"mirror_url":null,"open_issues_count":145,"forks":44,"open_issues":145,"watchers":83,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/pulls/821"},"html":{"href":"https://github.com/GoogleCloudPlatform/gcloud-java/pull/821"},"issue":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/issues/821"},"comments":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/issues/821/comments"},"review_comments":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/pulls/821/comments"},"review_comment":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/pulls/821/commits"},"statuses":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-java/statuses/561b04fbc2c37101a7a071bbca509f3990cfb347"}}}}