Search in sources :

Example 1 with WorkflowStateListener

use of org.opencastproject.workflow.api.WorkflowStateListener in project opencast by opencast.

the class PauseFinalOperationTest method testHoldAndResumeFinalOperation.

@Test()
public void testHoldAndResumeFinalOperation() throws Exception {
    // Start a new workflow, and wait for it to pause
    WorkflowStateListener pauseListener = new WorkflowStateListener(WorkflowState.PAUSED);
    service.addWorkflowListener(pauseListener);
    synchronized (pauseListener) {
        workflow = service.start(def, mp, null);
        pauseListener.wait();
    }
    service.removeWorkflowListener(pauseListener);
    // Ensure that "start" was called on the first operation handler, but not resume
    Assert.assertTrue(handler.isStarted());
    Assert.assertTrue(!handler.isResumed());
    // The workflow should be in the paused state
    Assert.assertEquals(WorkflowState.PAUSED, service.getWorkflowById(workflow.getId()).getState());
    // Resume the workflow
    WorkflowStateListener succeedListener = new WorkflowStateListener(WorkflowState.SUCCEEDED);
    service.addWorkflowListener(succeedListener);
    synchronized (succeedListener) {
        service.resume(workflow.getId());
        succeedListener.wait();
    }
    service.removeWorkflowListener(succeedListener);
    Assert.assertEquals(WorkflowState.SUCCEEDED, service.getWorkflowById(workflow.getId()).getState());
}
Also used : WorkflowStateListener(org.opencastproject.workflow.api.WorkflowStateListener) Test(org.junit.Test)

Example 2 with WorkflowStateListener

use of org.opencastproject.workflow.api.WorkflowStateListener in project opencast by opencast.

the class WorkflowServiceImplTest method testManyConcurrentWorkflows.

/**
 * Starts many concurrent workflows to test DB deadlock.
 *
 * @throws Exception
 */
@Test
public void testManyConcurrentWorkflows() throws Exception {
    int count = 10;
    Assert.assertEquals(0, service.countWorkflowInstances());
    List<WorkflowInstance> instances = new ArrayList<WorkflowInstance>();
    WorkflowStateListener stateListener = new WorkflowStateListener(WorkflowState.SUCCEEDED, WorkflowState.FAILED);
    service.addWorkflowListener(stateListener);
    for (int i = 0; i < count; i++) {
        MediaPackage mp = i % 2 == 0 ? mediapackage1 : mediapackage2;
        mp.setIdentifier(new UUIDIdBuilderImpl().createNew());
        instances.add(service.start(workingDefinition, mp, null));
    }
    while (stateListener.countStateChanges() < count) {
        synchronized (stateListener) {
            stateListener.wait();
        }
    }
    Assert.assertEquals(count, service.countWorkflowInstances());
    Assert.assertEquals(count, stateListener.countStateChanges(WorkflowState.SUCCEEDED));
}
Also used : WorkflowStateListener(org.opencastproject.workflow.api.WorkflowStateListener) ArrayList(java.util.ArrayList) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) UUIDIdBuilderImpl(org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl) Test(org.junit.Test)

Example 3 with WorkflowStateListener

use of org.opencastproject.workflow.api.WorkflowStateListener in project opencast by opencast.

the class WorkflowStatisticsTest method testStatistics.

/**
 * Tests whether the workflow service statistics are gathered correctly.
 */
