Skip to content

Commit

Permalink
Merge pull request #96 from GoogleCloudPlatform/fix-flaky-test
Browse files Browse the repository at this point in the history
Fix consistency issue with sparkjava demo tests.
  • Loading branch information
tswast committed Feb 23, 2016
2 parents 27944b7 + 82e1abe commit 860a9b3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion managed_vms/sparkjava/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<dependency>
<groupId>com.google.gcloud</groupId>
<artifactId>gcloud-java</artifactId>
<version>0.1.3</version>
<version>0.1.4</version>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public class UserController {
public UserController(final UserService userService) {
Spark.staticFileLocation("/public");

get("/api/users", (req, res) -> userService.getAllUsers(), UserController::toJson);
get("/api/users", (req, res) -> userService.getAllUsers(), json());

get("/api/users/:id", (req, res) -> userService.getUser(req.params(":id")), json());

post("/api/users",
(req, res) -> userService.createUser(req.queryParams("name"), req.queryParams("email")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public List<User> getAllUsers() {
return users;
}

/**
* Return the user with the given id.
*/
User getUser(String id) {
Entity entity = datastore.get(keyFactory.newKey(id));
return entity == null
? null
: new User(entity.getString("id"), entity.getString("name"), entity.getString("email"));
}

/**
* Create a new user and add it to Cloud Datastore.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.gson.Gson;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -50,13 +52,14 @@ public static void beforeClass() {

@Before
public void setUp() throws IOException {
User[] allUsers = getAllUsers();
for (User user : allUsers) {
deleteUser(user.getId());
}
userId = createUser(USER_NAME, USER_EMAIL).getId();
}

@After
public void tearDown() throws IOException {
deleteUser(userId);
}

@AfterClass
public static void afterClass() {
Spark.stop();
Expand All @@ -65,11 +68,7 @@ public static void afterClass() {
@Test
public void testGetAllUsers() throws IOException {
User[] users = getAllUsers();
assertEquals(1, users.length);
User user = users[0];
assertEquals(userId, user.getId());
assertEquals(USER_NAME, user.getName());
assertEquals(USER_EMAIL, user.getEmail());
assertTrue(users.length <= 1);
}

@Test
Expand All @@ -92,8 +91,9 @@ public void testCreateUserInvalidRequest() {

@Test
public void testDeleteUser() throws IOException {
assertNotNull(getUser(userId));
assertEquals("\"ok\"", deleteUser(userId));
assertEquals(0, getAllUsers().length);
assertNull(getUser(userId));
}

@Test
Expand Down Expand Up @@ -127,6 +127,10 @@ private static String deleteUser(String id) throws IOException {
return executeRequest("DELETE", "/api/users/" + id);
}

private static User getUser(String id) throws IOException {
return new Gson().fromJson(executeRequest("GET", "/api/users/" + id), User.class);
}

private static User[] getAllUsers() throws IOException {
return new Gson().fromJson(executeRequest("GET", "/api/users"), User[].class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class UserServiceTest {
@BeforeClass
public static void beforeClass() throws IOException, InterruptedException {
if (!LocalGcdHelper.isActive(PROJECT_ID, PORT)) {
gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT);
gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT, 1.0);
}
datastore = DatastoreOptions.builder()
.projectId(PROJECT_ID)
Expand Down

0 comments on commit 860a9b3

Please sign in to comment.