From a1e882ea21947e5218b6ff9e6122baa41050671a Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 9 Feb 2016 11:39:43 -0800 Subject: [PATCH] Fix flaky test --- .../com/google/appengine/sparkdemo/Main.java | 10 +++++++++- .../google/appengine/sparkdemo/UserService.java | 17 ++++++++--------- .../appengine/sparkdemo/UserControllerTest.java | 3 ++- .../appengine/sparkdemo/UserServiceTest.java | 5 +++-- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/Main.java b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/Main.java index 9ca3abdf356..9864d2ef7c2 100644 --- a/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/Main.java +++ b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/Main.java @@ -27,7 +27,15 @@ public class Main { */ public static void main(String[] args) { port(8080); + String kind = "DemoUser"; + if (args != null) { + for (String arg : args) { + if (arg.startsWith("kind=")) { + kind = arg.substring("kind=".length()); + } + } + } UserController userController = - new UserController(new UserService(DatastoreOptions.defaultInstance().service())); + new UserController(new UserService(DatastoreOptions.defaultInstance().service(), kind)); } } diff --git a/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java index 21b58ecef1f..99f24fdf3e2 100644 --- a/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java +++ b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java @@ -20,7 +20,6 @@ import com.google.gcloud.datastore.Datastore; import com.google.gcloud.datastore.Entity; -import com.google.gcloud.datastore.FullEntity; import com.google.gcloud.datastore.Key; import com.google.gcloud.datastore.KeyFactory; import com.google.gcloud.datastore.Query; @@ -31,11 +30,14 @@ public class UserService { - private static final String KINDNAME = "DEMO_USER"; - private Datastore datastore; + private final Datastore datastore; + private final KeyFactory keyFactory; + private final String kind; - public UserService(Datastore datastore) { + public UserService(Datastore datastore, String kind) { this.datastore = datastore; + this.keyFactory = datastore.newKeyFactory().kind(kind); + this.kind = kind; } /** @@ -43,7 +45,7 @@ public UserService(Datastore datastore) { */ public List getAllUsers() { Query query = - Query.gqlQueryBuilder(Query.ResultType.ENTITY, "SELECT * FROM " + KINDNAME).build(); + Query.gqlQueryBuilder(Query.ResultType.ENTITY, "SELECT * FROM " + kind).build(); QueryResults results = datastore.run(query); List users = new ArrayList<>(); while (results.hasNext()) { @@ -60,9 +62,8 @@ public List getAllUsers() { public User createUser(String name, String email) { failIfInvalid(name, email); User user = new User(name, email); - KeyFactory keyFactory = datastore.newKeyFactory().kind(KINDNAME); Key key = keyFactory.newKey(user.getId()); - FullEntity entity = Entity.builder(key) + Entity entity = Entity.builder(key) .set("id", user.getId()) .set("name", name) .set("email", email) @@ -75,7 +76,6 @@ public User createUser(String name, String email) { * Delete a user from Cloud Datastore. */ public String deleteUser(String id) { - KeyFactory keyFactory = datastore.newKeyFactory().kind(KINDNAME); Key key = keyFactory.newKey(id); datastore.delete(key); return "ok"; @@ -86,7 +86,6 @@ public String deleteUser(String id) { */ public User updateUser(String id, String name, String email) { failIfInvalid(name, email); - KeyFactory keyFactory = datastore.newKeyFactory().kind(KINDNAME); Key key = keyFactory.newKey(id); Entity entity = datastore.get(key); if (entity == null) { diff --git a/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java b/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java index 5be8925b2f8..2c252a01cf6 100644 --- a/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java +++ b/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java @@ -34,6 +34,7 @@ import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; +import java.util.UUID; public class UserControllerTest { @@ -43,7 +44,7 @@ public class UserControllerTest { @BeforeClass public static void beforeClass() { - Main.main(null); + Main.main(new String[] {"kind=DemoUser" + UUID.randomUUID().toString().replaceAll("-", "")}); Spark.awaitInitialization(); } diff --git a/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java b/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java index 375c2dfdf9e..eeb4fae6308 100644 --- a/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java +++ b/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java @@ -47,7 +47,8 @@ public class UserServiceTest { private static final String USER_NAME = "myName"; private static final String USER_EMAIL = "my@email.com"; private static final User USER = new User(USER_ID, USER_NAME, USER_EMAIL); - private static final Key USER_KEY = Key.builder(PROJECT_ID, "DEMO_USER", USER_ID).build(); + private static final String KIND = "DemoUser"; + private static final Key USER_KEY = Key.builder(PROJECT_ID, KIND, USER_ID).build(); private static final Entity USER_RECORD = Entity.builder(USER_KEY) .set("id", USER_ID) .set("name", USER_NAME) @@ -67,7 +68,7 @@ public static void beforeClass() throws IOException, InterruptedException { .host("http://localhost:" + PORT) .build() .service(); - userService = new UserService(datastore); + userService = new UserService(datastore, KIND); } @Before