@Test
public void testStatistics() throws Exception {
    // Start the workflows and advance them in "random" order. With every definition, an instance is started for every
    // operation that is part of the definition. So we end up with an instance per definition and operation, and there
    // are no two workflows that are in the same operation.
    int total = 0;
    int paused = 0;
    int failed = 0;
    int failing = 0;
    int instantiated = 0;
    int running = 0;
    int stopped = 0;
    int succeeded = 0;
    WorkflowStateListener listener = new WorkflowStateListener(WorkflowState.PAUSED);
    service.addWorkflowListener(listener);
    List<WorkflowInstance> instances = new ArrayList<WorkflowInstance>();
    for (WorkflowDefinition def : workflowDefinitions) {
        for (int j = 0; j < def.getOperations().size(); j++) {
            mediaPackage.setIdentifier(new UUIDIdBuilderImpl().createNew());
            instances.add(service.start(def, mediaPackage));
            total++;
            paused++;
        }
    }
    // Wait for all the workflows to go into "paused" state
    synchronized (listener) {
        while (listener.countStateChanges() < WORKFLOW_DEFINITION_COUNT * OPERATION_COUNT) {
            listener.wait();
        }
    }
    service.removeWorkflowListener(listener);
    // Resume all of them, so some will be finished, some won't
    int j = 0;
    for (WorkflowInstance instance : instances) {
        WorkflowListener instanceListener = new IndividualWorkflowListener(instance.getId());
        service.addWorkflowListener(instanceListener);
        for (int k = 0; k <= (j % OPERATION_COUNT - 1); k++) {
            synchronized (instanceListener) {
                service.resume(instance.getId(), null);
                instanceListener.wait();
            }
        }
        j++;
    }
    // TODO: Add failed, failing, stopped etc. workflows as well
    // Get the statistics
    WorkflowStatistics stats = service.getStatistics();
    assertEquals(failed, stats.getFailed());
    assertEquals(failing, stats.getFailing());
    assertEquals(instantiated, stats.getInstantiated());
    assertEquals(succeeded, stats.getFinished());
    assertEquals(paused, stats.getPaused());
    assertEquals(running, stats.getRunning());
    assertEquals(stopped, stats.getStopped());
    assertEquals(total, stats.getTotal());
// TODO: Test the operations
// Make sure they are as expected
// for (WorkflowDefinitionReport report : stats.getDefinitions()) {
// 
// }
}
Also used : WorkflowStateListener(org.opencastproject.workflow.api.WorkflowStateListener) ArrayList(java.util.ArrayList) WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) WorkflowListener(org.opencastproject.workflow.api.WorkflowListener) WorkflowStatistics(org.opencastproject.workflow.api.WorkflowStatistics) UUIDIdBuilderImpl(org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl) Test(org.junit.Test)

Example 4 with WorkflowStateListener

use of org.opencastproject.workflow.api.WorkflowStateListener in project opencast by opencast.

the class WorkflowServiceImplTest method retryAndWait.

protected WorkflowInstance retryAndWait(WorkflowInstance instance, String retryStrategy, WorkflowState stateToWaitFor) throws Exception {
    WorkflowStateListener stateListener = new WorkflowStateListener(stateToWaitFor);
    service.addWorkflowListener(stateListener);
    Map<String, String> props = new HashMap<String, String>();
    props.put("retryStrategy", retryStrategy);
    WorkflowInstance wfInstance = null;
    synchronized (stateListener) {
        wfInstance = service.resume(instance.getId(), props);
        stateListener.wait();
    }
    service.removeWorkflowListener(stateListener);
    return wfInstance;
}
Also used : WorkflowStateListener(org.opencastproject.workflow.api.WorkflowStateListener) HashMap(java.util.HashMap) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance)

Example 5 with WorkflowStateListener

use of org.opencastproject.workflow.api.WorkflowStateListener in project opencast by opencast.

the class WorkflowServiceImplTest method startAndWait.

protected WorkflowInstance startAndWait(WorkflowDefinition definition, MediaPackage mp, Long parentId, WorkflowState stateToWaitFor) throws Exception {
    WorkflowStateListener stateListener = new WorkflowStateListener(stateToWaitFor);
    service.addWorkflowListener(stateListener);
    WorkflowInstance instance = null;
    synchronized (stateListener) {
        if (parentId == null) {
            instance = service.start(definition, mp);
        } else {
            instance = service.start(definition, mp, parentId, null);
        }
        stateListener.wait();
    }
    service.removeWorkflowListener(stateListener);
    return instance;
}
Also used : WorkflowStateListener(org.opencastproject.workflow.api.WorkflowStateListener) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance)

Aggregations

WorkflowStateListener (org.opencastproject.workflow.api.WorkflowStateListener)10 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)8 Test (org.junit.Test)7 HashMap (java.util.HashMap)4 UUIDIdBuilderImpl (org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl)3 ArrayList (java.util.ArrayList)2 MediaPackage (org.opencastproject.mediapackage.MediaPackage)1 WorkflowDefinition (org.opencastproject.workflow.api.WorkflowDefinition)1 WorkflowListener (org.opencastproject.workflow.api.WorkflowListener)1 WorkflowStatistics (org.opencastproject.workflow.api.WorkflowStatistics)1