Search in sources :

Example 1 with CancellableSleepAction

use of org.alfresco.repo.action.ActionServiceImplTest.CancellableSleepAction in project alfresco-repository by Alfresco.

the class ActionTrackingServiceImplTest method testCancellation.

/**
 * Cancel related
 */
public void testCancellation() throws Exception {
    // Ensure we get the right answers checking
    CancellableSleepAction sleepAction1 = (CancellableSleepAction) createWorkingSleepAction(null);
    CancellableSleepAction sleepAction2 = (CancellableSleepAction) createWorkingSleepAction(null);
    actionTrackingService.recordActionExecuting(sleepAction1);
    actionTrackingService.recordActionExecuting(sleepAction2);
    assertEquals(false, actionTrackingService.isCancellationRequested(sleepAction1));
    assertEquals(false, actionTrackingService.isCancellationRequested(sleepAction2));
    // Cancel with the action
    actionTrackingService.requestActionCancellation(sleepAction1);
    assertEquals(true, actionTrackingService.isCancellationRequested(sleepAction1));
    assertEquals(false, actionTrackingService.isCancellationRequested(sleepAction2));
    // Cancel with the summary
    ExecutionSummary s2 = ActionTrackingServiceImpl.buildExecutionSummary(sleepAction2);
    actionTrackingService.requestActionCancellation(s2);
    assertEquals(true, actionTrackingService.isCancellationRequested(sleepAction1));
    assertEquals(true, actionTrackingService.isCancellationRequested(sleepAction2));
    // If the action had gone missing from the cache,
    // then a check will put it back
    CancellableSleepAction sleepAction3 = (CancellableSleepAction) createWorkingSleepAction(null);
    String key3 = ActionTrackingServiceImpl.generateCacheKey(sleepAction3);
    assertNull(executingActionsCache.get(key3));
    assertEquals(false, actionTrackingService.isCancellationRequested(sleepAction3));
    assertNotNull(executingActionsCache.get(key3));
    executingActionsCache.remove(key3);
    assertNull(executingActionsCache.get(key3));
    assertEquals(false, actionTrackingService.isCancellationRequested(sleepAction3));
    assertNotNull(executingActionsCache.get(key3));
    actionTrackingService.requestActionCancellation(sleepAction3);
    assertEquals(true, actionTrackingService.isCancellationRequested(sleepAction3));
    assertNotNull(executingActionsCache.get(key3));
    // Now have one execute and cancel it, ensure it does
    final SleepActionExecuter sleepActionExec = (SleepActionExecuter) ctx.getBean(SleepActionExecuter.NAME);
    sleepActionExec.setSleepMs(10000);
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    executingActionsCache.remove(key3);
    this.actionService.executeAction(sleepAction3, this.nodeRef, false, true);
    // End the transaction. Should allow the async action
    // to be started
    txn.commit();
    Thread.sleep(150);
    // Get the updated key, and check
    key3 = ActionTrackingServiceImpl.generateCacheKey(sleepAction3);
    ExecutionSummary s3 = ActionTrackingServiceImpl.buildExecutionSummary(key3);
    assertEquals(false, actionTrackingService.isCancellationRequested(sleepAction3));
    assertEquals(false, actionTrackingService.getExecutionDetails(s3).isCancelRequested());
    assertNotNull(executingActionsCache.get(key3));
    actionTrackingService.requestActionCancellation(sleepAction3);
    assertEquals(true, actionTrackingService.isCancellationRequested(sleepAction3));
    assertEquals(true, actionTrackingService.getExecutionDetails(s3).isCancelRequested());
    assertNotNull(executingActionsCache.get(key3));
    // Have it finish sleeping, will have been cancelled
    // (Can't use the policy, as cancel is counted as a failure)
    sleepActionExec.getExecutingThread().interrupt();
    Thread.sleep(150);
    // Ensure the proper cancelled tracking
    assertEquals(ActionStatus.Cancelled, sleepAction3.getExecutionStatus());
    assertEquals(null, sleepAction3.getExecutionFailureMessage());
}
Also used : UserTransaction(javax.transaction.UserTransaction) CancellableSleepAction(org.alfresco.repo.action.ActionServiceImplTest.CancellableSleepAction) ExecutionSummary(org.alfresco.service.cmr.action.ExecutionSummary) SleepActionExecuter(org.alfresco.repo.action.ActionServiceImplTest.SleepActionExecuter)

Aggregations

UserTransaction (javax.transaction.UserTransaction)1 CancellableSleepAction (org.alfresco.repo.action.ActionServiceImplTest.CancellableSleepAction)1 SleepActionExecuter (org.alfresco.repo.action.ActionServiceImplTest.SleepActionExecuter)1 ExecutionSummary (org.alfresco.service.cmr.action.ExecutionSummary)1