You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by to...@apache.org on 2014/07/31 00:00:18 UTC
[02/10] git commit: Fixes tests to be clearer
Fixes tests to be clearer
Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/6c8416d2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/6c8416d2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/6c8416d2
Branch: refs/heads/master
Commit: 6c8416d29c1158e5a2fdb12e4fd1213f959221e7
Parents: 4065b5e
Author: Todd Nine <to...@apache.org>
Authored: Mon Jul 28 16:15:25 2014 -0600
Committer: Todd Nine <to...@apache.org>
Committed: Mon Jul 28 16:15:25 2014 -0600
----------------------------------------------------------------------
.../usergrid/batch/job/SchedulerRuntime1IT.java | 5 +-
.../usergrid/batch/job/SchedulerRuntime2IT.java | 13 ++--
.../usergrid/batch/job/SchedulerRuntime3IT.java | 4 +-
.../usergrid/batch/job/SchedulerRuntime4IT.java | 11 +++-
.../usergrid/batch/job/SchedulerRuntime5IT.java | 4 +-
.../usergrid/batch/job/SchedulerRuntime6IT.java | 8 ++-
.../usergrid/batch/job/SchedulerRuntime7IT.java | 11 ++--
.../usergrid/batch/job/SchedulerRuntime8IT.java | 4 +-
.../usergrid/batch/job/TestJobListener.java | 67 +++++++-------------
.../usergrid/batch/job/TestJobListenerTest.java | 7 +-
10 files changed, 69 insertions(+), 65 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6c8416d2/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime1IT.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime1IT.java b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime1IT.java
index 0cde716..a24d12e 100644
--- a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime1IT.java
+++ b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime1IT.java
@@ -42,6 +42,9 @@ public class SchedulerRuntime1IT extends AbstractSchedulerRuntimeIT {
// set the counter job latch size
counterJob.setLatch( getCount() );
+
+ getJobListener().setExpected( getCount() );
+
for ( int i = 0; i < getCount(); i++ ) {
scheduler.createJob( "countdownLatch", System.currentTimeMillis(), new JobData() );
}
@@ -61,7 +64,7 @@ public class SchedulerRuntime1IT extends AbstractSchedulerRuntimeIT {
// now:
// blockTilDone look into the JobListener hook and blocked until jobs are completed.
// TODO : need a retry count so it doesn't reblock forever
- while (!getJobListener().blockTilDone(getCount(), waitTime)) {
+ while (!getJobListener().blockTilDone(waitTime)) {
logger.warn("Jobs not yet finished after waited {}, block again" , waitTime);
}
assertEquals( "Expected success job: " + getCount()+ ". Actual :" + getJobListener().getSuccessCount() + ". Total count: " + getJobListener().getDoneCount() , getCount() , getJobListener().getSuccessCount() );
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6c8416d2/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime2IT.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime2IT.java b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime2IT.java
index c05bdb8..f0e1975 100644
--- a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime2IT.java
+++ b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime2IT.java
@@ -42,15 +42,13 @@ public class SchedulerRuntime2IT extends AbstractSchedulerRuntimeIT {
// set the counter job latch size
counterJob.setLatch( getCount() );
+ getJobListener().setExpected( getCount() );
+
for ( int i = 0; i < getCount(); i++ ) {
scheduler.createJob( "countdownLatch", System.currentTimeMillis(), new JobData() );
}
- // previously:
- // now wait until everything fires
- // boolean waited = getJobListener().blockTilDone( getCount(), 15000L );
- // assertTrue( "Jobs ran", waited );
- // assertTrue( getCount() + " successful jobs ran", getCount() == getJobListener().getSuccessCount() );
+
// now:
// note that the waitForCount only wait for job execution. It does NOT wait for job Completion
@@ -60,7 +58,7 @@ public class SchedulerRuntime2IT extends AbstractSchedulerRuntimeIT {
// now:
// blockTilDone look into the JobListener hook and blocked until jobs are completed.
// TODO : need a retry count so it doesn't reblock forever
- while (!getJobListener().blockTilDone(getCount(), waitTime)) {
+ while (!getJobListener().blockTilDone(waitTime)) {
logger.warn("Jobs not yet finished after waited {}, block again" , waitTime);
}
assertEquals( "Expected success job: " + getCount()+ ". Actual :" + getJobListener().getSuccessCount() + ". Total count: " + getJobListener().getDoneCount() , getCount() , getJobListener().getSuccessCount() );
@@ -69,6 +67,7 @@ public class SchedulerRuntime2IT extends AbstractSchedulerRuntimeIT {
// set the counter job latch size
counterJob.setLatch( getCount() );
+ getJobListener().setExpected( getCount() );
for ( int i = 0; i < getCount(); i++ ) {
scheduler.createJob( "countdownLatch", System.currentTimeMillis(), new JobData() );
@@ -90,7 +89,7 @@ public class SchedulerRuntime2IT extends AbstractSchedulerRuntimeIT {
// now:
// blockTilDone look into the JobListener hook and blocked until jobs are completed.
// TODO : need a retry count so it doesn't reblock forever
- while (!getJobListener().blockTilDone(getCount(), waitTime)) {
+ while (!getJobListener().blockTilDone(waitTime)) {
logger.warn("Jobs not yet finished after waited {}, block again" , waitTime);
}
assertEquals( "Expected success job: " +2 * getCount()+ ". Actual :" + getJobListener().getSuccessCount() + ". Total count: " + getJobListener().getDoneCount() , 2 * getCount() , getJobListener().getSuccessCount() );
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6c8416d2/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime3IT.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime3IT.java b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime3IT.java
index e244668..a423729 100644
--- a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime3IT.java
+++ b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime3IT.java
@@ -47,11 +47,13 @@ public class SchedulerRuntime3IT extends AbstractSchedulerRuntimeIT {
job.setLatch( latchValue );
+ getJobListener().setExpected( 3 );
+
JobData returned = scheduler.createJob( "failureJobExceuction", System.currentTimeMillis(), new JobData() );
// sleep until the job should have failed. We sleep 1 extra cycle just to
// make sure we're not racing the test
- boolean waited = getJobListener().blockTilDone( 3, ( failCount + 2 ) * sleepTime + 5000L );
+ boolean waited = getJobListener().blockTilDone(( failCount + 2 ) * sleepTime + 5000L );
//we shouldn't trip the latch. It should fail failCount times, and not run again
assertTrue( "Jobs ran", waited );
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6c8416d2/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime4IT.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime4IT.java b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime4IT.java
index 9f6e526..8798462 100644
--- a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime4IT.java
+++ b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime4IT.java
@@ -47,13 +47,18 @@ public class SchedulerRuntime4IT extends AbstractSchedulerRuntimeIT {
DelayExecution job = cassandraResource.getBean( "delayExecution", DelayExecution.class );
job.setTimeout( customRetry );
- job.setLatch( delayCount + 1 );
+
+ int runCount = delayCount +1;
+
+ job.setLatch( runCount );
+
+ getJobListener().setExpected( runCount );
JobData returned = scheduler.createJob( "delayExecution", System.currentTimeMillis(), new JobData() );
// sleep until the job should have failed. We sleep 1 extra cycle just to
// make sure we're not racing the test
- boolean waited = getJobListener().blockTilDone( 3, 5000L + sleepTime * 2 );
+ boolean waited = getJobListener().blockTilDone( 50000L + sleepTime * 2 );
assertTrue( "Job ran to complete", waited );
@@ -62,7 +67,7 @@ public class SchedulerRuntime4IT extends AbstractSchedulerRuntimeIT {
// we should have only marked this as run once since we delayed furthur execution
// we should have only marked this as run once
assertEquals( 1, stat.getTotalAttempts() );
- assertEquals( delayCount + 1, stat.getRunCount() );
+ assertEquals( runCount, stat.getRunCount() );
assertEquals( delayCount, stat.getDelayCount() );
}
}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6c8416d2/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime5IT.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime5IT.java b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime5IT.java
index a51336c..f646e4c 100644
--- a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime5IT.java
+++ b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime5IT.java
@@ -50,11 +50,13 @@ public class SchedulerRuntime5IT extends AbstractSchedulerRuntimeIT {
job.setTimeout( customRetry );
job.setLatch( heartbeatCount + 1 );
+ getJobListener().setExpected( 1 );
+
JobData returned = scheduler.createJob( "delayHeartbeat", System.currentTimeMillis(), new JobData() );
// sleep until the job should have failed. We sleep 1 extra cycle just to
// make sure we're not racing the test
- boolean waited = getJobListener().blockTilDone( 1, customRetry * ( heartbeatCount * 2 ) + 5000L );
+ boolean waited = getJobListener().blockTilDone( customRetry * ( heartbeatCount * 2 ) + 5000L );
assertTrue( "Job ran to complete", waited );
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6c8416d2/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime6IT.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime6IT.java b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime6IT.java
index 79ec3ea..6dc7091 100644
--- a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime6IT.java
+++ b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime6IT.java
@@ -54,15 +54,19 @@ public class SchedulerRuntime6IT extends AbstractSchedulerRuntimeIT {
job.setLatch( numberOfRuns );
job.setDelay( sleepTime );
+
+ getJobListener().setExpected(1);
+
JobData returned = scheduler.createJob( "onlyOnceExceution", System.currentTimeMillis(), new JobData() );
// sleep until the job should have failed. We sleep 1 extra cycle just to
// make sure we're not racing the test
- boolean waited = getJobListener().blockTilDone( 1, customRetry * numberOfRuns * 2 + 5000L );
+ boolean waited = getJobListener().blockTilDone( customRetry * numberOfRuns * 2 + 5000L );
assertTrue( "Job ran twice", waited );
+ getJobListener().setExpected( 2 );
//reset our latch immediately for further tests
job.setLatch( numberOfRuns );
@@ -82,7 +86,7 @@ public class SchedulerRuntime6IT extends AbstractSchedulerRuntimeIT {
//now wait again to see if the job fires one more time, it shouldn't
- waited = getJobListener().blockTilDone( 2, customRetry * numberOfRuns * 2 );
+ waited = getJobListener().blockTilDone( customRetry * numberOfRuns * 2 );
assertFalse( "Job ran twice", waited );
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6c8416d2/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime7IT.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime7IT.java b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime7IT.java
index 0f64ba8..befe5bf 100644
--- a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime7IT.java
+++ b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime7IT.java
@@ -24,6 +24,7 @@ import org.apache.usergrid.persistence.entities.JobStat;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -31,7 +32,7 @@ import static org.junit.Assert.assertTrue;
* Class to test job runtimes
*/
@Concurrent
-@org.junit.Ignore( "Todd you need to take a look at this since it's not clear to me what was intended in this test." )
+//@org.junit.Ignore( "Todd you need to take a look at this since it's not clear to me what was intended in this test." )
public class SchedulerRuntime7IT extends AbstractSchedulerRuntimeIT {
/** Test that we're only running once, even when a job exceeds the heartbeat time */
@@ -52,16 +53,18 @@ public class SchedulerRuntime7IT extends AbstractSchedulerRuntimeIT {
job.setLatch( numberOfRuns );
job.setDelay( sleepTime );
+ getJobListener().setExpected( 2 );
+
JobData returned =
scheduler.createJob( "onlyOnceUnlockOnFailExceution", System.currentTimeMillis(), new JobData() );
// sleep until the job should have failed. We sleep 1 extra cycle just to make sure we're not racing the test
- boolean waited = getJobListener().blockTilDone( 1, runLoop * numberOfRuns * 2 + 5000L );
+ boolean waited = getJobListener().blockTilDone( runLoop * numberOfRuns * 2 + 5000L );
- assertTrue( "Job threw exception", waited );
+ assertTrue( "Both runs executed" , waited);
assertTrue( "Job failed", getJobListener().getFailureCount() == 1 );
- assertTrue( "No Job succeeded", getJobListener().getSuccessCount() == 0 );
+ assertTrue( "No Job succeeded", getJobListener().getSuccessCount() == 1 );
JobStat stat = scheduler.getStatsForJob( returned.getJobName(), returned.getUuid() );
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6c8416d2/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime8IT.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime8IT.java b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime8IT.java
index 593f94c..3c44e05 100644
--- a/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime8IT.java
+++ b/stack/core/src/test/java/org/apache/usergrid/batch/job/SchedulerRuntime8IT.java
@@ -56,6 +56,8 @@ public class SchedulerRuntime8IT extends AbstractSchedulerRuntimeIT {
test.setProperty( "stringprop", "test" );
test.setProperty( "notificationId", notificationId );
+ getJobListener().setExpected( 1 );
+
JobData saved = scheduler.createJob( "countdownLatch", fireTime, test );
// now query and make sure it equals the saved value
@@ -88,7 +90,7 @@ public class SchedulerRuntime8IT extends AbstractSchedulerRuntimeIT {
long waitTime = Math.max( 0, fireTime - System.currentTimeMillis() + 1000 );
- boolean waited = getJobListener().blockTilDone( 1, waitTime );
+ boolean waited = getJobListener().blockTilDone( waitTime );
assertFalse( "Job ran ", waited );
}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6c8416d2/stack/core/src/test/java/org/apache/usergrid/batch/job/TestJobListener.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/batch/job/TestJobListener.java b/stack/core/src/test/java/org/apache/usergrid/batch/job/TestJobListener.java
index 484bcf8..f212311 100644
--- a/stack/core/src/test/java/org/apache/usergrid/batch/job/TestJobListener.java
+++ b/stack/core/src/test/java/org/apache/usergrid/batch/job/TestJobListener.java
@@ -17,10 +17,13 @@
package org.apache.usergrid.batch.job;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+
import org.apache.usergrid.batch.JobExecution;
import org.apache.usergrid.batch.service.JobListener;
@@ -30,14 +33,19 @@ import org.apache.usergrid.batch.service.JobListener;
*/
public class TestJobListener implements JobListener {
// public static final long WAIT_MAX_MILLIS = 250;
- public static final long WAIT_MAX_MILLIS = 60000L; // max wait 1 minutes
+ public static final long WAIT_MAX_MILLIS = 60000L; // max wait 1 minutes
private static final Logger LOG = LoggerFactory.getLogger( TestJobListener.class );
private AtomicInteger submittedCounter = new AtomicInteger();
private AtomicInteger failureCounter = new AtomicInteger();
private AtomicInteger successCounter = new AtomicInteger();
- private final Object lock = new Object();
+ private CountDownLatch latch;
+
+ public void setExpected(int count){
+ latch = new CountDownLatch( count );
+ }
+
public int getSubmittedCount() {
return submittedCounter.get();
}
@@ -59,62 +67,35 @@ public class TestJobListener implements JobListener {
public void onSubmit( JobExecution execution ) {
- LOG.debug( "Job execution {} submitted with count {}.", execution,
- submittedCounter.incrementAndGet() );
+ LOG.debug( "Job execution {} submitted with count {}.", execution, submittedCounter.incrementAndGet() );
}
public void onSuccess( JobExecution execution ) {
- LOG.debug( "Job execution {} succeeded with count {}.", execution,
- successCounter.incrementAndGet() );
+ LOG.debug( "Job execution {} succeeded with count {}.", execution, successCounter.incrementAndGet() );
+
+ latch.countDown();
}
public void onFailure( JobExecution execution ) {
- LOG.debug( "Job execution {} failed with count {}.", execution,
- failureCounter.incrementAndGet() );
- }
+ LOG.debug( "Job execution {} failed with count {}.", execution, failureCounter.incrementAndGet() );
+ latch.countDown();
+ }
/**
* block until submitted jobs are all accounted for.
- *
+ *
* @param jobCount total submitted job
* @param idleTime idleTime in millisecond.
- * @return true when all jobs are completed (could be succeed or failed) within the given idleTime range, or {@value #WAIT_MAX_MILLIS}, whichever is smaller
- * @throws InterruptedException
+ *
+ * @return true when all jobs are completed (could be succeed or failed) within the given idleTime range, or {@value
+ * #WAIT_MAX_MILLIS}, whichever is smaller
*/
- public boolean blockTilDone( int jobCount, long idleTime ) throws InterruptedException
- {
- final long waitTime = Math.min( idleTime, WAIT_MAX_MILLIS );
- long lastChangeTime = System.currentTimeMillis();
- long timeNotChanged = 0;
- int currentCount;
- int startCount = getDoneCount();
-
- do {
- currentCount = getDoneCount();
-
- if ( startCount == currentCount ) {
-// if ( timeNotChanged > idleTime ) {
- if ( timeNotChanged > waitTime ) {
- return false;
- }
-
- timeNotChanged = System.currentTimeMillis() - lastChangeTime;
- }
- else {
- timeNotChanged = 0;
- startCount = currentCount;
- lastChangeTime = System.currentTimeMillis();
- }
-
- synchronized ( lock ) {
- lock.wait( waitTime );
- }
- } while ( currentCount < jobCount );
-
- return true;
+ public boolean blockTilDone( long idleTime ) throws InterruptedException {
+
+ return latch.await( idleTime, TimeUnit.MILLISECONDS );
}
}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6c8416d2/stack/core/src/test/java/org/apache/usergrid/batch/job/TestJobListenerTest.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/batch/job/TestJobListenerTest.java b/stack/core/src/test/java/org/apache/usergrid/batch/job/TestJobListenerTest.java
index f2705d6..8d72210 100644
--- a/stack/core/src/test/java/org/apache/usergrid/batch/job/TestJobListenerTest.java
+++ b/stack/core/src/test/java/org/apache/usergrid/batch/job/TestJobListenerTest.java
@@ -110,7 +110,8 @@ public class TestJobListenerTest {
TestJobListener listener = new TestJobListener();
long waitTime = 1000L;
long startTime = System.currentTimeMillis();
- listener.blockTilDone( 100, waitTime );
+ listener.setExpected( 100 );
+ listener.blockTilDone( waitTime );
long elapsedTime = System.currentTimeMillis() - startTime;
LOG.info( "IdleOut in {} millis", elapsedTime );
// assertTrue( elapsedTime >= ( 1000L + TestJobListener.WAIT_MAX_MILLIS ) );
@@ -122,6 +123,8 @@ public class TestJobListenerTest {
public void testHitCount() throws InterruptedException {
final TestJobListener listener = new TestJobListener();
+ listener.setExpected( 1000 );
+
Thread t = new Thread( new Runnable() {
@Override
public void run() {
@@ -138,6 +141,6 @@ public class TestJobListenerTest {
});
t.start();
- assertTrue( listener.blockTilDone( 1000, 1000L ) );
+ assertTrue( listener.blockTilDone( 1000L ) );
}
}