Ejemplo n.º 1
0
        /// <summary>
        /// Gathers the exploration strategy statistics for
        /// the latest testing iteration.
        /// </summary>
        /// <param name="runtime">TestingRuntime</param>
        private void GatherIterationStatistics(TestingRuntime runtime)
        {
            TestReport report = runtime.Scheduler.GetReport();

            report.CoverageInfo.Merge(runtime.CoverageInfo);
            this.TestReport.Merge(report);
        }
Ejemplo n.º 2
0
        public void Setup()
        {
            var runtime = new TestingRuntime();
            runtime.Start();

            ScenarioContext.Current.Set(runtime);
            ScenarioContext.Current.Set(runtime.ServiceLocator.Resolve<IDomainRepository>());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes the shared counter.
        /// </summary>
        /// <param name="value">Initial value</param>
        /// <param name="runtime">TestingRuntime</param>
        public MockSharedCounter(int value, TestingRuntime runtime)
        {
            this.Runtime        = runtime;
            this.CounterMachine = this.Runtime.CreateMachine(typeof(SharedCounterMachine));
            var currentMachine = this.Runtime.GetCurrentMachine();

            this.Runtime.SendEvent(this.CounterMachine, SharedCounterEvent.SetEvent(currentMachine.Id, value));
            currentMachine.Receive(typeof(SharedCounterResponseEvent)).Wait();
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="runtime">TestingRuntime</param>
 /// <param name="strategy">SchedulingStrategy</param>
 internal BugFindingScheduler(TestingRuntime runtime, ISchedulingStrategy strategy)
 {
     this.Runtime                  = runtime;
     this.Strategy                 = strategy;
     this.SchedulableInfoMap       = new Dictionary <ulong, SchedulableInfo>();
     this.CompletionSource         = new TaskCompletionSource <bool>();
     this.IsSchedulerRunning       = true;
     this.BugFound                 = false;
     this.HasFullyExploredSchedule = false;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Constructs a reproducable trace.
        /// </summary>
        /// <param name="runtime">TestingRuntime</param>
        private void ConstructReproducableTrace(TestingRuntime runtime)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (this.Strategy.IsFair())
            {
                stringBuilder.Append("--fair-scheduling").Append(Environment.NewLine);
            }

            if (base.Configuration.EnableCycleDetection)
            {
                stringBuilder.Append("--cycle-detection").Append(Environment.NewLine);
                stringBuilder.Append("--liveness-temperature-threshold:" +
                                     base.Configuration.LivenessTemperatureThreshold).
                Append(Environment.NewLine);
            }
            else
            {
                stringBuilder.Append("--liveness-temperature-threshold:" +
                                     base.Configuration.LivenessTemperatureThreshold).
                Append(Environment.NewLine);
            }

            if (!base.Configuration.TestMethodName.Equals(""))
            {
                stringBuilder.Append("--test-method:" +
                                     base.Configuration.TestMethodName).
                Append(Environment.NewLine);
            }

            for (int idx = 0; idx < runtime.ScheduleTrace.Count; idx++)
            {
                ScheduleStep step = runtime.ScheduleTrace[idx];
                if (step.Type == ScheduleStepType.SchedulingChoice)
                {
                    stringBuilder.Append($"({step.ScheduledMachineId})");
                }
                else if (step.BooleanChoice != null)
                {
                    stringBuilder.Append(step.BooleanChoice.Value);
                }
                else
                {
                    stringBuilder.Append(step.IntegerChoice.Value);
                }

                if (idx < runtime.ScheduleTrace.Count - 1)
                {
                    stringBuilder.Append(Environment.NewLine);
                }
            }

            this.ReproducableTrace = stringBuilder.ToString();
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes the shared dictionary.
 /// </summary>
 /// <param name="comparer">Comparre for keys</param>
 /// <param name="runtime">TestingRuntime</param>
 public MockSharedDictionary(IEqualityComparer <TKey> comparer, TestingRuntime runtime)
 {
     this.Runtime = runtime;
     if (comparer != null)
     {
         this.DictionaryMachine = this.Runtime.CreateMachine(typeof(SharedDictionaryMachine <TKey, TValue>),
                                                             SharedDictionaryEvent.InitEvent(comparer));
     }
     else
     {
         this.DictionaryMachine = this.Runtime.CreateMachine(typeof(SharedDictionaryMachine <TKey, TValue>));
     }
 }
Ejemplo n.º 7
0
 public static void TestRun()
 {
     Runtime = new TestingRuntime();
     Runtime.Start();
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="runtime">PSharpBugFindingRuntime</param>
 /// <param name="taskMap">Task map</param>
 internal AsynchronousTaskScheduler(TestingRuntime runtime, ConcurrentDictionary <int, Machine> taskMap)
 {
     this.Runtime = runtime;
     this.TaskMap = taskMap;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Registers the testing runtime.
 /// </summary>
 /// <param name="runtime">The testing runtime.</param>
 public void RegisterRuntime(PSharpRuntime runtime)
 {
     runtime.Assert((runtime as TestingRuntime) != null,
                    "Requires passed runtime to support method GetCurrentMachineId");
     this.Runtime = runtime as TestingRuntime;
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes the shared register.
 /// </summary>
 /// <param name="value">Initial value</param>
 /// <param name="runtime">this.Runtime</param>
 public MockSharedRegister(T value, TestingRuntime runtime)
 {
     this.Runtime         = runtime;
     this.RegisterMachine = this.Runtime.CreateMachine(typeof(SharedRegisterMachine <T>));
     this.Runtime.SendEvent(this.RegisterMachine, SharedRegisterEvent.SetEvent(value));
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Runs the next testing iteration.
        /// </summary>
        /// <param name="iteration">Iteration</param>
        private void RunNextIteration(int iteration)
        {
            if (this.ShouldPrintIteration(iteration + 1))
            {
                base.Logger.WriteLine($"..... Iteration #{iteration + 1}");
                // Flush when logging to console
                if (base.Logger is ConsoleLogger)
                {
                    Console.Out.Flush();
                }
            }

            // Runtime used to serialize and test the program in this iteration.
            TestingRuntime runtime = null;

            // Logger used to intercept the program output if no custom logger
            // is installed and if verbosity is turned off.
            InMemoryLogger runtimeLogger = null;

            // Gets a handle to the standard output and error streams.
            var stdOut = Console.Out;
            var stdErr = Console.Error;

            try
            {
                // Creates a new instance of the bug-finding runtime.
                if (base.TestRuntimeFactoryMethod != null)
                {
                    runtime = (TestingRuntime)base.TestRuntimeFactoryMethod.Invoke(null,
                                                                                   new object[] { base.Configuration, base.Strategy, base.Reporter });
                }
                else
                {
                    runtime = new TestingRuntime(base.Configuration, base.Strategy, base.Reporter);
                }

                if (base.Configuration.EnableDataRaceDetection)
                {
                    this.Reporter.RegisterRuntime(runtime);
                }

                // If verbosity is turned off, then intercept the program log, and also dispose
                // the standard output and error streams.
                if (base.Configuration.Verbose < 2)
                {
                    runtimeLogger = new InMemoryLogger();
                    runtime.SetLogger(runtimeLogger);

                    // Sets the scheduling strategy logger to the in-memory logger.
                    base.SchedulingStrategyLogger.SetLogger(runtimeLogger);

                    var writer = new LogWriter(new DisposingLogger());
                    Console.SetOut(writer);
                    Console.SetError(writer);
                }

                // Runs the test inside the P# test-harness machine.
                runtime.RunTestHarness(base.TestMethod, base.TestAction);

                // Wait for the test to terminate.
                runtime.Wait();

                // Invokes user-provided cleanup for this iteration.
                if (base.TestIterationDisposeMethod != null)
                {
                    // Disposes the test state.
                    base.TestIterationDisposeMethod.Invoke(null, null);
                }

                // Invoke the per iteration callbacks, if any.
                foreach (var callback in base.PerIterationCallbacks)
                {
                    callback(iteration);
                }

                if (base.Configuration.RaceFound)
                {
                    string message = IO.Utilities.Format("Found a race");
                    runtime.Scheduler.NotifyAssertionFailure(message, false);
                    foreach (var report in this.TestReport.BugReports)
                    {
                        runtime.Logger.WriteLine(report);
                    }
                }

                // Checks that no monitor is in a hot state at termination. Only
                // checked if no safety property violations have been found.
                if (!runtime.Scheduler.BugFound)
                {
                    runtime.CheckNoMonitorInHotStateAtTermination();
                }

                if (runtime.Scheduler.BugFound)
                {
                    base.ErrorReporter.WriteErrorLine(runtime.Scheduler.BugReport);
                }

                this.GatherIterationStatistics(runtime);

                if (base.TestReport.NumOfFoundBugs > 0)
                {
                    if (runtimeLogger != null)
                    {
                        this.ReadableTrace  = runtimeLogger.ToString();
                        this.ReadableTrace += this.TestReport.GetText(base.Configuration, "<StrategyLog>");
                    }

                    this.BugTrace = runtime.BugTrace;
                    this.ConstructReproducableTrace(runtime);
                }
            }
            finally
            {
                if (base.Configuration.Verbose < 2)
                {
                    // Restores the standard output and error streams.
                    Console.SetOut(stdOut);
                    Console.SetError(stdErr);
                }

                if (base.Configuration.PerformFullExploration && runtime.Scheduler.BugFound)
                {
                    base.Logger.WriteLine($"..... Iteration #{iteration + 1} " +
                                          $"triggered bug #{base.TestReport.NumOfFoundBugs} " +
                                          $"[task-{this.Configuration.TestingProcessId}]");
                }

                // Resets the scheduling strategy logger to the default logger.
                base.SchedulingStrategyLogger.ResetToDefaultLogger();

                // Cleans up the runtime before the next iteration starts.
                runtimeLogger?.Dispose();
                runtime?.Dispose();
            }
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="runtime">TestingRuntime</param>
 internal StateCache(TestingRuntime runtime)
 {
     Runtime      = runtime;
     Fingerprints = new HashSet <Fingerprint>();
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Creates a bug-reproducing task.
        /// </summary>
        /// <returns>Task</returns>
        private Task CreateBugReproducingTask()
        {
            Task task = new Task(() =>
            {
                // Runtime used to serialize and test the program.
                TestingRuntime runtime = null;

                // Logger used to intercept the program output if no custom logger
                // is installed and if verbosity is turned off.
                InMemoryLogger runtimeLogger = null;

                // Gets a handle to the standard output and error streams.
                var stdOut = Console.Out;
                var stdErr = Console.Error;

                try
                {
                    if (base.TestInitMethod != null)
                    {
                        // Initializes the test state.
                        base.TestInitMethod.Invoke(null, new object[] { });
                    }

                    // Creates a new instance of the bug-finding runtime.
                    if (base.TestRuntimeFactoryMethod != null)
                    {
                        runtime = (TestingRuntime)base.TestRuntimeFactoryMethod.Invoke(null,
                                                                                       new object[] { base.Configuration, base.Strategy, base.Reporter });
                    }
                    else
                    {
                        runtime = new TestingRuntime(base.Configuration, base.Strategy, base.Reporter);
                    }


                    // If verbosity is turned off, then intercept the program log, and also redirect
                    // the standard output and error streams into the runtime logger.
                    if (base.Configuration.Verbose < 2)
                    {
                        runtimeLogger = new InMemoryLogger();
                        runtime.SetLogger(runtimeLogger);

                        var writer = new LogWriter(new DisposingLogger());
                        Console.SetOut(writer);
                        Console.SetError(writer);
                    }

                    // Runs the test inside the P# test-harness machine.
                    runtime.RunTestHarness(base.TestMethod, base.TestAction);

                    // Wait for the test to terminate.
                    runtime.Wait();

                    // Invokes user-provided cleanup for this iteration.
                    if (base.TestIterationDisposeMethod != null)
                    {
                        // Disposes the test state.
                        base.TestIterationDisposeMethod.Invoke(null, new object[] { });
                    }

                    // Invokes user-provided cleanup for all iterations.
                    if (base.TestDisposeMethod != null)
                    {
                        // Disposes the test state.
                        base.TestDisposeMethod.Invoke(null, new object[] { });
                    }

                    this.InternalError = (base.Strategy as ReplayStrategy).ErrorText;

                    // Checks that no monitor is in a hot state at termination. Only
                    // checked if no safety property violations have been found.
                    if (!runtime.Scheduler.BugFound && this.InternalError.Length == 0)
                    {
                        runtime.CheckNoMonitorInHotStateAtTermination();
                    }

                    if (runtime.Scheduler.BugFound && this.InternalError.Length == 0)
                    {
                        base.ErrorReporter.WriteErrorLine(runtime.Scheduler.BugReport);
                    }

                    TestReport report = runtime.Scheduler.GetReport();
                    report.CoverageInfo.Merge(runtime.CoverageInfo);
                    this.TestReport.Merge(report);
                }
                catch (TargetInvocationException ex)
                {
                    if (!(ex.InnerException is TaskCanceledException))
                    {
                        ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                    }
                }
                finally
                {
                    if (base.Configuration.Verbose < 2)
                    {
                        // Restores the standard output and error streams.
                        Console.SetOut(stdOut);
                        Console.SetError(stdErr);
                    }

                    // Cleans up the runtime.
                    runtimeLogger?.Dispose();
                    runtime?.Dispose();
                }
            }, base.CancellationTokenSource.Token);

            return(task);
        }
Ejemplo n.º 14
0
 public static void TestRun()
 {
     Runtime = new TestingRuntime();
     Runtime.Start();
 }