Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.amazonaws.services.simpleworkflow.flow.junit;
  
  
  import  org.junit.runners.model.Statement;
  
 
 public class WorkflowTestStatement extends Statement {
 
     private final Callable<WorkflowTestBaseworkflowTestAccessor;
 
     private final Statement base;
 
     private Long timeout;
 
     boolean completed;
 
     private Throwable failure;
 
     boolean waitingOnTimer;
 
     private Class<? extends ThrowableexpectedException;
 
     private boolean flowTestRunner;
 
     public WorkflowTestStatement(Callable<WorkflowTestBaseworkflowTestAccessor, Statement baseLong timeout,
             Class<? extends ThrowableexpectedException) {
         this. = workflowTestAccessor;
         this. = base;
         this. = timeout;
         this. = expectedException;
     }
 
     @Override
     public void evaluate() throws Throwable {
         if (!) {
             throw new IllegalStateException(
                     "WorkflowTest rule can be used only with flow specific test runners: FlowBlockJUnit4ClassRunner and FlowSpringJUnit4ClassRunner");
         }
         final WorkflowTestBase workflowTest = .call();
         Thread t = null;
         if ( == null ||  == 0) {
             try {
                 asyncEvaluate(workflowTest);
                  = true;
             }
             catch (Throwable e) {
                  = e;
             }
         }
         else {
             t = new Thread() {
 
                 public void run() {
                     try {
                         asyncEvaluate(workflowTest);
                          = true;
                     }
                     catch (Throwable e) {
                          = e;
                     }
                 }
             };
             t.start();
             t.join();
         }
         if ( != null) {
             if ( != null && .isAssignableFrom(.getClass())) {
                 return;
             }
             throw ;
         }
         if (!) {
             if () {
                 AssertionError e = new AssertionError("Test timed out after " + 
                         + " milliseconds. The following asynchrous tasks are outstanding: \n"
                         + workflowTest.scope.getAsynchronousThreadDumpAsString());
                 throw e;
             }
             else {
                 AssertionError e = new AssertionError("Test timed out after " +  + " milliseconds");
                 if (t != null) {
                     e.setStackTrace(t.getStackTrace());
                 }
                 throw e;
             }
         }
         if ( != null) {
             throw new AssertionError("Expected exception: " + );
         }
 
     }
 
     private void asyncEvaluate(final WorkflowTestBase workflowTestthrows ThrowableInterruptedException {
         try {
             workflowTest.scope = new AsyncScope() {
                @Override
                protected void doAsync() throws Throwable {
                    new TryCatchFinally() {
                        @Override
                        protected void doTry() throws Throwable {
                            workflowTest.beforeEvaluate(workflowTest.decisionContext);
                            .evaluate();
                        }
                        @Override
                        protected void doCatch(Throwable ethrows Throwable {
                            if (e instanceof IllegalStateException) {
                                if ("Called outside of the workflow definition code.".equals(e.getMessage())) {
                                    throw new RuntimeException(
                                            "Possible use of \"timeout\" parameter of @Test annotation without using Flow JUnit runner. "
                                                    + "Supported runners are FlowBlockJUnit4ClassRunner and FlowSpringJUnit4ClassRunner.",
                                            e);
                                }
                            }
                            throw e;
                        }
                        @Override
                        protected void doFinally() throws Throwable {
                            workflowTest.afterEvaluate();
                        }
                    };
                }
            };
            boolean outstandingTasks = false;
            while (!workflowTest.scope.isComplete()) {
                outstandingTasks = workflowTest.scope.eventLoop();
                if (workflowTest.waits.size() == 0) {
                    Long toNextTimerDelay = workflowTest.workflowClock.fireTimers();
                    if (toNextTimerDelay == null) {
                        break;
                    }
                    long timeToSleep = (long) (toNextTimerDelay / workflowTest.clockAcceleration);
                    if (timeToSleep > 5) {
                         = true;
                        try {
                            // If you are using @Test(timeout=...) annotation and your test timed out
                            // pointing to the Thread.sleep that follows this comment consider 
                            // changing test runner to FlowBlockJUnit4ClassRunner or 
                            // FlowSpringJUnit4ClassRunner to enable asynchronous thread dump.
                            Thread.sleep(timeToSleep);
                        }
                        finally {
                             = false;
                        }
                    }
                    workflowTest.workflowClock.advanceMilliseconds(toNextTimerDelay);
                    continue;
                }
                for (Settable<Voidlistener : workflowTest.waits) {
                    listener.set(null);
                }
                workflowTest.waits.clear();
            }
            if (!workflowTest.disableOutstandingTasksCheck && !outstandingTasks) {
                throw new IllegalStateException("There are outstanding tasks after test completed execution: \n"
                        + workflowTest.scope.getAsynchronousThreadDumpAsString());
            }
        }
        finally {
            workflowTest.afterEvaluate();
        }
    }
    public void setTestTimeoutActualTimeMilliseconds(long timeout) {
        if (timeout > 0) {
            this. = timeout;
        }
    }
    public void setExpectedException(Class<? extends ThrowableexpectedException) {
        this. = expectedException;
    }
    public void setFlowTestRunner(boolean flowTestRunner) {
        this. = flowTestRunner;
    }
New to GrepCode? Check out our FAQ X