private void runScenario(
      ExecutionToken executionToken,
      HandlerManager handlerManager,
      ScenarioToken scenario,
      boolean skip)
      throws Exception {
    executionListenerSupport.notifyScenarioStarted(executionToken, scenario);
    log.info(String.format("Processing scenario: %s", scenario.getName()));

    // reset the ChorusContext for the scenario
    ChorusContext.destroy();

    handlerManager.setCurrentScenario(scenario);
    List<Object> handlerInstances = handlerManager.getOrCreateHandlersForScenario();
    handlerManager.processStartOfScope(Scope.SCENARIO, handlerInstances);

    createTimeoutTasks(
        Thread
            .currentThread()); // will interrupt or eventually kill thread / interpreter if blocked

    log.debug("Running scenario steps for Scenario " + scenario);
    StepInvokerProvider p = getStepInvokers(handlerInstances);
    stepProcessor.runSteps(executionToken, p, scenario.getSteps(), skip);

    stopTimeoutTasks();

    // the special start or end scenarios don't count in the execution stats
    if (!scenario.isStartOrEndScenario()) {
      updateExecutionStats(executionToken, scenario);
    }

    handlerManager.processEndOfScope(Scope.SCENARIO, handlerInstances);
    executionListenerSupport.notifyScenarioCompleted(executionToken, scenario);
  }
  private void runScenarios(
      ExecutionToken executionToken,
      FeatureToken feature,
      List<ScenarioToken> scenarios,
      HandlerManager handlerManager)
      throws Exception {
    log.debug("Now running scenarios " + scenarios + " for feature " + feature);
    for (Iterator<ScenarioToken> iterator = scenarios.iterator(); iterator.hasNext(); ) {
      ScenarioToken scenario = iterator.next();

      // if the feature start scenario exists and failed we skip all but feature end scenario
      boolean skip =
          !scenario.isFeatureStartScenario()
              && !scenario.isFeatureEndScenario()
              && feature.isFeatureStartScenarioFailed();

      if (skip) {
        log.warn(
            "Skipping scenario "
                + scenario
                + " since "
                + KeyWord.FEATURE_START_SCENARIO_NAME
                + " failed");
      }

      runScenario(executionToken, handlerManager, scenario, skip);
    }
  }
 private void killInterpreterIfStillRunning(Thread t) {
   if (t.isAlive()) {
     log.error(
         "Scenario did not respond to thread.kill() after timeout, will now kill the interpreter");
     System.exit(1);
   }
 }
 private void timeoutIfStillRunning(Thread t) {
   if (t.isAlive()) {
     log.warn("Scenario timed out after " + scenarioTimeoutMillis + " millis, will interrupt");
     stepProcessor.setInterruptingOnTimeout(true);
     t.interrupt(); // first try to interrupt to see if this can unblock/fail the scenario
   }
 }
 private void stopThreadIfStillRunning(Thread t) {
   if (t.isAlive()) {
     log.error(
         "Scenario did not respond to interrupt after timeout, "
             + "will stop the interpreter thread and fail the tests");
     t.stop(); // this will trigger a ThreadDeath exception which we should allow to propagate
     // and will terminate the interpreter
   }
 }
 public void runFeatures(ExecutionToken executionToken, List<FeatureToken> features) {
   // RUN EACH FEATURE
   for (FeatureToken feature : features) {
     try {
       runFeature(executionToken, feature);
     } catch (Throwable t) {
       log.error("Exception while running feature " + feature, t);
       executionToken.incrementFeaturesFailed();
     }
   }
 }
  public MBeanServerConnection connect() {
    MBeanServerConnection result;
    try {
      String serviceURL =
          String.format("service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi", host, jmxPort);
      jmxConnector = JMXConnectorFactory.connect(new JMXServiceURL(serviceURL), null);
      log.debug("Connecting to JMX service URL: " + serviceURL);

      result = jmxConnector.getMBeanServerConnection();

    } catch (Exception e) {
      String msg = String.format("Failed to connect to mBean server on (%s:%s)", host, jmxPort);
      if (log.isDebugEnabled()) {
        log.debug(msg, e);
      } else {
        log.warn(msg);
      }
      throw new ChorusException(msg, e);
    }
    mBeanServerConnection = result;
    return result;
  }
  private void runFeature(ExecutionToken executionToken, FeatureToken feature) {

    executionListenerSupport.notifyFeatureStarted(executionToken, feature);
    // notify we started, even if there are missing handlers
    // (but nothing will be done)
    // this is still important so execution listeners at least see the feature (but will show as
    // 'unimplemented')
    String config = feature.isConfiguration() ? " in config " + feature.getConfigurationName() : "";
    log.info("Running feature from file: " + feature.getFeatureFile() + config);

    // check that the required handler classes are all available and list them in order of
    // precedence
    List<Class> orderedHandlerClasses = new ArrayList<>();
    StringBuilder unavailableHandlersMessage =
        handlerClassDiscovery.findHandlerClasses(allHandlerClasses, feature, orderedHandlerClasses);
    boolean foundAllHandlerClasses = unavailableHandlersMessage.length() == 0;

    // run the scenarios in the feature
    if (foundAllHandlerClasses) {
      log.debug("The following handlers will be used " + orderedHandlerClasses);
      List<ScenarioToken> scenarios = feature.getScenarios();

      try {
        HandlerManager handlerManager =
            new HandlerManager(
                feature,
                orderedHandlerClasses,
                springContextSupport,
                subsystemManager,
                executionToken.getProfile());
        handlerManager.createFeatureScopedHandlers();
        handlerManager.processStartOfFeature();

        runScenarios(executionToken, feature, scenarios, handlerManager);

        handlerManager.processEndOfFeature();
      } catch (Exception e) {
        log.error("Exception while running feature " + feature.getName(), e);
      }

      String description =
          feature.getEndState() == EndState.PASSED
              ? " passed! "
              : feature.getEndState() == EndState.PENDING ? " pending! " : " failed! ";
      log.trace("The feature " + description);

      if (feature.getEndState() == EndState.PASSED) {
        executionToken.incrementFeaturesPassed();
      } else if (feature.getEndState() == EndState.PENDING) {
        executionToken.incrementFeaturesPending();
      } else {
        executionToken.incrementFeaturesFailed();
      }
    } else {
      log.warn(
          "The following handlers were not available, failing feature "
              + feature.getName()
              + " "
              + unavailableHandlersMessage);
      feature.setUnavailableHandlersMessage(unavailableHandlersMessage.toString());
      executionToken.incrementUnavailableHandlers();
      executionToken.incrementFeaturesFailed();
    }

    executionListenerSupport.notifyFeatureCompleted(executionToken, feature);
  }