You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2014/07/14 17:36:01 UTC

[05/11] git commit: Fix occasionally failing tests - relaying on non-guaranteed time delays.

Fix occasionally failing tests - relaying on non-guaranteed time delays.

The code relies on having at least 1ms between the separate test steps
which is non-guaranteed. Include artificial slowdown steps to guarantee
1ms intervals. The concrete problem is that the API doesn't accept
times in the future, while the test wants to check the state 1ms post-event.


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/388087d5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/388087d5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/388087d5

Branch: refs/heads/master
Commit: 388087d52581bf76b2d4c082308ffef1732a0f82
Parents: cec9243
Author: Svetoslav Neykov <sv...@cloudsoftcorp.com>
Authored: Thu Jul 10 19:33:37 2014 +0300
Committer: Svetoslav Neykov <sv...@cloudsoftcorp.com>
Committed: Thu Jul 10 19:33:37 2014 +0300

----------------------------------------------------------------------
 .../rest/resources/UsageResourceTest.java       | 28 ++++++++++++++++----
 1 file changed, 23 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/388087d5/usage/rest-server/src/test/java/brooklyn/rest/resources/UsageResourceTest.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/test/java/brooklyn/rest/resources/UsageResourceTest.java b/usage/rest-server/src/test/java/brooklyn/rest/resources/UsageResourceTest.java
index e73f4cf..82629da 100644
--- a/usage/rest-server/src/test/java/brooklyn/rest/resources/UsageResourceTest.java
+++ b/usage/rest-server/src/test/java/brooklyn/rest/resources/UsageResourceTest.java
@@ -100,14 +100,17 @@ public class UsageResourceTest extends BrooklynRestResourceTest {
         assertAppUsage(usage, appId, ImmutableList.of(Status.STARTING, Status.RUNNING), roundDown(preStart), postStart);
 
         // check app ignored if endDate before app started
-        response = client().resource("/v1/usage/applications?start="+0+"&end="+preStart.getTime()).get(ClientResponse.class);
+        response = client().resource("/v1/usage/applications?start="+0+"&end="+(preStart.getTime()-1)).get(ClientResponse.class);
         assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
         usages = response.getEntity(new GenericType<List<UsageStatistics>>() {});
         assertTrue(Iterables.isEmpty(usages), "usages="+usages);
+        
+        long afterPostStart = postStart.getTime()+1;
+        waitForFuture(afterPostStart);
 
         // check app start and end date truncated, even if running for longer
         // note that start==end means we get a snapshot of the apps in use at that exact time.
-        response = client().resource("/v1/usage/applications?start="+postStart.getTime()+"&end="+postStart.getTime()).get(ClientResponse.class);
+        response = client().resource("/v1/usage/applications?start="+afterPostStart+"&end="+afterPostStart).get(ClientResponse.class);
         assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
         usages = response.getEntity(new GenericType<List<UsageStatistics>>() {});
         usage = Iterables.getOnlyElement(usages);
@@ -127,7 +130,10 @@ public class UsageResourceTest extends BrooklynRestResourceTest {
         assertAppUsage(usage, appId, ImmutableList.of(Status.STARTING, Status.RUNNING, Status.DESTROYED), roundDown(preStart), postDelete);
         assertAppUsage(ImmutableList.copyOf(usage.getStatistics()).subList(2, 3), appId, ImmutableList.of(Status.DESTROYED), roundDown(preDelete), postDelete);
 
-        response = client().resource("/v1/usage/applications?start=" + (postDelete.getTime()+1)).get(ClientResponse.class);
+        long afterPostDelete = postDelete.getTime()+1;
+        waitForFuture(afterPostDelete);
+        
+        response = client().resource("/v1/usage/applications?start=" + afterPostDelete).get(ClientResponse.class);
         assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
         usages = response.getEntity(new GenericType<List<UsageStatistics>>() {});
         assertTrue(Iterables.isEmpty(usages), "usages="+usages);
@@ -193,7 +199,9 @@ public class UsageResourceTest extends BrooklynRestResourceTest {
         assertAppUsage(ImmutableList.copyOf(usage.getStatistics()).subList(2, 3), appId, ImmutableList.of(Status.DESTROYED), roundDown(preDelete), postDelete);
 
         // Deleted app not returned if terminated before time range begins
-        response = client().resource("/v1/usage/applications/" + appId +"?start=" + (postDelete.getTime()+1)).get(ClientResponse.class);
+        long afterPostDelete = postDelete.getTime()+1;
+        waitForFuture(afterPostDelete);
+        response = client().resource("/v1/usage/applications/" + appId +"?start=" + afterPostDelete).get(ClientResponse.class);
         assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
         usage = response.getEntity(new GenericType<UsageStatistics>() {});
         assertTrue(usage.getStatistics().isEmpty(), "usages="+usage);
@@ -277,7 +285,9 @@ public class UsageResourceTest extends BrooklynRestResourceTest {
         assertMachineUsage(ImmutableList.copyOf(usage.getStatistics()).subList(1,2), appId, machine.getId(), ImmutableList.of(Status.DESTROYED), roundDown(preStop), postStop);
 
         // Terminated machines ignored if terminated since start-time
-        response = client().resource("/v1/usage/applications?start=" + (postStop.getTime()+1)).get(ClientResponse.class);
+        long futureTime = postStop.getTime()+1;
+        waitForFuture(futureTime);
+        response = client().resource("/v1/usage/applications?start=" + futureTime).get(ClientResponse.class);
         assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
         usages = response.getEntity(new GenericType<List<UsageStatistics>>() {});
         assertTrue(Iterables.isEmpty(usages), "usages="+usages);
@@ -415,4 +425,12 @@ public class UsageResourceTest extends BrooklynRestResourceTest {
             getManagementContext().getLocationManager().unmanage(machine);
         }
     }
+
+    private void waitForFuture(long futureTime) throws InterruptedException {
+        long now;
+        while ((now = System.currentTimeMillis()) < futureTime) {
+            Thread.sleep(futureTime - now);
+        }
+    }
+
 }