Пример #1
0
 private TOut TryOp <TOut>(Func <TOut> tryOp)
 {
     try
     {
         return(tryOp());
     }
     catch (Exception e)
     {
         var error = new ExperimentError
         {
             LastException = e,
             LastStep      = CurrentState.CurrentStep,
             ErrorMessage  = string.Format(
                 "An Exception was thrown running Step '{0}'. Exception message: {1}",
                 CurrentState.CurrentStep, e.Message),
             ExperimentName = CurrentState.Name
         };
         throw new StepFailedException(error);
     }
 }
Пример #2
0
        private T RunControl(IExperimentResult <TPublish> results, out Exception controlException)
        {
            controlException         = null;
            CurrentState.Name        = ControlName;
            CurrentState.CurrentStep = Operations.Control;
            if (Steps.Control == null)
            {
                // Can there be valid experiments with no Control?? Dunno but seems restrictive to enfore it
                // if so, return default(T) instead of Exception
                return(default(T));
                // Throw exception if we want to enforce having a Control
                //throw new InvalidOperationException(
                //    "The Control was never set for this Experiment! Can't run an Experiment without a Control!");
            }
            IExperimentError stepError = null;
            object           context   = null;
            T        result            = default(T);
            TPublish value             = default(TPublish);

            try
            {
                context = TrySetContext();
                CurrentState.Context = context;
            }
            catch (StepFailedException sfe)
            {
                stepError = sfe.ExperimentError;
                TryOnError(stepError);
            }
            try
            {
                TrySetup();
            }
            catch (StepFailedException sfe)
            {
                stepError = sfe.ExperimentError;
                TryOnError(stepError);
            }
            var timer = new Stopwatch();

            CurrentState.CurrentStep = Operations.Control;
            try
            {
                timer.Start();
                result = Steps.Control();
                timer.Stop();
            }
            catch (Exception e)
            {
                timer.Stop();
                controlException = e; //need to throw this at the end
                var error = new ExperimentError
                {
                    LastException = e,
                    LastStep      = Operations.Control,
                    ErrorMessage  = "An Exception was thrown running the Control! Exception message: "
                                    + e.Message,
                    ExperimentName = ControlName
                };
                TryOnError(error);//TODO: Should we run OnError on Control??
                results.Control = new Observation <TPublish>
                {
                    ExperimentError     = error,
                    ElapsedMilliseconds = timer.ElapsedMilliseconds,
                    Context             = context,
                    Name            = ControlName,
                    IsMismatched    = false,
                    ExceptionThrown = true
                };
                return(result);
            }
            try
            {
                value = TryPrepare(result);
            }
            catch (StepFailedException sfe)
            {
                stepError = sfe.ExperimentError;
                TryOnError(stepError);
                //We must continue on because we've already gotten a result
            }
            try
            {
                TryTeardown();
            }
            catch (StepFailedException sfe)
            {
                stepError = sfe.ExperimentError;
                TryOnError(stepError);
            }
            results.Control = new Observation <TPublish>
            {
                ElapsedMilliseconds = timer.ElapsedMilliseconds,
                Context             = context,
                Name            = ControlName,
                IsMismatched    = false, //Control can't be mismatched
                ExceptionThrown = stepError != null,
                ExperimentError = stepError,
                Value           = value
            };
            return(result);
        }