Extended test for AsyncActionsExecutor to also test for reset queue after some time

This commit is contained in:
Simon Pamies committed 2015-02-09 19:00:49 +08:00
1 parent 1da9c4eb68
commit 04117ad249
2 files changed
+75 -34

No files matched your search

@@ -5,74 +5,114 @@ import org.junit.Test;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sap.sse.common.Duration;
import com.sap.sse.common.TimePoint;
import com.sap.sse.common.impl.MillisecondsTimePoint;
import com.sap.sse.gwt.client.async.AsyncAction;
import com.sap.sse.gwt.client.async.AsyncActionsExecutor;
/**
*
* Some simple tests for the {@link AsyncActionsExecutor}.
*
* @author Simon Marcel Pamies
*/
public class AsyncActionsExecutorTest extends GWTTestCase {
private AsyncActionsExecutor executor;
private AsyncAction<Void> voidAsyncActionNotTriggeringCallCompleted = new AsyncAction<Void>() {
private class AsyncActionWithExecutionCounter implements AsyncAction<Void> {
private int executionCounter = 0;
private final boolean doSucceed;
private final boolean doFail;
public AsyncActionWithExecutionCounter(boolean doSucceed, boolean doFail) {
this.doFail = doFail;
this.doSucceed = doSucceed;
}
@Override
public void execute(AsyncCallback<Void> callback) {
// do nothing as this action emulates a network failure where
// no onSuccess or onFailure is being triggered
if (doSucceed) {
callback.onSuccess(null);
}
if (doFail) {
callback.onFailure(new RuntimeException());
}
executionCounter++;
}
};
public int getExecutionCounter() {
return executionCounter;
}
}
private AsyncAction<Void> voidAsyncActionSuccessfullyCompleting = new AsyncAction<Void>() {
@Override
public void execute(AsyncCallback<Void> callback) {
callback.onSuccess(null);
}
};
private AsyncAction<Void> voidAsyncActionFailingToComplete = new AsyncAction<Void>() {
@Override
public void execute(AsyncCallback<Void> callback) {
callback.onFailure(new RuntimeException());
}
};
private AsyncCallback<Void> voidAsyncCallbackWithNoCallbackAction = new AsyncCallback<Void>() {
private AsyncCallback<Void> defaultCallback = new AsyncCallback<Void>() {
@Override
public void onFailure(Throwable caught) {
}
@Override
public void onSuccess(Void result) {
}
};
};
private AsyncActionsExecutor executor;
@Test
public void testThatANonTriggeringActionLeadsToPendingCall() {
executor = new AsyncActionsExecutor(/*maxPendingCalls*/6, /*maxPendingCallsPerType*/5,
/*durationAfterToResetQueue*/Duration.ONE_SECOND);
// add a job that will never execute successfully
executor.execute(voidAsyncActionNotTriggeringCallCompleted, voidAsyncCallbackWithNoCallbackAction);
AsyncAction<Void> nonTriggeringAction = new AsyncActionWithExecutionCounter(/*doSucceed*/false, /*doFail*/false);
executor.execute(nonTriggeringAction, defaultCallback);
// now the executor should have one pending action
assertNotNull(executor.getNumberOfPendingActionsPerType(voidAsyncActionNotTriggeringCallCompleted.getClass().getName()));
assertEquals(1, executor.getNumberOfPendingActionsPerType(voidAsyncActionNotTriggeringCallCompleted.getClass().getName()));
assertNotNull(executor.getNumberOfPendingActionsPerType(nonTriggeringAction.getClass().getName()));
assertEquals(1, executor.getNumberOfPendingActionsPerType(nonTriggeringAction.getClass().getName()));
}
@Test
public void testTriggeringActionsLeadingToEmptyPendingQueue() {
public void testTriggeringActionsLeadingToPendingQueue() {
executor = new AsyncActionsExecutor(/*maxPendingCalls*/6, /*maxPendingCallsPerType*/6,
/*durationAfterToResetQueue*/Duration.ONE_HOUR);
// test completing jobs leading to empty pending queue
executor.execute(voidAsyncActionSuccessfullyCompleting, voidAsyncCallbackWithNoCallbackAction);
assertEquals(0, executor.getNumberOfPendingActionsPerType(voidAsyncActionSuccessfullyCompleting.getClass().getName()));
executor.execute(voidAsyncActionFailingToComplete, voidAsyncCallbackWithNoCallbackAction);
assertEquals(0, executor.getNumberOfPendingActionsPerType(voidAsyncActionFailingToComplete.getClass().getName()));
AsyncAction<Void> successfulAction = new AsyncActionWithExecutionCounter(/*doSucceed*/true, /*doFail*/false);
executor.execute(successfulAction, defaultCallback);
assertEquals(0, executor.getNumberOfPendingActionsPerType(successfulAction.getClass().getName()));
AsyncAction<Void> failingAction = new AsyncActionWithExecutionCounter(/*doSucceed*/false, /*doFail*/true);
executor.execute(failingAction, defaultCallback);
assertEquals(0, executor.getNumberOfPendingActionsPerType(failingAction.getClass().getName()));
// test that max pending calls is being used
AsyncAction<Void> nonTriggeringAction = new AsyncActionWithExecutionCounter(/*doSucceed*/false, /*doFail*/false);
for (int i=0;i<=11;i++) {
executor.execute(voidAsyncActionNotTriggeringCallCompleted, voidAsyncCallbackWithNoCallbackAction);
executor.execute(nonTriggeringAction, defaultCallback);
}
assertEquals(6, executor.getNumberOfPendingActions());
assertEquals(6, executor.getNumberOfPendingActionsPerType(voidAsyncActionNotTriggeringCallCompleted.getClass().getName()));
assertEquals(6, executor.getNumberOfPendingActionsPerType(nonTriggeringAction.getClass().getName()));
}
@Test
public void testAutomaticReleaseOfActionsImmediatelyAndAfterSomeTime() {
executor = new AsyncActionsExecutor(/*maxPendingCalls*/6, /*maxPendingCallsPerType*/6,
/*durationAfterToResetQueue*/Duration.ONE_SECOND.times(2));
// execute action that does not get through but then retries
AsyncActionWithExecutionCounter nonTriggeringAction = new AsyncActionWithExecutionCounter(/*doSucceed*/false, /*doFail*/false);
assertEquals(0, nonTriggeringAction.getExecutionCounter());
TimePoint startOfExecution = MillisecondsTimePoint.now();
executor.execute(nonTriggeringAction, defaultCallback);
assertEquals(1, nonTriggeringAction.getExecutionCounter());
for (int i=0;i<=4;i++) {
executor.execute(nonTriggeringAction, defaultCallback);
}
assertEquals(6, nonTriggeringAction.getExecutionCounter());
assertEquals(6, executor.getNumberOfPendingActions());
assertEquals(6, executor.getNumberOfPendingActionsPerType(nonTriggeringAction.getClass().getName()));
// now the action should be retriggered - we can not use Thread.sleep() here as it is not
// compilable by the GWT compiler. Using a hack to wait a little bit.
while (MillisecondsTimePoint.now().minus(Duration.ONE_SECOND.times(3)).before(startOfExecution)) {
// waiting...
}
executor.execute(nonTriggeringAction, defaultCallback);
assertEquals(7, nonTriggeringAction.getExecutionCounter());
assertEquals(1, executor.getNumberOfPendingActions());
assertEquals(1, executor.getNumberOfPendingActionsPerType(nonTriggeringAction.getClass().getName()));
}
@Override